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: 20 additions & 6 deletions cmd/thv/app/run_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,14 @@ 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 := getTelemetryFromFlags(cmd, config,
runFlags.OtelEndpoint, runFlags.OtelSamplingRate, runFlags.OtelEnvironmentVariables)
finalOtelEndpoint, finalOtelSamplingRate, finalOtelEnvironmentVariables, finalOtelInsecure,
finalOtelEnablePrometheusMetricsPath := getTelemetryFromFlags(cmd, config, runFlags.OtelEndpoint,
runFlags.OtelSamplingRate, runFlags.OtelEnvironmentVariables, runFlags.OtelInsecure,
runFlags.OtelEnablePrometheusMetricsPath)

return createTelemetryConfig(finalOtelEndpoint, runFlags.OtelEnablePrometheusMetricsPath,
return createTelemetryConfig(finalOtelEndpoint, finalOtelEnablePrometheusMetricsPath,
runFlags.OtelServiceName, runFlags.OtelTracingEnabled, runFlags.OtelMetricsEnabled, finalOtelSamplingRate,
runFlags.OtelHeaders, runFlags.OtelInsecure, finalOtelEnvironmentVariables)
runFlags.OtelHeaders, finalOtelInsecure, finalOtelEnvironmentVariables)
}

// setupRuntimeAndValidation creates container runtime and selects environment variable validator
Expand Down Expand Up @@ -575,7 +577,8 @@ func getOidcFromFlags(cmd *cobra.Command) (string, string, string, string, strin

// getTelemetryFromFlags extracts telemetry configuration from command flags
func getTelemetryFromFlags(cmd *cobra.Command, config *cfg.Config, otelEndpoint string, otelSamplingRate float64,
otelEnvironmentVariables []string) (string, float64, []string) {
otelEnvironmentVariables []string, otelInsecure bool, otelEnablePrometheusMetricsPath bool) (
string, float64, []string, bool, bool) {
// 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 @@ -592,7 +595,18 @@ func getTelemetryFromFlags(cmd *cobra.Command, config *cfg.Config, otelEndpoint
finalOtelEnvironmentVariables = config.OTEL.EnvVars
}

return finalOtelEndpoint, finalOtelSamplingRate, finalOtelEnvironmentVariables
finalOtelInsecure := otelInsecure
if !cmd.Flags().Changed("otel-insecure") {
finalOtelInsecure = config.OTEL.Insecure
}

finalOtelEnablePrometheusMetricsPath := otelEnablePrometheusMetricsPath
if !cmd.Flags().Changed("otel-enable-prometheus-metrics-path") {
finalOtelEnablePrometheusMetricsPath = config.OTEL.EnablePrometheusMetricsPath
}

return finalOtelEndpoint, finalOtelSamplingRate, finalOtelEnvironmentVariables,
finalOtelInsecure, finalOtelEnablePrometheusMetricsPath
}

