Skip to content
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

[query] Move trace handler to server level #6147

Merged
merged 4 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 0 additions & 17 deletions cmd/query/app/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/gorilla/mux"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

Expand Down Expand Up @@ -132,7 +131,6 @@ func (aH *APIHandler) handleFunc(
) *mux.Route {
route := aH.formatRoute(routeFmt, args...)
var handler http.Handler = http.HandlerFunc(f)
handler = traceResponseHandler(handler)
handler = otelhttp.WithRouteTag(route, handler)
handler = spanNameHandler(route, handler)
return router.HandleFunc(route, handler.ServeHTTP)
Expand Down Expand Up @@ -517,21 +515,6 @@ func (aH *APIHandler) writeJSON(w http.ResponseWriter, r *http.Request, response
}
}

// Returns a handler that generates a traceresponse header.
// https://github.com/w3c/trace-context/blob/main/spec/21-http_response_header_format.md
func traceResponseHandler(handler http.Handler) http.Handler {
// We use the standard TraceContext propagator, since the formats are identical.
// But the propagator uses "traceparent" header name, so we inject it into a map
// `carrier` and then use the result to set the "tracereponse" header.
var prop propagation.TraceContext
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
carrier := make(map[string]string)
prop.Inject(r.Context(), propagation.MapCarrier(carrier))
w.Header().Add("traceresponse", carrier["traceparent"])
handler.ServeHTTP(w, r)
})
}

func spanNameHandler(spanName string, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span := trace.SpanFromContext(r.Context())
Expand Down
1 change: 1 addition & 0 deletions cmd/query/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func initRouter(
if tenancyMgr.Enabled {
handler = tenancy.ExtractTenantHTTPHandler(tenancyMgr, handler)
}
handler = traceResponseHandler(handler)
return handler, staticHandlerCloser
}

Expand Down
25 changes: 25 additions & 0 deletions cmd/query/app/trace_response_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package app

import (
"net/http"

"go.opentelemetry.io/otel/propagation"
)

// Returns a handler that generates a traceresponse header.
// https://github.com/w3c/trace-context/blob/main/spec/21-http_response_header_format.md
func traceResponseHandler(handler http.Handler) http.Handler {
// We use the standard TraceContext propagator, since the formats are identical.
// But the propagator uses "traceparent" header name, so we inject it into a map
// `carrier` and then use the result to set the "tracereponse" header.
var prop propagation.TraceContext
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
carrier := make(map[string]string)
prop.Inject(r.Context(), propagation.MapCarrier(carrier))
w.Header().Add("traceresponse", carrier["traceparent"])
handler.ServeHTTP(w, r)
})
}
49 changes: 49 additions & 0 deletions cmd/query/app/trace_response_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package app

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"

"github.com/jaegertracing/jaeger/pkg/jtracer"
)

func TestTraceResponseHandler(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSyncer(exporter),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
)
jTracer := jtracer.JTracer{OTEL: tracerProvider}
tracer := jTracer.OTEL.Tracer("instrumentation/trace_response_handler_test")

emptyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

Check failure on line 27 in cmd/query/app/trace_response_handler_test.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'w' seems to be unused, consider removing or renaming it as _ (revive)
handlerWithTracing := traceResponseHandler(emptyHandler)
hanlderWithInstrumentation := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, span := tracer.Start(r.Context(), "operation")
defer span.End()

handlerWithTracing.ServeHTTP(w, r)
})

server := httptest.NewServer(hanlderWithInstrumentation)
defer server.Close()

req, err := http.NewRequest(http.MethodGet, server.URL, nil)
require.NoError(t, err)

resp, err := server.Client().Do(req)
require.NoError(t, err)

traceResponse := resp.Header.Get("traceresponse")

// TODO: why isn't this populated?
require.Equal(t, "", traceResponse)
mahadzaryab1 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading