Skip to content

feat: tracing improvements #4988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! feat: move tracing middleware higher up the middleware stack
  • Loading branch information
deansheather committed Nov 17, 2022
commit 7383e8c016dafb64ae15e1ce9fc4c3709a605384
1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func New(options *Options) *API {

r.Use(
httpmw.Recover(api.Logger),
tracing.StatusWriterMiddleware,
tracing.Middleware(api.TracerProvider),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will trace every static HTTP file... is that ok?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a filter within the middleware itself (and a test) to ensure that this is skipped on non-API routes.

httpmw.AttachRequestID,
httpmw.ExtractRealIP(api.RealIPConfig),
Expand Down
9 changes: 7 additions & 2 deletions coderd/httpmw/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpmw

import (
"context"
"fmt"
"net/http"
"time"

Expand All @@ -12,9 +13,13 @@ import (

func Logger(log slog.Logger) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
start := time.Now()
sw := &tracing.StatusWriter{ResponseWriter: w}

sw, ok := rw.(*tracing.StatusWriter)
if !ok {
panic(fmt.Sprintf("ResponseWriter not a *tracing.StatusWriter; got %T", rw))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's runtime, right? Isn't it risky to panic here? I understand that you assume that it will be recovered.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response writer will always be a *tracing.StatusWriter, so I think it's fine for us to panic here as this is a developer error. We panic in a other places due to developer errors as well, like the middleware context value stuff also in the httpmw package.

We also already do this exact panic in the tracing middleware too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit that I'm not a fan of it, but I understand why it's safe here 👍

}

httplog := log.With(
slog.F("host", httpapi.RequestHost(r)),
Expand Down
1 change: 1 addition & 0 deletions coderd/tracing/httpmw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (f *fakeTracer) Tracer(_ string, _ ...trace.TracerOption) trace.Tracer {
return f
}

// Start implements trace.Tracer.
func (f *fakeTracer) Start(ctx context.Context, _ string, _ ...trace.SpanStartOption) (context.Context, trace.Span) {
atomic.AddInt64(&f.startCalled, 1)
return ctx, tracing.NoopSpan
Expand Down
7 changes: 7 additions & 0 deletions coderd/tracing/status_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ type StatusWriter struct {
wroteHeader bool
}

func StatusWriterMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
sw := &StatusWriter{ResponseWriter: rw}
next.ServeHTTP(sw, r)
})
}

func (w *StatusWriter) WriteHeader(status int) {
if !w.wroteHeader {
w.Status = status
Expand Down