Skip to content

Commit e2d4199

Browse files
authored
Merge pull request #40 from AnGREATern/env-st-flds
Add parsing as env to config
2 parents 0ffb9b7 + 9511b54 commit e2d4199

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "uxum"
3-
version = "0.7.1"
3+
version = "0.7.2"
44
authors = ["Alex Unigovsky <unik@devrandom.ru>"]
55
license = "MIT OR Apache-2.0"
66
edition = "2021"
@@ -75,6 +75,7 @@ problemdetails = { version = "0.5", features = ["axum"] }
7575
prometheus = "0.14"
7676
prost = { version = "0.13", optional = true }
7777
recloser = "1.1"
78+
regex = "1.11.2"
7879
reqwest = { version = "0.12", default-features = false, features = ["charset", "hickory-dns", "http2", "json", "macos-system-configuration", "rustls-tls-native-roots"] }
7980
reqwest-middleware = { version = "0.4", features = ["multipart", "json"] }
8081
reqwest-tracing = { version = "0.5.7", features = ["opentelemetry_0_29"] }

examples/inner_service/config.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ logging:
2121
flatten_metadata: true
2222
current_span: true
2323
static_fields:
24-
env: dev
25-
something: something
24+
user: $USER
25+
something: $PATH, lol $KEK and $USER ($USER)
2626
other: 42
27+
parse_env_in_static: true
2728
metrics:
2829
labels:
2930
env: example

src/logging/json.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Custom JSON formatter and writer for use in logging.
22
3-
use std::{borrow::Cow, collections::BTreeMap, fmt, io, marker::PhantomData};
3+
use std::{borrow::Cow, collections::BTreeMap, env, fmt, io, marker::PhantomData, sync::LazyLock};
44

5+
use regex::Regex;
56
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
67
use serde_json::{Serializer as JsonSerializer, Value};
78
use tracing::{Event, Subscriber};
@@ -16,6 +17,9 @@ use tracing_subscriber::{
1617
registry::{LookupSpan, SpanRef},
1718
};
1819

20+
static ENV_VAR_REGEX: LazyLock<Regex> =
21+
LazyLock::new(|| Regex::new(r"\$([A-Za-z_][A-Za-z0-9_]*)").unwrap());
22+
1923
/// Custom names JSON keys.
2024
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
2125
#[non_exhaustive]
@@ -434,7 +438,28 @@ impl<T> ExtensibleJsonFormat<T> {
434438
}
435439

436440
/// Add static fields to generated JSON objects.
437-
pub(crate) fn with_static_fields(self, static_fields: BTreeMap<String, Value>) -> Self {
441+
pub(crate) fn with_static_fields(
442+
self,
443+
mut static_fields: BTreeMap<String, Value>,
444+
parse_env: bool,
445+
) -> Self {
446+
if parse_env {
447+
let expand_env_vars = |input: &str| -> Value {
448+
ENV_VAR_REGEX
449+
.replace_all(input, |caps: &regex::Captures<'_>| {
450+
let var_name = &caps[1];
451+
env::var(var_name).unwrap_or_else(|_| format!("${}", var_name))
452+
})
453+
.into()
454+
};
455+
456+
for val in static_fields.values_mut() {
457+
if let Some(value) = val.as_str() {
458+
*val = expand_env_vars(value);
459+
}
460+
}
461+
}
462+
438463
Self {
439464
static_fields,
440465
..self

src/logging/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ impl LoggingSubscriberConfig {
159159
current_span,
160160
ref static_fields,
161161
ref key_names,
162+
parse_env_in_static,
162163
} => {
163164
let json_fmt = ExtensibleJsonFormat::new()
164165
.with_target(self.print.target)
@@ -169,7 +170,7 @@ impl LoggingSubscriberConfig {
169170
.with_thread_ids(self.print.thread_id)
170171
.flatten_event(flatten_metadata)
171172
.with_current_span(current_span)
172-
.with_static_fields(static_fields.clone())
173+
.with_static_fields(static_fields.clone(), parse_env_in_static)
173174
.with_key_names(*key_names.clone());
174175
layer.json().event_format(json_fmt).boxed()
175176
}
@@ -230,6 +231,9 @@ pub enum LoggingFormat {
230231
/// Custom names for JSON keys.
231232
#[serde(default)]
232233
key_names: Box<JsonKeyNames>,
234+
/// Parse environment variables in static fields
235+
#[serde(default)]
236+
parse_env_in_static: bool,
233237
},
234238
}
235239

0 commit comments

Comments
 (0)