Skip to content

Commit

Permalink
Addressing review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Dec 14, 2022
1 parent 0b84ae9 commit 08d3435
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added

- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068)
- `otelmux`: Add new `WithSpanNameFormatter` option to `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux` to allow customizing span names. (#3040)
- `otelmux`: Add new `WithSpanNameFormatter` option to `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux` to allow customizing span names. (#3041)

## [1.12.0/0.37.0/0.6.0]

Expand Down
39 changes: 23 additions & 16 deletions instrumentation/github.com/gorilla/mux/otelmux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ func TestResponseWriterInterfaces(t *testing.T) {

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

tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))

Expand All @@ -180,26 +179,34 @@ func TestCustomSpanNameFormatter(t *testing.T) {
expected string
}{
{nil, routeTpl},
{func(string, *http.Request) string { return "custom" }, "custom"},
{func(name string, r *http.Request) string {
return fmt.Sprintf("%s %s", r.Method, name)
}, "GET " + routeTpl},
{
func(string, *http.Request) string { return "custom" },
"custom",
},
{
func(name string, r *http.Request) string {
return fmt.Sprintf("%s %s", r.Method, name)
},
"GET " + routeTpl,
},
}

for _, d := range testdata {
router := mux.NewRouter()
router.Use(Middleware("foobar", WithTracerProvider(tp), WithSpanNameFormatter(d.spanNameFormatter)))
router.HandleFunc(routeTpl, func(w http.ResponseWriter, r *http.Request) {})
for i, d := range testdata {
t.Run(fmt.Sprintf("%d_%s", i, d.expected), func(t *testing.T) {
router := mux.NewRouter()
router.Use(Middleware("foobar", WithTracerProvider(tp), WithSpanNameFormatter(d.spanNameFormatter)))
router.HandleFunc(routeTpl, func(w http.ResponseWriter, r *http.Request) {})

r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()

router.ServeHTTP(w, r)
router.ServeHTTP(w, r)

spans := exporter.GetSpans()
require.Len(t, spans, 1)
assert.Equal(t, d.expected, spans[0].Name)
spans := exporter.GetSpans()
require.Len(t, spans, 1)
assert.Equal(t, d.expected, spans[0].Name)

exporter.Reset()
exporter.Reset()
})
}
}

0 comments on commit 08d3435

Please sign in to comment.