Skip to content
Merged
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
4 changes: 2 additions & 2 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ func New(options *Options) *API {

apps := func(r chi.Router) {
r.Use(
tracing.Middleware(api.TracerProvider),
httpmw.RateLimitPerMinute(options.APIRateLimit),
tracing.HTTPMW(api.TracerProvider),
httpmw.ExtractAPIKey(options.Database, oauthConfigs, true),
httpmw.ExtractUserParam(api.Database),
// Extracts the <workspace.agent> from the url
Expand Down Expand Up @@ -227,9 +227,9 @@ func New(options *Options) *API {
})
})
r.Use(
tracing.Middleware(api.TracerProvider),
// Specific routes can specify smaller limits.
httpmw.RateLimitPerMinute(options.APIRateLimit),
tracing.HTTPMW(api.TracerProvider),
)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
httpapi.Write(w, http.StatusOK, codersdk.Response{
Expand Down
14 changes: 9 additions & 5 deletions coderd/tracing/httpmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import (
"go.opentelemetry.io/otel/trace"
)

// HTTPMW adds tracing to http routes.
func HTTPMW(tracerProvider trace.TracerProvider) func(http.Handler) http.Handler {
// Middleware adds tracing to http routes.
func Middleware(tracerProvider trace.TracerProvider) func(http.Handler) http.Handler {
var tracer trace.Tracer
if tracerProvider != nil {
tracer = tracerProvider.Tracer(TracerName)
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if tracerProvider == nil {
if tracer == nil {
next.ServeHTTP(rw, r)
return
}

// start span with default span name. Span name will be updated to "method route" format once request finishes.
ctx, span := tracerProvider.Tracer("").Start(r.Context(), fmt.Sprintf("%s %s", r.Method, r.RequestURI))
ctx, span := tracer.Start(r.Context(), fmt.Sprintf("%s %s", r.Method, r.RequestURI))
defer span.End()
r = r.WithContext(ctx)

Expand Down Expand Up @@ -59,5 +63,5 @@ func EndHTTPSpan(r *http.Request, status int, span trace.Span) {
}

func StartSpan(ctx context.Context, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return trace.SpanFromContext(ctx).TracerProvider().Tracer("").Start(ctx, FuncNameSkip(1), opts...)
return trace.SpanFromContext(ctx).TracerProvider().Tracer(TracerName).Start(ctx, FuncNameSkip(1), opts...)
}
2 changes: 2 additions & 0 deletions coderd/tracing/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
)

const TracerName = "coderd"

func FuncName() string {
fnpc, _, _, ok := runtime.Caller(1)
if !ok {
Expand Down