Skip to content

Commit

Permalink
Merge pull request #36 from owncloud/fix/tracing
Browse files Browse the repository at this point in the history
Report trace on a single service basis
  • Loading branch information
refs authored Mar 9, 2020
2 parents 7e0d3ca + 990bd70 commit 5c659fd
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions middleware/tracing.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middleware

import (
"context"
"net/http"

"go.opencensus.io/plugin/ochttp/propagation/tracecontext"
Expand All @@ -10,14 +11,19 @@ import (
// Trace unpacks the request context looking for an existing trace id.
func Trace(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var ctx context.Context
var span *trace.Span

tc := tracecontext.HTTPFormat{}
sc, ok := tc.SpanContextFromRequest(r)
if ok {
// reconstruct span context from request
if sc, ok := tc.SpanContextFromRequest(r); ok {
// if there is one, add it to the new span
ctx, span = trace.StartSpanWithRemoteParent(r.Context(), r.URL.String(), sc)
defer span.End()
} else {
// create a new span if there is no context
ctx, span = trace.StartSpan(r.Context(), r.URL.String())
defer span.End()
}

next.ServeHTTP(w, r.WithContext(ctx))
Expand Down

0 comments on commit 5c659fd

Please sign in to comment.