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:
commit
3f1a8a4171
|
@ -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.
|
||||
```
|
|
@ -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 (
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue