Skip to content

Commit

Permalink
add telemetry config flags
Browse files Browse the repository at this point in the history
  • Loading branch information
wesl-ee committed Aug 15, 2024
1 parent 1cbdd93 commit 58016dc
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/connect/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const (
DefaultPrometheusServerAddress = "0.0.0.0:8002"
// DefaultMetricsEnabled is the default value for enabling prometheus metrics in slinky.
DefaultMetricsEnabled = true
// DefaultTelemetryDisabled is the default value for disabling telemetry
DefaultTelemetryDisabled = false
// DefaultTelemetryDisabled is the default value for disabling telemetry
DefaultTelemetryPushAddress = "127.0.0.1:8125"
// DefaultHost is the default for the slinky oracle server host.
DefaultHost = "0.0.0.0"
// DefaultPort is the default for the slinky oracle server port.
Expand All @@ -40,6 +44,10 @@ func DefaultOracleConfig() config.OracleConfig {
Metrics: config.MetricsConfig{
PrometheusServerAddress: DefaultPrometheusServerAddress,
Enabled: DefaultMetricsEnabled,
Telemetry: config.TelemetryConfig{
Disabled: DefaultTelemetryDisabled,
PushAddress: DefaultTelemetryPushAddress,
},
},
Providers: make(map[string]config.ProviderConfig),
Host: DefaultHost,
Expand Down
2 changes: 2 additions & 0 deletions cmd/connect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ var (

// oracle config flags.
flagMetricsEnabled = "metrics-enabled"
flagTelemetryDisabled = "telemetry-disabled"
flagTelemetryPushAddress = "telemetry-push-address"
flagMetricsPrometheusAddress = "metrics-prometheus-address"
flagHost = "host"
flagPort = "port"
Expand Down
23 changes: 23 additions & 0 deletions oracle/config/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ type MetricsConfig struct {
// metrics to.
PrometheusServerAddress string `json:"prometheusServerAddress"`

Telemetry TelemetryConfig `json:"telemetry"`

// Enabled indicates whether metrics should be enabled.
Enabled bool `json:"enabled"`
}

type TelemetryConfig struct {
// Toggle to disable opt-out telemetry
Disabled bool `json:"disabled"`

// Address of the remote server to push telemetry to
PushAddress string `json:"pushAddress"`
}

// ValidateBasic performs basic validation of the config.
func (c *MetricsConfig) ValidateBasic() error {
if !c.Enabled {
Expand All @@ -26,5 +36,18 @@ func (c *MetricsConfig) ValidateBasic() error {
return fmt.Errorf("must supply a non-empty prometheus server address if metrics are enabled")
}

return c.Telemetry.ValidateBasic()
}

// ValidateBasic performs basic validation of the config.
func (c *TelemetryConfig) ValidateBasic() error {
if c.Disabled {
return nil
}

if len(c.PushAddress) == 0 {
return fmt.Errorf("must supply a non-empty push address when telemetry is not disabled")
}

return nil
}
25 changes: 25 additions & 0 deletions oracle/config/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func TestMetricsConfig(t *testing.T) {
config: config.MetricsConfig{
Enabled: true,
PrometheusServerAddress: "localhost:9090",
Telemetry: config.TelemetryConfig{
Disabled: false,
PushAddress: "localhost:8152",
},
},
expectedErr: false,
},
Expand All @@ -30,6 +34,17 @@ func TestMetricsConfig(t *testing.T) {
},
expectedErr: true,
},
{
name: "bad config with no telemetry push address",
config: config.MetricsConfig{
Enabled: true,
Telemetry: config.TelemetryConfig{
Disabled: false,
PushAddress: "",
},
},
expectedErr: true,
},
{
name: "no metrics enabled",
config: config.MetricsConfig{
Expand All @@ -38,6 +53,16 @@ func TestMetricsConfig(t *testing.T) {
},
expectedErr: false,
},
{
name: "telemetry disabled",
config: config.MetricsConfig{
Enabled: false,
PrometheusServerAddress: "",
Telemetry: config.TelemetryConfig{
},
},
expectedErr: false,
},
}

for _, tc := range testCases {
Expand Down
12 changes: 12 additions & 0 deletions oracle/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var (
oracleCfg = config.OracleConfig{
Metrics: config.MetricsConfig{
Enabled: false,
Telemetry: config.TelemetryConfig{
Disabled: true,
},
},
UpdateInterval: 1500 * time.Millisecond,
MaxPriceAge: 2 * time.Minute,
Expand Down Expand Up @@ -64,6 +67,9 @@ var (
oracleCfgWithMapper = config.OracleConfig{
Metrics: config.MetricsConfig{
Enabled: false,
Telemetry: config.TelemetryConfig{
Disabled: true,
},
},
UpdateInterval: 1500 * time.Millisecond,
MaxPriceAge: 2 * time.Minute,
Expand Down Expand Up @@ -93,6 +99,9 @@ var (
oracleCfgWithMockMapper = config.OracleConfig{
Metrics: config.MetricsConfig{
Enabled: false,
Telemetry: config.TelemetryConfig{
Disabled: true,
},
},
UpdateInterval: 1500 * time.Millisecond,
MaxPriceAge: 2 * time.Minute,
Expand Down Expand Up @@ -122,6 +131,9 @@ var (
oracleCfgWithOnlyMockMapper = config.OracleConfig{
Metrics: config.MetricsConfig{
Enabled: false,
Telemetry: config.TelemetryConfig{
Disabled: true,
},
},
UpdateInterval: 1500 * time.Millisecond,
MaxPriceAge: 2 * time.Minute,
Expand Down

0 comments on commit 58016dc

Please sign in to comment.