fix(otel): resolve exporter protocol through startup configuration

This commit is contained in:
ryan
2026-11-09 10:23:47 +08:00
parent b281b65e78
commit acd63ff410
10 changed files with 65 additions and 24 deletions
+8
View File
@@ -273,6 +273,14 @@ func applyEnvOverrides(c *configModel) {
c.Log.Output = envStr("LOG_OUTPUT", c.Log.Output)
// ─── OTel ───
protocol := strings.TrimSpace(envStr("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", ""))
if protocol == "" {
protocol = strings.TrimSpace(envStr("OTEL_EXPORTER_OTLP_PROTOCOL", ""))
}
if protocol == "" {
protocol = c.Otel.ExportProtocol
}
c.Otel.ExportProtocol = strings.ToLower(strings.TrimSpace(protocol))
c.Otel.SamplingRate = envFloat64("OTEL_SAMPLING_RATE", c.Otel.SamplingRate)
c.Otel.TracerName = envStr("OTEL_TRACER_NAME", c.Otel.TracerName)
+21
View File
@@ -47,3 +47,24 @@ func TestApplyClickHouseDefaultsEnablesWhenConfigured(t *testing.T) {
t.Fatalf("hosts default not applied: %v", cfg.ClickHouse.Hosts)
}
}
// TestOTLPProtocolPrecedence covers signal-specific, general, YAML and default settings.
func TestOTLPProtocolPrecedence(t *testing.T) {
for _, tt := range []struct{ name, signal, general, configured, want string }{
{name: "default"},
{name: "yaml", configured: "http/protobuf", want: "http/protobuf"},
{name: "general", general: "http/protobuf", configured: "grpc", want: "http/protobuf"},
{name: "signal", signal: " HTTP/PROTOBUF ", general: "grpc", want: "http/protobuf"},
{name: "empty_signal", signal: " ", general: "grpc", configured: "http/protobuf", want: "grpc"},
} {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", tt.signal)
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", tt.general)
cfg := &configModel{Otel: otelConfig{ExportProtocol: tt.configured}}
applyEnvOverrides(cfg)
if cfg.Otel.ExportProtocol != tt.want {
t.Errorf("OTLP protocol = %q, want %q", cfg.Otel.ExportProtocol, tt.want)
}
})
}
}
+3 -2
View File
@@ -138,6 +138,7 @@ type QueueConfig struct {
// otelConfig OpenTelemetry 配置
type otelConfig struct {
SamplingRate float64 `mapstructure:"sampling_rate"`
TracerName string `mapstructure:"tracer_name"`
ExportProtocol string `mapstructure:"export_protocol"`
SamplingRate float64 `mapstructure:"sampling_rate"`
TracerName string `mapstructure:"tracer_name"`
}