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 #5796

Open
wants to merge 2 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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [Unreleased]

### Added

- Implement `http.Hijacker` in`go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. (#5796)
- Add `NewProducer` to `go.opentelemetry.io/contrib/instrumentation/runtime`, which allows collecting the `go.schedule.duration` histogram metric from the Go runtime. (#5991)
- Add gRPC protocol support for OTLP log exporter in `go.opentelemetry.io/contrib/exporters/autoexport`. (#6083)

Expand Down
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 @@ -76,6 +78,13 @@ type recordingResponseWriter struct {
status int
}

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

var rrwPool = &sync.Pool{
New: func() interface{} {
return &recordingResponseWriter{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,63 @@ import (
"go.opentelemetry.io/otel/trace"
)

func TestRecordingResponseWriterHijackWithMiddleware(t *testing.T) {
// Create a mock HTTP handler
mockHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hj, ok := w.(http.Hijacker)
require.True(t, ok, "ResponseWriter does not implement http.Hijacker")

conn, rw, err := hj.Hijack()
require.NoError(t, err)
assert.NotNil(t, conn)
assert.NotNil(t, rw)

err = conn.Close()
require.NoError(t, err)
})

// Wrap the handler with otelmux.Middleware
router := mux.NewRouter()
router.Use(otelmux.Middleware("test-service"))
router.Handle("/hijack", mockHandler)

// Create a mock HTTP request and response writer
req := httptest.NewRequest("GET", "http://example.com/hijack", nil)
rr := httptest.NewRecorder()

// Serve the HTTP request using the wrapped handler
router.ServeHTTP(rr, req)

// Verify the response status
assert.Equal(t, http.StatusOK, rr.Code)
}

func TestRecordingResponseWriterHijackNonHijackerWithMiddleware(t *testing.T) {
// Create a mock HTTP handler
mockHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hj, ok := w.(http.Hijacker)
require.False(t, ok, "ResponseWriter should not implement http.Hijacker")

_, _, err := hj.Hijack()
assert.Error(t, err)
})

// Wrap the handler with otelmux.Middleware
router := mux.NewRouter()
router.Use(otelmux.Middleware("test-service"))
router.Handle("/non-hijack", mockHandler)

// Create a mock HTTP request and response writer
req := httptest.NewRequest("GET", "http://example.com/non-hijack", nil)
rr := httptest.NewRecorder()

// Serve the HTTP request using the wrapped handler
router.ServeHTTP(rr, req)

// Verify the response status
assert.Equal(t, http.StatusOK, rr.Code)
}

func TestCustomSpanNameFormatter(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()

Expand Down
Loading