Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/expfmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ exclude = [

[features]
default = ["arrow", "dto"]
arrow = ["dep:arrow-array", "dep:arrow-buffer", "dep:arrow-schema"]
arrow = ["dep:arrow-array", "dep:arrow-buffer", "dep:arrow-schema", "dep:prometheus-relabel"]
dto = ["dep:prost", "dep:prost-types"]

[dependencies]
arrow-array = { workspace = true, optional = true }
arrow-buffer = { workspace = true, optional = true }
arrow-schema = { workspace = true, optional = true }
prometheus-relabel = { workspace = true, optional = true }
prost = { workspace = true, optional = true }
prost-types = { workspace = true, optional = true }
thiserror = { workspace = true }
Expand Down
61 changes: 60 additions & 1 deletion crates/expfmt/benches/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ use std::future::Future;
use std::hint::black_box;
use std::path::Path;
use std::pin::{Pin, pin};
use std::sync::Arc;
use std::task::{Context, Poll, Waker};

use benchfmt::{CountingAlloc, Suite};
use prometheus_expfmt::arrow::{ArrowDecoder, DecodeBudget, DecodeOptions, DecodeStep};
use prometheus_expfmt::arrow::{
ArrowDecoder, DecodeBudget, DecodeOptions, DecodeStep, MetricRelabelPlan, TargetAttributes,
};
use prometheus_expfmt::format::Format;
use prometheus_expfmt::relabel::{Action, Config, METRIC_NAME_LABEL, Regexp};

#[global_allocator]
static ALLOC: CountingAlloc = CountingAlloc::new();
Expand Down Expand Up @@ -232,5 +236,60 @@ fn main() {
);
}

let relabel_target = Arc::new(TargetAttributes::from_target_labels([
("job", "prometheus"),
("instance", "127.0.0.1:9090"),
("env", "prod"),
("handler", "target-handler"),
]));
let relabel_plan = Arc::new(MetricRelabelPlan::new(vec![
Config {
source_labels: vec![METRIC_NAME_LABEL.to_owned()],
regex: Regexp::must_new("(prometheus|go)_.*"),
action: Action::Keep,
..Config::default()
},
Config {
regex: Regexp::must_new("le_unused_.*"),
action: Action::LabelDrop,
..Config::default()
},
Config {
source_labels: vec!["handler".to_owned()],
regex: Regexp::must_new("/.*"),
target_label: "route_group".to_owned(),
replacement: "http".to_owned(),
action: Action::Replace,
..Config::default()
},
]));
let relabel_opts = DecodeOptions::new()
.with_timestamp_ms(0)
.with_target_attributes(relabel_target)
.with_honor_labels(false)
.with_metric_relabel_plan(relabel_plan);
let full_bytes_len = full_bytes.len() as u64;

suite = suite.bench(
"Decode/case=alltypes.237mfs/mode=relabel_stage1_stage2",
move |b| {
let mut decoder = ArrowDecoder::new();
black_box(
decoder
.decode(full_bytes, &Format::TEXT, &relabel_opts)
.unwrap(),
);

b.set_bytes(full_bytes_len);
b.report_allocs();
while b.keep_running() {
let records = decoder
.decode(black_box(full_bytes), &Format::TEXT, &relabel_opts)
.unwrap();
black_box(records);
}
},
);

suite.run();
}
187 changes: 136 additions & 51 deletions crates/expfmt/src/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod family;
mod intern;
mod options;
mod records;
mod relabel;
mod schema;
#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -53,6 +54,8 @@ pub use self::options::{
UnitSuffixPolicy, UntypedPolicy,
};
pub use self::records::{MetricPayloadType, OtapMetricRecords};
pub use self::relabel::MetricRelabelPlan;
use self::relabel::{Stage2Scratch, apply_stage2_relabeling};
pub use self::schema::{PROMETHEUS_TYPE_METADATA_KEY, SERVICE_INSTANCE_ID_ATTR, SERVICE_NAME_ATTR};

/// Maximum distinct `UnivariateMetrics` rows representable in a single OTAP batch (`u16` IDs).
Expand Down Expand Up @@ -313,6 +316,10 @@ pub struct ArrowDecoder {
hist_dp: HistogramDpTable,
temp_attrs: Vec<(u32, u32)>,
scratch: ScratchBuffers,
stage1_last_raw_name: Vec<u8>,
stage1_last_kept: bool,
stage1_cache_valid: bool,
stage2_scratch: Stage2Scratch,
}

