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;
11
12use mz_build_info::BuildInfo;
13use mz_ore::metric;
14use mz_ore::metrics::{MetricsRegistry, UIntGauge};
15use mz_ore::now::NowFn;
16use mz_sql::catalog::EnvironmentId;
17use prometheus::IntCounter;
18
19mod backend;
20mod frontend;
21mod params;
22mod sync;
23
24pub use backend::SystemParameterBackend;
25pub use frontend::SystemParameterFrontend;
26pub use params::{ModifiedParameter, SynchronizedParameters};
27pub use sync::system_parameter_sync;
28
29/// A factory for [SystemParameterFrontend] instances.
30#[derive(Clone, Debug)]
31pub struct SystemParameterSyncConfig {
32    /// The environment ID that should identify connected clients.
33    env_id: EnvironmentId,
34    /// Build info for the environment running this.
35    build_info: &'static BuildInfo,
36    /// Parameter sync metrics.
37    metrics: Metrics,
38    /// Function to return the current time.
39    now_fn: NowFn,
40    /// The SDK key.
41    ld_sdk_key: String,
42    /// A map from parameter names to LaunchDarkly feature keys
43    /// to use when populating the [SynchronizedParameters]
44    /// instance in [SystemParameterFrontend::pull].
45    ld_key_map: BTreeMap<String, String>,
46}
47
48impl SystemParameterSyncConfig {
49    /// Construct a new [SystemParameterFrontend] instance.
50    pub fn new(
51        env_id: EnvironmentId,
52        build_info: &'static BuildInfo,
53        registry: &MetricsRegistry,
54        now_fn: NowFn,
55        ld_sdk_key: String,
56        ld_key_map: BTreeMap<String, String>,
57    ) -> Self {
58        Self {
59            env_id,
60            build_info,
61            metrics: Metrics::register_into(registry),
62            now_fn,
63            ld_sdk_key,
64            ld_key_map,
65        }
66    }
67}
68
69#[derive(Debug, Clone)]
70pub(super) struct Metrics {
71    pub last_cse_time_seconds: UIntGauge,
72    pub last_sse_time_seconds: UIntGauge,
73    pub params_changed: IntCounter,
74}
75
76impl Metrics {
77    pub(super) fn register_into(registry: &MetricsRegistry) -> Self {
78        Self {
79            last_cse_time_seconds: registry.register(metric!(
80                name: "mz_parameter_frontend_last_cse_time_seconds",
81                help: "The last known time when the LaunchDarkly client sent an event to the LaunchDarkly server (as unix timestamp).",
82            )),
83            last_sse_time_seconds: registry.register(metric!(
84                name: "mz_parameter_frontend_last_sse_time_seconds",
85                help: "The last known time when the LaunchDarkly client received an event from the LaunchDarkly server (as unix timestamp).",
86            )),
87            params_changed: registry.register(metric!(
88                name: "mz_parameter_frontend_params_changed",
89                help: "The number of parameter changes pulled from the LaunchDarkly frontend.",
90            )),
91        }
92    }
93}