forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macaron.go
59 lines (49 loc) · 1.93 KB
/
macaron.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelmacaron // import "go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron"
import (
"fmt"
"net/http"
"gopkg.in/macaron.v1"
"go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron/internal/semconvutil"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
oteltrace "go.opentelemetry.io/otel/trace"
)
// ScopeName is the instrumentation scope name.
const ScopeName = "go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron"
// Middleware returns a macaron Handler to trace requests to the server.
func Middleware(service string, opts ...Option) macaron.Handler {
cfg := newConfig(opts)
tracer := cfg.TracerProvider.Tracer(
ScopeName,
oteltrace.WithInstrumentationVersion(Version()),
)
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
savedCtx := c.Req.Request.Context()
defer func() {
c.Req.Request = c.Req.Request.WithContext(savedCtx)
}()
ctx := cfg.Propagators.Extract(savedCtx, propagation.HeaderCarrier(c.Req.Header))
opts := []oteltrace.SpanStartOption{
oteltrace.WithAttributes(semconvutil.HTTPServerRequest(service, c.Req.Request)...),
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
}
// TODO: span name should be router template not the actual request path, eg /user/:id vs /user/123
spanName := c.Req.RequestURI
if spanName == "" {
spanName = fmt.Sprintf("HTTP %s route not found", c.Req.Method)
}
ctx, span := tracer.Start(ctx, spanName, opts...)
defer span.End()
// pass the span through the request context
c.Req.Request = c.Req.Request.WithContext(ctx)
// serve the request to the next middleware
c.Next()
status := c.Resp.Status()
span.SetStatus(semconvutil.HTTPServerStatus(status))
if status > 0 {
span.SetAttributes(semconv.HTTPStatusCode(status))
}
}
}