impl Default for ArrowDecoder {
Expand Down Expand Up @@ -357,6 +364,10 @@ impl ArrowDecoder {
hist_dp: HistogramDpTable::default(),
temp_attrs: Vec::new(),
scratch: ScratchBuffers::with_capacity(metrics, data_points, attrs),
stage1_last_raw_name: Vec::new(),
stage1_last_kept: true,
stage1_cache_valid: false,
stage2_scratch: Stage2Scratch::default(),
}
}

Expand All @@ -378,6 +389,8 @@ impl ArrowDecoder {
+ self.hist_dp.retained_memory_bytes()
+ self.temp_attrs.capacity() * size_of::<(u32, u32)>()
+ self.scratch.retained_memory_bytes()
+ self.stage1_last_raw_name.capacity()
+ self.stage2_scratch.retained_memory_bytes()
}

/// Decodes a single exposition scrape buffer of `format` into OTAP metric
Expand Down Expand Up @@ -730,6 +743,9 @@ impl ArrowDecoder {
self.scrape_id = next_scrape_id();
self.current_family.clear();
self.assembly.clear();
self.stage1_last_raw_name.clear();
self.stage1_last_kept = true;
self.stage1_cache_valid = false;
self.reset_batch();
}

Expand Down Expand Up @@ -848,6 +864,9 @@ impl ArrowDecoder {
if CHUNKED {
self.reset_batch();
}
if batch.is_empty() {
return Ok(DecodeStep::Done);
}
return Ok(DecodeStep::Batch(batch));
}
}
Expand All @@ -869,68 +888,88 @@ impl ArrowDecoder {
offset: name_offset,
});
}
let role = if self.current_family.active
&& self.current_family.raw_name.as_slice() == raw_name
&& !matches!(
self.current_family.metric_type,
MetricType::Histogram | MetricType::GaugeHistogram
) {
if self.current_family.metric_type == MetricType::Summary {
SeriesRole::SummaryQuantile
} else {
SeriesRole::Scalar
let mut stage1_kept = true;
if let Some(plan) = opts.metric_relabel_plan.as_deref() {
let stage1 = plan.stage1_configs();
if !stage1.is_empty() {
stage1_kept =
self.check_stage1_metric_name(raw_name, name_offset, stage1)?;
}
} else {
let unescaped = unescape_bytes_into(raw_name, true, &mut self.unescape_buf);
if let Some(r) = self.current_family.classify_series(unescaped) {
r
}
if stage1_kept {
let role = if self.current_family.active
&& self.current_family.raw_name.as_slice() == raw_name
&& !matches!(
self.current_family.metric_type,
MetricType::Histogram | MetricType::GaugeHistogram
) {
if self.current_family.metric_type == MetricType::Summary {
SeriesRole::SummaryQuantile
} else {
SeriesRole::Scalar
}
} else {
self.series_name_buf.clear();
self.series_name_buf.extend_from_slice(unescaped);
self.flush_pending_family(opts, default_ts_ns)?;
if CHUNKED && self.total_data_points() >= max_data_points {
let unescaped =
unescape_bytes_into(raw_name, true, &mut self.unescape_buf);
if let Some(r) = self.current_family.classify_series(unescaped) {
r
} else {
self.series_name_buf.clear();
self.series_name_buf.extend_from_slice(unescaped);
self.flush_pending_family(opts, default_ts_ns)?;
if CHUNKED && self.total_data_points() >= max_data_points {
self.current_family
.start_new(raw_name, &self.series_name_buf);
*pending_series = true;
let batch =
self.finish_batches(opts, default_ts_ns.is_some())?;
self.reset_batch();
if !batch.is_empty() {
return Ok(DecodeStep::Batch(batch));
}
continue;
}
self.current_family
.start_new(raw_name, &self.series_name_buf);
SeriesRole::Scalar
}
};

if CHUNKED {
let split_assembled = !self.assembly.is_empty()
&& self.batch_data_points() >= max_data_points
&& !self.series_matches_staged_group(parser, role);
let split_scalar = self.assembly.is_empty()
&& (self.total_data_points() >= max_data_points
|| (self.current_family.batch_id.is_none()
&& self.metrics.len() >= MAX_METRICS_PER_BATCH));
if split_assembled || split_scalar {
if split_assembled {
self.flush_pending_family(opts, default_ts_ns)?;
}
*pending_series = true;
let batch = self.finish_batches(opts, default_ts_ns.is_some())?;
self.reset_batch();
return Ok(DecodeStep::Batch(batch));
if !batch.is_empty() {
return Ok(DecodeStep::Batch(batch));
}
continue;
}
self.current_family
.start_new(raw_name, &self.series_name_buf);
SeriesRole::Scalar
}
};

if CHUNKED {
let split_assembled = !self.assembly.is_empty()
&& self.batch_data_points() >= max_data_points
&& !self.series_matches_staged_group(parser, role);
let split_scalar = self.assembly.is_empty()
&& (self.total_data_points() >= max_data_points
|| (self.current_family.batch_id.is_none()
&& self.metrics.len() >= MAX_METRICS_PER_BATCH));
if split_assembled || split_scalar {
if split_assembled {
self.flush_pending_family(opts, default_ts_ns)?;
}
*pending_series = true;
self.ingest_series_entry(parser, role, opts, default_ts_ns)?;

if CHUNKED
&& self.assembly.is_empty()
&& self.total_data_points() >= max_data_points
{
let batch = self.finish_batches(opts, default_ts_ns.is_some())?;
self.reset_batch();
return Ok(DecodeStep::Batch(batch));
if !batch.is_empty() {
return Ok(DecodeStep::Batch(batch));
}
}
}

self.ingest_series_entry(parser, role, opts, default_ts_ns)?;

if CHUNKED
&& self.assembly.is_empty()
&& self.total_data_points() >= max_data_points
{
let batch = self.finish_batches(opts, default_ts_ns.is_some())?;
self.reset_batch();
return Ok(DecodeStep::Batch(batch));
}
}
}

Expand Down Expand Up @@ -1549,6 +1588,28 @@ impl ArrowDecoder {
Ok(())
}

#[inline]
fn check_stage1_metric_name(
&mut self,
raw_name: &[u8],
name_offset: usize,
stage1: &[prometheus_relabel::Config],
) -> Result<bool, DecodeError> {
if self.stage1_cache_valid && self.stage1_last_raw_name.as_slice() == raw_name {
return Ok(self.stage1_last_kept);
}
let unescaped = unescape_bytes_into(raw_name, true, &mut self.unescape_buf);
let name_str = std::str::from_utf8(unescaped).map_err(|_| DecodeError::InvalidUtf8 {
offset: name_offset,
})?;
let kept = prometheus_relabel::keep_metric_name(name_str, stage1);
self.stage1_last_raw_name.clear();
self.stage1_last_raw_name.extend_from_slice(raw_name);
self.stage1_last_kept = kept;
self.stage1_cache_valid = true;
Ok(kept)
}

fn finish_batches(
&mut self,
opts: &DecodeOptions,
Expand All @@ -1558,9 +1619,33 @@ impl ArrowDecoder {
return Ok(OtapMetricRecords::default());
}

let resource_attrs = match opts.target_attributes.as_deref() {
Some(target) => target.resource_attrs_batch()?,
None => None,
let target_attrs = opts.target_attributes.as_deref();
let has_target_labels = target_attrs.is_some_and(|t| !t.target_labels().is_empty());
let has_stage2 = opts
.metric_relabel_plan
.as_deref()
.is_some_and(|p| !p.stage2_configs().is_empty());

let resource_attrs = if (!opts.honor_labels && has_target_labels) || has_stage2 {
let res = apply_stage2_relabeling(
opts,
&mut self.metrics,
&mut self.key_interner,
&mut self.val_interner,
&mut self.num_dp,
&mut self.sum_dp,
&mut self.hist_dp,
&mut self.stage2_scratch,
)?;
if self.metrics.is_empty() {
return Ok(OtapMetricRecords::default());
}
res
} else {
match target_attrs {
Some(target) => target.resource_attrs_batch()?,
None => None,
}
};
let univariate_metrics = Some(self.metrics.build_univariate_metrics(&mut self.scratch)?);
let number_data_points = self.num_dp.build_batch(has_default_ts, &mut self.scratch)?;
Expand Down
Loading
Loading