Skip to content

Commit

Permalink
Add all beholder config options to loop/EnvConfig; set beholder confi…
Browse files Browse the repository at this point in the history
…g options from loop EnvConfig
  • Loading branch information
pkcll committed Dec 4, 2024
1 parent 9d0f27e commit 4d865c3
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 63 deletions.
70 changes: 49 additions & 21 deletions pkg/loop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ const (
envTracingAttribute = "CL_TRACING_ATTRIBUTE_"
envTracingTLSCertPath = "CL_TRACING_TLS_CERT_PATH"

envTelemetryEnabled = "CL_TELEMETRY_ENABLED"
envTelemetryEndpoint = "CL_TELEMETRY_ENDPOINT"
envTelemetryInsecureConn = "CL_TELEMETRY_INSECURE_CONNECTION"
envTelemetryCACertFile = "CL_TELEMETRY_CA_CERT_FILE"
envTelemetryAttribute = "CL_TELEMETRY_ATTRIBUTE_"
envTelemetryTraceSampleRatio = "CL_TELEMETRY_TRACE_SAMPLE_RATIO"
envTelemetryAuthHeader = "CL_TELEMETRY_AUTH_HEADER"
envTelemetryAuthPubKeyHex = "CL_TELEMETRY_AUTH_PUB_KEY_HEX"
envTelemetryEmitterBatchProcessor = "CL_TELEMETRY_EMITTER_BATCH_PROCESSOR"
envTelemetryEmitterExportTimeout = "CL_TELEMETRY_EMITTER_EXPORT_TIMEOUT"
envTelemetryEnabled = "CL_TELEMETRY_ENABLED"
envTelemetryEndpoint = "CL_TELEMETRY_ENDPOINT"
envTelemetryInsecureConn = "CL_TELEMETRY_INSECURE_CONNECTION"
envTelemetryCACertFile = "CL_TELEMETRY_CA_CERT_FILE"
envTelemetryAttribute = "CL_TELEMETRY_ATTRIBUTE_"
envTelemetryTraceSampleRatio = "CL_TELEMETRY_TRACE_SAMPLE_RATIO"
envTelemetryAuthHeader = "CL_TELEMETRY_AUTH_HEADER"
envTelemetryAuthPubKeyHex = "CL_TELEMETRY_AUTH_PUB_KEY_HEX"
envTelemetryEmitterBatchProcessor = "CL_TELEMETRY_EMITTER_BATCH_PROCESSOR"
envTelemetryEmitterExportTimeout = "CL_TELEMETRY_EMITTER_EXPORT_TIMEOUT"
envTelemetryEmitterExportInterval = "CL_TELEMETRY_EMITTER_EXPORT_INTERVAL"
envTelemetryEmitterExportMaxBatchSize = "CL_TELEMETRY_EMITTER_EXPORT_MAX_BATCH_SIZE"
envTelemetryEmitterMaxQueueSize = "CL_TELEMETRY_EMITTER_MAX_QUEUE_SIZE"
)

// EnvConfig is the configuration between the application and the LOOP executable. The values
Expand All @@ -46,16 +49,19 @@ type EnvConfig struct {
TracingTLSCertPath string
TracingAttributes map[string]string

TelemetryEnabled bool
TelemetryEndpoint string
TelemetryInsecureConnection bool
TelemetryCACertFile string
TelemetryAttributes OtelAttributes
TelemetryTraceSampleRatio float64
TelemetryAuthHeaders map[string]string
TelemetryAuthPubKeyHex string
TelemetryEmitterBatchProcessor bool
TelemetryEmitterExportTimeout time.Duration
TelemetryEnabled bool
TelemetryEndpoint string
TelemetryInsecureConnection bool
TelemetryCACertFile string
TelemetryAttributes OtelAttributes
TelemetryTraceSampleRatio float64
TelemetryAuthHeaders map[string]string
TelemetryAuthPubKeyHex string
TelemetryEmitterBatchProcessor bool
TelemetryEmitterExportTimeout time.Duration
TelemetryEmitterExportInterval time.Duration
TelemetryEmitterExportMaxBatchSize int
TelemetryEmitterMaxQueueSize int
}

