1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Dyncfgs used by the balancer.

use std::str::FromStr;
use std::time::Duration;

use anyhow::anyhow;
use mz_dyncfg::{Config, ConfigSet, ConfigUpdates};
use mz_tracing::params::TracingParameters;
use mz_tracing::{CloneableEnvFilter, SerializableDirective};
use tracing_subscriber::filter::Directive;

// The defaults here must be set to an appropriate value in case LaunchDarkly is down because we
// continue startup even in that case.
//
// All configuration names should be prefixed with "balancerd_" to avoid name collisions.

/// Duration to wait after SIGTERM for outstanding connections to complete.
pub const SIGTERM_WAIT: Config<Duration> = Config::new(
    "balancerd_sigterm_wait",
    Duration::from_secs(60 * 10),
    "Duration to wait after SIGTERM for outstanding connections to complete.",
);

/// Whether to inject tcp proxy protocol headers to downstream http servers.
pub const INJECT_PROXY_PROTOCOL_HEADER_HTTP: Config<bool> = Config::new(
    "balancerd_inject_proxy_protocol_header_http",
    false,
    "Whether to inject tcp proxy protocol headers to downstream http servers.",
);

/// Sets the filter to apply to stderr logging.
pub const LOGGING_FILTER: Config<&str> = Config::new(
    "balancerd_log_filter",
    "info",
    "Sets the filter to apply to stderr logging.",
);

/// Sets the filter to apply to OpenTelemetry-backed distributed tracing.
pub const OPENTELEMETRY_FILTER: Config<&str> = Config::new(
    "balancerd_opentelemetry_filter",
    "info",
    "Sets the filter to apply to OpenTelemetry-backed distributed tracing.",
);

/// Sets additional default directives to apply to stderr logging.
/// These apply to all variations of `log_filter`. Directives other than
/// `module=off` are likely incorrect. Comma separated list.
pub const LOGGING_FILTER_DEFAULTS: Config<fn() -> String> = Config::new(
    "balancerd_log_filter_defaults",
    || mz_ore::tracing::LOGGING_DEFAULTS_STR.join(","),
    "Sets additional default directives to apply to stderr logging. \
    These apply to all variations of `log_filter`. Directives other than \
    `module=off` are likely incorrect. Comma separated list.",
);

/// Sets additional default directives to apply to OpenTelemetry-backed
/// distributed tracing.
/// These apply to all variations of `opentelemetry_filter`. Directives other than
/// `module=off` are likely incorrect. Comma separated list.
pub const OPENTELEMETRY_FILTER_DEFAULTS: Config<fn() -> String> = Config::new(
    "balancerd_opentelemetry_filter_defaults",
    || mz_ore::tracing::OPENTELEMETRY_DEFAULTS_STR.join(","),
    "Sets additional default directives to apply to OpenTelemetry-backed \
    distributed tracing. \
    These apply to all variations of `opentelemetry_filter`. Directives other than \
    `module=off` are likely incorrect. Comma separated list.",
);

/// Sets additional default directives to apply to sentry logging. \
/// These apply on top of a default `info` directive. Directives other than \
/// `module=off` are likely incorrect. Comma separated list.
pub const SENTRY_FILTERS: Config<fn() -> String> = Config::new(
    "balancerd_sentry_filters",
    || mz_ore::tracing::SENTRY_DEFAULTS_STR.join(","),
    "Sets additional default directives to apply to sentry logging. \
    These apply on top of a default `info` directive. Directives other than \
    `module=off` are likely incorrect. Comma separated list.",
);

/// Adds the full set of all balancer `Config`s.
pub fn all_dyncfgs(configs: ConfigSet) -> ConfigSet {
    configs
        .add(&SIGTERM_WAIT)
        .add(&INJECT_PROXY_PROTOCOL_HEADER_HTTP)
        .add(&LOGGING_FILTER)
        .add(&OPENTELEMETRY_FILTER)
        .add(&LOGGING_FILTER_DEFAULTS)
        .add(&OPENTELEMETRY_FILTER_DEFAULTS)
        .add(&SENTRY_FILTERS)
}

/// Overrides default values for the Balancerd ConfigSet.
///
/// This is meant to be used in combination with clap cli flag
/// `--default-config key=value`
/// Not all ConfigSet values can be defaulted with this
/// function. An error will be returned if a key does
/// not accept default overrides, or if there is a value
/// parsing error..
pub(crate) fn set_defaults(
    config_set: &mut ConfigSet,
    default_config: Vec<(String, String)>,
) -> Result<(), anyhow::Error> {
    let mut config_updates = ConfigUpdates::default();
    for (k, v) in default_config.iter() {
        if k.as_str() == INJECT_PROXY_PROTOCOL_HEADER_HTTP.name() {
            config_updates.add_dynamic(
                INJECT_PROXY_PROTOCOL_HEADER_HTTP.name(),
                mz_dyncfg::ConfigVal::Bool(bool::from_str(v)?),
            )
        } else {
            return Err(anyhow!("Invalid default config value {k}"));
        }
    }
    config_updates.apply(config_set);
    Ok(())
}

/// Get all dynamic tracing config parameters from this [`ConfigSet`].
pub fn tracing_config(configs: &ConfigSet) -> Result<TracingParameters, String> {
    fn to_serializable_directives(
        config: &Config<fn() -> String>,
        configs: &ConfigSet,
    ) -> Result<Vec<SerializableDirective>, String> {
        let directives = config.get(configs);
        let directives: Vec<_> = directives
            .split(',')
            .map(Directive::from_str)
            .collect::<Result<_, _>>()
            .map_err(|e| e.to_string())?;
        Ok(directives.into_iter().map(|d| d.into()).collect())
    }

    let log_filter = LOGGING_FILTER.get(configs);
    let log_filter = CloneableEnvFilter::from_str(&log_filter).map_err(|e| e.to_string())?;

    let opentelemetry_filter = OPENTELEMETRY_FILTER.get(configs);
    let opentelemetry_filter =
        CloneableEnvFilter::from_str(&opentelemetry_filter).map_err(|e| e.to_string())?;

    let log_filter_defaults = to_serializable_directives(&LOGGING_FILTER_DEFAULTS, configs)?;

    let opentelemetry_filter_defaults =
        to_serializable_directives(&OPENTELEMETRY_FILTER_DEFAULTS, configs)?;

    let sentry_filters = to_serializable_directives(&SENTRY_FILTERS, configs)?;

    Ok(TracingParameters {
        log_filter: Some(log_filter),
        opentelemetry_filter: Some(opentelemetry_filter),
        log_filter_defaults,
        opentelemetry_filter_defaults,
        sentry_filters,
    })
}

/// Returns true if `updates` contains an update to a tracing config, false otherwise.
pub fn has_tracing_config_update(updates: &ConfigUpdates) -> bool {
    [
        LOGGING_FILTER.name(),
        OPENTELEMETRY_FILTER.name(),
        LOGGING_FILTER_DEFAULTS.name(),
        OPENTELEMETRY_FILTER_DEFAULTS.name(),
        SENTRY_FILTERS.name(),
    ]
    .into_iter()
    .any(|name| updates.updates.contains_key(name))
}