-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
67 lines (60 loc) · 2.24 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Package tracing provides utilities for working with open telemetry tracing.
*/
package tracing
import (
"log"
"go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
)
/*
Open Telemetry tracing with Gin:
1) Initialize the tracing (see main.go)
2) Enable trace propagation (see tracePropagation below)
3) Use handler middleware (see main.go)
import "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
router.Use(otelgin.Middleware("virtual-service"))
4) For http client, create a Request from Context (see backend.go)
newCtx, span := b.tracer.Start(ctx, "backendHTTP.fetch")
req, errReq := http.NewRequestWithContext(newCtx, "GET", u, nil)
client := http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
resp, errGet := client.Do(req)
*/
// TracerProvider creates a trace provider.
func TracerProvider(service, url string) (*tracesdk.TracerProvider, error) {
log.Printf("tracerProvider: service=%s collector=%s", service, url)
// Create the Jaeger exporter
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url)))
if err != nil {
return nil, err
}
tp := tracesdk.NewTracerProvider(
// Always be sure to batch in production.
tracesdk.WithBatcher(exp),
// Record information about this application in a Resource.
tracesdk.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(service),
//attribute.String("environment", environment),
//attribute.Int64("ID", id),
)),
)
return tp, nil
}
// TracePropagation enables trace propagation.
func TracePropagation() {
// In order to propagate trace context over the wire, a propagator must be registered with the OpenTelemetry API.
// https://opentelemetry.io/docs/instrumentation/go/manual/
//otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
b3.New(b3.WithInjectEncoding(b3.B3MultipleHeader)),
//propagation.Baggage{},
//propagation.TraceContext{},
//ot.OT{},
))
}