Skip to content

Commit

Permalink
add gorilla mux
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Apr 12, 2023
1 parent 0e994ca commit e1b06b6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
26 changes: 26 additions & 0 deletions contrib/gorilla/mux/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mux

import (
"net/http"

ddspanname "github.com/RangelReale/opentelemetry-go-datadog-spanname"
"go.opentelemetry.io/otel/trace"
)

// NewHandler should be used with [github.com/gorilla/mux].
func NewHandler(opts ...Option) trace.TracerProvider {
optns := options{
operationName: "http.request",
}
for _, opt := range opts {
opt(&optns)
}
return ddspanname.NewTracerProvider(optns.operationName,
ddspanname.WithTracerProvider(optns.tracerProvider))
}

// SpanNameFormatter should be used with [otelmux.WithSpanNameFormatter] because
// the default formatter only outputs the route without the http method.
func SpanNameFormatter(routeName string, r *http.Request) string {
return r.Method + " " + routeName
}
22 changes: 22 additions & 0 deletions contrib/gorilla/mux/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mux

import "go.opentelemetry.io/otel/trace"

type options struct {
tracerProvider trace.TracerProvider
operationName string
}

type Option func(*options)

func WithTracerProvider(tracerProvider trace.TracerProvider) Option {
return func(o *options) {
o.tracerProvider = tracerProvider
}
}

func WithOperationName(name string) Option {
return func(o *options) {
o.operationName = name
}
}

0 comments on commit e1b06b6

Please sign in to comment.