Skip to content

feat: add headers to OTEL config #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions otlp/otlpmetrics/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package otlpmetrics

import (
"strings"
"time"

"github.com/spf13/cobra"
Expand All @@ -19,6 +20,7 @@
OtelMetricsExporterOTLPModeFlag = "otel-metrics-exporter-otlp-mode"
OtelMetricsExporterOTLPEndpointFlag = "otel-metrics-exporter-otlp-endpoint"
OtelMetricsExporterOTLPInsecureFlag = "otel-metrics-exporter-otlp-insecure"
OtelMetricsExporterOTLPHeadersFlag = "otel-metrics-exporter-otlp-headers"
)

func AddFlags(flags *flag.FlagSet) {
Expand All @@ -31,6 +33,7 @@
flags.String(OtelMetricsExporterOTLPModeFlag, "grpc", "OpenTelemetry metrics OTLP exporter mode (grpc|http)")
flags.String(OtelMetricsExporterOTLPEndpointFlag, "", "OpenTelemetry metrics grpc endpoint")
flags.Bool(OtelMetricsExporterOTLPInsecureFlag, false, "OpenTelemetry metrics grpc insecure")
flags.StringSlice(OtelMetricsExporterOTLPHeadersFlag, nil, "OpenTelemetry metrics grpc headers")

Check warning on line 36 in otlp/otlpmetrics/cli.go

View check run for this annotation

Codecov / codecov/patch

otlp/otlpmetrics/cli.go#L36

Added line #L36 was not covered by tests

// notes(gfyrag): apps are in charge of exposing in memory metrics using whatever protocol it wants to
flags.Bool(OtelMetricsKeepInMemoryFlag, false, "Allow to keep metrics in memory")
Expand All @@ -45,12 +48,19 @@
otelMetricsRuntimeMinimumReadMemStatsInterval, _ := cmd.Flags().GetDuration(OtelMetricsRuntimeMinimumReadMemStatsIntervalFlag)
otelMetricsExporterPushInterval, _ := cmd.Flags().GetDuration(OtelMetricsExporterPushIntervalFlag)
otelMetricsKeepInMemory, _ := cmd.Flags().GetBool(OtelMetricsKeepInMemoryFlag)
otelMetricsExporterOTLPHeaders, _ := cmd.Flags().GetStringSlice(OtelMetricsExporterOTLPHeadersFlag)
headersMap := make(map[string]string)
for _, header := range otelMetricsExporterOTLPHeaders {
parts := strings.SplitN(header, "=", 2)
headersMap[parts[0]] = parts[1]
}

Check warning on line 56 in otlp/otlpmetrics/cli.go

View check run for this annotation

Codecov / codecov/patch

otlp/otlpmetrics/cli.go#L51-L56

Added lines #L51 - L56 were not covered by tests
Comment on lines +51 to +56
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add validation to prevent panic with malformed headers

The current implementation assumes that splitting the header always produces at least two parts, which could cause a panic if a malformed header is provided (e.g., a header without an '=' character).

Apply this diff to fix the issue:

 headersMap := make(map[string]string)
 for _, header := range otelMetricsExporterOTLPHeaders {
 	parts := strings.SplitN(header, "=", 2)
-	headersMap[parts[0]] = parts[1]
+	if len(parts) == 2 {
+		headersMap[parts[0]] = parts[1]
+	}
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
otelMetricsExporterOTLPHeaders, _ := cmd.Flags().GetStringSlice(OtelMetricsExporterOTLPHeadersFlag)
headersMap := make(map[string]string)
for _, header := range otelMetricsExporterOTLPHeaders {
parts := strings.SplitN(header, "=", 2)
headersMap[parts[0]] = parts[1]
}
otelMetricsExporterOTLPHeaders, _ := cmd.Flags().GetStringSlice(OtelMetricsExporterOTLPHeadersFlag)
headersMap := make(map[string]string)
for _, header := range otelMetricsExporterOTLPHeaders {
parts := strings.SplitN(header, "=", 2)
if len(parts) == 2 {
headersMap[parts[0]] = parts[1]
}
}


return MetricsModule(ModuleConfig{
OTLPConfig: &OTLPConfig{
Mode: otelMetricsExporterOTLPMode,
Endpoint: otelMetricsExporterOTLPEndpoint,
Insecure: otelMetricsExporterOTLPInsecure,
Headers: headersMap,

Check warning on line 63 in otlp/otlpmetrics/cli.go

View check run for this annotation

Codecov / codecov/patch

otlp/otlpmetrics/cli.go#L63

Added line #L63 was not covered by tests
},
Exporter: otelMetricsExporter,
RuntimeMetrics: otelMetricsRuntime,
Expand Down
11 changes: 11 additions & 0 deletions otlp/otlpmetrics/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
Mode string
Endpoint string
Insecure bool
Headers map[string]string
}

func ProvideMetricsProviderOption(v any, annotations ...fx.Annotation) fx.Option {
Expand Down Expand Up @@ -132,6 +133,11 @@
return otlpmetricgrpc.WithInsecure()
}))
}
if cfg.OTLPConfig.Headers != nil {
options = append(options, ProvideOTLPMetricsGRPCOption(func() otlpmetricgrpc.Option {
return otlpmetricgrpc.WithHeaders(cfg.OTLPConfig.Headers)
}))

Check warning on line 139 in otlp/otlpmetrics/module.go

View check run for this annotation

Codecov / codecov/patch

otlp/otlpmetrics/module.go#L136-L139

Added lines #L136 - L139 were not covered by tests
}
}

