fn0 0.2.47

FaaS platform powered by wasmtime
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Per-project OTLP metrics cardinality gate, attached to the OTLP hijack by
//! the worker (absent in `forte dev`).
//!
//! The gate caps how many distinct series a project may carry so one
//! cardinality explosion (e.g. an unbounded label value) cannot monopolise the
//! account-wide Grafana active-series pool that every project shares. It
//! mirrors [`crate::presign_gate::PresignGate`]: worker-local state, enforced
//! inline on the telemetry path. Grafana's own plan limit is the backstop;
//! this is the early, per-project, owner-visible defense.
//!
//! Semantics are "keep existing, drop new": a series admitted once stays
//! admitted while it keeps reporting, and only newly appearing series are
//! refused once a cap is hit. Series go stale after [`STALE_WINDOW_SECS`]
//! without a sample (matching Alloy's `deltatocumulative.max_stale`), freeing
//! their slot, so the tracked set reflects *active* series the way Grafana
//! bills them, not every series ever seen.

use bytes::Bytes;
use opentelemetry_proto::tonic::collector::metrics::v1::ExportMetricsServiceRequest;
use opentelemetry_proto::tonic::common::v1::{any_value, AnyValue, KeyValue};
use opentelemetry_proto::tonic::metrics::v1::{
    metric, number_data_point, ExponentialHistogramDataPoint, HistogramDataPoint, Metric,
    NumberDataPoint, ResourceMetrics, ScopeMetrics, Sum, SummaryDataPoint,
};
use prost::Message;
use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};

pub const METRIC_ACTIVE_SERIES_PER_PROJECT: usize = 1_000;
pub const METRIC_NAMES_PER_PROJECT: usize = 100;
pub const METRIC_LABEL_VALUES_PER_KEY: usize = 100;

const STALE_WINDOW_SECS: i64 = 300;
pub const DROPPED_METRIC_NAME: &str = "fn0.metrics.dropped";

#[derive(Debug, Default)]
pub struct MetricCardinalityGate {
    state: Mutex<HashMap<String, ProjectState>>,
}

#[derive(Debug, Default)]
struct ProjectState {
    series: HashMap<String, i64>,
    names: HashMap<String, i64>,
    label_values: HashMap<String, HashMap<String, i64>>,
}

impl ProjectState {
    fn evict(&mut self, now: i64) {
        let stale = |t: &i64| now - *t > STALE_WINDOW_SECS;
        self.series.retain(|_, t| !stale(t));
        self.names.retain(|_, t| !stale(t));
        for values in self.label_values.values_mut() {
            values.retain(|_, t| !stale(t));
        }
        self.label_values.retain(|_, values| !values.is_empty());
    }

    fn admit(&mut self, name: &str, pairs: &[(String, String)], now: i64) -> bool {
        let key = series_key(name, pairs);
        let existing = self.series.contains_key(&key);
        let allowed = existing
            || (self.series.len() < METRIC_ACTIVE_SERIES_PER_PROJECT
                && (self.names.contains_key(name) || self.names.len() < METRIC_NAMES_PER_PROJECT)
                && pairs.iter().all(|(k, v)| {
                    self.label_values
                        .get(k)
                        .is_none_or(|values| values.contains_key(v) || values.len() < METRIC_LABEL_VALUES_PER_KEY)
                }));
        if allowed {
            self.series.insert(key, now);
            self.names.insert(name.to_string(), now);
            for (k, v) in pairs {
                self.label_values.entry(k.clone()).or_default().insert(v.clone(), now);
            }
        }
        allowed
    }
}

trait HasAttributes {
    fn attributes(&self) -> &[KeyValue];
}

impl HasAttributes for NumberDataPoint {
    fn attributes(&self) -> &[KeyValue] {
        &self.attributes
    }
}

impl HasAttributes for HistogramDataPoint {
    fn attributes(&self) -> &[KeyValue] {
        &self.attributes
    }
}

impl HasAttributes for ExponentialHistogramDataPoint {
    fn attributes(&self) -> &[KeyValue] {
        &self.attributes
    }
}

impl HasAttributes for SummaryDataPoint {
    fn attributes(&self) -> &[KeyValue] {
        &self.attributes
    }
}

fn filter_points<T: HasAttributes>(
    state: &mut ProjectState,
    name: &str,
    points: &mut Vec<T>,
    now: i64,
) -> u64 {
    let before = points.len();
    points.retain(|point| {
        let pairs = label_pairs(point.attributes());
        state.admit(name, &pairs, now)
    });
    (before - points.len()) as u64
}

