Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/metrics/log_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use serde::{Serialize, Serializer as _};
use serde_json::Serializer;
use std::cell::Cell;
use std::collections::HashMap;
use std::io::Write;
use std::rc::Rc;

Expand Down Expand Up @@ -43,6 +44,22 @@
fn encode<W: Write>(&self, families: &[MetricFamily], dest: &mut W) -> prometheus::Result<()> {
let events = families_to_json_events(families);

let metrics = events
.iter()
.map(|event| {
let value = MapMetric {
data: event.metric.data.clone(),
tags: event.metric.tags.clone(),
};
(event.metric.name, value)
})
.collect::<HashMap<_, _>>();

match serde_json::to_string(&metrics) {
Ok(json) => info!(target: "metrics", "{{\"metrics\":{json}}}"),
Err(error) => warn!(target: "metrics", "Failed to serialize metrics: {error}"),

Check warning on line 60 in src/metrics/log_encoder.rs

View check run for this annotation

Codecov / codecov/patch

src/metrics/log_encoder.rs#L60

Added line #L60 was not covered by tests
}

let chunks = serialize_and_split_list(events.iter(), CHUNKS_MAX_SIZE_BYTES)
.map_err(|e| Error::Msg(e.to_string()))?;

Expand Down Expand Up @@ -180,6 +197,13 @@
}
}

#[derive(Serialize, Debug, PartialEq)]
struct MapMetric<'a> {
#[serde(flatten)]
data: VectorMetricData,
tags: IndexMap<&'a str, &'a str>,
}

#[derive(Serialize, Debug, PartialEq)]
struct VectorEvent<'a> {
metric: VectorMetric<'a>,
Expand All @@ -194,7 +218,7 @@
tags: IndexMap<&'a str, &'a str>,
}

#[derive(Serialize, Debug, PartialEq)]
#[derive(Serialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
enum VectorMetricData {
AggregatedHistogram {
Expand Down