-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathotel.go
133 lines (107 loc) · 3.19 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package otel
import (
"fmt"
"net/http"
"strings"
"github.com/alexfalkowski/go-service/meta"
"github.com/alexfalkowski/go-service/otel"
tstrings "github.com/alexfalkowski/go-service/transport/strings"
"github.com/alexfalkowski/go-service/version"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
"go.opentelemetry.io/otel/semconv/v1.18.0/httpconv"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
)
// TracerParams for otel.
type TracerParams struct {
fx.In
Lifecycle fx.Lifecycle
Config *otel.Config
Version version.Version
}
// NewTracer for otel.
func NewTracer(params TracerParams) (Tracer, error) {
return otel.NewTracer(otel.TracerParams{Lifecycle: params.Lifecycle, Name: "http", Config: params.Config, Version: params.Version})
}
// Tracer for otel.
type Tracer trace.Tracer
// Handler for otel.
type Handler struct {
tracer Tracer
http.Handler
}
// NewHandler for otel.
func NewHandler(tracer Tracer, handler http.Handler) *Handler {
return &Handler{tracer: tracer, Handler: handler}
}
// ServeHTTP for otel.
func (h *Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
service, method := req.URL.Path, strings.ToLower(req.Method)
if tstrings.IsHealth(service) {
h.Handler.ServeHTTP(resp, req)
return
}
ctx := extract(req.Context(), req)
attrs := []attribute.KeyValue{
semconv.HTTPRoute(service),
semconv.HTTPMethod(method),
}
attrs = append(attrs, httpconv.ServerRequest("", req)...)
operationName := fmt.Sprintf("%s %s", method, service)
ctx, span := h.tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)),
operationName,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attrs...),
)
defer span.End()
h.Handler.ServeHTTP(resp, req.WithContext(ctx))
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
}
// NewRoundTripper for otr.
func NewRoundTripper(tracer Tracer, hrt http.RoundTripper) *RoundTripper {
return &RoundTripper{tracer: tracer, RoundTripper: hrt}
}
// RoundTripper for otel.
type RoundTripper struct {
tracer Tracer
http.RoundTripper
}
// RoundTrip for otel.
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if tstrings.IsHealth(req.URL.String()) {
return r.RoundTripper.RoundTrip(req)
}
service, method := req.URL.Hostname(), strings.ToLower(req.Method)
ctx := req.Context()
operationName := fmt.Sprintf("%s %s", method, service)
attrs := []attribute.KeyValue{
semconv.HTTPRoute(service),
semconv.HTTPMethod(method),
}
attrs = append(attrs, httpconv.ClientRequest(req)...)
ctx, span := r.tracer.Start(
ctx,
operationName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
)
defer span.End()
inject(ctx, req)
resp, err := r.RoundTripper.RoundTrip(req.WithContext(ctx))
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return nil, err
}
span.SetAttributes(semconv.HTTPStatusCode(resp.StatusCode))
span.SetAttributes(httpconv.ClientResponse(resp)...)
return resp, nil
}