Skip to content

Commit

Permalink
fix reported issues
Browse files Browse the repository at this point in the history
  • Loading branch information
stackempty committed Feb 26, 2025
1 parent b8f469a commit f25d896
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
Add support for labels with gcp_stackdriver_logs config
Add support for labels from structured logs matching key specified via `labels_key` gcp_stackdriver_logs config
Add support for static labels in gcp_stackdriver_logs sink.

This enhancement enables users to define static labels directly in the
gcp_stackdriver_logs sink configuration. Static labels are key-value pairs
that are consistently applied to all log entries sent to Google Cloud Logging,
improving log organization and filtering capabilities.


Add support for dynamic labels in gcp_stackdriver_logs sink via `labels_key`.

This enhancement allows Vector to automatically map fields from structured
log entries to Google Cloud LogEntry labels. When a structured log contains
fields matching the configured `labels_key`, Vector will populate the
corresponding labels in the Google Cloud LogEntry, enabling better log
organization and filtering in Google Cloud Logging.

authors: stackempty
6 changes: 3 additions & 3 deletions src/sinks/gcp/stackdriver/logs/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub(super) struct StackdriverLabelConfig {
/// and extract their values to set as LogEntry labels.
#[configurable(metadata(docs::examples = "logging.googleapis.com/labels"))]
#[serde(default = "default_labels_key")]
pub(super) labels_key: String,
pub(super) labels_key: Option<String>,

/// A map of key, value pairs that provides additional information about the log entry.
#[configurable(metadata(
Expand All @@ -186,8 +186,8 @@ fn labels_examples() -> HashMap<String, String> {
example
}

fn default_labels_key() -> String {
"logging.googleapis.com/labels".to_string()
fn default_labels_key() -> Option<String> {
Some("logging.googleapis.com/labels".to_string())
}

/// A monitored resource.
Expand Down
21 changes: 13 additions & 8 deletions src/sinks/gcp/stackdriver/logs/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,20 @@ impl StackdriverLogsEncoder {
.map(remap_severity)
.unwrap_or_else(|| 0.into());

let labels_key = self
.label_config
.labels_key
.as_ref()
.map(|s| s.as_str())
.unwrap_or_else(|| "logging.googleapis.com/labels".into());

// merge log_labels in the specified labels_key into the labels map.
if let Some(log_labels) = log.remove(event_path!(self.label_config.labels_key.as_str())) {
if let Value::Object(log_labels) = log_labels {
labels.extend(
log_labels
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string_lossy().into_owned())),
);
}
if let Some(Value::Object(log_labels)) = log.remove(event_path!(labels_key)) {
labels.extend(
log_labels
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string_lossy().into_owned())),
);
}

let mut event = Event::Log(log);
Expand Down
6 changes: 3 additions & 3 deletions src/sinks/gcp/stackdriver/logs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn encode_valid() {
Template::try_from("{{ log_id }}").unwrap(),
StackdriverLogName::Project("project".to_owned()),
StackdriverLabelConfig {
labels_key: "logging.googleapis.com/labels".to_owned(),
labels_key: Some("logging.googleapis.com/labels".to_owned()),
labels: HashMap::from([(
"config_user_label_1".to_owned(),
Template::try_from("config_user_value_1").unwrap(),
Expand Down Expand Up @@ -143,7 +143,7 @@ fn encode_inserts_timestamp() {
Template::try_from("testlogs").unwrap(),
StackdriverLogName::Project("project".to_owned()),
StackdriverLabelConfig {
labels_key: "user_label_key".to_owned(),
labels_key: Some("user_label_key".to_owned()),
labels: HashMap::from([(
"config_user_label_1".to_owned(),
Template::try_from("value_1").unwrap(),
Expand Down Expand Up @@ -225,7 +225,7 @@ async fn correct_request() {
Template::try_from("testlogs").unwrap(),
StackdriverLogName::Project("project".to_owned()),
StackdriverLabelConfig {
labels_key: "user_label_key".to_owned(),
labels_key: Some("user_label_key".to_owned()),
labels: HashMap::from([(
"config_user_label_1".to_owned(),
Template::try_from("value_1").unwrap(),
Expand Down

0 comments on commit f25d896

Please sign in to comment.