internal/telemetry: address review feedback

- Extract auth header construction before the switch to avoid
  duplication between HTTP and gRPC cases.
- Return an error if a URL path is provided with a gRPC endpoint,
  since gRPC uses service/method routing rather than URL paths.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Barnabas Busa 2026-03-03 14:50:00 +01:00
parent 0d1fb19613
commit b6897d3951
No known key found for this signature in database
GPG key ID: D7AF98F214C59E4E

View file

@ -84,6 +84,14 @@ func SetupTelemetry(cfg node.OpenTelemetryConfig, stack *node.Node) error {
if err != nil { if err != nil {
return fmt.Errorf("invalid rpc tracing endpoint URL: %w", err) return fmt.Errorf("invalid rpc tracing endpoint URL: %w", err)
} }
// Build auth headers once, shared across transports.
var authHeaders map[string]string
if cfg.AuthUser != "" {
authHeaders = map[string]string{
"Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(cfg.AuthUser+":"+cfg.AuthPassword)),
}
}
var exporter *otlptrace.Exporter var exporter *otlptrace.Exporter
switch u.Scheme { switch u.Scheme {
case "http", "https": case "http", "https":
@ -96,23 +104,22 @@ func SetupTelemetry(cfg node.OpenTelemetryConfig, stack *node.Node) error {
if u.Path != "" && u.Path != "/" { if u.Path != "" && u.Path != "/" {
opts = append(opts, otlptracehttp.WithURLPath(u.Path)) opts = append(opts, otlptracehttp.WithURLPath(u.Path))
} }
if cfg.AuthUser != "" { if authHeaders != nil {
opts = append(opts, otlptracehttp.WithHeaders(map[string]string{ opts = append(opts, otlptracehttp.WithHeaders(authHeaders))
"Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(cfg.AuthUser+":"+cfg.AuthPassword)),
}))
} }
exporter = otlptracehttp.NewUnstarted(opts...) exporter = otlptracehttp.NewUnstarted(opts...)
case "grpc", "grpcs": case "grpc", "grpcs":
if u.Path != "" && u.Path != "/" {
return fmt.Errorf("gRPC endpoints do not support URL paths: %s", u.Path)
}
opts := []otlptracegrpc.Option{ opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(u.Host), otlptracegrpc.WithEndpoint(u.Host),
} }
if u.Scheme == "grpc" { if u.Scheme == "grpc" {
opts = append(opts, otlptracegrpc.WithInsecure()) opts = append(opts, otlptracegrpc.WithInsecure())
} }
if cfg.AuthUser != "" { if authHeaders != nil {
opts = append(opts, otlptracegrpc.WithHeaders(map[string]string{ opts = append(opts, otlptracegrpc.WithHeaders(authHeaders))
"Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(cfg.AuthUser+":"+cfg.AuthPassword)),
}))
} }
exporter = otlptracegrpc.NewUnstarted(opts...) exporter = otlptracegrpc.NewUnstarted(opts...)
default: default: