main
go 76 lines 2.46 KB
Raw
1 package telemetry
2
3 // EmitFromTrace updates relevant Prometheus metrics from a completed
4 // SelectionTrace. It is safe to call concurrently.
5 //
6 // Metrics updated:
7 // - relay_selected_total{relay, reason} — one increment per OutputURL.
8 // - selection_duration_seconds — one observation for the whole invocation.
9 // - congestion_mode — set according to Congested + NonLinear.
10 // - selection_skipped_total{reason} — one increment per suppressed URL that
11 // has a reason entry.
12 // - rtt_seconds{relay} — one observation per Ranked entry with non-zero RTT.
13 //
14 // Metrics NOT updated here (wired by later phases / other code paths):
15 // - relay_pool_size — set by RelaySet pool management.
16 // - active_tunnels_per_relay — incremented/decremented at tunnel accept/close.
17 // - failures_total — incremented on discovery/active failure events.
18 func EmitFromTrace(t SelectionTrace) {
19 // --- relay_selected_total ---
20 reason := selectionReason(t)
21 for _, url := range t.OutputURLs {
22 RelaySelectedTotal.WithLabelValues(BoundedRelay(url), reason).Inc()
23 }
24
25 // --- selection_duration_seconds ---
26 SelectionDurationSeconds.Observe(t.SelectionTook.Seconds())
27
28 // --- congestion_mode ---
29 CongestionMode.Set(congestionModeValue(t.Congested, t.NonLinear))
30
31 // --- selection_skipped_total ---
32 // Build suppressed set for O(1) lookup.
33 suppressedSet := make(map[string]struct{}, len(t.Suppressed))
34 for _, url := range t.Suppressed {
35 suppressedSet[url] = struct{}{}
36 }
37 for url, reason := range t.Reasons {
38 if _, ok := suppressedSet[url]; ok {
39 SelectionSkippedTotal.WithLabelValues(reason).Inc()
40 }
41 }
42
43 // --- rtt_seconds ---
44 for _, entry := range t.Ranked {
45 if entry.RTT != 0 {
46 RTTSeconds.WithLabelValues(BoundedRelay(entry.URL)).Observe(entry.RTT.Seconds())
47 }
48 }
49 }
50
51 // selectionReason derives the reason label for relay_selected_total from the
52 // trace flags. Explicit/fallback semantics are wired by later phases; this
53 // function defaults to "auto" for uninstrumented call sites.
54 func selectionReason(t SelectionTrace) string {
55 switch {
56 case t.NonLinear:
57 return "variant-grid"
58 case t.Congested:
59 return "congestion-promoted"
60 default:
61 return "auto"
62 }
63 }
64
65 // congestionModeValue maps the Congested + NonLinear pair to the metric value.
66 // 0 = normal, 1 = congested without variant-grid, 2 = variant-grid active.
67 func congestionModeValue(congested, nonLinear bool) float64 {
68 switch {
69 case nonLinear:
70 return 2
71 case congested:
72 return 1
73 default:
74 return 0
75 }
76 }