-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlogger.rs
337 lines (305 loc) · 11.2 KB
/
logger.rs
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
#[cfg(feature = "shipper")]
use crate::shipper;
use crate::error::Error;
use google_logging2::api::{LogEntry, MonitoredResource, WriteLogEntriesRequest};
use slog::{self, Drain, Key, Level, Never, OwnedKVList, Record, KV};
use std::collections::HashMap;
use std::fmt;
use std::fmt::Write;
use serde_json::json;
use std::sync::mpsc::sync_channel;
use chrono::Utc;
/// Builder for the [`Logger`]
#[derive(Default, Debug)]
pub struct Builder {
log_name: String,
log_level_label: Option<String>,
resource_type: String,
default_labels: HashMap<String, String>,
resource_labels: Option<HashMap<String, String>>,
}
/// Main struct for the Google Logger drain
pub struct Logger {
log_name: String,
log_level_label: Option<String>,
default_labels: HashMap<String, String>,
resource: MonitoredResource,
sync_tx: std::sync::mpsc::SyncSender<WriteLogEntriesRequest>,
}
impl Builder {
/// Creates a Builder object.
///
/// # Parameters
/// - `log_name`: The `logName` string to be used in the [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry)
/// - `resource_type`: The required `type` field set in the `resource` [MonitoredResource](https://cloud.google.com/logging/docs/reference/v2/rest/v2/MonitoredResource) object of the [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry). For example: `k8s_container`.
///
/// # Example
///
/// ```
/// use slog_google::logger::Builder;
/// let (drain, _) = Builder::new(
/// "projects/my-gcp-project/logs/my-log-id",
/// "k8s_container",
/// )
/// .build();
/// ```
///
#[must_use = "The builder must be used"]
pub fn new(log_name: &str, resource_type: &str) -> Self {
Self {
log_name: log_name.to_string(),
resource_type: resource_type.to_string(),
..Default::default()
}
}
/// Sets resource labels to be applied.
///
/// These labels will populate the `labels` field in the `resource` [MonitoredResource](https://cloud.google.com/logging/docs/reference/v2/rest/v2/MonitoredResource) object of the [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).
///
/// # Example
///
/// ```
/// use serde_json::json;
/// let resource_labels = json!(
/// {
/// "pod_name": "dummy-value",
/// "location": "europe-west1-b",
/// "pod_name": std::env::var("HOSTNAME").unwrap_or_default(),
/// "container_name": "my-app",
/// "project_id": "my-gcp-project",
/// "cluster_name": "my-gke-cluster",
/// "namespace_name": "my-gke-namespace"
/// });
///
/// use slog_google::logger::Builder;
/// let (drain, _) = Builder::new(
/// "projects/my-gcp-project/logs/my-log-id",
/// "k8s_container",
/// )
/// .with_resource_labels(resource_labels)
/// .unwrap()
/// .build();
/// ```
///
/// # Errors
///
/// Will return `Err` if `labels` does not parse as JSON.
#[must_use = "The builder must be used"]
pub fn with_resource_labels(
self,
labels: serde_json::Value,
) -> Result<Self, Box<dyn std::error::Error>> {
Ok(Self {
resource_labels: Some(
serde_json::from_value(labels).map_err(Error::ResourceLabelsError)?,
),
..self
})
}
/// Sets default labels to be applied in the labels field.
///
/// These will populate the `labels` top level field of the [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry). These labels are added in addition to any labels set in the logger statement.
///
/// # Example
///
/// ```
/// use serde_json::json;
/// let default_labels = json!(
/// {
/// "application": "my-application",
/// "team": "my-team",
/// "version": "my-app-version",
/// "role": "my-app-role",
/// "environment": "production",
/// "platform": "gcp",
/// });
/// ```
///
/// # Errors
///
/// Will return `Err` if `labels` does not parse as JSON.
#[must_use = "The builder must be used"]
pub fn with_default_labels(self, labels: serde_json::Value) -> Result<Self, Error> {
Ok(Self {
default_labels: serde_json::from_value(labels).map_err(Error::DefaultLabelsError)?,
..self
})
}
/// Sets the label name to store the log level
///
/// If set, the log level value is added under this label the `labels` top level field of the [LogEntry](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry)
///
/// If not set, the log level is not propagated, but you will still have the [severity](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity), which is always there.
#[must_use = "The builder must be used"]
pub fn with_log_level_label(self, log_level_label: &str) -> Self {
Self {
log_level_label: Some(log_level_label.into()),
..self
}
}
/// This returns a tuple with a [`Logger`](struct@Logger), which can be passed to the slog root logger [as usual](https://docs.rs/slog/latest/slog/#where-to-start), and a [`std::sync::mpsc::Receiver`] channel.
/// The `Logger` sends the [`WriteLogEntries`](https://cloud.google.com/logging/docs/reference/v2/rpc/google.logging.v2#google.logging.v2.LoggingServiceV2.WriteLogEntries) it creates to this channel.
///
/// For instance you could output these to the console, if you have an external agent that reads the process' output and ships it to Google Logging.
///
#[must_use = "The logger and receiver must be used to handle logging correctly"]
#[allow(dead_code)]
pub fn build(self) -> (Logger, std::sync::mpsc::Receiver<WriteLogEntriesRequest>) {
let (sync_tx, sync_rx) = sync_channel::<WriteLogEntriesRequest>(100);
(
Logger {
log_name: self.log_name,
log_level_label: self.log_level_label,
default_labels: self.default_labels,
resource: MonitoredResource {
type_: Some(self.resource_type),
labels: self.resource_labels,
},
sync_tx,
},
sync_rx,
)
}
/// In an async context this 'shipper' sends the log entries directly to the [Google Logging API](https://cloud.google.com/logging/docs/reference/v2/rest).
///
/// # Example
///
/// ```
/// use tokio::runtime::Runtime;
/// use serde_json::json;
///
/// let mut rt = Runtime::new().unwrap();
/// rt.spawn(async {
/// let resource_labels = json!(
/// {
/// "pod_name": "dummy-value",
/// "location": "europe-west1-b",
/// "pod_name": std::env::var("HOSTNAME").unwrap_or_default(),
/// "container_name": "my-app",
/// "project_id": "my-gcp-project",
/// "cluster_name": "my-gke-cluster",
/// "namespace_name": "my-gke-namespace"
/// });
///
/// use slog_google::logger::Builder;
/// let (drain, mut shipper) = Builder::new(
/// "projects/my-gcp-project/logs/my-log-id",
/// "k8s_container",
/// )
/// .with_resource_labels(resource_labels)
/// .unwrap()
/// .build_with_async_shipper();
///
/// // Forward messages from the sync channel to the async channel where the
/// // shipper sends it to Google Cloud Logging
/// let bridge = shipper.yield_bridge();
/// tokio::task::spawn_blocking(move || {
/// bridge.run_sync_to_async_bridge();
/// });
///
/// tokio::spawn(async move {
/// shipper.run_log_shipper().await;
/// });
/// });
///
/// ```
#[cfg(feature = "shipper")]
#[must_use = "The logger and shipper must be used to handle logging correctly"]
pub fn build_with_async_shipper(self) -> (Logger, shipper::Shipper) {
let (sync_tx, sync_rx) = sync_channel::<WriteLogEntriesRequest>(100);
(
Logger {
log_name: self.log_name,
log_level_label: self.log_level_label,
default_labels: self.default_labels,
resource: MonitoredResource {
type_: Some(self.resource_type),
labels: self.resource_labels,
},
sync_tx,
},
shipper::Shipper::new(sync_rx),
)
}
}
impl Logger {
// Determine a sensible severity based on the log level
fn get_severity(log_level: Level) -> String {
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity
match log_level {
Level::Critical => "CRITICAL".into(),
Level::Error => "ERROR".into(),
Level::Warning => "WARNING".into(),
Level::Info => "INFO".into(),
Level::Debug | Level::Trace => "DEBUG".into(),
}
}
fn construct_log_entry(
&self,
message: &str,
log_level: Level,
serializer: Serializer,
) -> LogEntry {
let mut labels = self.default_labels.clone();
if !serializer.map.is_empty() {
labels.extend(serializer.map);
}
// We add the log level to the labels if requested
if let Some(label) = &self.log_level_label {
labels.insert(label.clone(), log_level.as_str().to_string());
}
let resource = Some(self.resource.clone());
// TODO: support both text_payload and json_payload
let json_payload = HashMap::from([("message".to_string(), json!(message))]);
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity
LogEntry {
json_payload: Some(json_payload),
labels: Some(labels),
severity: Some(Self::get_severity(log_level)),
timestamp: Some(Utc::now()),
resource,
..Default::default()
}
}
}
#[derive(Debug)]
struct Serializer {
map: HashMap<String, String>,
}
impl Serializer {
fn new() -> Self {
Self {
map: HashMap::new(),
}
}
}
impl slog::Serializer for Serializer {
fn emit_arguments(&mut self, key: Key, val: &fmt::Arguments) -> slog::Result {
let mut value = String::new();
write!(value, "{val}")?;
self.map.insert(key.into(), value);
Ok(())
}
}
impl Drain for Logger {
type Ok = ();
type Err = Never; // TODO: Handle errors
fn log(&self, record: &Record<'_>, values: &OwnedKVList) -> Result<Self::Ok, Self::Err> {
let mut serializer = Serializer::new();
let kv = record.kv();
let _ = kv.serialize(record, &mut serializer);
let _ = values.serialize(record, &mut serializer);
let log_entry = self.construct_log_entry(
format!("{}", record.msg()).as_str(),
record.level(),
serializer,
);
let body = WriteLogEntriesRequest {
log_name: Some(self.log_name.clone()),
entries: Some(vec![log_entry]),
..Default::default()
};
let _ = self.sync_tx.send(body);
Ok(())
}
}