Merge pull request #8564 from woz5999/support-env-var-expansion-in-statsd-url

support env var expansion in envoy statsd urls
This commit is contained in:
Daniel Nephin 2021-03-18 20:03:27 -04:00 committed by GitHub
commit 3f1a8a4171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 3 deletions

3
.changelog/8564.txt Normal file
View File

@ -0,0 +1,3 @@
```release-notes:improvement
cli: the `consul connect envoy --envoy_statsd_url` flag will now resolve the `$HOST_IP` environment variable, as part of a full url.
```

View File

@ -280,6 +280,8 @@ func (c *BootstrapConfig) generateStatsSinkJSON(name string, typeName string, ad
// Resolve address ENV var
if len(addr) > 2 && addr[0] == '$' {
addr = os.Getenv(addr[1:])
} else {
addr = os.Expand(addr, statsSinkEnvMapping)
}
u, err := url.Parse(addr)
@ -318,6 +320,18 @@ func (c *BootstrapConfig) generateStatsSinkJSON(name string, typeName string, ad
}`, nil
}
func statsSinkEnvMapping(s string) string {
allowedStatsSinkEnvVars := map[string]bool{
"HOST_IP": true,
}
if !allowedStatsSinkEnvVars[s] {
// if the specified env var isn't explicitly allowed, unexpand it
return fmt.Sprintf("${%s}", s)
}
return os.Getenv(s)
}
// resourceTagSpecifiers returns patterns used to generate tags from cluster and filter metric names.
func resourceTagSpecifiers(omitDeprecatedTags bool) ([]string, error) {
const (

View File

@ -485,6 +485,40 @@ func TestBootstrapConfig_ConfigureArgs(t *testing.T) {
},
wantErr: false,
},
{
name: "simple-statsd-sink-inline-env-allowed",
input: BootstrapConfig{
StatsdURL: "udp://$HOST_IP:9125",
},
env: []string{"HOST_IP=127.0.0.1"},
wantArgs: BootstrapTplArgs{
StatsConfigJSON: defaultStatsConfigJSON,
StatsSinksJSON: `[{
"name": "envoy.stat_sinks.statsd",
"typedConfig": {
"@type": "type.googleapis.com/envoy.config.metrics.v3.StatsdSink",
"address": {
"socket_address": {
"address": "127.0.0.1",
"port_value": 9125
}
}
}
}]`,
},
wantErr: false,
},
{
name: "simple-statsd-sink-inline-env-disallowed",
input: BootstrapConfig{
StatsdURL: "udp://$HOST_ADDRESS:9125",
},
env: []string{"HOST_ADDRESS=127.0.0.1"},
wantArgs: BootstrapTplArgs{
StatsConfigJSON: defaultStatsConfigJSON,
},
wantErr: true,
},
{
name: "simple-dogstatsd-sink",
input: BootstrapConfig{

View File

@ -130,15 +130,17 @@ definition](/docs/connect/registration/service-registration) or
~> **Note:** currently the url **must use an ip address** not a dns name due
to the way Envoy is setup for StatsD.
Expansion of the environment variable `HOST_IP` is supported, e.g.
`udp://${HOST_IP}:8125`.
Users can also specify the whole parameter in the form `$ENV_VAR_NAME`, which
will cause the `consul connect envoy` command to resolve the actual URL from
the named environment variable when it runs. This, for example, allows each
pod in a Kubernetes cluster to learn of a pod-specific IP address for StatsD
when the Envoy instance is bootstrapped while still allowing global
configuration of all proxies to use StatsD in the [global `proxy-defaults`
configuration entry](/docs/connect/config-entries/proxy-defaults). The env variable must contain a full valid URL
value as specified above and nothing else. It is not currently possible to use
environment variables as only part of the URL.
configuration entry](/docs/connect/config-entries/proxy-defaults). The env
variable must contain a full valid URL value as specified above and nothing else.
- `envoy_dogstatsd_url` - The same as `envoy_statsd_url` with the following
differences in behavior: