Repository navigation
Expand file tree
/
Copy pathtelemetry.go
More file actions
59 lines (54 loc) · 1.75 KB
/
Copy pathtelemetry.go
File metadata and controls
59 lines (54 loc) · 1.75 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
package copilot
import (
"context"
"strconv"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
)
func (config *TelemetryConfig) environment() map[string]string {
if config == nil {
return nil
}
env := map[string]string{"COPILOT_OTEL_ENABLED": "true"}
for key, value := range map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": config.OTLPEndpoint,
"OTEL_EXPORTER_OTLP_PROTOCOL": config.OTLPProtocol,
"COPILOT_OTEL_FILE_EXPORTER_PATH": config.FilePath,
"COPILOT_OTEL_EXPORTER_TYPE": config.ExporterType,
"COPILOT_OTEL_SOURCE_NAME": config.SourceName,
} {
if value != "" {
env[key] = value
}
}
if config.CaptureContent != nil {
mode := "NO_CONTENT"
if *config.CaptureContent {
mode = "SPAN_ONLY"
}
env["OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT"] = mode
env["OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"] = strconv.FormatBool(*config.CaptureContent)
}
return env
}
// getTraceContext extracts the current W3C Trace Context (traceparent/tracestate)
// from the Go context using the global OTel propagator.
func getTraceContext(ctx context.Context) (traceparent, tracestate string) {
carrier := propagation.MapCarrier{}
otel.GetTextMapPropagator().Inject(ctx, carrier)
return carrier.Get("traceparent"), carrier.Get("tracestate")
}
// contextWithTraceParent returns a new context with trace context extracted from
// the provided W3C traceparent and tracestate headers.
func contextWithTraceParent(ctx context.Context, traceparent, tracestate string) context.Context {
if traceparent == "" {
return ctx
}
carrier := propagation.MapCarrier{
"traceparent": traceparent,
}
if tracestate != "" {
carrier["tracestate"] = tracestate
}
return otel.GetTextMapPropagator().Extract(ctx, carrier)
}