/// Returns whether the metric still has data — a metric whose `data` is `None`
/// carries only metadata and is kept — and how many points were dropped.
fn filter_metric(state: &mut ProjectState, metric: &mut Metric, now: i64) -> (bool, u64) {
    let name = metric.name.clone();
    let dropped = match &mut metric.data {
        Some(metric::Data::Gauge(g)) => filter_points(state, &name, &mut g.data_points, now),
        Some(metric::Data::Sum(s)) => filter_points(state, &name, &mut s.data_points, now),
        Some(metric::Data::Histogram(h)) => filter_points(state, &name, &mut h.data_points, now),
        Some(metric::Data::ExponentialHistogram(e)) => {
            filter_points(state, &name, &mut e.data_points, now)
        }
        Some(metric::Data::Summary(s)) => filter_points(state, &name, &mut s.data_points, now),
        None => 0,
    };
    let has_data = match &metric.data {
        Some(metric::Data::Gauge(g)) => !g.data_points.is_empty(),
        Some(metric::Data::Sum(s)) => !s.data_points.is_empty(),
        Some(metric::Data::Histogram(h)) => !h.data_points.is_empty(),
        Some(metric::Data::ExponentialHistogram(e)) => !e.data_points.is_empty(),
        Some(metric::Data::Summary(s)) => !s.data_points.is_empty(),
        None => true,
    };
    (has_data, dropped)
}

fn enforce_project(state: &mut ProjectState, request: &mut ExportMetricsServiceRequest, now: i64) -> u64 {
    let mut dropped = 0;
    for resource in &mut request.resource_metrics {
        for scope in &mut resource.scope_metrics {
            scope.metrics.retain_mut(|metric| {
                let (has_data, dropped_here) = filter_metric(state, metric, now);
                dropped += dropped_here;
                has_data
            });
        }
        resource.scope_metrics.retain(|scope| !scope.metrics.is_empty());
    }
    request.resource_metrics.retain(|resource| !resource.scope_metrics.is_empty());
    dropped
}

fn series_key(name: &str, pairs: &[(String, String)]) -> String {
    let mut key = String::with_capacity(name.len() + pairs.len() * 16);
    key.push_str(name);
    key.push('\x1f');
    for (k, v) in pairs {
        key.push_str(k);
        key.push('=');
        key.push_str(v);
        key.push('\x1e');
    }
    key
}

fn label_pairs(attributes: &[KeyValue]) -> Vec<(String, String)> {
    let mut pairs: Vec<(String, String)> = attributes
        .iter()
        .map(|kv| (kv.key.clone(), any_value_to_key(&kv.value)))
        .collect();
    pairs.sort();
    pairs
}

fn any_value_to_key(value: &Option<AnyValue>) -> String {
    match value.as_ref().and_then(|v| v.value.as_ref()) {
        Some(any_value::Value::StringValue(s)) => s.clone(),
        Some(any_value::Value::BoolValue(b)) => b.to_string(),
        Some(any_value::Value::IntValue(i)) => i.to_string(),
        Some(any_value::Value::DoubleValue(d)) => d.to_string(),
        Some(any_value::Value::BytesValue(bytes)) => format!("bytes:{}", bytes.len()),
        Some(_) => "nested".to_string(),
        None => String::new(),
    }
}

/// Appends the drop count to the project's own payload so Alloy stamps
/// `fn0.project_id` on it from the trusted hijack header, making the
/// throttling visible to the project owner rather than only to operators.
pub fn inject_dropped_metric(request: &mut ExportMetricsServiceRequest, dropped: u64, now_nanos: u64) {
    let point = NumberDataPoint {
        time_unix_nano: now_nanos,
        value: Some(number_data_point::Value::AsInt(dropped as i64)),
        ..Default::default()
    };
    let metric = Metric {
        name: DROPPED_METRIC_NAME.to_string(),
        unit: "1".to_string(),
        data: Some(metric::Data::Sum(Sum {
            data_points: vec![point],
            aggregation_temporality: 1,
            is_monotonic: true,
        })),
        ..Default::default()
    };
    if request.resource_metrics.is_empty() {
        request.resource_metrics.push(ResourceMetrics::default());
    }
    let resource = &mut request.resource_metrics[0];
    if resource.scope_metrics.is_empty() {
        resource.scope_metrics.push(ScopeMetrics::default());
    }
    resource.scope_metrics[0].metrics.push(metric);
}