// AsCmdEnv returns a slice of environment variable key/value pairs for an exec.Cmd.
Expand Down Expand Up @@ -93,7 +99,9 @@ func (e *EnvConfig) AsCmdEnv() (env []string) {
add(envTelemetryAuthPubKeyHex, e.TelemetryAuthPubKeyHex)
add(envTelemetryEmitterBatchProcessor, strconv.FormatBool(e.TelemetryEmitterBatchProcessor))
add(envTelemetryEmitterExportTimeout, e.TelemetryEmitterExportTimeout.String())

add(envTelemetryEmitterExportInterval, e.TelemetryEmitterExportInterval.String())
add(envTelemetryEmitterExportMaxBatchSize, strconv.Itoa(e.TelemetryEmitterExportMaxBatchSize))
add(envTelemetryEmitterMaxQueueSize, strconv.Itoa(e.TelemetryEmitterMaxQueueSize))
return
}

Expand Down Expand Up @@ -142,6 +150,26 @@ func (e *EnvConfig) parse() error {
e.TelemetryTraceSampleRatio = getFloat64OrZero(envTelemetryTraceSampleRatio)
e.TelemetryAuthHeaders = getMap(envTelemetryAuthHeader)
e.TelemetryAuthPubKeyHex = os.Getenv(envTelemetryAuthPubKeyHex)
e.TelemetryEmitterBatchProcessor, err = getBool(envTelemetryEmitterBatchProcessor)
if err != nil {
return fmt.Errorf("failed to parse %s: %w", envTelemetryEmitterBatchProcessor, err)
}
e.TelemetryEmitterExportTimeout, err = time.ParseDuration(os.Getenv(envTelemetryEmitterExportTimeout))
if err != nil {
return fmt.Errorf("failed to parse %s: %w", envTelemetryEmitterExportTimeout, err)
}
e.TelemetryEmitterExportInterval, err = time.ParseDuration(os.Getenv(envTelemetryEmitterExportInterval))
if err != nil {
return fmt.Errorf("failed to parse %s: %w", envTelemetryEmitterExportInterval, err)
}
e.TelemetryEmitterExportMaxBatchSize, err = strconv.Atoi(os.Getenv(envTelemetryEmitterExportMaxBatchSize))
if err != nil {
return fmt.Errorf("failed to parse %s: %w", envTelemetryEmitterExportMaxBatchSize, err)
}
e.TelemetryEmitterMaxQueueSize, err = strconv.Atoi(os.Getenv(envTelemetryEmitterMaxQueueSize))
if err != nil {
return fmt.Errorf("failed to parse %s: %w", envTelemetryEmitterMaxQueueSize, err)
}
}
return nil
}
Expand Down
175 changes: 142 additions & 33 deletions pkg/loop/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,74 @@ import (

func TestEnvConfig_parse(t *testing.T) {
cases := []struct {
name string
envVars map[string]string
expectError bool
expectedDatabaseURL string
expectedPrometheusPort int
expectedTracingEnabled bool
expectedTracingCollectorTarget string
expectedTracingSamplingRatio float64
expectedTracingTLSCertPath string
name string
envVars map[string]string
expectError bool
expectedDatabaseURL string
expectedPrometheusPort int
expectedTracingEnabled bool
expectedTracingCollectorTarget string
expectedTracingSamplingRatio float64
expectedTracingTLSCertPath string
expectedTelemetryEnabled bool
expectedTelemetryEndpoint string
expectedTelemetryInsecureConn bool
expectedTelemetryCACertFile string
expectedTelemetryAttributes OtelAttributes
expectedTelemetryTraceSampleRatio float64
expectedTelemetryAuthHeaders map[string]string
expectedTelemetryAuthPubKeyHex string
expectedTelemetryEmitterBatchProcessor bool
expectedTelemetryEmitterExportTimeout time.Duration
expectedTelemetryEmitterExportInterval time.Duration
expectedTelemetryEmitterExportMaxBatchSize int
expectedTelemetryEmitterMaxQueueSize int
}{
{
name: "All variables set correctly",
envVars: map[string]string{
envDatabaseURL: "postgres://user:password@localhost:5432/db",
envPromPort: "8080",
envTracingEnabled: "true",
envTracingCollectorTarget: "some:target",
envTracingSamplingRatio: "1.0",
envTracingTLSCertPath: "internal/test/fixtures/client.pem",
envTracingAttribute + "XYZ": "value",
envDatabaseURL: "postgres://user:password@localhost:5432/db",
envPromPort: "8080",
envTracingEnabled: "true",
envTracingCollectorTarget: "some:target",
envTracingSamplingRatio: "1.0",
envTracingTLSCertPath: "internal/test/fixtures/client.pem",
envTracingAttribute + "XYZ": "value",
envTelemetryEnabled: "true",
envTelemetryEndpoint: "example.com/beholder",
envTelemetryInsecureConn: "true",
envTelemetryCACertFile: "foo/bar",
envTelemetryAttribute + "foo": "bar",
envTelemetryAttribute + "baz": "42",
envTelemetryTraceSampleRatio: "0.42",
envTelemetryAuthHeader + "header-key": "header-value",
envTelemetryAuthPubKeyHex: "pub-key-hex",
envTelemetryEmitterBatchProcessor: "true",
envTelemetryEmitterExportTimeout: "1s",
envTelemetryEmitterExportInterval: "2s",
envTelemetryEmitterExportMaxBatchSize: "100",
envTelemetryEmitterMaxQueueSize: "1000",
},
expectError: false,
expectedDatabaseURL: "postgres://user:password@localhost:5432/db",
expectedPrometheusPort: 8080,
expectedTracingEnabled: true,
expectedTracingCollectorTarget: "some:target",
expectedTracingSamplingRatio: 1.0,
expectedTracingTLSCertPath: "internal/test/fixtures/client.pem",
expectError: false,
expectedDatabaseURL: "postgres://user:password@localhost:5432/db",
expectedPrometheusPort: 8080,
expectedTracingEnabled: true,
expectedTracingCollectorTarget: "some:target",
expectedTracingSamplingRatio: 1.0,
expectedTracingTLSCertPath: "internal/test/fixtures/client.pem",
expectedTelemetryEnabled: true,
expectedTelemetryEndpoint: "example.com/beholder",
expectedTelemetryInsecureConn: true,
expectedTelemetryCACertFile: "foo/bar",
expectedTelemetryAttributes: OtelAttributes{"foo": "bar", "baz": "42"},
expectedTelemetryTraceSampleRatio: 0.42,
expectedTelemetryAuthHeaders: map[string]string{"header-key": "header-value"},
expectedTelemetryAuthPubKeyHex: "pub-key-hex",
expectedTelemetryEmitterBatchProcessor: true,
expectedTelemetryEmitterExportTimeout: 1 * time.Second,
expectedTelemetryEmitterExportInterval: 2 * time.Second,
expectedTelemetryEmitterExportMaxBatchSize: 100,
expectedTelemetryEmitterMaxQueueSize: 1000,
},
{
name: "CL_DATABASE_URL parse error",
Expand Down Expand Up @@ -106,12 +146,75 @@ func TestEnvConfig_parse(t *testing.T) {
if config.TracingTLSCertPath != tc.expectedTracingTLSCertPath {
t.Errorf("Expected tracingTLSCertPath %s, got %s", tc.expectedTracingTLSCertPath, config.TracingTLSCertPath)
}
if config.TelemetryEnabled != tc.expectedTelemetryEnabled {
t.Errorf("Expected telemetryEnabled %v, got %v", tc.expectedTelemetryEnabled, config.TelemetryEnabled)
}
if config.TelemetryEndpoint != tc.expectedTelemetryEndpoint {
t.Errorf("Expected telemetryEndpoint %s, got %s", tc.expectedTelemetryEndpoint, config.TelemetryEndpoint)
}
if config.TelemetryInsecureConnection != tc.expectedTelemetryInsecureConn {
t.Errorf("Expected telemetryInsecureConn %v, got %v", tc.expectedTelemetryInsecureConn, config.TelemetryInsecureConnection)
}
if config.TelemetryCACertFile != tc.expectedTelemetryCACertFile {
t.Errorf("Expected telemetryCACertFile %s, got %s", tc.expectedTelemetryCACertFile, config.TelemetryCACertFile)
}
if !equalOtelAttributes(config.TelemetryAttributes, tc.expectedTelemetryAttributes) {
t.Errorf("Expected telemetryAttributes %v, got %v", tc.expectedTelemetryAttributes, config.TelemetryAttributes)
}
if config.TelemetryTraceSampleRatio != tc.expectedTelemetryTraceSampleRatio {
t.Errorf("Expected telemetryTraceSampleRatio %f, got %f", tc.expectedTelemetryTraceSampleRatio, config.TelemetryTraceSampleRatio)
}
if !equalStringMaps(config.TelemetryAuthHeaders, tc.expectedTelemetryAuthHeaders) {
t.Errorf("Expected telemetryAuthHeaders %v, got %v", tc.expectedTelemetryAuthHeaders, config.TelemetryAuthHeaders)
}
if config.TelemetryAuthPubKeyHex != tc.expectedTelemetryAuthPubKeyHex {
t.Errorf("Expected telemetryAuthPubKeyHex %s, got %s", tc.expectedTelemetryAuthPubKeyHex, config.TelemetryAuthPubKeyHex)
}
if config.TelemetryEmitterBatchProcessor != tc.expectedTelemetryEmitterBatchProcessor {
t.Errorf("Expected telemetryEmitterBatchProcessor %v, got %v", tc.expectedTelemetryEmitterBatchProcessor, config.TelemetryEmitterBatchProcessor)
}
if config.TelemetryEmitterExportTimeout != tc.expectedTelemetryEmitterExportTimeout {
t.Errorf("Expected telemetryEmitterExportTimeout %v, got %v", tc.expectedTelemetryEmitterExportTimeout, config.TelemetryEmitterExportTimeout)
}
if config.TelemetryEmitterExportInterval != tc.expectedTelemetryEmitterExportInterval {
t.Errorf("Expected telemetryEmitterExportInterval %v, got %v", tc.expectedTelemetryEmitterExportInterval, config.TelemetryEmitterExportInterval)
}
if config.TelemetryEmitterExportMaxBatchSize != tc.expectedTelemetryEmitterExportMaxBatchSize {
t.Errorf("Expected telemetryEmitterExportMaxBatchSize %d, got %d", tc.expectedTelemetryEmitterExportMaxBatchSize, config.TelemetryEmitterExportMaxBatchSize)
}
if config.TelemetryEmitterMaxQueueSize != tc.expectedTelemetryEmitterMaxQueueSize {
t.Errorf("Expected telemetryEmitterMaxQueueSize %d, got %d", tc.expectedTelemetryEmitterMaxQueueSize, config.TelemetryEmitterMaxQueueSize)
}
}
}
})
}
}

