Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement http.Hijacker for otelmux #6562

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

- Implement `http.Hijacker` in`go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. (#5402)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Implement `http.Hijacker` in`go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. (#5402)
- Implement `http.Hijacker` in`go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. (#6562)


### Added

- Generate server metrics with semantic conventions v1.26 in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` when `OTEL_SEMCONV_STABILITY_OPT_IN` is set to `http/dup`. (#6411)
Expand Down
9 changes: 9 additions & 0 deletions instrumentation/github.com/gorilla/mux/otelmux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package otelmux // import "go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"

import (
"bufio"
"fmt"
"net"
"net/http"
"sync"

Expand Down Expand Up @@ -108,6 +110,13 @@ func getRRW(writer http.ResponseWriter) *recordingResponseWriter {
return rrw
}

func (h *recordingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hijacker, ok := h.writer.(http.Hijacker); ok {
return hijacker.Hijack()
}
return nil, nil, fmt.Errorf("underlying ResponseWriter does not support hijacking")
}

func putRRW(rrw *recordingResponseWriter) {
rrw.writer = nil
rrwPool.Put(rrw)
Expand Down
26 changes: 19 additions & 7 deletions instrumentation/github.com/gorilla/mux/otelmux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,14 @@ func TestPassthroughSpanFromGlobalTracer(t *testing.T) {
// span context in the incoming request context.
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
got := trace.SpanFromContext(r.Context()).SpanContext()
assert.Equal(t, sc, got)
w.WriteHeader(http.StatusOK)
}))

r := httptest.NewRequest("GET", "/user/123", nil)
r = r.WithContext(trace.ContextWithRemoteSpanContext(context.Background(), sc))
req := httptest.NewRequest("GET", "/user/123", nil)
req = req.WithContext(trace.ContextWithSpanContext(context.Background(), sc))
w := httptest.NewRecorder()
router.ServeHTTP(w, req)

router.ServeHTTP(w, r)
assert.True(t, called, "failed to run test")
assert.True(t, called)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this test need changing?

}

func TestPropagationWithGlobalPropagators(t *testing.T) {
Expand Down Expand Up @@ -190,3 +187,18 @@ func TestFilter(t *testing.T) {
assert.Equal(t, 1, calledHealth, "failed to run test")
assert.Equal(t, 1, calledTest, "failed to run test")
}

func TestRecordingResponseWriterHijack(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those two tests need to be fixed (so does the linter).
Also, we should be testing calling Hijack() when hijacker is implemented.

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rrw := getRRW(w)
conn, rw, err := rrw.Hijack()
assert.Nil(t, conn)
assert.Nil(t, rw)
assert.NotNil(t, err)
assert.Equal(t, "underlying ResponseWriter does not support hijacking", err.Error())
})

req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
}
22 changes: 22 additions & 0 deletions instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,25 @@ func TestWithPublicEndpointFn(t *testing.T) {
})
}
}

func getRRW(w http.ResponseWriter) http.Hijacker {
if hijacker, ok := w.(http.Hijacker); ok {
return hijacker
}
return nil
}

func TestRecordingResponseWriterHijack(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rrw := getRRW(w)
conn, rw, err := rrw.Hijack()
assert.Nil(t, conn)
assert.Nil(t, rw)
assert.NotNil(t, err)
assert.Equal(t, "underlying ResponseWriter does not support hijacking", err.Error())
})

req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
}
Loading