Skip to content
Merged
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
26 changes: 14 additions & 12 deletions cmd/thv/app/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func setOtelMetricsEnabledCmdFunc(_ *cobra.Command, args []string) error {

// Update the configuration
err = config.UpdateConfig(func(c *config.Config) {
c.OTEL.MetricsEnabled = enabled
c.OTEL.MetricsEnabled = &enabled
})
if err != nil {
return fmt.Errorf("failed to update configuration: %w", err)
Expand All @@ -413,28 +413,29 @@ func getOtelMetricsEnabledCmdFunc(_ *cobra.Command, _ []string) error {
configProvider := config.NewDefaultProvider()
cfg := configProvider.GetConfig()

fmt.Printf("Current OpenTelemetry metrics enabled: %t\n", cfg.OTEL.MetricsEnabled)
metricsEnabled := cfg.OTEL.MetricsEnabled != nil && *cfg.OTEL.MetricsEnabled
fmt.Printf("Current OpenTelemetry metrics enabled: %t\n", metricsEnabled)
return nil
}

func unsetOtelMetricsEnabledCmdFunc(_ *cobra.Command, _ []string) error {
configProvider := config.NewDefaultProvider()
cfg := configProvider.GetConfig()

if !cfg.OTEL.MetricsEnabled {
fmt.Println("OpenTelemetry metrics enabled is already disabled.")
if cfg.OTEL.MetricsEnabled == nil {
fmt.Println("OpenTelemetry metrics enabled is not configured.")
return nil
}

// Update the configuration
err := config.UpdateConfig(func(c *config.Config) {
c.OTEL.MetricsEnabled = false
c.OTEL.MetricsEnabled = nil
})
if err != nil {
return fmt.Errorf("failed to update configuration: %w", err)
}

fmt.Println("Successfully disabled OpenTelemetry metrics enabled configuration.")
fmt.Println("Successfully unset OpenTelemetry metrics enabled configuration.")
return nil
}

Expand All @@ -446,7 +447,7 @@ func setOtelTracingEnabledCmdFunc(_ *cobra.Command, args []string) error {

// Update the configuration
err = config.UpdateConfig(func(c *config.Config) {
c.OTEL.TracingEnabled = enabled
c.OTEL.TracingEnabled = &enabled
})
if err != nil {
return fmt.Errorf("failed to update configuration: %w", err)
Expand All @@ -460,28 +461,29 @@ func getOtelTracingEnabledCmdFunc(_ *cobra.Command, _ []string) error {
configProvider := config.NewDefaultProvider()
cfg := configProvider.GetConfig()

fmt.Printf("Current OpenTelemetry tracing enabled: %t\n", cfg.OTEL.TracingEnabled)
tracingEnabled := cfg.OTEL.TracingEnabled != nil && *cfg.OTEL.TracingEnabled
fmt.Printf("Current OpenTelemetry tracing enabled: %t\n", tracingEnabled)
return nil
}

func unsetOtelTracingEnabledCmdFunc(_ *cobra.Command, _ []string) error {
configProvider := config.NewDefaultProvider()
cfg := configProvider.GetConfig()

if !cfg.OTEL.TracingEnabled {
fmt.Println("OpenTelemetry tracing enabled is already disabled.")
if cfg.OTEL.TracingEnabled == nil {
fmt.Println("OpenTelemetry tracing enabled is not configured.")
return nil
}

// Update the configuration
err := config.UpdateConfig(func(c *config.Config) {
c.OTEL.TracingEnabled = false
c.OTEL.TracingEnabled = nil
})
if err != nil {
return fmt.Errorf("failed to update configuration: %w", err)
}

fmt.Println("Successfully disabled OpenTelemetry tracing enabled configuration.")
fmt.Println("Successfully unset OpenTelemetry tracing enabled configuration.")
return nil
}

Expand Down
76 changes: 63 additions & 13 deletions cmd/thv/app/run_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,17 @@ func setupOIDCConfiguration(cmd *cobra.Command, runFlags *RunFlags) (*auth.Token
func setupTelemetryConfiguration(cmd *cobra.Command, runFlags *RunFlags) *telemetry.Config {
configProvider := cfg.NewDefaultProvider()
config := configProvider.GetConfig()
finalOtelEndpoint, finalOtelSamplingRate, finalOtelEnvironmentVariables, finalOtelInsecure,
finalOtelEnablePrometheusMetricsPath, finalOtelUseLegacyAttributes := getTelemetryFromFlags(
finalTelemetry := getTelemetryFromFlags(
cmd, config, runFlags.OtelEndpoint,
runFlags.OtelSamplingRate, runFlags.OtelEnvironmentVariables, runFlags.OtelInsecure,
runFlags.OtelEnablePrometheusMetricsPath, runFlags.OtelUseLegacyAttributes)

return createTelemetryConfig(finalOtelEndpoint, finalOtelEnablePrometheusMetricsPath,
runFlags.OtelServiceName, runFlags.OtelTracingEnabled, runFlags.OtelMetricsEnabled, finalOtelSamplingRate,
runFlags.OtelHeaders, finalOtelInsecure, finalOtelEnvironmentVariables, runFlags.OtelCustomAttributes,
finalOtelUseLegacyAttributes)
runFlags.OtelEnablePrometheusMetricsPath, runFlags.OtelUseLegacyAttributes,
runFlags.OtelTracingEnabled, runFlags.OtelMetricsEnabled)

return createTelemetryConfig(finalTelemetry.OtelEndpoint, finalTelemetry.OtelEnablePrometheusMetricsPath,
runFlags.OtelServiceName, finalTelemetry.OtelTracingEnabled, finalTelemetry.OtelMetricsEnabled,
finalTelemetry.OtelSamplingRate, runFlags.OtelHeaders, finalTelemetry.OtelInsecure,
finalTelemetry.OtelEnvironmentVariables, runFlags.OtelCustomAttributes,
finalTelemetry.OtelUseLegacyAttributes)
}

// setupRuntimeAndValidation creates container runtime and selects environment variable validator
Expand Down Expand Up @@ -712,6 +713,18 @@ func configureMiddlewareAndOptions(
oidcScopes := extractOIDCValues(oidcConfig)
finalOtelEndpoint, finalOtelSamplingRate, finalOtelEnvironmentVariables := extractTelemetryValues(telemetryConfig)

// Extract resolved tracing/metrics values from the middleware telemetry config.
// These must match what setupTelemetryConfiguration resolved (with global config
// fallbacks) rather than the raw runFlags values, which ignore global config.
// Default to false when telemetryConfig is nil (both signals disabled or no endpoint)
// rather than falling back to runFlags defaults, which would re-enable signals
// that the user explicitly disabled via global config.
var finalTracingEnabled, finalMetricsEnabled bool
if telemetryConfig != nil {
finalTracingEnabled = telemetryConfig.TracingEnabled
finalMetricsEnabled = telemetryConfig.MetricsEnabled
}

// Set additional configurations that are still needed in old format for other parts of the system
opts = append(opts,
runner.WithOIDCConfig(
Expand All @@ -720,7 +733,7 @@ func configureMiddlewareAndOptions(
runFlags.JWKSAllowPrivateIP, runFlags.InsecureAllowHTTP, oidcScopes,
),
runner.WithTelemetryConfigFromFlags(finalOtelEndpoint, runFlags.OtelEnablePrometheusMetricsPath,
runFlags.OtelTracingEnabled, runFlags.OtelMetricsEnabled, runFlags.OtelServiceName,
finalTracingEnabled, finalMetricsEnabled, runFlags.OtelServiceName,
finalOtelSamplingRate, runFlags.OtelHeaders, runFlags.OtelInsecure, finalOtelEnvironmentVariables,
runFlags.OtelUseLegacyAttributes,
),
Comment thread
amirejaz marked this conversation as resolved.
Expand Down Expand Up @@ -959,11 +972,23 @@ func getOidcFromFlags(cmd *cobra.Command) (string, string, string, string, strin
return oidcIssuer, oidcAudience, oidcJwksURL, introspectionURL, oidcClientID, oidcClientSecret, oidcScopes
}

// finalTelemetry holds the telemetry configuration values after applying
// global config fallbacks to CLI flag values.
type finalTelemetry struct {
OtelEndpoint string
OtelSamplingRate float64
OtelEnvironmentVariables []string
OtelInsecure bool
OtelEnablePrometheusMetricsPath bool
OtelUseLegacyAttributes bool
OtelTracingEnabled bool
OtelMetricsEnabled bool
}

// getTelemetryFromFlags extracts telemetry configuration from command flags
func getTelemetryFromFlags(cmd *cobra.Command, config *cfg.Config, otelEndpoint string, otelSamplingRate float64,
otelEnvironmentVariables []string, otelInsecure bool, otelEnablePrometheusMetricsPath bool,
otelUseLegacyAttributes bool) (
string, float64, []string, bool, bool, bool) {
otelUseLegacyAttributes bool, otelTracingEnabled bool, otelMetricsEnabled bool) finalTelemetry {
// Use config values as fallbacks for OTEL flags if not explicitly set
finalOtelEndpoint := otelEndpoint
if !cmd.Flags().Changed("otel-endpoint") && config.OTEL.Endpoint != "" {
Expand All @@ -990,6 +1015,16 @@ func getTelemetryFromFlags(cmd *cobra.Command, config *cfg.Config, otelEndpoint
finalOtelEnablePrometheusMetricsPath = config.OTEL.EnablePrometheusMetricsPath
}

finalOtelTracingEnabled := otelTracingEnabled
if !cmd.Flags().Changed("otel-tracing-enabled") && config.OTEL.TracingEnabled != nil {
finalOtelTracingEnabled = *config.OTEL.TracingEnabled
}

finalOtelMetricsEnabled := otelMetricsEnabled
if !cmd.Flags().Changed("otel-metrics-enabled") && config.OTEL.MetricsEnabled != nil {
finalOtelMetricsEnabled = *config.OTEL.MetricsEnabled
}

// UseLegacyAttributes defaults to true for this release to avoid breaking existing
// dashboards and alerts. When the config file explicitly sets this field (non-nil),
// use the config value. Otherwise, use the CLI flag value (which defaults to true).
Expand All @@ -999,8 +1034,16 @@ func getTelemetryFromFlags(cmd *cobra.Command, config *cfg.Config, otelEndpoint
finalOtelUseLegacyAttributes = *config.OTEL.UseLegacyAttributes
}

return finalOtelEndpoint, finalOtelSamplingRate, finalOtelEnvironmentVariables,
finalOtelInsecure, finalOtelEnablePrometheusMetricsPath, finalOtelUseLegacyAttributes
return finalTelemetry{
OtelEndpoint: finalOtelEndpoint,
OtelSamplingRate: finalOtelSamplingRate,
OtelEnvironmentVariables: finalOtelEnvironmentVariables,
OtelInsecure: finalOtelInsecure,
OtelEnablePrometheusMetricsPath: finalOtelEnablePrometheusMetricsPath,
OtelUseLegacyAttributes: finalOtelUseLegacyAttributes,
OtelTracingEnabled: finalOtelTracingEnabled,
OtelMetricsEnabled: finalOtelMetricsEnabled,
}
}

// createOIDCConfig creates an OIDC configuration if any OIDC parameters are provided
Expand Down Expand Up @@ -1032,6 +1075,13 @@ func createTelemetryConfig(otelEndpoint string, otelEnablePrometheusMetricsPath
return nil
}

// If both tracing and metrics are disabled, skip telemetry entirely.
// This allows users to disable telemetry via global config while keeping
// the endpoint configured for later re-enablement.
if !otelTracingEnabled && !otelMetricsEnabled && !otelEnablePrometheusMetricsPath {
return nil
}
Comment thread
amirejaz marked this conversation as resolved.

// Parse headers from key=value format
headers := make(map[string]string)
for _, header := range otelHeaders {
Expand Down
Loading
Loading