-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathotel.go
More file actions
171 lines (144 loc) · 4.35 KB
/
otel.go
File metadata and controls
171 lines (144 loc) · 4.35 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package docker
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"github.com/jasoet/pkg/v2/otel"
)
// otelInstrumentation holds OpenTelemetry instrumentation components.
type otelInstrumentation struct {
tracer trace.Tracer
meter metric.Meter
config *otel.Config
enabled bool
// Metrics
containersStarted metric.Int64Counter
containersStopped metric.Int64Counter
containersTerminated metric.Int64Counter
containersRestarted metric.Int64Counter
containerErrors metric.Int64Counter
}
// newOTelInstrumentation creates OpenTelemetry instrumentation.
func newOTelInstrumentation(cfg *otel.Config) *otelInstrumentation {
if cfg == nil {
return &otelInstrumentation{enabled: false}
}
inst := &otelInstrumentation{
config: cfg,
enabled: true,
}
// Get tracer
if cfg.TracerProvider != nil {
inst.tracer = cfg.TracerProvider.Tracer(
"github.com/jasoet/pkg/v2/docker",
trace.WithInstrumentationVersion("v2.0.0"),
)
}
// Get meter and create metrics
if cfg.MeterProvider != nil {
inst.meter = cfg.MeterProvider.Meter(
"github.com/jasoet/pkg/v2/docker",
metric.WithInstrumentationVersion("v2.0.0"),
)
// Create counters (errors intentionally ignored - metrics are optional)
inst.containersStarted, _ = inst.meter.Int64Counter( //nolint:errcheck
"docker.containers.started",
metric.WithDescription("Number of containers started"),
metric.WithUnit("{container}"),
)
inst.containersStopped, _ = inst.meter.Int64Counter( //nolint:errcheck
"docker.containers.stopped",
metric.WithDescription("Number of containers stopped"),
metric.WithUnit("{container}"),
)
inst.containersTerminated, _ = inst.meter.Int64Counter( //nolint:errcheck
"docker.containers.terminated",
metric.WithDescription("Number of containers terminated"),
metric.WithUnit("{container}"),
)
inst.containersRestarted, _ = inst.meter.Int64Counter( //nolint:errcheck
"docker.containers.restarted",
metric.WithDescription("Number of containers restarted"),
metric.WithUnit("{container}"),
)
inst.containerErrors, _ = inst.meter.Int64Counter( //nolint:errcheck
"docker.container.errors",
metric.WithDescription("Number of container operation errors"),
metric.WithUnit("{error}"),
)
}
return inst
}
// startSpan starts a new trace span.
func (i *otelInstrumentation) startSpan(ctx context.Context, name string) (context.Context, trace.Span) {
if !i.enabled || i.tracer == nil {
return ctx, trace.SpanFromContext(ctx)
}
return i.tracer.Start(ctx, name)
}
// recordError records an error metric and adds it to the span.
func (i *otelInstrumentation) recordError(ctx context.Context, errorType string, err error) {
if !i.enabled {
return
}
// Add error to span
if span := trace.SpanFromContext(ctx); span.IsRecording() {
span.RecordError(err)
span.SetAttributes(
attribute.String("error.type", errorType),
attribute.String("error.message", err.Error()),
)
}
// Increment error counter
if i.containerErrors != nil {
i.containerErrors.Add(ctx, 1,
metric.WithAttributes(
attribute.String("error.type", errorType),
),
)
}
}
// incrementCounter increments a counter metric.
func (i *otelInstrumentation) incrementCounter(ctx context.Context, counterName string, value int64) {
if !i.enabled {
return
}
var counter metric.Int64Counter
switch counterName {
case "containers_started":
counter = i.containersStarted
case "containers_stopped":
counter = i.containersStopped
case "containers_terminated":
counter = i.containersTerminated
case "containers_restarted":
counter = i.containersRestarted
}
if counter != nil {
counter.Add(ctx, value)
}
}
// addSpanAttributes adds common attributes to the current span.
func (i *otelInstrumentation) addSpanAttributes(ctx context.Context, attrs ...attribute.KeyValue) {
if !i.enabled {
return
}
if span := trace.SpanFromContext(ctx); span.IsRecording() {
span.SetAttributes(attrs...)
}
}
// setSpanStatus sets the span status.
func (i *otelInstrumentation) setSpanStatus(ctx context.Context, code int, description string) {
if !i.enabled {
return
}
if span := trace.SpanFromContext(ctx); span.IsRecording() {
if code != 0 {
span.SetStatus(codes.Error, description)
} else {
span.SetStatus(codes.Ok, description)
}
}
}