Skip to content

Commit

Permalink
Fix Request Tracing
Browse files Browse the repository at this point in the history
Quote, unquote.  But it does allow us to generate an ID in a client and
display it in logs and in jaeger.  Once I figure out how to typescript
this will use the full trace context specification.
  • Loading branch information
spjmurray committed Mar 4, 2024
1 parent 4ed60f6 commit 9758d95
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/server/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
Expand Down Expand Up @@ -125,18 +126,18 @@ func (*LoggingSpanProcessor) ForceFlush(ctx context.Context) error {

// Logger attaches logging context to the request.
func Logger() func(next http.Handler) http.Handler {
// TODO: this needs an implmenetation of https://www.w3.org/TR/trace-context/.
// Like everything here, OpenTelemetry is very good at doing nothing by default.
propagator := otel.GetTextMapPropagator()

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Extract the tracing information from the HTTP headers. See above
// for what this entails.
ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))

var attributes []attribute.KeyValue

// Extract the tracing information from the HTTP headers.
ctx := otel.GetTextMapPropagator().Extract(r.Context(), propagation.HeaderCarrier(r.Header))
baggage := baggage.FromContext(ctx)

for _, member := range baggage.Members() {
attributes = append(attributes, attribute.Key("baggage."+member.Key()).String(member.Value()))
}

// Add in service information.
attributes = append(attributes, semconv.ServiceName(constants.Application))
attributes = append(attributes, semconv.ServiceVersion(constants.Version))
Expand Down
4 changes: 4 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/pflag"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/trace"

"github.com/unikorn-cloud/unikorn/pkg/server/generated"
Expand Down Expand Up @@ -68,6 +69,9 @@ func (s *Server) SetupLogging() {
func (s *Server) SetupOpenTelemetry(ctx context.Context) error {
otel.SetLogger(log.Log)

// TODO: use a full w3c trace context.
otel.SetTextMapPropagator(propagation.Baggage{})

opts := []trace.TracerProviderOption{
trace.WithSpanProcessor(&middleware.LoggingSpanProcessor{}),
}
Expand Down

0 comments on commit 9758d95

Please sign in to comment.