diff --git a/metrics/provider.go b/metrics/provider.go index adeece0..23045f9 100644 --- a/metrics/provider.go +++ b/metrics/provider.go @@ -1,7 +1,6 @@ package metrics import ( - "context" "net/http" "time" ) @@ -23,27 +22,6 @@ type Provider interface { RecordDuration(metricName string, duration time.Duration) } -// Trace represents an active trace that can contain multiple spans and attributes. -type Trace interface { - // StartSpan starts a new span within the trace - StartSpan(name string) Span - - // AddAttribute adds a key-value attribute to the trace - AddAttribute(key string, value interface{}) - - // OnError records an error on the trace - OnError(err error) - - // SetRequest sets HTTP request information on the trace - SetRequest(r Request) - - // SetResponse sets the HTTP response writer for the trace - SetResponse(w http.ResponseWriter) http.ResponseWriter - - // End completes the trace - End() -} - // Request contains HTTP request information for tracing type Request struct { Header http.Header @@ -51,29 +29,3 @@ type Request struct { Method string Transport string } - -// Span represents a timed span within a trace for tracing individual operations. -type Span interface { - // AddAttribute adds a key-value attribute to the span - AddAttribute(key string, value interface{}) - - // End completes the span - End() -} - -// traceContextKey is the context key for storing the current trace -type traceContextKey struct{} - -// TraceKey is the context key for Trace -var TraceKey = traceContextKey{} - -// NewContext returns a new context with the trace attached -func NewContext(ctx context.Context, trace Trace) context.Context { - return context.WithValue(ctx, TraceKey, trace) -} - -// TraceFromContext retrieves the trace from context, if present -func TraceFromContext(ctx context.Context) Trace { - trace, _ := ctx.Value(TraceKey).(Trace) - return trace -} diff --git a/metrics/tracing.go b/metrics/tracing.go index f757dfc..af5c265 100644 --- a/metrics/tracing.go +++ b/metrics/tracing.go @@ -3,8 +3,39 @@ package metrics import ( "context" "fmt" + "net/http" ) +// Trace represents an active trace that can contain multiple spans and attributes. +type Trace interface { + // StartSpan starts a new span within the trace + StartSpan(name string) Span + + // AddAttribute adds a key-value attribute to the trace + AddAttribute(key string, value interface{}) + + // OnError records an error on the trace + OnError(err error) + + // SetRequest sets HTTP request information on the trace + SetRequest(r Request) + + // SetResponse sets the HTTP response writer for the trace + SetResponse(w http.ResponseWriter) http.ResponseWriter + + // End completes the trace + End() +} + +// Span represents a timed span within a trace for tracing individual operations. +type Span interface { + // AddAttribute adds a key-value attribute to the span + AddAttribute(key string, value interface{}) + + // End completes the span + End() +} + // TraceMethodCall traces a method call with a given struct/package and method names func TraceMethodCall(ctx context.Context, structOrPackageName, methodName string) *MethodTracer { trace := TraceFromContext(ctx) @@ -68,3 +99,20 @@ func (t *MethodTracer) End() { t.span.End() } + +// traceContextKey is the context key for storing the current trace +type traceContextKey struct{} + +// TraceKey is the context key for Trace +var TraceKey = traceContextKey{} + +// NewContext returns a new context with the trace attached +func NewContext(ctx context.Context, trace Trace) context.Context { + return context.WithValue(ctx, TraceKey, trace) +} + +// TraceFromContext retrieves the trace from context, if present +func TraceFromContext(ctx context.Context) Trace { + trace, _ := ctx.Value(TraceKey).(Trace) + return trace +}