-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathjson_logging.rs
More file actions
384 lines (350 loc) · 12.4 KB
/
json_logging.rs
File metadata and controls
384 lines (350 loc) · 12.4 KB
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
use chrono::Utc;
use colored::{ColoredString, Colorize};
use serde_json::{Map, Value};
use tracing::Subscriber;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::fmt::format::{self, Format, Writer};
use tracing_subscriber::fmt::{FormatEvent, FormatFields};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::util::SubscriberInitExt;
#[derive(Clone, Copy, Debug)]
pub enum ServiceName {
Manager,
Runner,
CompilerServer,
ControlPlane,
KubernetesRunner,
Pipeline,
ApiServer,
}
impl ServiceName {
pub fn as_str(self) -> &'static str {
match self {
ServiceName::Manager => "manager",
ServiceName::Runner => "runner",
ServiceName::CompilerServer => "compiler-server",
ServiceName::ControlPlane => "control-plane",
ServiceName::KubernetesRunner => "kubernetes-runner",
ServiceName::Pipeline => "pipeline",
ServiceName::ApiServer => "api-server",
}
}
}
impl From<ServiceName> for String {
fn from(value: ServiceName) -> Self {
value.as_str().to_string()
}
}
#[derive(Clone)]
pub enum LogIdentity {
/// Control-plane component (manager | runner | compiler-server | control-plane).
Service { service_name: Option<ServiceName> },
/// Pipeline log source.
Pipeline {
pipeline_name: Option<String>,
pipeline_id: Option<String>,
},
}
/// Shared JSON formatter that injects pipeline/service name and structured fields.
pub struct JsonPipelineFormat {
identity: LogIdentity,
}
impl JsonPipelineFormat {
pub fn new(identity: LogIdentity) -> Self {
Self { identity }
}
}
impl<S, N> FormatEvent<S, N> for JsonPipelineFormat
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &tracing_subscriber::fmt::FmtContext<'_, S, N>,
mut writer: Writer<'_>,
event: &tracing::Event<'_>,
) -> std::fmt::Result {
let mut visitor = SerdeVisitor::default();
event.record(&mut visitor);
// Defaults from the configured identity.
let mut pipeline_name = match &self.identity {
LogIdentity::Service { .. } => None,
LogIdentity::Pipeline {
pipeline_name,
pipeline_id: _,
} => pipeline_name.clone(),
};
let mut pipeline_id = match &self.identity {
LogIdentity::Service { .. } => None,
LogIdentity::Pipeline { pipeline_id, .. } => pipeline_id.clone(),
};
let mut feldera_service = match &self.identity {
LogIdentity::Service { service_name } => {
service_name.map(|service| service.as_str().to_string())
}
LogIdentity::Pipeline { .. } => Some(ServiceName::Pipeline.as_str().to_string()),
};
// Allow structured fields to override the defaults.
if let Some(value) = visitor.fields.remove("pipeline-name") {
pipeline_name = value_to_string(value);
} else if let Some(value) = visitor.fields.remove("pipeline") {
pipeline_name = value_to_string(value);
}
if let Some(value) = visitor.fields.remove("pipeline-id") {
pipeline_id = value_to_string(value);
} else if let Some(value) = visitor.fields.remove("pipeline_id") {
pipeline_id = value_to_string(value);
}
if let Some(value) = visitor.fields.remove("feldera-service") {
feldera_service = value_to_string(value);
}
let metadata = event.metadata();
// Default service tagging based on module path when logs come from shared binaries.
if metadata.target().starts_with("pipeline_manager::runner")
&& feldera_service.as_deref() != Some("runner")
{
feldera_service = Some("runner".to_string());
} else if metadata.target().starts_with("pipeline_manager::compiler")
&& feldera_service.as_deref() != Some("compiler-server")
{
feldera_service = Some("compiler-server".to_string());
} else if metadata
.target()
.starts_with("cluster_control_plane::kubernetes_runner")
&& feldera_service.as_deref() != Some("kubernetes-runner")
{
feldera_service = Some("kubernetes-runner".to_string());
}
let mut obj = Map::new();
obj.insert("timestamp".to_string(), Value::String(now_timestamp()));
obj.insert(
"level".to_string(),
Value::String(metadata.level().as_str().to_string()),
);
obj.insert(
"target".to_string(),
Value::String(metadata.target().to_string()),
);
if let Some(service_name) = feldera_service {
obj.insert("feldera-service".to_string(), Value::String(service_name));
}
if let Some(pipeline_name) = pipeline_name {
obj.insert("pipeline-name".to_string(), Value::String(pipeline_name));
}
if let Some(pipeline_id) = pipeline_id {
obj.insert("pipeline-id".to_string(), Value::String(pipeline_id));
}
obj.insert("fields".to_string(), Value::Object(visitor.fields));
if let Some(span) = ctx.lookup_current() {
obj.insert("span".to_string(), Value::String(span.name().to_string()));
}
let json = Value::Object(obj);
writeln!(writer, "{}", json)
}
}
#[derive(Default)]
pub struct SerdeVisitor {
pub fields: Map<String, Value>,
}
impl tracing::field::Visit for SerdeVisitor {
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
self.fields.insert(
field.name().to_string(),
Value::String(format!("{value:?}")),
);
}
fn record_f64(&mut self, field: &tracing::field::Field, value: f64) {
let as_number = serde_json::Number::from_f64(value);
let val = as_number
.map(Value::Number)
.unwrap_or_else(|| Value::String(value.to_string()));
self.fields.insert(field.name().to_string(), val);
}
fn record_i128(&mut self, field: &tracing::field::Field, value: i128) {
let val = i64::try_from(value)
.map(|v| Value::Number(v.into()))
.unwrap_or_else(|_| Value::String(value.to_string()));
self.fields.insert(field.name().to_string(), val);
}
fn record_u128(&mut self, field: &tracing::field::Field, value: u128) {
let val = u64::try_from(value)
.map(|v| Value::Number(v.into()))
.unwrap_or_else(|_| Value::String(value.to_string()));
self.fields.insert(field.name().to_string(), val);
}
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
self.fields
.insert(field.name().to_string(), Value::String(value.to_string()));
}
fn record_i64(&mut self, field: &tracing::field::Field, value: i64) {
self.fields
.insert(field.name().to_string(), Value::Number(value.into()));
}
fn record_u64(&mut self, field: &tracing::field::Field, value: u64) {
self.fields
.insert(field.name().to_string(), Value::Number(value.into()));
}
fn record_bool(&mut self, field: &tracing::field::Field, value: bool) {
self.fields
.insert(field.name().to_string(), Value::Bool(value));
}
}
fn now_timestamp() -> String {
Utc::now().format("%Y-%m-%dT%H:%M:%S%.6fZ").to_string()
}
/// True when structured JSON logging is requested via `FELDERA_LOG_JSON`.
pub fn use_json_log_format() -> bool {
matches!(
std::env::var("FELDERA_LOG_JSON")
.unwrap_or_default()
.to_ascii_lowercase()
.as_str(),
"1" | "true" | "yes" | "on"
)
}
/// Remove surrounding `[]` often used in pretty log prefixes.
pub fn sanitize_pipeline_name(name: impl AsRef<str>) -> String {
name.as_ref()
.trim_matches(|c| c == '[' || c == ']')
.to_string()
}
/// Initialize logging with either the JSON or text pipeline format.
pub fn init_pipeline_logging(
pipeline_name: ColoredString,
env_filter: EnvFilter,
) -> Result<(), tracing_subscriber::util::TryInitError> {
init_pipeline_logging_with_id(pipeline_name, None, env_filter)
}
/// Initialize logging with either the JSON or text pipeline format, optionally attaching a pipeline id.
pub fn init_pipeline_logging_with_id(
pipeline_name: ColoredString,
pipeline_id: Option<String>,
env_filter: EnvFilter,
) -> Result<(), tracing_subscriber::util::TryInitError> {
init_logging(
pipeline_name,
LogIdentity::Pipeline {
pipeline_name: None,
pipeline_id,
},
env_filter,
)
}
/// Initialize logging for a control-plane service (manager | runner | compiler-server | control-plane).
pub fn init_service_logging(
service_name: ColoredString,
feldera_service: ServiceName,
env_filter: EnvFilter,
) -> Result<(), tracing_subscriber::util::TryInitError> {
init_logging(
service_name,
LogIdentity::Service {
service_name: Some(feldera_service),
},
env_filter,
)
}
fn init_logging(
prefix: ColoredString,
identity: LogIdentity,
env_filter: EnvFilter,
) -> Result<(), tracing_subscriber::util::TryInitError> {
let identity = match identity {
LogIdentity::Service { service_name } => LogIdentity::Service {
service_name: service_name.or(Some(ServiceName::ControlPlane)),
},
LogIdentity::Pipeline {
pipeline_name,
pipeline_id,
} => LogIdentity::Pipeline {
pipeline_name: Some(
pipeline_name
.unwrap_or_else(|| sanitize_pipeline_name(prefix.clone().clear().to_string())),
),
pipeline_id,
},
};
if use_json_log_format() {
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.event_format(JsonPipelineFormat::new(identity))
.with_ansi(false),
)
.with(env_filter)
.with(sentry::integrations::tracing::layer())
.try_init()
} else {
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.event_format(FormatWithPrefix::new(prefix))
.fmt_fields(plain_text_fields()),
)
.with(env_filter)
.with(sentry::integrations::tracing::layer())
.try_init()
}
}
/// Text formatter that prepends the pipeline name to each event.
pub struct FormatWithPrefix {
pipeline_name: ColoredString,
inner: Format,
}
impl FormatWithPrefix {
pub fn new(pipeline_name: ColoredString) -> Self {
Self {
pipeline_name,
inner: Format::default(),
}
}
}
impl<S, N> FormatEvent<S, N> for FormatWithPrefix
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &tracing_subscriber::fmt::FmtContext<'_, S, N>,
mut writer: Writer<'_>,
event: &tracing::Event<'_>,
) -> std::fmt::Result {
write!(writer, "{} ", self.pipeline_name)?;
self.inner.format_event(ctx, writer, event)
}
}
fn value_to_string(value: Value) -> Option<String> {
match value {
Value::String(s) => Some(s),
Value::Number(n) => Some(n.to_string()),
Value::Bool(b) => Some(b.to_string()),
Value::Null => None,
other => Some(other.to_string()),
}
}
/// In plain text, render the message plus selected structured fields.
fn plain_text_fields() -> impl for<'writer> tracing_subscriber::fmt::FormatFields<'writer> + Clone {
format::debug_fn(|writer, field, value| {
match field.name() {
"message" => {
write!(writer, " {value:?}")?;
}
"pipeline" | "pipeline_name" | "pipeline-name" => {
write!(writer, " pipeline-name={value:?}")?;
}
"pipeline_id" | "pipeline-id" => {
write!(writer, " pipeline-id={value:?}")?;
}
"tenant" => {
write!(writer, " tenant={value:?}")?;
}
_ => {
write!(writer, " {field}={value:?}")?;
}
}
Ok(())
})
}