Skip to content

Commit

Permalink
Refactor: move ExtractTraceID functions to tracing package
Browse files Browse the repository at this point in the history
Signed-off-by: Bryan Boreham <[email protected]>
  • Loading branch information
bboreham committed Aug 26, 2021
1 parent e649eff commit fbf3e28
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
31 changes: 0 additions & 31 deletions middleware/http_tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
jaeger "github.com/uber/jaeger-client-go"
"golang.org/x/net/context"
)

// Dummy dependency to enforce that we have a nethttp version newer
Expand Down Expand Up @@ -40,32 +38,3 @@ func (t Tracer) Wrap(next http.Handler) http.Handler {

return nethttp.Middleware(opentracing.GlobalTracer(), next, options...)
}

// ExtractTraceID extracts the trace id, if any from the context.
func ExtractTraceID(ctx context.Context) (string, bool) {
sp := opentracing.SpanFromContext(ctx)
if sp == nil {
return "", false
}
sctx, ok := sp.Context().(jaeger.SpanContext)
if !ok {
return "", false
}

return sctx.TraceID().String(), true
}

// ExtractSampledTraceID works like ExtractTraceID but the returned bool is only
// true if the returned trace id is sampled.
func ExtractSampledTraceID(ctx context.Context) (string, bool) {
sp := opentracing.SpanFromContext(ctx)
if sp == nil {
return "", false
}
sctx, ok := sp.Context().(jaeger.SpanContext)
if !ok {
return "", false
}

return sctx.TraceID().String(), sctx.IsSampled()
}
3 changes: 2 additions & 1 deletion middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/weaveworks/common/logging"
"github.com/weaveworks/common/tracing"
"github.com/weaveworks/common/user"
)

Expand All @@ -21,7 +22,7 @@ type Log struct {
// logWithRequest information from the request and context as fields.
func (l Log) logWithRequest(r *http.Request) logging.Interface {
localLog := l.Log
traceID, ok := ExtractTraceID(r.Context())
traceID, ok := tracing.ExtractTraceID(r.Context())
if ok {
localLog = localLog.WithField("traceID", traceID)
}
Expand Down
32 changes: 32 additions & 0 deletions tracing/tracing.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package tracing

import (
"context"
"io"

"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
jaeger "github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
jaegerprom "github.com/uber/jaeger-lib/metrics/prometheus"
)
Expand Down Expand Up @@ -45,3 +48,32 @@ func NewFromEnv(serviceName string, options ...jaegercfg.Option) (io.Closer, err

return installJaeger(serviceName, cfg, options...)
}

// ExtractTraceID extracts the trace id, if any from the context.
func ExtractTraceID(ctx context.Context) (string, bool) {
sp := opentracing.SpanFromContext(ctx)
if sp == nil {
return "", false
}
sctx, ok := sp.Context().(jaeger.SpanContext)
if !ok {
return "", false
}

return sctx.TraceID().String(), true
}

// ExtractSampledTraceID works like ExtractTraceID but the returned bool is only
// true if the returned trace id is sampled.
func ExtractSampledTraceID(ctx context.Context) (string, bool) {
sp := opentracing.SpanFromContext(ctx)
if sp == nil {
return "", false
}
sctx, ok := sp.Context().(jaeger.SpanContext)
if !ok {
return "", false
}

return sctx.TraceID().String(), sctx.IsSampled()
}

0 comments on commit fbf3e28

Please sign in to comment.