-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgintrace.go
245 lines (221 loc) · 7.31 KB
/
gintrace.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Based on https://github.com/DataDog/dd-trace-go/blob/8fb554ff7cf694267f9077ae35e27ce4689ed8b6/contrib/gin-gonic/gin/gintrace.go
package otelgin // import "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
import (
"bytes"
"fmt"
"io"
"net/http"
"time"
otelmetric "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"github.com/gin-gonic/gin"
"github.com/Cyprinus12138/otelgin/internal/semconvutil"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
oteltrace "go.opentelemetry.io/otel/trace"
)
const (
tracerKey = "otel-go-contrib-tracer"
meterKey = "otel-go-contrib-meter"
// ScopeName is the instrumentation scope name.
ScopeName = "github.com/Cyprinus12138/otelgin"
role = "server"
one = 1
)
// Middleware returns middleware that will trace incoming requests.
// The service parameter should describe the name of the (virtual)
// server handling the request.
func Middleware(service string, opts ...Option) gin.HandlerFunc {
var err error
cfg := config{}
for _, opt := range opts {
opt.apply(&cfg)
}
if cfg.TracerProvider == nil {
cfg.TracerProvider = otel.GetTracerProvider()
}
if cfg.MeterProvider == nil {
cfg.MeterProvider = otel.GetMeterProvider()
}
tracer := cfg.TracerProvider.Tracer(
ScopeName,
oteltrace.WithInstrumentationVersion(Version()),
)
meter := cfg.MeterProvider.Meter(
ScopeName,
otelmetric.WithInstrumentationVersion(Version()),
)
if cfg.Propagators == nil {
cfg.Propagators = otel.GetTextMapPropagator()
}
cfg.reqDuration, err = meter.Float64Histogram("http."+role+".request.duration",
otelmetric.WithDescription("Measures the duration of inbound RPC."),
otelmetric.WithUnit("ms"))
if err != nil {
otel.Handle(err)
if cfg.reqDuration == nil {
cfg.reqDuration = noop.Float64Histogram{}
}
}
cfg.reqSize, err = meter.Int64UpDownCounter("http."+role+".request.body.size",
otelmetric.WithDescription("Measures size of RPC request messages (uncompressed)."),
otelmetric.WithUnit("By"))
if err != nil {
otel.Handle(err)
if cfg.reqSize == nil {
cfg.reqSize = noop.Int64UpDownCounter{}
}
}
cfg.respSize, err = meter.Int64UpDownCounter("http."+role+".response.body.size",
otelmetric.WithDescription("Measures size of RPC response messages (uncompressed)."),
otelmetric.WithUnit("By"))
if err != nil {
otel.Handle(err)
if cfg.respSize == nil {
cfg.respSize = noop.Int64UpDownCounter{}
}
}
cfg.activeReqs, err = meter.Int64UpDownCounter("http."+role+".active_requests",
otelmetric.WithDescription("Measures the number of messages received per RPC. Should be 1 for all non-streaming RPCs."),
otelmetric.WithUnit("{count}"))
if err != nil {
otel.Handle(err)
if cfg.activeReqs == nil {
cfg.activeReqs = noop.Int64UpDownCounter{}
}
}
return func(c *gin.Context) {
var (
metricAttrs []attribute.KeyValue
rAttr attribute.KeyValue
)
for _, f := range cfg.Filters {
if !f(c.Request) {
// Serve the request to the next middleware
// if a filter rejects the request.
c.Next()
return
}
}
c.Set(tracerKey, tracer)
c.Set(meterKey, meter)
savedCtx := c.Request.Context()
defer func() {
c.Request = c.Request.WithContext(savedCtx)
}()
ctx := cfg.Propagators.Extract(savedCtx, propagation.HeaderCarrier(c.Request.Header))
httpTraceAttrs := semconvutil.HTTPServerRequest(service, c.Request)
opts := []oteltrace.SpanStartOption{
oteltrace.WithAttributes(httpTraceAttrs...),
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
}
metricAttrs = semconvutil.HTTPServerRequestMetrics(service, c.Request)
var spanName string
if cfg.SpanNameFormatter == nil {
spanName = c.FullPath()
} else {
spanName = cfg.SpanNameFormatter(c.Request)
}
if spanName == "" {
spanName = fmt.Sprintf("HTTP %s route not found", c.Request.Method)
} else {
rAttr = semconv.HTTPRoute(spanName)
opts = append(opts, oteltrace.WithAttributes(rAttr))
metricAttrs = append(metricAttrs, rAttr)
}
ctx, span := tracer.Start(ctx, spanName, opts...)
defer span.End()
// pass the span through the request context
c.Request = c.Request.WithContext(ctx)
// calculate the size of the request.
reqSize := calcReqSize(c)
before := time.Now()
// serve the request to the next middleware
c.Next()
// Use floating point division here for higher precision (instead of Millisecond method).
elapsedTime := float64(time.Since(before)) / float64(time.Millisecond)
respSize := c.Writer.Size()
// If nothing written in the response yet, a value of -1 may be returned.
if respSize < 0 {
respSize = 0
}
status := c.Writer.Status()
span.SetStatus(semconvutil.HTTPServerStatus(status))
cfg.reqSize.Add(ctx, int64(reqSize), otelmetric.WithAttributes(metricAttrs...))
cfg.respSize.Add(ctx, int64(respSize), otelmetric.WithAttributes(metricAttrs...))
if status > 0 {
statusAttr := semconv.HTTPStatusCode(status)
span.SetAttributes(statusAttr)
metricAttrs = append(metricAttrs, statusAttr)
}
if len(c.Errors) > 0 {
errAttr := attribute.String("gin.errors", c.Errors.String())
span.SetAttributes(errAttr)
metricAttrs = append(metricAttrs, errAttr)
}
cfg.reqDuration.Record(ctx, elapsedTime, otelmetric.WithAttributes(metricAttrs...))
cfg.activeReqs.Add(ctx, one, otelmetric.WithAttributes(metricAttrs...))
}
}
// calcReqSize returns the total size of the request.
// It will calculate the header size by iterate all the header KVs
// and add with body size.
func calcReqSize(c *gin.Context) int {
// Read the request body
body, err := io.ReadAll(c.Request.Body)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "failed to read request body"})
return 0
}
// Restore the request body for further processing
c.Request.Body = io.NopCloser(bytes.NewReader(body))
// Calculate the size of headers
headerSize := 0
for name, values := range c.Request.Header {
headerSize += len(name) + 2 // Colon and space
for _, value := range values {
headerSize += len(value)
}
}
// Calculate the total size of the request (headers + body)
return headerSize + len(body)
}
// HTML will trace the rendering of the template as a child of the
// span in the given context. This is a replacement for
// gin.Context.HTML function - it invokes the original function after
// setting up the span.
func HTML(c *gin.Context, code int, name string, obj interface{}) {
var tracer oteltrace.Tracer
tracerInterface, ok := c.Get(tracerKey)
if ok {
tracer, ok = tracerInterface.(oteltrace.Tracer)
}
if !ok {
tracer = otel.GetTracerProvider().Tracer(
ScopeName,
oteltrace.WithInstrumentationVersion(Version()),
)
}
savedContext := c.Request.Context()
defer func() {
c.Request = c.Request.WithContext(savedContext)
}()
opt := oteltrace.WithAttributes(attribute.String("go.template", name))
_, span := tracer.Start(savedContext, "gin.renderer.html", opt)
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("error rendering template:%s: %s", name, r)
span.RecordError(err)
span.SetStatus(codes.Error, "template failure")
span.End()
panic(r)
}
span.End()
}()
c.HTML(code, name, obj)
}