/
otel.go
117 lines (102 loc) · 3.35 KB
/
otel.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package middleware
import (
"fmt"
"github.com/ichxxx/shack"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
oteltrace "go.opentelemetry.io/otel/trace"
)
const (
tracerKey = "otel-go-contrib-tracer"
tracerName = "otel-shack"
)
type config struct {
TracerProvider oteltrace.TracerProvider
Propagators propagation.TextMapPropagator
}
// OtelOption specifies instrumentation configuration options.
type OtelOption interface {
apply(*config)
}
type optionFunc func(*config)
func (o optionFunc) apply(c *config) {
o(c)
}
func OpenTelemetry(service string, opts ...OtelOption) shack.Handler {
cfg := config{}
for _, opt := range opts {
opt.apply(&cfg)
}
if cfg.TracerProvider == nil {
cfg.TracerProvider = otel.GetTracerProvider()
}
tracer := cfg.TracerProvider.Tracer(
tracerName,
oteltrace.WithInstrumentationVersion(SemVersion()),
)
if cfg.Propagators == nil {
cfg.Propagators = otel.GetTextMapPropagator()
}
return func(ctx *shack.Context) {
ctx.Set(tracerKey, tracer)
savedCtx := ctx.Request.Context()
defer func() {
ctx.Request.Request = ctx.Request.WithContext(savedCtx)
}()
c := cfg.Propagators.Extract(savedCtx, propagation.HeaderCarrier(ctx.Request.Request.Header))
opts := []oteltrace.SpanStartOption{
oteltrace.WithAttributes(semconv.NetAttributesFromHTTPRequest("tcp", ctx.Request.Request)...),
oteltrace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(ctx.Request.Request)...),
oteltrace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(service, ctx.Request.Path(), ctx.Request.Request)...),
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
}
spanName := ctx.Request.Path()
if spanName == "" {
spanName = fmt.Sprintf("HTTP %s route not found", ctx.Request.Method())
}
c, span := tracer.Start(c, spanName, opts...)
defer span.End()
// pass the span through the request context
ctx.Request.Request = ctx.Request.Request.WithContext(c)
// serve the request to the next middleware
ctx.Next()
status := ctx.Response.StatusCode
attrs := semconv.HTTPAttributesFromHTTPStatusCode(status)
spanStatus, spanMessage := semconv.SpanStatusFromHTTPStatusCode(status)
span.SetAttributes(attrs...)
span.SetStatus(spanStatus, spanMessage)
if ctx.Err != nil {
span.SetAttributes(attribute.String("error", ctx.Err.Error()))
}
}
}
// WithPropagators specifies propagators to use for extracting
// information from the HTTP requests. If none are specified, global
// ones will be used.
func WithPropagators(propagators propagation.TextMapPropagator) OtelOption {
return optionFunc(func(cfg *config) {
if propagators != nil {
cfg.Propagators = propagators
}
})
}
// WithTracerProvider specifies a tracer provider to use for creating a tracer.
// If none is specified, the global provider is used.
func WithTracerProvider(provider oteltrace.TracerProvider) OtelOption {
return optionFunc(func(cfg *config) {
if provider != nil {
cfg.TracerProvider = provider
}
})
}
// Version is the current release version of the gin instrumentation.
func Version() string {
return "0.29.0"
// This string is updated by the pre_release.sh script during release
}
// SemVersion is the semantic version to be supplied to tracer/meter creation.
func SemVersion() string {
return "semver:" + Version()
}