55 "fmt"
66 "os"
77 "path/filepath"
8+ "strings"
89 "time"
910
1011 "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
@@ -22,9 +23,13 @@ import (
2223)
2324
2425const (
25- OtelMetricExportInterval = 1 * time .Minute
26- OtelTraceExportInterval = 1 * time .Minute
27- OtelLogExportInterval = 10 * time .Second
26+ OtelMetricExportInterval = 1 * time .Minute
27+ OtelTraceExportInterval = 1 * time .Minute
28+ OtelLogExportInterval = 10 * time .Second
29+ OtelProtocolHTTP = "http"
30+ OtelProtocolGRPC = "grpc"
31+ OtelSpanLimitAttributePerEvent = 16
32+ OtelSpanLimitEventCount = 64
2833)
2934
3035func newPropagator () propagation.TextMapPropagator {
@@ -43,21 +48,22 @@ func newTracerProvider(ctx context.Context, config OtelConfig, serviceResource *
4348 endpoint = config .ObservabilityEndpoint
4449 }
4550
46- switch config .Protocol {
51+ switch strings . ToLower ( config .Protocol ) {
4752 case OtelProtocolGRPC :
4853 exporter , err = otlptracegrpc .New (ctx , otlptracegrpc .WithEndpointURL (endpoint ))
4954 case OtelProtocolHTTP :
5055 exporter , err = otlptracehttp .New (ctx , otlptracehttp .WithEndpointURL (endpoint ))
5156 default :
52- return nil , fmt .Errorf ("unknown observability protocol '%s' " , config .Protocol )
57+ return nil , fmt .Errorf ("unknown observability protocol %q " , config .Protocol )
5358 }
59+
5460 if err != nil {
5561 return nil , err
5662 }
5763
5864 spanLimits := trace.SpanLimits {
59- AttributePerEventCountLimit : 16 ,
60- EventCountLimit : 64 ,
65+ AttributePerEventCountLimit : OtelSpanLimitAttributePerEvent ,
66+ EventCountLimit : OtelSpanLimitEventCount ,
6167 }
6268
6369 traceProvider := trace .NewTracerProvider (
@@ -81,14 +87,15 @@ func newMeterProvider(ctx context.Context, config OtelConfig, serviceResource *r
8187 endpoint = config .ObservabilityEndpoint
8288 }
8389
84- switch config .Protocol {
90+ switch strings . ToLower ( config .Protocol ) {
8591 case OtelProtocolGRPC :
8692 exporter , err = otlpmetricgrpc .New (ctx , otlpmetricgrpc .WithEndpointURL (endpoint ))
8793 case OtelProtocolHTTP :
8894 exporter , err = otlpmetrichttp .New (ctx , otlpmetrichttp .WithEndpointURL (endpoint ))
8995 default :
90- return nil , fmt .Errorf ("unknown observability protocol '%s' " , config .Protocol )
96+ return nil , fmt .Errorf ("unknown observability protocol %q " , config .Protocol )
9197 }
98+
9299 if err != nil {
93100 return nil , err
94101 }
@@ -111,14 +118,15 @@ func newLoggerProvider(ctx context.Context, config OtelConfig, serviceResource *
111118 endpoint = config .ObservabilityEndpoint
112119 }
113120
114- switch config .Protocol {
121+ switch strings . ToLower ( config .Protocol ) {
115122 case OtelProtocolGRPC :
116123 exporter , err = otlploggrpc .New (ctx , otlploggrpc .WithEndpointURL (endpoint ))
117124 case OtelProtocolHTTP :
118125 exporter , err = otlploghttp .New (ctx , otlploghttp .WithEndpointURL (endpoint ))
119126 default :
120- return nil , fmt .Errorf ("unknown observability protocol '%s' " , config .Protocol )
127+ return nil , fmt .Errorf ("unknown observability protocol %q " , config .Protocol )
121128 }
129+
122130 if err != nil {
123131 return nil , err
124132 }
@@ -137,12 +145,11 @@ func newServiceResource(ctx context.Context, name string) (*resource.Resource, e
137145 if err != nil {
138146 return nil , err
139147 }
140- serviceName := semconv .ServiceNameKey .String (filepath .Base (providerBinary ))
141- providerName := semconv .ServiceInstanceIDKey .String (name )
148+
142149 return resource .New (ctx ,
143150 resource .WithAttributes (
144- serviceName ,
145- providerName ,
151+ semconv . ServiceNameKey . String ( filepath . Base ( providerBinary )) ,
152+ semconv . ServiceInstanceIDKey . String ( name ) ,
146153 ),
147154 )
148155}
0 commit comments