Debian Codegraph @

crt/s2n/bindings/rust/standard/s2n-tls-metrics-schema/src/metric_names.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 //! Canonical metric name definitions and caching for TLS telemetry.
5 //!
6 //! All serialized metric names representations are defined here.
7 //! Downstream crates should import from this module rather than
8 //! duplicating string literals.
9
10 use std::{
11 collections::HashMap,
12 fmt::Display,
13 sync::{LazyLock, RwLock},
14 };
15
16 use crate::static_lists::{
17 Alert, CERT_KEY_COUNT, CERT_SIG_COUNT, CIPHER_COUNT, CertKeyType, CertSignatureAlgorithm,
18 Cipher, DEFINED_ALERTS_COUNT, FiniteCounter, GROUP_COUNT, Group, PROTOCOL_COUNT,
19 SIGNATURE_COUNT, Signature, Version,
20 };
21
22 /// Cache key keyed by slot index so the cache type stays non-generic.
23 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24 struct TelemetryLabel {
25 prefix: &'static str,
26 counter_slot: usize,
27 }
28
29 /// We want all of our metrics counters to be prefixed, e.g. `group.negotiated.secp256r1`
30 /// This will allow much easier CloudWatch graphs, because you can say things like
31 /// "graph all `group.negotiated.*` metrics"
32 ///
33 /// metrique needs the string to be static, so we deliberately "leak" the data.
34 ///
35 /// This is acceptable because it's just a finite set of values.
36 #[derive(Debug, Default)]
37 struct TelemetryLabeller(RwLock<HashMap<TelemetryLabel, &'static str>>);
38
39 impl TelemetryLabeller {
40 fn get(&self, telemetry: &TelemetryLabel) -> Option<&'static str> {
41 self.0.read().unwrap().get(telemetry).map(|label| &**label)
42 }
43
44 fn insert(&self, telemetry: &TelemetryLabel, value: String) -> &'static str {
45 let mut write_lock = self.0.write().unwrap();
46 // it's important that we only leak _after_ we have acquired the write lock.
47 // otherwise we might end up leaking extra copies of the metric label
48 let label = value.leak();
49 write_lock.insert(*telemetry, label);
50 label
51 }
52 }
53
54 /// lookup from metric to the prefixed string, e.g. "group.negotiated.secp256r1"
55 pub fn telemetry_label<T>(counter_slot: usize, item: T, prefix: &'static str) -> &'static str
56 where
57 T: Display,
58 {
59 static PREFIXER: LazyLock<TelemetryLabeller> = LazyLock::new(TelemetryLabeller::default);
60
61 let key = TelemetryLabel {
62 prefix,
63 counter_slot,
64 };
65
66 match PREFIXER.get(&key) {
67 Some(label) => label,
68 None => PREFIXER.insert(&key, format!("{prefix}.{item}")),
69 }
70 }
71
72 pub const HANDSHAKE_SUCCESS_COUNT: &str = "handshake_success_count";
73 pub const HANDSHAKE_FAILURE_COUNT: &str = "handshake_failure_count";
74 pub const COMPATIBILITY_GENERAL20251201: &str = "compatibility.general20251201";
75 pub const COMPATIBILITY_FIPS20251201: &str = "compatibility.fips20251201";
76 pub const COMPATIBILITY_CNSA1: &str = "compatibility.cnsa1";
77 pub const COMPATIBILITY_CNSA2: &str = "compatibility.cnsa2";
78 pub const SECURITY_POLICY_PREFIX: &str = "tls_policy";
79 pub const SECURITY_POLICY_TOO_MANY: &str = "tls_policy.TOO_MANY";
80
81 pub fn security_policy_name(policy: &str) -> String {
82 format!("{SECURITY_POLICY_PREFIX}.{policy}")
83 }
84
85 pub const SSLV2_CLIENT_HELLO: &str = "sslv2_client_hello";
86 pub const HANDSHAKE_DURATION_US: &str = "handshake_duration_us";
87 pub const HANDSHAKE_COMPUTE_US: &str = "handshake_compute_us";
88 pub const SYNTHETIC_TRAFFIC_COUNT: &str = "synthetic_traffic_count";
89
90 pub const ALL_SCALARS: &[&str] = &[
91 COMPATIBILITY_GENERAL20251201,
92 COMPATIBILITY_FIPS20251201,
93 COMPATIBILITY_CNSA1,
94 COMPATIBILITY_CNSA2,
95 SSLV2_CLIENT_HELLO,
96 HANDSHAKE_SUCCESS_COUNT,
97 HANDSHAKE_FAILURE_COUNT,
98 HANDSHAKE_DURATION_US,
99 HANDSHAKE_COMPUTE_US,
100 SYNTHETIC_TRAFFIC_COUNT,
101 SERVER_CERT_PARSE_FAILURE,
102 CLIENT_CERT_PARSE_FAILURE,
103 ];
104
105 /// A counter group descriptor: prefix string, element count, and cached name accessor.
106 ///
107 /// Each group represents one (TlsParam, State) combination, e.g. "cipher.negotiated".
108 /// Individual metric names are formed as `"{prefix}.{element}"` and cached via
109 /// [`telemetry_label`] for zero-allocation access after the first call.
110 pub struct CounterGroup {
111 pub prefix: &'static str,
112 pub count: usize,
113 name_from_slot: fn(usize, &'static str) -> &'static str,
114 }
115
116 impl CounterGroup {
117 /// Returns the cached metric name for a slot index.
118 pub fn metric_name(&self, slot: usize) -> &'static str {
119 debug_assert!(
120 slot < self.count,
121 "slot {slot} out of range for {}",
122 self.prefix
123 );
124 (self.name_from_slot)(slot, self.prefix)
125 }
126
127 /// Returns the cached metric name when the caller already has the element.
128 pub fn metric_name_for<T: Display>(&self, slot: usize, element: T) -> &'static str {
129 telemetry_label(slot, element, self.prefix)
130 }
131 }
132
133 fn version_metric_name(slot: usize, prefix: &'static str) -> &'static str {
134 telemetry_label(slot, Version::key_from_slot(slot).unwrap(), prefix)
135 }
136
137 fn cipher_metric_name(slot: usize, prefix: &'static str) -> &'static str {
138 telemetry_label(slot, Cipher::key_from_slot(slot).unwrap(), prefix)
139 }
140
141 fn group_metric_name(slot: usize, prefix: &'static str) -> &'static str {
142 telemetry_label(slot, Group::key_from_slot(slot).unwrap(), prefix)
143 }
144
145 fn signature_metric_name(slot: usize, prefix: &'static str) -> &'static str {
146 telemetry_label(slot, Signature::key_from_slot(slot).unwrap(), prefix)
147 }
148
149 fn alert_metric_name(slot: usize, prefix: &'static str) -> &'static str {
150 telemetry_label(slot, Alert::key_from_slot(slot).unwrap(), prefix)
151 }
152
153 pub const ALERTS: CounterGroup = CounterGroup {
154 prefix: "alert",
155 count: DEFINED_ALERTS_COUNT,
156 name_from_slot: alert_metric_name,
157 };
158
159 fn cert_key_metric_name(slot: usize, prefix: &'static str) -> &'static str {
160 telemetry_label(slot, CertKeyType::key_from_slot(slot).unwrap(), prefix)
161 }
162
163 fn cert_sig_metric_name(slot: usize, prefix: &'static str) -> &'static str {
164 telemetry_label(
165 slot,
166 CertSignatureAlgorithm::key_from_slot(slot).unwrap(),
167 prefix,
168 )
169 }
170
171 pub const SERVER_CERT_PARSE_FAILURE: &str = "cert.server.subscriber_parse_failure";
172 pub const CLIENT_CERT_PARSE_FAILURE: &str = "cert.client.subscriber_parse_failure";
173
174 pub mod cert {
175 use super::*;
176
177 pub const SERVER_LEAF_KEY: CounterGroup = CounterGroup {
178 prefix: "cert.server.leaf.key",
179 count: CERT_KEY_COUNT,
180 name_from_slot: cert_key_metric_name,
181 };
182 pub const SERVER_LEAF_SIG: CounterGroup = CounterGroup {
183 prefix: "cert.server.leaf.sig",
184 count: CERT_SIG_COUNT,
185 name_from_slot: cert_sig_metric_name,
186 };
187 pub const SERVER_CHAIN_KEY: CounterGroup = CounterGroup {
188 prefix: "cert.server.chain.key",
189 count: CERT_KEY_COUNT,
190 name_from_slot: cert_key_metric_name,
191 };
192 pub const SERVER_CHAIN_SIG: CounterGroup = CounterGroup {
193 prefix: "cert.server.chain.sig",
194 count: CERT_SIG_COUNT,
195 name_from_slot: cert_sig_metric_name,
196 };
197 pub const CLIENT_LEAF_KEY: CounterGroup = CounterGroup {
198 prefix: "cert.client.leaf.key",
199 count: CERT_KEY_COUNT,
200 name_from_slot: cert_key_metric_name,
201 };
202 pub const CLIENT_LEAF_SIG: CounterGroup = CounterGroup {
203 prefix: "cert.client.leaf.sig",
204 count: CERT_SIG_COUNT,
205 name_from_slot: cert_sig_metric_name,
206 };
207 pub const CLIENT_CHAIN_KEY: CounterGroup = CounterGroup {
208 prefix: "cert.client.chain.key",
209 count: CERT_KEY_COUNT,
210 name_from_slot: cert_key_metric_name,
211 };
212 pub const CLIENT_CHAIN_SIG: CounterGroup = CounterGroup {
213 prefix: "cert.client.chain.sig",
214 count: CERT_SIG_COUNT,
215 name_from_slot: cert_sig_metric_name,
216 };
217 }
218
219 macro_rules! define_counter_groups {
220 ($mod_name:ident, $state:literal) => {
221 pub mod $mod_name {
222 use super::*;
223
224 pub const VERSIONS: CounterGroup = CounterGroup {
225 prefix: concat!("version.", $state),
226 count: PROTOCOL_COUNT,
227 name_from_slot: version_metric_name,
228 };
229
230 pub const CIPHERS: CounterGroup = CounterGroup {
231 prefix: concat!("cipher.", $state),
232 count: CIPHER_COUNT,
233 name_from_slot: cipher_metric_name,
234 };
235
236 pub const GROUPS: CounterGroup = CounterGroup {
237 prefix: concat!("group.", $state),
238 count: GROUP_COUNT,
239 name_from_slot: group_metric_name,
240 };
241
242 pub const SIGNATURES: CounterGroup = CounterGroup {
243 prefix: concat!("signature_scheme.", $state),
244 count: SIGNATURE_COUNT,
245 name_from_slot: signature_metric_name,
246 };
247
248 pub const ALL: &[&CounterGroup] = &[&VERSIONS, &CIPHERS, &GROUPS, &SIGNATURES];
249 }
250 };
251 }
252
253 define_counter_groups!(negotiated, "negotiated");
254 define_counter_groups!(supported, "supported");
255
256 #[cfg(test)]
257 mod tests {
258 use super::*;
259
260 #[test]
261 fn label_output() {
262 assert_eq!(
263 telemetry_label(0, Cipher::TLS_AES_256_GCM_SHA384, "cipher.negotiated"),
264 "cipher.negotiated.TLS_AES_256_GCM_SHA384"
265 );
266 }
267 }