/// Enforces the per-project caps on a raw OTLP metrics payload, returning the
/// bytes to forward. Undecodable payloads pass through untouched rather than
/// failing the export: the gate is a cost guard, not a validator, and the
/// collector is entitled to reject what it cannot parse.
pub fn enforce_request_bytes(gate: &MetricCardinalityGate, project_id: &str, bytes: Bytes) -> Bytes {
    let Ok(mut request) = ExportMetricsServiceRequest::decode(bytes.as_ref()) else {
        return bytes;
    };
    let now_secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_secs() as i64)
        .unwrap_or(0);
    let dropped = gate.enforce(project_id, &mut request, now_secs);
    if dropped == 0 {
        return bytes;
    }
    tracing::warn!(project_id, dropped, "otlp metrics dropped by cardinality cap");
    inject_dropped_metric(
        &mut request,
        dropped,
        (now_secs as u64).saturating_mul(1_000_000_000),
    );
    let mut buf = Vec::with_capacity(request.encoded_len());
    if request.encode(&mut buf).is_err() {
        return bytes;
    }
    Bytes::from(buf)
}

impl MetricCardinalityGate {
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the number of data points dropped.
    pub fn enforce(
        &self,
        project_id: &str,
        request: &mut ExportMetricsServiceRequest,
        now_secs: i64,
    ) -> u64 {
        let mut state = self.state.lock().unwrap();
        let project = state.entry(project_id.to_string()).or_default();
        project.evict(now_secs);
        enforce_project(project, request, now_secs)
    }

