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
use crate::request::JobRequest;
use tracing::{Level, Span};
use super::DEFAULT_MESSAGE_LEVEL;
pub trait MakeSpan<B> {
fn make_span(&mut self, request: &JobRequest<B>) -> Span;
}
impl<B> MakeSpan<B> for Span {
fn make_span(&mut self, _request: &JobRequest<B>) -> Span {
self.clone()
}
}
impl<F, B> MakeSpan<B> for F
where
F: FnMut(&JobRequest<B>) -> Span,
{
fn make_span(&mut self, request: &JobRequest<B>) -> Span {
self(request)
}
}
#[derive(Debug, Clone)]
pub struct DefaultMakeSpan {
level: Level,
}
impl DefaultMakeSpan {
pub fn new() -> Self {
Self {
level: DEFAULT_MESSAGE_LEVEL,
}
}
pub fn level(mut self, level: Level) -> Self {
self.level = level;
self
}
}
impl Default for DefaultMakeSpan {
fn default() -> Self {
Self::new()
}
}
impl<B> MakeSpan<B> for DefaultMakeSpan {
fn make_span(&mut self, req: &JobRequest<B>) -> Span {
let span = Span::current();
macro_rules! make_span {
($level:expr) => {
tracing::span!(
parent: span,
$level,
"job",
job_id = req.id().as_str(),
current_attempt = req.attempts(),
)
};
}
match self.level {
Level::ERROR => {
make_span!(Level::ERROR)
}
Level::WARN => {
make_span!(Level::WARN)
}
Level::INFO => {
make_span!(Level::INFO)
}
Level::DEBUG => {
make_span!(Level::DEBUG)
}
Level::TRACE => {
make_span!(Level::TRACE)
}
}
}
}