Skip to content

Commit

Permalink
fix: add StatsD config to .toml
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 9, 2024
1 parent 61f6b05 commit f39ff3f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
28 changes: 26 additions & 2 deletions features/file_config.testfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generate a configuration file with provided values
run :anycable_gen_config,
["sh", "-c", './dist/anycable-go --noauth --port 2024 --broadcast_adapter=http,redisx --sse --metrics_tags=env:production,node_id:xyz --print-config > ./anycable.toml'],
env: {"ANYCABLE_SECRET" => "file-secret", "ANYCABLE_NODE_ID" => "node-1"}, clean_env: true
["sh", "-c", './dist/anycable-go --noauth --port 2024 --broadcast_adapter=http,redisx --sse --metrics_tags=env:production,node_id:xyz --statsd_tags_format=datadog --print-config > ./anycable.toml'],
env: {"ANYCABLE_SECRET" => "file-secret", "ANYCABLE_NODE_ID" => "node-1", "ANYCABLE_STATSD_HOST" => "localhost:8125"}, clean_env: true

unless File.exist?("anycable.toml")
fail "Config file hasn't been generated"
Expand Down Expand Up @@ -29,6 +29,8 @@ assert_equal("broadcast adapters", %w[http redisx], config["broadcast_adapters"]
assert_equal("server.port", 2024, config.dig("server", "port"))
assert_equal("sse.enabled", true, config.dig("sse", "enabled"))
assert_equal("metrics.tags", {"env" => "production", "node_id" => "xyz"}, config.dig("metrics", "tags"))
assert_equal("metrics.statsd.host", "localhost:8125", config.dig("metrics", "statsd", "host"))
assert_equal("metrics.statsd.tags_format", "datadog", config.dig("metrics", "statsd", "tags_format"))

if $errors.any?
fail $errors.join("\n")
Expand All @@ -42,3 +44,25 @@ config = PerfectTOML.parse(stdout(:anycable_ignore_config_path))
$errors.clear
assert_equal("node ID", nil, config["node_id"])
assert_equal("secret", nil, config["secret"])

# Overriding config data
run :anycable_override_config, "dist/anycable-go --port=2025 --statsd_tags_format=influx --print-config",
clean_env: true,
env: {"ANYCABLE_NODE_ID" => "node-2", "ANYCABLE_SECRET" => "override"}

config = PerfectTOML.parse(stdout(:anycable_override_config))

$errors.clear

# top-level params
assert_equal("node ID", "node-2", config["node_id"])
assert_equal("noauth", true, config["noauth"])
assert_equal("secret", "override", config["secret"])
assert_equal("broadcast adapters", %w[http redisx], config["broadcast_adapters"])

# nested params
assert_equal("server.port", 2025, config.dig("server", "port"))
assert_equal("sse.enabled", true, config.dig("sse", "enabled"))
assert_equal("metrics.tags", {"env" => "production", "node_id" => "xyz"}, config.dig("metrics", "tags"))
assert_equal("metrics.statsd.host", "localhost:8125", config.dig("metrics", "statsd", "host"))
assert_equal("metrics.statsd.tags_format", "influx", config.dig("metrics", "statsd", "tags_format"))
10 changes: 10 additions & 0 deletions metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ func (c Config) ToToml() string {
result.WriteString("# tags.key = \"value\"\n")
}

result.WriteString("# StatsD configuration\n")
var prefix = "# "
if c.Statsd.Enabled() {
prefix = ""
}
result.WriteString(fmt.Sprintf("%sstatsd.host = \"%s\"\n", prefix, c.Statsd.Host))
result.WriteString(fmt.Sprintf("%sstatsd.prefix = \"%s\"\n", prefix, c.Statsd.Prefix))
result.WriteString(fmt.Sprintf("%sstatsd.tags_format = \"%s\"\n", prefix, c.Statsd.TagFormat))
result.WriteString(fmt.Sprintf("%sstatsd.max_packet_size = %d\n", prefix, c.Statsd.MaxPacketSize))

result.WriteString("\n")

return result.String()
Expand Down
8 changes: 4 additions & 4 deletions metrics/statsd_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

type StatsdConfig struct {
Host string
Prefix string
TagFormat string
MaxPacketSize int
Host string `toml:"host"`
Prefix string `toml:"prefix"`
TagFormat string `toml:"tags_format"`
MaxPacketSize int `toml:"max_packet_size"`
}

type StatsdLogger struct {
Expand Down

0 comments on commit f39ff3f

Please sign in to comment.