Skip to content

Commit

Permalink
Add bool param to middleware for decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
renaz6 committed Jun 5, 2023
1 parent 7e49b82 commit 6737d67
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package candlelight

import (
"context"
"crypto/rand"
"encoding/base64"
"net/http"
Expand Down Expand Up @@ -62,34 +63,42 @@ func (traceConfig *TraceConfig) TraceMiddleware(delegate http.Handler) http.Hand
})
}

// EchoFirstNodeTraceInfo captures the trace information from a request and writes it
// back in the response headers if the request is the first one in the trace path.
func EchoFirstTraceNodeInfo(propagator propagation.TextMapPropagator) func(http.Handler) http.Handler {
// EchoFirstNodeTraceInfo captures the trace information from a request, writes it
// back in the response headers, and adds it to the request's context
// It can also decode the request and save the resulting WRP object in the context if isDecodable is true
func EchoFirstTraceNodeInfo(propagator propagation.TextMapPropagator, isDecodable bool) func(http.Handler) http.Handler {

Check warning on line 69 in middleware.go

View check run for this annotation

Codecov / codecov/patch

middleware.go#L69

Added line #L69 was not covered by tests
return func(delegate http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

if req, err := wrphttp.DecodeRequest(r, nil); err == nil {
r = req
}
var ctx context.Context

Check warning on line 73 in middleware.go

View check run for this annotation

Codecov / codecov/patch

middleware.go#L73

Added line #L73 was not covered by tests

var traceHeaders []string
ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
if msg, ok := wrpcontext.Get[*wrp.Message](ctx); ok {
traceHeaders = msg.Headers
}
if isDecodable {
if req, err := wrphttp.DecodeRequest(r, nil); err == nil {
r = req
}

Check warning on line 78 in middleware.go

View check run for this annotation

Codecov / codecov/patch

middleware.go#L75-L78

Added lines #L75 - L78 were not covered by tests

// Iterate through the trace headers (if any), format them, and add them to ctx
var tmp propagation.TextMapCarrier = propagation.MapCarrier{}
for _, f := range traceHeaders {
if f != "" {
parts := strings.Split(f, ":")
// Remove leading space if there's any
parts[1] = strings.Trim(parts[1], " ")
tmp.Set(parts[0], parts[1])
var traceHeaders []string
ctx = propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
if msg, ok := wrpcontext.Get[*wrp.Message](ctx); ok {
traceHeaders = msg.Headers

Check warning on line 83 in middleware.go

View check run for this annotation

Codecov / codecov/patch

middleware.go#L80-L83

Added lines #L80 - L83 were not covered by tests
}

// Iterate through the trace headers (if any), format them, and add them to ctx
var tmp propagation.TextMapCarrier = propagation.MapCarrier{}
for _, f := range traceHeaders {
if f != "" {
parts := strings.Split(f, ":")
// Remove leading space if there's any
parts[1] = strings.Trim(parts[1], " ")
tmp.Set(parts[0], parts[1])
}

Check warning on line 94 in middleware.go

View check run for this annotation

Codecov / codecov/patch

middleware.go#L87-L94

Added lines #L87 - L94 were not covered by tests
}

ctx = propagation.TraceContext{}.Extract(ctx, tmp)
} else {
ctx = propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))

Check warning on line 99 in middleware.go

View check run for this annotation

Codecov / codecov/patch

middleware.go#L97-L99

Added lines #L97 - L99 were not covered by tests
}

ctx = propagation.TraceContext{}.Extract(ctx, tmp)
sc := trace.SpanContextFromContext(ctx)
if sc.IsValid() {
w.Header().Set(spanIDHeaderName, sc.SpanID().String())
Expand Down

0 comments on commit 6737d67

Please sign in to comment.