// createOIDCConfig creates an OIDC configuration if any OIDC parameters are provided
Expand Down
131 changes: 83 additions & 48 deletions cmd/thv/app/run_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ func TestBuildRunnerConfig_TelemetryProcessing(t *testing.T) {
logger.Initialize()

tests := []struct {
name string
setupFlags func(*cobra.Command)
configOTEL config.OpenTelemetryConfig
runFlags *RunFlags
expectedEndpoint string
expectedSamplingRate float64
expectedEnvironmentVariables []string
name string
setupFlags func(*cobra.Command)
configOTEL config.OpenTelemetryConfig
runFlags *RunFlags
expectedEndpoint string
expectedSamplingRate float64
expectedEnvironmentVariables []string
expectedInsecure bool
expectedEnablePrometheusMetricsPath bool
}{
{
name: "CLI flags provided, taking precedence over config file",
Expand All @@ -64,25 +66,33 @@ func TestBuildRunnerConfig_TelemetryProcessing(t *testing.T) {
cmd.Flags().Set("otel-sampling-rate", "0.8")
cmd.Flags().Set("otel-env-vars", "CLI_VAR1=value1")
cmd.Flags().Set("otel-env-vars", "CLI_VAR2=value2")
cmd.Flags().Set("otel-insecure", "true")
cmd.Flags().Set("otel-enable-prometheus-metrics-path", "true")
},
configOTEL: config.OpenTelemetryConfig{
Endpoint: "https://config-endpoint.example.com",
SamplingRate: 0.2,
EnvVars: []string{"CONFIG_VAR1=configvalue1", "CONFIG_VAR2=configvalue2"},
Endpoint: "https://config-endpoint.example.com",
SamplingRate: 0.2,
EnvVars: []string{"CONFIG_VAR1=configvalue1", "CONFIG_VAR2=configvalue2"},
Insecure: false,
EnablePrometheusMetricsPath: false,
},
runFlags: &RunFlags{
OtelEndpoint: "https://cli-endpoint.example.com",
OtelSamplingRate: 0.8,
OtelEnvironmentVariables: []string{"CLI_VAR1=value1", "CLI_VAR2=value2"},
OtelEndpoint: "https://cli-endpoint.example.com",
OtelSamplingRate: 0.8,
OtelEnvironmentVariables: []string{"CLI_VAR1=value1", "CLI_VAR2=value2"},
OtelInsecure: true,
OtelEnablePrometheusMetricsPath: true,
// Set other required fields to avoid nil pointer errors
Transport: "sse",
ProxyMode: "sse",
Host: "localhost",
PermissionProfile: "none",
},
expectedEndpoint: "https://cli-endpoint.example.com",
expectedSamplingRate: 0.8,
expectedEnvironmentVariables: []string{"CLI_VAR1=value1", "CLI_VAR2=value2"},
expectedEndpoint: "https://cli-endpoint.example.com",
expectedSamplingRate: 0.8,
expectedEnvironmentVariables: []string{"CLI_VAR1=value1", "CLI_VAR2=value2"},
expectedInsecure: true,
expectedEnablePrometheusMetricsPath: true,
},
{
name: "No CLI flags provided, config takes precedence",
Expand All @@ -91,52 +101,66 @@ func TestBuildRunnerConfig_TelemetryProcessing(t *testing.T) {
// This simulates the case where user doesn't provide CLI flags
},
configOTEL: config.OpenTelemetryConfig{
Endpoint: "https://config-endpoint.example.com",
SamplingRate: 0.3,
EnvVars: []string{"CONFIG_VAR1=configvalue1", "CONFIG_VAR2=configvalue2"},
Endpoint: "https://config-endpoint.example.com",
SamplingRate: 0.3,
EnvVars: []string{"CONFIG_VAR1=configvalue1", "CONFIG_VAR2=configvalue2"},
Insecure: true,
EnablePrometheusMetricsPath: true,
},
runFlags: &RunFlags{
OtelEndpoint: "",
OtelSamplingRate: 0.1,
OtelEnvironmentVariables: nil,
Transport: "sse",
ProxyMode: "sse",
Host: "localhost",
PermissionProfile: "none",
OtelEndpoint: "",
OtelSamplingRate: 0.1,
OtelEnvironmentVariables: nil,
OtelInsecure: false,
OtelEnablePrometheusMetricsPath: false,
Transport: "sse",
ProxyMode: "sse",
Host: "localhost",
PermissionProfile: "none",
},
expectedEndpoint: "https://config-endpoint.example.com",
expectedSamplingRate: 0.3,
expectedEnvironmentVariables: []string{"CONFIG_VAR1=configvalue1", "CONFIG_VAR2=configvalue2"},
expectedEndpoint: "https://config-endpoint.example.com",
expectedSamplingRate: 0.3,
expectedEnvironmentVariables: []string{"CONFIG_VAR1=configvalue1", "CONFIG_VAR2=configvalue2"},
expectedInsecure: true,
expectedEnablePrometheusMetricsPath: true,
},
{
name: "Partial CLI flags provided, mix of CLI and config values",
setupFlags: func(cmd *cobra.Command) {
// Only set endpoint flag, leave others to use config values
// Only set endpoint and insecure flags, leave others to use config values
cmd.Flags().Set("otel-endpoint", "https://partial-cli-endpoint.example.com")
cmd.Flags().Set("otel-insecure", "true")
},
configOTEL: config.OpenTelemetryConfig{
Endpoint: "https://config-endpoint.example.com",
SamplingRate: 0.5,
EnvVars: []string{"CONFIG_VAR1=configvalue1"},
Endpoint: "https://config-endpoint.example.com",
SamplingRate: 0.5,
EnvVars: []string{"CONFIG_VAR1=configvalue1"},
Insecure: false,
EnablePrometheusMetricsPath: true,
},
runFlags: &RunFlags{
OtelEndpoint: "https://partial-cli-endpoint.example.com",
OtelSamplingRate: 0.1,
OtelEnvironmentVariables: nil,
Transport: "sse",
ProxyMode: "sse",
Host: "localhost",
PermissionProfile: "none",
OtelEndpoint: "https://partial-cli-endpoint.example.com",
OtelSamplingRate: 0.1,
OtelEnvironmentVariables: nil,
OtelInsecure: true,
OtelEnablePrometheusMetricsPath: false,
Transport: "sse",
ProxyMode: "sse",
Host: "localhost",
PermissionProfile: "none",
},
expectedEndpoint: "https://partial-cli-endpoint.example.com",
expectedSamplingRate: 0.5,
expectedEnvironmentVariables: []string{"CONFIG_VAR1=configvalue1"},
expectedEndpoint: "https://partial-cli-endpoint.example.com",
expectedSamplingRate: 0.5,
expectedEnvironmentVariables: []string{"CONFIG_VAR1=configvalue1"},
expectedInsecure: true,
expectedEnablePrometheusMetricsPath: true,
},
{
name: "Empty config values, CLI flags should be used",
setupFlags: func(cmd *cobra.Command) {
cmd.Flags().Set("otel-endpoint", "https://cli-only-endpoint.example.com")
cmd.Flags().Set("otel-sampling-rate", "0.9")
cmd.Flags().Set("otel-insecure", "true")
},
configOTEL: config.OpenTelemetryConfig{
Endpoint: "",
Expand All @@ -147,14 +171,17 @@ func TestBuildRunnerConfig_TelemetryProcessing(t *testing.T) {
OtelEndpoint: "https://cli-only-endpoint.example.com",
OtelSamplingRate: 0.9,
OtelEnvironmentVariables: nil,
OtelInsecure: true,
Transport: "sse",
ProxyMode: "sse",
Host: "localhost",
PermissionProfile: "none",
},
expectedEndpoint: "https://cli-only-endpoint.example.com",
expectedSamplingRate: 0.9,
expectedEnvironmentVariables: nil,
expectedEndpoint: "https://cli-only-endpoint.example.com",
expectedSamplingRate: 0.9,
expectedEnvironmentVariables: nil,
expectedInsecure: true,
expectedEnablePrometheusMetricsPath: false,
},
}

Expand All @@ -169,18 +196,22 @@ func TestBuildRunnerConfig_TelemetryProcessing(t *testing.T) {
})
defer cleanup()
configInstance := configProvider.GetConfig()
finalEndpoint, finalSamplingRate, finalEnvVars := getTelemetryFromFlags(
finalEndpoint, finalSamplingRate, finalEnvVars, finalInsecure, finalEnablePrometheusMetricsPath := getTelemetryFromFlags(
cmd,
configInstance,
tt.runFlags.OtelEndpoint,
tt.runFlags.OtelSamplingRate,
tt.runFlags.OtelEnvironmentVariables,
tt.runFlags.OtelInsecure,
tt.runFlags.OtelEnablePrometheusMetricsPath,
)

// Assert the results
assert.Equal(t, tt.expectedEndpoint, finalEndpoint, "OTEL endpoint should match expected value")
assert.Equal(t, tt.expectedSamplingRate, finalSamplingRate, "OTEL sampling rate should match expected value")
assert.Equal(t, tt.expectedEnvironmentVariables, finalEnvVars, "OTEL environment variables should match expected value")
assert.Equal(t, tt.expectedInsecure, finalInsecure, "OTEL insecure setting should match expected value")
assert.Equal(t, tt.expectedEnablePrometheusMetricsPath, finalEnablePrometheusMetricsPath, "OTEL enable Prometheus metrics path setting should match expected value")
})
}
}
Expand Down Expand Up @@ -214,16 +245,20 @@ func TestBuildRunnerConfig_TelemetryProcessing_Integration(t *testing.T) {
defer cleanup()

configInstance := configProvider.GetConfig()
finalEndpoint, finalSamplingRate, finalEnvVars := getTelemetryFromFlags(
finalEndpoint, finalSamplingRate, finalEnvVars, finalInsecure, finalEnablePrometheusMetricsPath := getTelemetryFromFlags(
cmd,
configInstance,
runFlags.OtelEndpoint,
runFlags.OtelSamplingRate,
runFlags.OtelEnvironmentVariables,
runFlags.OtelInsecure,
runFlags.OtelEnablePrometheusMetricsPath,
)

// Verify that CLI values take precedence
assert.Equal(t, "https://integration-test.example.com", finalEndpoint, "CLI endpoint should take precedence over config")
assert.Equal(t, 0.7, finalSamplingRate, "CLI sampling rate should take precedence over config")
assert.Equal(t, []string{"CONFIG_VAR=value"}, finalEnvVars, "Environment variables should fall back to config when not set via CLI")
assert.Equal(t, false, finalInsecure, "Insecure setting should use runFlags value when not set via CLI")
assert.Equal(t, false, finalEnablePrometheusMetricsPath, "Enable Prometheus metrics path should use runFlags value when not set via CLI")
}
Loading