options = append(options, ProvideOTLPMetricsGRPCExporter())
Expand All @@ -147,6 +153,11 @@
return otlpmetrichttp.WithInsecure()
}))
}
if cfg.OTLPConfig.Headers != nil {
options = append(options, ProvideOTLPMetricsHTTPOption(func() otlpmetrichttp.Option {
return otlpmetrichttp.WithHeaders(cfg.OTLPConfig.Headers)
}))

Check warning on line 159 in otlp/otlpmetrics/module.go

View check run for this annotation

Codecov / codecov/patch

otlp/otlpmetrics/module.go#L156-L159

Added lines #L156 - L159 were not covered by tests
}
}

options = append(options, ProvideOTLPMetricsHTTPExporter())
Expand Down
14 changes: 14 additions & 0 deletions otlp/otlptraces/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package otlptraces

import (
"strings"

"github.com/formancehq/go-libs/v2/otlp"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand All @@ -16,6 +18,7 @@
OtelTracesExporterOTLPModeFlag = "otel-traces-exporter-otlp-mode"
OtelTracesExporterOTLPEndpointFlag = "otel-traces-exporter-otlp-endpoint"
OtelTracesExporterOTLPInsecureFlag = "otel-traces-exporter-otlp-insecure"
OtelTracesExporterOTLPHeadersFlag = "otel-traces-exporter-otlp-headers"
)

func AddFlags(flags *flag.FlagSet) {
Expand All @@ -29,6 +32,7 @@
flags.String(OtelTracesExporterOTLPModeFlag, "grpc", "OpenTelemetry traces OTLP exporter mode (grpc|http)")
flags.String(OtelTracesExporterOTLPEndpointFlag, "", "OpenTelemetry traces grpc endpoint")
flags.Bool(OtelTracesExporterOTLPInsecureFlag, false, "OpenTelemetry traces grpc insecure")
flags.StringSlice(OtelTracesExporterOTLPHeadersFlag, nil, "OpenTelemetry traces grpc headers")
}

func FXModuleFromFlags(cmd *cobra.Command) fx.Option {
Expand All @@ -47,11 +51,21 @@
mode, _ := cmd.Flags().GetString(OtelTracesExporterOTLPModeFlag)
endpoint, _ := cmd.Flags().GetString(OtelTracesExporterOTLPEndpointFlag)
insecure, _ := cmd.Flags().GetBool(OtelTracesExporterOTLPInsecureFlag)
headers, _ := cmd.Flags().GetStringSlice(OtelTracesExporterOTLPHeadersFlag)

headersMap := make(map[string]string)
for _, header := range headers {
parts := strings.SplitN(header, "=", 2)
if len(parts) == 2 {
headersMap[parts[0]] = parts[1]
}

Check warning on line 61 in otlp/otlptraces/cli.go

View check run for this annotation

Codecov / codecov/patch

otlp/otlptraces/cli.go#L58-L61

Added lines #L58 - L61 were not covered by tests
}

return &OTLPConfig{
Mode: mode,
Endpoint: endpoint,
Insecure: insecure,
Headers: headersMap,
}
}(),
ServiceName: serviceName,
Expand Down
11 changes: 11 additions & 0 deletions otlp/otlptraces/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
Mode string
Endpoint string
Insecure bool
Headers map[string]string
}

type ModuleConfig struct {
Expand Down Expand Up @@ -109,6 +110,11 @@
return otlptracegrpc.WithInsecure()
}))
}
if cfg.OTLPConfig.Headers != nil {
options = append(options, ProvideOTLPTracerGRPCClientOption(func() otlptracegrpc.Option {
return otlptracegrpc.WithHeaders(cfg.OTLPConfig.Headers)
}))
}
case otlp.ModeHTTP:
if cfg.OTLPConfig.Endpoint != "" {
options = append(options, ProvideOTLPTracerHTTPClientOption(func() otlptracehttp.Option {
Expand All @@ -120,6 +126,11 @@
return otlptracehttp.WithInsecure()
}))
}
if cfg.OTLPConfig.Headers != nil {
options = append(options, ProvideOTLPTracerHTTPClientOption(func() otlptracehttp.Option {
return otlptracehttp.WithHeaders(cfg.OTLPConfig.Headers)
}))

Check warning on line 132 in otlp/otlptraces/traces.go

View check run for this annotation

Codecov / codecov/patch

otlp/otlptraces/traces.go#L130-L132

Added lines #L130 - L132 were not covered by tests
}
}
}
switch mode {
Expand Down