/
http.go
35 lines (29 loc) · 1.11 KB
/
http.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
package middleware
import (
"context"
"fmt"
"net/http"
cmotel "github.com/coordimap/cm-otel-go"
"go.opentelemetry.io/otel"
)
// CoordimapMiddleware initiates the cmOtel object and creates the first span that holds information about the endpoint being called.
func CoordimapMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
var cmOtel cmotel.CMOtel
prefix := cmotel.GetEnvWithPrefix("", cmotel.EnvCmPrefix)
cmOtel = cmotel.New(
otel.Tracer(cmotel.GetEnvWithPrefix(prefix, cmotel.EnvTracerName)),
cmotel.GetEnvWithPrefix(prefix, cmotel.EnvServiceName),
)
traceParentsMap, errTraceParentsMap := cmotel.UnmarshalToSpanMap(r.Header.Get(cmotel.EnvTraceParentsMapHeaderName))
if errTraceParentsMap == nil {
for key, val := range traceParentsMap {
if errSet := cmOtel.SetSpanFromTraceparent(key, val); errSet != nil {
fmt.Printf("could not set span %s from traceparent because %s", key, errSet.Error())
}
}
}
ctx := context.WithValue(r.Context(), cmotel.ContextKey, cmOtel)
next.ServeHTTP(rw, r.WithContext(ctx))
})
}