/
trace.go
67 lines (59 loc) · 2.47 KB
/
trace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package common
import (
"context"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
browsertrace "github.com/grafana/xk6-browser/trace"
)
// Tracer defines the interface with the tracing methods used in the common package.
type Tracer interface {
TraceAPICall(
ctx context.Context, targetID string, spanName string, opts ...trace.SpanStartOption,
) (context.Context, trace.Span)
TraceNavigation(
ctx context.Context, targetID string, opts ...trace.SpanStartOption,
) (context.Context, trace.Span)
TraceEvent(
ctx context.Context, targetID string, eventName string, spanID string, opts ...trace.SpanStartOption,
) (context.Context, trace.Span)
}
// TraceAPICall is a helper method that retrieves the Tracer from the given ctx and
// calls its TraceAPICall implementation. If the Tracer is not present in the given
// ctx, it returns a noopSpan and the given context.
func TraceAPICall(
ctx context.Context, targetID string, spanName string, opts ...trace.SpanStartOption,
) (context.Context, trace.Span) {
if tracer := GetTracer(ctx); tracer != nil {
return tracer.TraceAPICall(ctx, targetID, spanName, opts...)
}
return ctx, browsertrace.NoopSpan{}
}
// TraceNavigation is a helper method that retrieves the Tracer from the given ctx and
// calls its TraceNavigation implementation. If the Tracer is not present in the given
// ctx, it returns a noopSpan and the given context.
func TraceNavigation(
ctx context.Context, targetID string, opts ...trace.SpanStartOption,
) (context.Context, trace.Span) {
if tracer := GetTracer(ctx); tracer != nil {
return tracer.TraceNavigation(ctx, targetID, opts...)
}
return ctx, browsertrace.NoopSpan{}
}
// TraceEvent is a helper method that retrieves the Tracer from the given ctx and
// calls its TraceEvent implementation. If the Tracer is not present in the given
// ctx, it returns a noopSpan and the given context.
func TraceEvent(
ctx context.Context, targetID string, eventName string, spanID string, options ...trace.SpanStartOption,
) (context.Context, trace.Span) {
if tracer := GetTracer(ctx); tracer != nil {
return tracer.TraceEvent(ctx, targetID, eventName, spanID, options...)
}
return ctx, browsertrace.NoopSpan{}
}
// spanRecordError will set the status of the span to error and record the
// error on the span. Check the documentation for trace.SetStatus and
// trace.RecordError for more details.
func spanRecordError(span trace.Span, err error) {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
}