-
Notifications
You must be signed in to change notification settings - Fork 157
/
telemetry.go
124 lines (107 loc) · 3.37 KB
/
telemetry.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package telemetry
import (
"context"
"fmt"
"strings"
"time"
"github.com/google/uuid"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/credentials"
)
type TracerOpts struct {
ServiceName string
CollectorURL string
Insecure bool
}
func InitTracer(opts *TracerOpts) (func(context.Context) error, error) {
if opts.CollectorURL == "" {
// no-op
return func(context.Context) error {
return nil
}, nil
}
var secureOption otlptracegrpc.Option
if !opts.Insecure {
secureOption = otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, ""))
} else {
secureOption = otlptracegrpc.WithInsecure()
}
exporter, err := otlptrace.New(
context.Background(),
otlptracegrpc.NewClient(
secureOption,
otlptracegrpc.WithEndpoint(opts.CollectorURL),
),
)
if err != nil {
return nil, fmt.Errorf("failed to create exporter: %w", err)
}
resources, err := resource.New(
context.Background(),
resource.WithAttributes(
attribute.String("service.name", opts.ServiceName),
attribute.String("library.language", "go"),
),
)
if err != nil {
return nil, fmt.Errorf("failed to set resources: %w", err)
}
otel.SetTracerProvider(
sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resources),
),
)
return exporter.Shutdown, nil
}
func NewSpan(ctx context.Context, name string) (context.Context, trace.Span) {
ctx, span := otel.Tracer("").Start(ctx, prefixSpanKey(name))
return ctx, span
}
type AttributeKey string
// AttributeKV is a wrapper for otel attributes KV
type AttributeKV struct {
Key AttributeKey
Value any
}
func WithAttributes(span trace.Span, attrs ...AttributeKV) {
for _, attr := range attrs {
if attr.Key != "" {
switch val := attr.Value.(type) {
case uuid.UUID:
span.SetAttributes(attribute.String(prefixSpanKey(string(attr.Key)), val.String()))
case string:
span.SetAttributes(attribute.String(prefixSpanKey(string(attr.Key)), val))
case []string:
span.SetAttributes(attribute.String(prefixSpanKey(string(attr.Key)), strings.Join(val, ", ")))
case int:
span.SetAttributes(attribute.Int(prefixSpanKey(string(attr.Key)), val))
case int64:
span.SetAttributes(attribute.Int64(prefixSpanKey(string(attr.Key)), val))
case int32:
span.SetAttributes(attribute.Int64(prefixSpanKey(string(attr.Key)), int64(val)))
case uint:
span.SetAttributes(attribute.Int(prefixSpanKey(string(attr.Key)), int(val)))
case float64:
span.SetAttributes(attribute.Float64(prefixSpanKey(string(attr.Key)), val))
case bool:
span.SetAttributes(attribute.Bool(prefixSpanKey(string(attr.Key)), val))
case time.Time:
span.SetAttributes(attribute.String(prefixSpanKey(string(attr.Key)), val.String()))
zone, offset := val.Zone()
span.SetAttributes(attribute.String(prefixSpanKey(fmt.Sprintf("%s-timezone", string(attr.Key))), zone))
span.SetAttributes(attribute.Int(prefixSpanKey(fmt.Sprintf("%s-offset", string(attr.Key))), offset))
}
}
}
}
func prefixSpanKey(name string) string {
return fmt.Sprintf("hatchet.run/%s", name)
}