Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions metrics/provider.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package metrics

import (
"context"
"net/http"
"time"
)
Expand All @@ -23,57 +22,10 @@ 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
URL interface{} // *url.URL
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
}
48 changes: 48 additions & 0 deletions metrics/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}