Skip to content

Commit

Permalink
Adding test case for otelmux and fixing lint
Browse files Browse the repository at this point in the history
Signed-off-by: Rehan Pasha <rehan.pasha@fmr.com>
  • Loading branch information
rehanpfmr committed Aug 27, 2024
1 parent 3314b6c commit 256b7de
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The next release will require at least [Go 1.22].
- Add the `WithMetricsAttributesFn` option to allow setting dynamic, per-request metric attributes in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#5876)
- The `go.opentelemetry.io/contrib/config` package supports configuring `with_resource_constant_labels` for the prometheus exporter. (#5890)
- Support [Go 1.23]. (#6017)
- Implement `http.Hijacker` in`go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. (#5796)

### Removed

Expand Down
Binary file added instrgen/driver/driver
Binary file not shown.
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
70 changes: 70 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 @@ -4,8 +4,10 @@
package test

import (
"bufio"
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -23,6 +25,74 @@ import (
"go.opentelemetry.io/otel/trace"
)

type recordingResponseWriter struct {
writer http.ResponseWriter
statusCode int
size int
}

func (r *recordingResponseWriter) Header() http.Header {
return r.writer.Header()
}

func (r *recordingResponseWriter) Write(b []byte) (int, error) {
if r.statusCode == 0 {
r.statusCode = http.StatusOK
}
size, err := r.writer.Write(b)
r.size += size
return size, err
}

func (r *recordingResponseWriter) WriteHeader(statusCode int) {
r.statusCode = statusCode
r.writer.WriteHeader(statusCode)
}

func (r *recordingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj, ok := r.writer.(http.Hijacker)
if !ok {
return nil, nil, http.ErrNotSupported
}
return hj.Hijack()
}

type mockHijacker struct {
http.ResponseWriter
}

func (m *mockHijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
conn, rw := net.Pipe()
return conn, bufio.NewReadWriter(bufio.NewReader(rw), bufio.NewWriter(rw)), nil
}

type mockNonHijacker struct {
http.ResponseWriter
}

func TestRecordingResponseWriterHijack(t *testing.T) {
mockWriter := &mockHijacker{httptest.NewRecorder()}
recWriter := &recordingResponseWriter{writer: mockWriter}

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

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

func TestRecordingResponseWriterHijackNonHijacker(t *testing.T) {
mockWriter := &mockNonHijacker{httptest.NewRecorder()}
recWriter := &recordingResponseWriter{writer: mockWriter}

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

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

Expand Down

0 comments on commit 256b7de

Please sign in to comment.