Skip to main content

pingora_cache/
trace.rs

1// Copyright 2026 Cloudflare, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Distributed tracing helpers
16
17use cf_rustracing_jaeger::span::SpanContextState;
18use std::time::SystemTime;
19
20use crate::{CacheMeta, CachePhase, HitStatus};
21
22pub use cf_rustracing::tag::Tag;
23
24pub type Span = cf_rustracing::span::Span<SpanContextState>;
25pub type SpanHandle = cf_rustracing::span::SpanHandle<SpanContextState>;
26
27#[derive(Debug)]
28pub(crate) struct CacheTraceCTX {
29    // parent span
30    pub cache_span: Span,
31    // only spans across multiple calls need to store here
32    pub miss_span: Span,
33    pub hit_span: Span,
34}
35
36pub fn tag_span_with_meta(span: &mut Span, meta: &CacheMeta) {
37    fn ts2epoch(ts: SystemTime) -> f64 {
38        ts.duration_since(SystemTime::UNIX_EPOCH)
39            .unwrap_or_default() // should never overflow but be safe here
40            .as_secs_f64()
41    }
42    let internal = &meta.0.internal;
43    span.set_tags(|| {
44        [
45            Tag::new("created", ts2epoch(internal.created)),
46            Tag::new("fresh_until", ts2epoch(internal.fresh_until)),
47            Tag::new("updated", ts2epoch(internal.updated)),
48            Tag::new("stale_if_error_sec", internal.stale_if_error_sec as i64),
49            Tag::new(
50                "stale_while_revalidate_sec",
51                internal.stale_while_revalidate_sec as i64,
52            ),
53            Tag::new("variance", internal.variance.is_some()),
54        ]
55    });
56}
57
58impl CacheTraceCTX {
59    pub fn new() -> Self {
60        CacheTraceCTX {
61            cache_span: Span::inactive(),
62            miss_span: Span::inactive(),
63            hit_span: Span::inactive(),
64        }
65    }
66
67    pub fn enable(&mut self, cache_span: Span) {
68        self.cache_span = cache_span;
69    }
70
71    pub fn get_cache_span(&self) -> SpanHandle {
72        self.cache_span.handle()
73    }
74
75    #[inline]
76    pub fn child(&self, name: &'static str) -> Span {
77        self.cache_span.child(name, |o| o.start())
78    }
79
80    pub fn start_miss_span(&mut self) {
81        self.miss_span = self.child("miss");
82    }
83
84    pub fn get_miss_span(&self) -> SpanHandle {
85        self.miss_span.handle()
86    }
87
88    pub fn finish_miss_span(&mut self) {
89        self.miss_span.set_finish_time(SystemTime::now);
90    }
91
92    pub fn start_hit_span(&mut self, phase: CachePhase, hit_status: HitStatus) {
93        self.hit_span = self.child("hit");
94        self.hit_span.set_tag(|| Tag::new("phase", phase.as_str()));
95        self.hit_span
96            .set_tag(|| Tag::new("status", hit_status.as_str()));
97    }
98
99    pub fn get_hit_span(&self) -> SpanHandle {
100        self.hit_span.handle()
101    }
102
103    pub fn finish_hit_span(&mut self) {
104        self.hit_span.set_finish_time(SystemTime::now);
105    }
106
107    pub fn log_meta_in_hit_span(&mut self, meta: &CacheMeta) {
108        tag_span_with_meta(&mut self.hit_span, meta);
109    }
110
111    pub fn log_meta_in_miss_span(&mut self, meta: &CacheMeta) {
112        tag_span_with_meta(&mut self.miss_span, meta);
113    }
114}