Skip to content

Commit

Permalink
[profiling] use internal Label instead of pprof::Label (#454)
Browse files Browse the repository at this point in the history
* use internal Label instead of pprof::Label

* clippy and fmt

* remove StringId::new
  • Loading branch information
taegyunkim authored May 28, 2024
1 parent 633b0b0 commit d8a3c9a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
11 changes: 1 addition & 10 deletions profiling/src/collections/identifiable/string_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ impl StringId {
Self(0)
}

// todo: remove when upscaling uses internal::* instead of pprof::*
pub fn new<T>(v: T) -> Self
where
T: TryInto<u32>,
T::Error: core::fmt::Debug,
{
Self(v.try_into().expect("StringId to fit into a u32"))
}

#[inline]
pub const fn is_zero(&self) -> bool {
self.0 == 0
Expand All @@ -39,7 +30,7 @@ impl Id for StringId {
type RawId = i64;

fn from_offset(inner: usize) -> Self {
Self::new(inner)
Self(inner.try_into().expect("StringId to fit into a u32"))
}

fn to_raw_id(&self) -> Self::RawId {
Expand Down
17 changes: 7 additions & 10 deletions profiling/src/internal/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl Profile {
let mut encoder = CompressedProtobufSerializer::with_capacity(INITIAL_PPROF_BUFFER_SIZE);

for (sample, timestamp, mut values) in std::mem::take(&mut self.observations).into_iter() {
let labels = self.translate_and_enrich_sample_labels(sample, timestamp)?;
let labels = self.enrich_sample_labels(sample, timestamp)?;
let location_ids: Vec<_> = self
.get_stacktrace(sample.stacktrace)?
.locations
Expand All @@ -223,6 +223,7 @@ impl Profile {
.collect();
self.upscaling_rules.upscale_values(&mut values, &labels)?;

let labels = labels.into_iter().map(pprof::Label::from).collect();
let item = pprof::Sample {
location_ids,
values,
Expand Down Expand Up @@ -481,20 +482,16 @@ impl Profile {
profile
}

fn translate_and_enrich_sample_labels(
fn enrich_sample_labels(
&self,
sample: Sample,
timestamp: Option<Timestamp>,
) -> anyhow::Result<Vec<pprof::Label>> {
) -> anyhow::Result<Vec<Label>> {
self.get_label_set(sample.labels)?
.iter()
.map(|l| self.get_label(*l).map(pprof::Label::from))
.chain(
self.get_endpoint_for_labels(sample.labels)
.transpose()
.map(|res| res.map(pprof::Label::from)),
)
.chain(timestamp.map(|ts| Ok(Label::num(self.timestamp_key, ts.get(), None).into())))
.map(|l| self.get_label(*l).copied())
.chain(self.get_endpoint_for_labels(sample.labels).transpose())
.chain(timestamp.map(|ts| Ok(Label::num(self.timestamp_key, ts.get(), None))))
.collect()
}

Expand Down
17 changes: 10 additions & 7 deletions profiling/src/internal/upscaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,20 @@ impl UpscalingRules {
self.rules.is_empty()
}

// TODO: Consider whether to use the internal Label here instead
pub fn upscale_values(
&self,
values: &mut [i64],
labels: &[pprof::Label],
) -> anyhow::Result<()> {
pub fn upscale_values(&self, values: &mut [i64], labels: &[Label]) -> anyhow::Result<()> {
if !self.is_empty() {
// get bylabel rules first (if any)
let mut group_of_rules = labels
.iter()
.filter_map(|label| self.get(&(StringId::new(label.key), StringId::new(label.str))))
.filter_map(|label| {
self.get(&(
label.get_key(),
match label.get_value() {
LabelValue::Str(str) => *str,
LabelValue::Num { .. } => StringId::ZERO,
},
))
})
.collect::<Vec<&Vec<UpscalingRule>>>();

// get byvalue rules if any
Expand Down

0 comments on commit d8a3c9a

Please sign in to comment.