-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtracing.go
46 lines (35 loc) · 1.37 KB
/
tracing.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
package ops
import (
"context"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
"go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/semconv/v1.17.0/httpconv"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
)
// GetTracingHeaders returns the tracing headers
func GetTracingHeaders(ctx context.Context) map[string]string {
// extract tracing information from the context
carrier := propagation.MapCarrier{}
otel.GetTextMapPropagator().Inject(ctx, &carrier)
return carrier
}
// traceHTTPAttributes sets relevant tracing attributes based on provided fasthttp.Request and fasthttp.Response.
func traceHTTPAttributes(span trace.Span, req *fasthttp.Request, resp *fasthttp.Response, err error) {
if err != nil {
span.RecordError(err)
}
span.SetAttributes(
// request attributes
semconv.HTTPURLKey.String(string(utils.CopyBytes(req.URI().FullURI()))),
semconv.HTTPMethodKey.String(string(utils.CopyBytes(req.Header.Method()))),
semconv.HTTPRequestContentLengthKey.Int(req.Header.ContentLength()),
// response attributes
semconv.HTTPStatusCodeKey.Int(resp.StatusCode()),
semconv.HTTPFlavorKey.String(string(utils.CopyBytes(resp.Header.Protocol()))),
semconv.HTTPResponseContentLengthKey.Int(resp.Header.ContentLength()),
)
span.SetStatus(httpconv.ClientStatus(resp.StatusCode()))
}