func equalOtelAttributes(a, b OtelAttributes) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if b[k] != v {
return false
}
}
return true
}

func equalStringMaps(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if b[k] != v {
return false
}
}
return true
}

func TestEnvConfig_AsCmdEnv(t *testing.T) {
envCfg := EnvConfig{
DatabaseURL: &url.URL{Scheme: "postgres", Host: "localhost:5432", User: url.UserPassword("user", "password"), Path: "/db"},
Expand All @@ -123,16 +226,19 @@ func TestEnvConfig_AsCmdEnv(t *testing.T) {
TracingTLSCertPath: "some/path",
TracingAttributes: map[string]string{"key": "value"},

TelemetryEnabled: true,
TelemetryEndpoint: "example.com/beholder",
TelemetryInsecureConnection: true,
TelemetryCACertFile: "foo/bar",
TelemetryAttributes: OtelAttributes{"foo": "bar", "baz": "42"},
TelemetryTraceSampleRatio: 0.42,
TelemetryAuthHeaders: map[string]string{"header-key": "header-value"},
TelemetryAuthPubKeyHex: "pub-key-hex",
TelemetryEmitterBatchProcessor: true,
TelemetryEmitterExportTimeout: 1 * time.Second,
TelemetryEnabled: true,
TelemetryEndpoint: "example.com/beholder",
TelemetryInsecureConnection: true,
TelemetryCACertFile: "foo/bar",
TelemetryAttributes: OtelAttributes{"foo": "bar", "baz": "42"},
TelemetryTraceSampleRatio: 0.42,
TelemetryAuthHeaders: map[string]string{"header-key": "header-value"},
TelemetryAuthPubKeyHex: "pub-key-hex",
TelemetryEmitterBatchProcessor: true,
TelemetryEmitterExportTimeout: 1 * time.Second,
TelemetryEmitterExportInterval: 2 * time.Second,
TelemetryEmitterExportMaxBatchSize: 100,
TelemetryEmitterMaxQueueSize: 1000,
}
got := map[string]string{}
for _, kv := range envCfg.AsCmdEnv() {
Expand Down Expand Up @@ -161,6 +267,9 @@ func TestEnvConfig_AsCmdEnv(t *testing.T) {
assert.Equal(t, "pub-key-hex", got[envTelemetryAuthPubKeyHex])
assert.Equal(t, "true", got[envTelemetryEmitterBatchProcessor])
assert.Equal(t, "1s", got[envTelemetryEmitterExportTimeout])
assert.Equal(t, "2s", got[envTelemetryEmitterExportInterval])
assert.Equal(t, "100", got[envTelemetryEmitterExportMaxBatchSize])
assert.Equal(t, "1000", got[envTelemetryEmitterMaxQueueSize])
}

func TestGetMap(t *testing.T) {
Expand Down
21 changes: 12 additions & 9 deletions pkg/loop/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,18 @@ func (s *Server) start() error {
}

beholderCfg := beholder.Config{
InsecureConnection: envCfg.TelemetryInsecureConnection,
CACertFile: envCfg.TelemetryCACertFile,
OtelExporterGRPCEndpoint: envCfg.TelemetryEndpoint,
ResourceAttributes: append(attributes, envCfg.TelemetryAttributes.AsStringAttributes()...),
TraceSampleRatio: envCfg.TelemetryTraceSampleRatio,
AuthHeaders: envCfg.TelemetryAuthHeaders,
AuthPublicKeyHex: envCfg.TelemetryAuthPubKeyHex,
EmitterBatchProcessor: envCfg.TelemetryEmitterBatchProcessor,
EmitterExportTimeout: envCfg.TelemetryEmitterExportTimeout,
InsecureConnection: envCfg.TelemetryInsecureConnection,
CACertFile: envCfg.TelemetryCACertFile,
OtelExporterGRPCEndpoint: envCfg.TelemetryEndpoint,
ResourceAttributes: append(attributes, envCfg.TelemetryAttributes.AsStringAttributes()...),
TraceSampleRatio: envCfg.TelemetryTraceSampleRatio,
AuthHeaders: envCfg.TelemetryAuthHeaders,
AuthPublicKeyHex: envCfg.TelemetryAuthPubKeyHex,
EmitterBatchProcessor: envCfg.TelemetryEmitterBatchProcessor,
EmitterExportTimeout: envCfg.TelemetryEmitterExportTimeout,
EmitterExportInterval: envCfg.TelemetryEmitterExportInterval,
EmitterExportMaxBatchSize: envCfg.TelemetryEmitterExportMaxBatchSize,
EmitterMaxQueueSize: envCfg.TelemetryEmitterMaxQueueSize,
}

if tracingConfig.Enabled {
Expand Down

0 comments on commit 4d865c3

Please sign in to comment.