-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathobservability.go
141 lines (119 loc) · 4.18 KB
/
observability.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package provider
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)
const (
OtelMetricExportInterval = 1 * time.Minute
OtelTraceExportInterval = 1 * time.Minute
OtelLogExportInterval = 10 * time.Second
OtelProtocolHTTP = "http"
OtelProtocolGRPC = "grpc"
OtelSpanLimitAttributePerEvent = 16
OtelSpanLimitEventCount = 64
)
func newPropagator() propagation.TextMapPropagator {
return propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
)
}
func newTracerProvider(ctx context.Context, config OtelConfig, serviceResource *resource.Resource) (*trace.TracerProvider, error) {
var exporter trace.SpanExporter
var err error
switch config.OtelProtocol() {
case OtelProtocolGRPC:
exporter, err = otlptracegrpc.New(ctx, otlptracegrpc.WithEndpointURL(config.TracesURL()))
case OtelProtocolHTTP:
exporter, err = otlptracehttp.New(ctx, otlptracehttp.WithEndpointURL(config.TracesURL()))
default:
return nil, fmt.Errorf("unknown observability protocol %q", config.Protocol)
}
if err != nil {
return nil, err
}
spanLimits := trace.SpanLimits{
AttributePerEventCountLimit: OtelSpanLimitAttributePerEvent,
EventCountLimit: OtelSpanLimitEventCount,
}
traceProvider := trace.NewTracerProvider(
trace.WithResource(serviceResource),
trace.WithBatcher(exporter,
trace.WithBatchTimeout(OtelTraceExportInterval),
),
trace.WithRawSpanLimits(spanLimits),
trace.WithSampler(trace.AlwaysSample()),
)
return traceProvider, nil
}
func newMeterProvider(ctx context.Context, config OtelConfig, serviceResource *resource.Resource) (*metric.MeterProvider, error) {
var exporter metric.Exporter
var err error
switch config.OtelProtocol() {
case OtelProtocolGRPC:
exporter, err = otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithEndpointURL(config.MetricsURL()))
case OtelProtocolHTTP:
exporter, err = otlpmetrichttp.New(ctx, otlpmetrichttp.WithEndpointURL(config.MetricsURL()))
default:
return nil, fmt.Errorf("unknown observability protocol %q", config.Protocol)
}
if err != nil {
return nil, err
}
meterProvider := metric.NewMeterProvider(
metric.WithResource(serviceResource),
metric.WithReader(metric.NewPeriodicReader(exporter,
metric.WithInterval(OtelMetricExportInterval))),
)
return meterProvider, nil
}
func newLoggerProvider(ctx context.Context, config OtelConfig, serviceResource *resource.Resource) (*log.LoggerProvider, error) {
var exporter log.Exporter
var err error
switch config.OtelProtocol() {
case OtelProtocolGRPC:
exporter, err = otlploggrpc.New(ctx, otlploggrpc.WithEndpointURL(config.LogsURL()))
case OtelProtocolHTTP:
exporter, err = otlploghttp.New(ctx, otlploghttp.WithEndpointURL(config.LogsURL()))
default:
return nil, fmt.Errorf("unknown observability protocol %q", config.Protocol)
}
if err != nil {
return nil, err
}
loggerProvider := log.NewLoggerProvider(
log.WithResource(serviceResource),
log.WithProcessor(log.NewBatchProcessor(exporter,
log.WithExportInterval(OtelLogExportInterval))),
)
return loggerProvider, nil
}
func newServiceResource(ctx context.Context, hostData HostData) (*resource.Resource, error) {
providerBinary, err := os.Executable()
if err != nil {
return nil, err
}
return resource.New(ctx,
resource.WithAttributes(
semconv.ServiceNameKey.String(hostData.ProviderKey),
semconv.HostIDKey.String(hostData.HostID),
semconv.ServiceInstanceIDKey.String(hostData.InstanceID),
semconv.ProcessExecutableNameKey.String(filepath.Base(providerBinary)),
),
)
}