Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 9fa866688216dd5f08a22c5f3c01da3337fc400f
Merge: 8b0f94e6 8a49875
Author: Pasha, Rehan <Rehan.Pasha@fmr.com>
Date:   Sat Sep 7 21:58:48 2024 +0530

    Merge branch 'main' into fix-5402

    Signed-off-by: Pasha, Rehan <Rehan.Pasha@fmr.com>

commit 8b0f94e6b500c5e2d65d1c65bbcbb63087afe7e7
Author: Pasha, Rehan <Rehan.Pasha@fmr.com>
Date:   Sat Sep 7 21:57:41 2024 +0530

    Update CHANGELOG.md

    Signed-off-by: Pasha, Rehan <Rehan.Pasha@fmr.com>

commit f76eb1d70480828fb0626b3285a46a82cd6c13a7
Author: Pasha, Rehan <Rehan.Pasha@fmr.com>
Date:   Sat Sep 7 20:35:39 2024 +0530

    Delete instrgen/driver/driver

    Signed-off-by: Pasha, Rehan <Rehan.Pasha@fmr.com>

commit 158e36cae0eff704203349929c919c0d8f4e669d
Author: Rehan Pasha <rehan.pasha@fmr.com>
Date:   Sat Sep 7 11:03:24 2024 -0400

    Updating test case and lint

    Signed-off-by: Rehan Pasha <rehan.pasha@fmr.com>

commit 256b7de
Author: Rehan Pasha <rehan.pasha@fmr.com>
Date:   Tue Aug 27 06:28:33 2024 -0400

    Adding test case for otelmux and fixing lint

    Signed-off-by: Rehan Pasha <rehan.pasha@fmr.com>

Signed-off-by: Rehan Pasha <rehan.pasha@fmr.com>
  • Loading branch information
rehanpfmr committed Sep 10, 2024
1 parent 38e6e1e commit f05ac63
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
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 @@ -76,6 +78,13 @@ type recordingResponseWriter struct {
status int
}

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")
}

var rrwPool = &sync.Pool{
New: func() interface{} {
return &recordingResponseWriter{}
Expand Down
57 changes: 57 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 @@ -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

0 comments on commit f05ac63

Please sign in to comment.