    /// Per-project active-series counts, stale series evicted first.
    pub fn snapshot(&self, now_secs: i64) -> Vec<(String, usize)> {
        let mut state = self.state.lock().unwrap();
        state.retain(|_, project| {
            project.evict(now_secs);
            !project.series.is_empty()
        });
        state
            .iter()
            .map(|(project_id, project)| (project_id.clone(), project.series.len()))
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use opentelemetry_proto::tonic::metrics::v1::Gauge;

    fn point_with(attr: &str, value: &str) -> NumberDataPoint {
        NumberDataPoint {
            attributes: vec![KeyValue {
                key: attr.to_string(),
                value: Some(AnyValue {
                    value: Some(any_value::Value::StringValue(value.to_string())),
                }),
            }],
            ..Default::default()
        }
    }

    // Two labels whose per-key value counts stay under the label-value cap, so
    // distinct (a, b) combinations exercise the *series* cap in isolation.
    fn point_combo(i: usize) -> NumberDataPoint {
        let attr = |key: &str, v: usize| KeyValue {
            key: key.to_string(),
            value: Some(AnyValue {
                value: Some(any_value::Value::StringValue(v.to_string())),
            }),
        };
        NumberDataPoint {
            attributes: vec![attr("a", i / 40), attr("b", i % 40)],
            ..Default::default()
        }
    }

    fn gauge_request(name: &str, points: Vec<NumberDataPoint>) -> ExportMetricsServiceRequest {
        ExportMetricsServiceRequest {
            resource_metrics: vec![ResourceMetrics {
                scope_metrics: vec![ScopeMetrics {
                    metrics: vec![Metric {
                        name: name.to_string(),
                        data: Some(metric::Data::Gauge(Gauge { data_points: points })),
                        ..Default::default()
                    }],
                    ..Default::default()
                }],
                ..Default::default()
            }],
        }
    }

    fn count_points(request: &ExportMetricsServiceRequest) -> usize {
        request
            .resource_metrics
            .iter()
            .flat_map(|r| &r.scope_metrics)
            .flat_map(|s| &s.metrics)
            .map(|m| match &m.data {
                Some(metric::Data::Gauge(g)) => g.data_points.len(),
                _ => 0,
            })
            .sum()
    }

    #[test]
    fn keeps_existing_drops_new_over_cap() {
        let gate = MetricCardinalityGate::new();
        let now = 1_000;

        let mut request = gauge_request(
            "m",
            (0..METRIC_ACTIVE_SERIES_PER_PROJECT + 5)
                .map(point_combo)
                .collect(),
        );
        let dropped = gate.enforce("p", &mut request, now);
        assert_eq!(dropped, 5);
        assert_eq!(count_points(&request), METRIC_ACTIVE_SERIES_PER_PROJECT);

        let mut again = gauge_request("m", vec![point_combo(0)]);
        assert_eq!(gate.enforce("p", &mut again, now + 1), 0);
        assert_eq!(count_points(&again), 1);

        let mut overflow = gauge_request("m", vec![point_combo(1599)]);
        assert_eq!(gate.enforce("p", &mut overflow, now + 1), 1);
        assert_eq!(count_points(&overflow), 0);
    }

    #[test]
    fn stale_series_free_slot() {
        let gate = MetricCardinalityGate::new();
        let now = 10_000;
        let mut request = gauge_request(
            "m",
            (0..METRIC_ACTIVE_SERIES_PER_PROJECT)
                .map(point_combo)
                .collect(),
        );
        gate.enforce("p", &mut request, now);

        let later = now + STALE_WINDOW_SECS + 1;
        let mut fresh = gauge_request("m", vec![point_combo(1599)]);
        assert_eq!(gate.enforce("p", &mut fresh, later), 0);
        assert_eq!(count_points(&fresh), 1);
    }

    #[test]
    fn label_value_cap_blocks_new_values_only() {
        let gate = MetricCardinalityGate::new();
        let now = 1_000;
        let mut request = gauge_request(
            "m",
            (0..METRIC_LABEL_VALUES_PER_KEY + 3)
                .map(|i| point_with("user", &format!("u{i}")))
                .collect(),
        );
        let dropped = gate.enforce("p", &mut request, now);
        assert_eq!(dropped, 3);
        assert_eq!(count_points(&request), METRIC_LABEL_VALUES_PER_KEY);
    }

    #[test]
    fn name_cap_blocks_new_names() {
        let gate = MetricCardinalityGate::new();
        let now = 1_000;
        for i in 0..METRIC_NAMES_PER_PROJECT {
            let mut request = gauge_request(&format!("metric{i}"), vec![point_with("k", "v")]);
            assert_eq!(gate.enforce("p", &mut request, now), 0);
        }
        // The name cap counts names, not series: an existing name still admits
        // new series after the cap is reached.
        let mut new_name = gauge_request("one_too_many", vec![point_with("k", "v")]);
        assert_eq!(gate.enforce("p", &mut new_name, now), 1);
        let mut existing_name = gauge_request("metric0", vec![point_with("k", "v2")]);
        assert_eq!(gate.enforce("p", &mut existing_name, now), 0);
    }

    #[test]
    fn dropped_metric_injected() {
        let mut request = ExportMetricsServiceRequest::default();
        inject_dropped_metric(&mut request, 7, 123);
        let metric = &request.resource_metrics[0].scope_metrics[0].metrics[0];
        assert_eq!(metric.name, DROPPED_METRIC_NAME);
        match &metric.data {
            Some(metric::Data::Sum(sum)) => {
                assert_eq!(sum.data_points.len(), 1);
                assert!(matches!(
                    sum.data_points[0].value,
                    Some(number_data_point::Value::AsInt(7))
                ));
            }
            other => panic!("expected sum, got {other:?}"),
        }
    }

    #[test]
    fn enforce_request_bytes_roundtrip() {
        let gate = MetricCardinalityGate::new();
        let request = gauge_request(
            "m",
            (0..METRIC_ACTIVE_SERIES_PER_PROJECT + 10)
                .map(point_combo)
                .collect(),
        );
        let mut buf = Vec::new();
        request.encode(&mut buf).unwrap();

        let out = enforce_request_bytes(&gate, "p", Bytes::from(buf));
        let decoded = ExportMetricsServiceRequest::decode(out.as_ref()).unwrap();

        let total_points: usize = decoded
            .resource_metrics
            .iter()
            .flat_map(|r| &r.scope_metrics)
            .flat_map(|s| &s.metrics)
            .map(|m| match &m.data {
                Some(metric::Data::Gauge(g)) => g.data_points.len(),
                Some(metric::Data::Sum(s)) => s.data_points.len(),
                _ => 0,
            })
            .sum();
        // Capped series plus the injected dropped metric.
        assert_eq!(total_points, METRIC_ACTIVE_SERIES_PER_PROJECT + 1);

        let dropped = decoded
            .resource_metrics
            .iter()
            .flat_map(|r| &r.scope_metrics)
            .flat_map(|s| &s.metrics)
            .find(|m| m.name == DROPPED_METRIC_NAME)
            .expect("dropped metric present");
        match &dropped.data {
            Some(metric::Data::Sum(sum)) => assert!(matches!(
                sum.data_points[0].value,
                Some(number_data_point::Value::AsInt(10))
            )),
            other => panic!("expected sum, got {other:?}"),
        }
    }
}