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
use crate::event_formatter::EventFormatter;
use std::{fmt, io, ops::Deref};
use tracing_core::{Event, Subscriber};
use tracing_subscriber::{
fmt::{format::JsonFields, MakeWriter},
registry::LookupSpan,
};
#[derive(Debug, thiserror::Error)]
enum Error {
#[error(transparent)]
Formatting(#[from] fmt::Error),
#[error("JSON serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Time formatting error: {0}")]
Time(#[from] time::error::Format),
}
impl From<Error> for fmt::Error {
fn from(_: Error) -> Self {
Self
}
}
pub fn layer<S>() -> Layer<S>
where
S: Subscriber + for<'span> LookupSpan<'span>,
{
Layer(
tracing_subscriber::fmt::layer()
.json()
.event_format(EventFormatter::default()),
)
}
pub struct Layer<S, W = fn() -> io::Stdout>(
tracing_subscriber::fmt::Layer<S, JsonFields, EventFormatter, W>,
)
where
S: Subscriber + for<'span> LookupSpan<'span>;
impl<S, W> Layer<S, W>
where
S: Subscriber + for<'span> LookupSpan<'span>,
W: for<'writer> MakeWriter<'writer> + 'static,
{
pub fn with_writer<M>(self, make_writer: M) -> Layer<S, M>
where
M: for<'writer> MakeWriter<'writer> + 'static,
{
Layer(self.0.with_writer(make_writer))
}
#[cfg_attr(docsrs, doc(cfg(feature = "opentelemetry")))]
#[cfg(any(docsrs, feature = "opentelemetry"))]
pub fn enable_cloud_trace(self, configuration: crate::CloudTraceConfiguration) -> Self {
Self(self.0.event_format(EventFormatter {
cloud_trace_configuration: Some(configuration),
}))
}
}
impl<S, W> tracing_subscriber::layer::Layer<S> for Layer<S, W>
where
S: Subscriber + for<'span> LookupSpan<'span>,
W: for<'writer> MakeWriter<'writer> + 'static,
{
fn on_new_span(
&self,
attrs: &tracing_core::span::Attributes<'_>,
id: &tracing_core::span::Id,
context: tracing_subscriber::layer::Context<'_, S>,
) {
self.0.on_new_span(attrs, id, context)
}
fn on_record(
&self,
span: &tracing_core::span::Id,
values: &tracing_core::span::Record<'_>,
context: tracing_subscriber::layer::Context<'_, S>,
) {
self.0.on_record(span, values, context)
}
fn on_enter(
&self,
id: &tracing_core::span::Id,
context: tracing_subscriber::layer::Context<'_, S>,
) {
self.0.on_enter(id, context)
}
fn on_exit(
&self,
id: &tracing_core::span::Id,
context: tracing_subscriber::layer::Context<'_, S>,
) {
self.0.on_exit(id, context)
}
fn on_close(
&self,
id: tracing_core::span::Id,
context: tracing_subscriber::layer::Context<'_, S>,
) {
self.0.on_close(id, context)
}
fn on_event(&self, event: &Event<'_>, context: tracing_subscriber::layer::Context<'_, S>) {
self.0.on_event(event, context)
}
unsafe fn downcast_raw(&self, id: std::any::TypeId) -> Option<*const ()> {
self.0.downcast_raw(id)
}
}
impl<S, W> Deref for Layer<S, W>
where
S: Subscriber + for<'span> LookupSpan<'span>,
{
type Target = tracing_subscriber::fmt::Layer<S, JsonFields, EventFormatter, W>;
fn deref(&self) -> &Self::Target {
&self.0
}
}