-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
98 lines (83 loc) · 2.71 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
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
// Copyright The Linux Foundation and its contributors.
// SPDX-License-Identifier: MIT
// The auth0-cas-service-go service.
package main
import (
"context"
"os"
"strconv"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
)
type otelErrorHandler struct{}
func (h *otelErrorHandler) Handle(err error) {
logrus.WithError(err).Error("opentelemetry-go error")
}
func init() {
otel.SetErrorHandler(&otelErrorHandler{})
}
// Initializes an OTLP exporter, and configures the corresponding trace and
// metric providers. Returns a shutdown function.
func initOTLP() func() {
ctx := context.Background()
exp, err := otlptracegrpc.New(ctx, otlptracegrpc.WithInsecure())
if err != nil {
logrus.WithError(err).Fatal("failed to create exporter")
}
bsp := sdktrace.NewBatchSpanProcessor(exp)
opts := []sdktrace.TracerProviderOption{
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(bsp),
}
// If OTEL_SERVICE_NAME name is set, assume the user is using that and
// OTEL_RESOURCE_ATTRIBUTES to define the resource and use autodiscovery.
// If it's not set: create the resource manually with a default service name.
if _, ok := os.LookupEnv("OTEL_SERVICE_NAME"); !ok {
res, err := resource.New(ctx,
resource.WithAttributes(
semconv.ServiceNameKey.String(serviceName),
),
)
if err != nil {
logrus.WithError(err).Fatal("failed to create resource")
}
opts = append(opts, sdktrace.WithResource(res))
}
tracerProvider := sdktrace.NewTracerProvider(opts...)
// Set global propagator to tracecontext (the default is no-op).
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(tracerProvider)
return func() {
if err = tracerProvider.Shutdown(ctx); err != nil {
logrus.WithError(err).Fatal("failed to shutdown provider")
}
if err = exp.Shutdown(ctx); err != nil {
logrus.WithError(err).Fatal("failed to stop exporter")
}
}
}
// convertTraceID converts the OpenTelemetry TraceID and SpanID (a 128-bit
// unsigned int and 64-bit unsigned int represented as a 32-hex-character and
// 16-hex-character lowercase string, respectively) into their Datadog formats
// (both a 64-bit unsigned int).
//
// Source:
// https://docs.datadoghq.com/tracing/connect_logs_and_traces/opentelemetry/
func convertTraceID(id string) string {
if len(id) < 16 {
return ""
}
if len(id) > 16 {
id = id[16:]
}
intValue, err := strconv.ParseUint(id, 16, 64)
if err != nil {
return ""
}
return strconv.FormatUint(intValue, 10)
}