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
+4 -3
View File
@@ -27,9 +27,10 @@ func init() {
// Config 链路追踪配置
type Config struct {
AppName string
SamplingRate float64
TracerName string
AppName string
SamplingRate float64
TracerName string
ExportProtocol string
}
// Init 初始化 Tracer Provider 并关联全局 Tracer 实例
+4 -6
View File
@@ -17,6 +17,7 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
// newTracerProvider combines SDK and application resources with a batched exporter.
func newTracerProvider(cfg Config) (*sdktrace.TracerProvider, error) {
// 获取主机名和容器信息
hostname, err := os.Hostname()
@@ -40,7 +41,7 @@ func newTracerProvider(cfg Config) (*sdktrace.TracerProvider, error) {
}
// 初始化 Exporter
traceExporter, err := newTraceExporter(context.Background())
traceExporter, err := newTraceExporter(context.Background(), cfg.ExportProtocol)
if err != nil {
return nil, err
}
@@ -56,11 +57,8 @@ func newTracerProvider(cfg Config) (*sdktrace.TracerProvider, error) {
// newTraceExporter selects the OTLP wire transport without changing existing gRPC defaults.
// The SDK exporters read standard OTEL_EXPORTER_OTLP_* endpoint and header settings.
func newTraceExporter(ctx context.Context) (sdktrace.SpanExporter, error) {
protocol := strings.ToLower(strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL")))
if protocol == "" {
protocol = strings.ToLower(strings.TrimSpace(os.Getenv("OTEL_EXPORTER_OTLP_PROTOCOL")))
}
func newTraceExporter(ctx context.Context, protocol string) (sdktrace.SpanExporter, error) {
protocol = strings.ToLower(strings.TrimSpace(protocol))
switch protocol {
case "", "grpc":
return otlptracegrpc.New(ctx)
+18 -8
View File
@@ -15,6 +15,7 @@ import (
"google.golang.org/protobuf/proto"
)
// TestOTLPHTTPTraceExportHeadersAndResources verifies wire payloads against a local collector.
func TestOTLPHTTPTraceExportHeadersAndResources(t *testing.T) {
type received struct {
headers http.Header
@@ -37,10 +38,8 @@ func TestOTLPHTTPTraceExportHeadersAndResources(t *testing.T) {
_, _ = w.Write(responseBody)
}))
defer server.Close()
t.Setenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", "")
t.Setenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", "")
t.Setenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS", "")
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf")
t.Setenv("OTEL_EXPORTER_OTLP_ENDPOINT", server.URL)
t.Setenv("OTEL_EXPORTER_OTLP_HEADERS", "X-API-Key=test-key,X-Client-Name=test%20client")
t.Setenv("OTEL_RESOURCE_ATTRIBUTES", "service.namespace=example,deployment.environment.name=test")
@@ -65,8 +64,6 @@ func TestOTLPHTTPTraceExportHeadersAndResources(t *testing.T) {
{
name: "trace_specific_overrides",
env: map[string]string{
"OTEL_EXPORTER_OTLP_PROTOCOL": "grpc",
"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL": "http/protobuf",
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": server.URL + "/custom/ingest",
"OTEL_EXPORTER_OTLP_TRACES_HEADERS": "X-API-Key=trace-key,X-Client-Name=trace%20client",
},
@@ -79,7 +76,7 @@ func TestOTLPHTTPTraceExportHeadersAndResources(t *testing.T) {
for key, value := range tt.env {
t.Setenv(key, value)
}
provider, err := newTracerProvider(Config{AppName: "openflare-control-plane", SamplingRate: 1})
provider, err := newTracerProvider(Config{AppName: "openflare-control-plane", SamplingRate: 1, ExportProtocol: "http/protobuf"})
if err != nil {
t.Fatal(err)
}
@@ -122,6 +119,7 @@ func TestOTLPHTTPTraceExportHeadersAndResources(t *testing.T) {
}
}
// protoBytes encodes a collector response for the HTTP test server.
func protoBytes(t *testing.T, v proto.Message) []byte {
t.Helper()
data, err := proto.Marshal(v)
@@ -131,11 +129,23 @@ func protoBytes(t *testing.T, v proto.Message) []byte {
return data
}
// TestUnknownOTLPProtocolFails rejects unsupported wire formats during initialization.
func TestUnknownOTLPProtocolFails(t *testing.T) {
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/json")
t.Setenv("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL", "")
_, err := newTraceExporter(context.Background())
_, err := newTraceExporter(context.Background(), "http/json")
if err == nil || !strings.Contains(err.Error(), "unsupported OTLP") {
t.Fatalf("expected protocol error, got %v", err)
}
}
// TestDefaultOTLPProtocolGRPC keeps empty and explicit gRPC configuration working.
func TestDefaultOTLPProtocolGRPC(t *testing.T) {
for _, protocol := range []string{"", "grpc"} {
exporter, err := newTraceExporter(context.Background(), protocol)
if err != nil {
t.Fatalf("newTraceExporter(%q): %v", protocol, err)
}
if err := exporter.Shutdown(context.Background()); err != nil {
t.Errorf("exporter.Shutdown(%q): %v", protocol, err)
}
}
}