mz_adapter/
config.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::collections::BTreeMap;
11use std::path::PathBuf;
12
13use mz_build_info::BuildInfo;
14use mz_ore::metric;
15use mz_ore::metrics::{MetricsRegistry, UIntGauge};
16use mz_ore::now::NowFn;
17use mz_sql::catalog::EnvironmentId;
18use prometheus::IntCounter;
19
20mod backend;
21mod frontend;
22mod params;
23mod sync;
24
25pub use backend::SystemParameterBackend;
26pub use frontend::SystemParameterFrontend;
27pub use params::{ModifiedParameter, SynchronizedParameters};
28pub use sync::system_parameter_sync;
29
30/// A factory for [SystemParameterFrontend] instances.
31#[derive(Clone, Debug)]
32pub struct SystemParameterSyncConfig {
33    /// The environment ID that should identify connected clients.
34    env_id: EnvironmentId,
35    /// Build info for the environment running this.
36    build_info: &'static BuildInfo,
37    /// Parameter sync metrics.
38    metrics: Metrics,
39    ///  /// A map from parameter names to LaunchDarkly feature keys
40    /// to use when populating the [SynchronizedParameters]
41    /// instance in [SystemParameterFrontend::pull].
42    key_map: BTreeMap<String, String>,
43    /// Configuration for the parameter backend that we're syncing with.
44    backend_config: SystemParameterSyncClientConfig,
45}
46
47#[derive(Clone, Debug)]
48pub enum SystemParameterSyncClientConfig {
49    File {
50        // Path to a JSON config file that contains system parameters.
51        path: PathBuf,
52    },
53    LaunchDarkly {
54        /// The LaunchDarkly SDK key
55        sdk_key: String,
56        /// Function to return the current time.
57        now_fn: NowFn,
58    },
59}
60
61impl SystemParameterSyncClientConfig {
62    fn is_launch_darkly(&self) -> bool {
63        match &self {
64            Self::LaunchDarkly { .. } => true,
65            _ => false,
66        }
67    }
68}
69
70impl SystemParameterSyncConfig {
71    /// Construct a new [SystemParameterFrontend] instance.
72    pub fn new(
73        env_id: EnvironmentId,
74        build_info: &'static BuildInfo,
75        registry: &MetricsRegistry,
76        key_map: BTreeMap<String, String>,
77        backend_config: SystemParameterSyncClientConfig,
78    ) -> Self {
79        Self {
80            env_id,
81            build_info,
82            metrics: Metrics::register_into(registry),
83            key_map,
84            backend_config,
85        }
86    }
87}
88
89#[derive(Debug, Clone)]
90pub(super) struct Metrics {
91    pub last_cse_time_seconds: UIntGauge,
92    pub last_sse_time_seconds: UIntGauge,
93    pub params_changed: IntCounter,
94}
95
96impl Metrics {
97    pub(super) fn register_into(registry: &MetricsRegistry) -> Self {
98        Self {
99            last_cse_time_seconds: registry.register(metric!(
100                name: "mz_parameter_frontend_last_cse_time_seconds",
101                help: "The last known time when the LaunchDarkly client sent an event to the LaunchDarkly server (as unix timestamp).",
102            )),
103            last_sse_time_seconds: registry.register(metric!(
104                name: "mz_parameter_frontend_last_sse_time_seconds",
105                help: "The last known time when the LaunchDarkly client received an event from the LaunchDarkly server (as unix timestamp).",
106            )),
107            params_changed: registry.register(metric!(
108                name: "mz_parameter_frontend_params_changed",
109                help: "The number of parameter changes pulled from the LaunchDarkly frontend.",
110            )),
111        }
112    }
113}