Skip to content

Commit

Permalink
opentelemetry: update metric name prefixes
Browse files Browse the repository at this point in the history
This patch changes the metric name prefixes from using upper snakecase,
to using lowercase and a `.` at the end. This is to align with the
OpenTelemetry metric naming conventions.
  • Loading branch information
bryangarza committed Jul 12, 2022
1 parent 3122f7a commit 1e26171
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
80 changes: 40 additions & 40 deletions tracing-opentelemetry/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use tracing_subscriber::{registry::LookupSpan, subscribe::Context, Subscribe};
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
const INSTRUMENTATION_LIBRARY_NAME: &str = "tracing/tracing-opentelemetry";

const METRIC_PREFIX_MONOTONIC_COUNTER: &str = "MONOTONIC_COUNTER_";
const METRIC_PREFIX_COUNTER: &str = "COUNTER_";
const METRIC_PREFIX_VALUE: &str = "VALUE_";
const METRIC_PREFIX_MONOTONIC_COUNTER: &str = "monotonic_counter.";
const METRIC_PREFIX_COUNTER: &str = "counter.";
const METRIC_PREFIX_VALUE: &str = "value.";
const I64_MAX: u64 = i64::MAX as u64;

#[derive(Default)]
Expand Down Expand Up @@ -142,18 +142,18 @@ impl<'a> Visit for MetricVisitor<'a> {
}

fn record_u64(&mut self, field: &Field, value: u64) {
if field.name().starts_with(METRIC_PREFIX_MONOTONIC_COUNTER) {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.instruments.update_metric(
self.meter,
InstrumentType::CounterU64(value),
field.name(),
metric_name,
);
} else if field.name().starts_with(METRIC_PREFIX_COUNTER) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
if value <= I64_MAX {
self.instruments.update_metric(
self.meter,
InstrumentType::UpDownCounterI64(value as i64),
field.name(),
metric_name,
);
} else {
eprintln!(
Expand All @@ -163,55 +163,55 @@ impl<'a> Visit for MetricVisitor<'a> {
value
);
}
} else if field.name().starts_with(METRIC_PREFIX_VALUE) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
self.instruments.update_metric(
self.meter,
InstrumentType::ValueRecorderU64(value),
field.name(),
metric_name,
);
}
}

fn record_f64(&mut self, field: &Field, value: f64) {
if field.name().starts_with(METRIC_PREFIX_MONOTONIC_COUNTER) {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.instruments.update_metric(
self.meter,
InstrumentType::CounterF64(value),
field.name(),
metric_name,
);
} else if field.name().starts_with(METRIC_PREFIX_COUNTER) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
self.instruments.update_metric(
self.meter,
InstrumentType::UpDownCounterF64(value),
field.name(),
metric_name,
);
} else if field.name().starts_with(METRIC_PREFIX_VALUE) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
self.instruments.update_metric(
self.meter,
InstrumentType::ValueRecorderF64(value),
field.name(),
metric_name,
);
}
}

fn record_i64(&mut self, field: &Field, value: i64) {
if field.name().starts_with(METRIC_PREFIX_MONOTONIC_COUNTER) {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.instruments.update_metric(
self.meter,
InstrumentType::CounterU64(value as u64),
field.name(),
metric_name,
);
} else if field.name().starts_with(METRIC_PREFIX_COUNTER) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
self.instruments.update_metric(
self.meter,
InstrumentType::UpDownCounterI64(value),
field.name(),
metric_name,
);
} else if field.name().starts_with(METRIC_PREFIX_VALUE) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
self.instruments.update_metric(
self.meter,
InstrumentType::ValueRecorderI64(value),
field.name(),
metric_name,
);
}
}
Expand Down Expand Up @@ -247,25 +247,25 @@ impl<'a> Visit for MetricVisitor<'a> {
/// To publish a new metric from your instrumentation point, all that is needed
/// is to add a key-value pair to your `tracing::Event` that contains a specific
/// prefix. One of the following:
/// - `MONOTONIC_COUNTER_` (non-negative numbers): Used when the counter should
/// - `monotonic_counter.` (non-negative numbers): Used when the counter should
/// only ever increase
/// - `COUNTER_`: Used when the counter can go up or down
/// - `VALUE_`: Used for discrete data points (i.e., summing them does not make
/// - `counter.`: Used when the counter can go up or down
/// - `value.`: Used for discrete data points (i.e., summing them does not make
/// semantic sense)
///
/// Examples:
/// ```
/// # use tracing::info;
/// info!(MONOTONIC_COUNTER_FOO = 1);
/// info!(MONOTONIC_COUNTER_BAR = 1.1);
/// info!(monotonic_counter.foo = 1);
/// info!(monotonic_counter.bar = 1.1);
///
/// info!(COUNTER_BAZ = 1);
/// info!(COUNTER_BAZ = -1);
/// info!(COUNTER_XYZ = 1.1);
/// info!(counter.baz = 1);
/// info!(counter.baz = -1);
/// info!(counter.xyz = 1.1);
///
/// info!(VALUE_QUX = 1);
/// info!(VALUE_ABC = -1);
/// info!(VALUE_DEF = 1.1);
/// info!(value.qux = 1);
/// info!(value.abc = -1);
/// info!(value.def = 1.1);
/// ```
///
/// # Mixing data types
Expand All @@ -279,15 +279,15 @@ impl<'a> Visit for MetricVisitor<'a> {
/// Don't do this:
/// ```
/// # use tracing::info;
/// info!(MONOTONIC_COUNTER_FOO = 1);
/// info!(MONOTONIC_COUNTER_FOO = 1.0);
/// info!(monotonic_counter.foo = 1);
/// info!(monotonic_counter.foo = 1.0);
/// ```
///
/// Do this:
/// ```
/// # use tracing::info;
/// info!(MONOTONIC_COUNTER_FOO = 1_f64);
/// info!(MONOTONIC_COUNTER_FOO = 1.1);
/// info!(monotonic_counter.foo = 1_f64);
/// info!(monotonic_counter.foo = 1.1);
/// ```
///
/// This is because all data published for a given metric name must be the same
Expand All @@ -304,17 +304,17 @@ impl<'a> Visit for MetricVisitor<'a> {
/// ```
/// # use tracing::info;
/// // The subscriber receives an i64
/// info!(COUNTER_BAZ = 1);
/// info!(counter.baz = 1);
///
/// // The subscriber receives an i64
/// info!(COUNTER_BAZ = -1);
/// info!(counter.baz = -1);
///
/// // The subscriber receives a u64, but casts it to i64 internally
/// info!(COUNTER_BAZ = 1_u64);
/// info!(counter.baz = 1_u64);
///
/// // The subscriber receives a u64, but cannot cast it to i64 because of
/// // overflow. An error is printed to stderr, and the metric is dropped.
/// info!(COUNTER_BAZ = (i64::MAX as u64) + 1)
/// info!(counter.baz = (i64::MAX as u64) + 1)
/// ```
///
/// # Under-the-hood
Expand Down
36 changes: 18 additions & 18 deletions tracing-opentelemetry/tests/metrics_publishing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,126 +30,126 @@ const INSTRUMENTATION_LIBRARY_NAME: &str = "tracing/tracing-opentelemetry";
#[tokio::test]
async fn u64_counter_is_exported() {
let subscriber = init_subscriber(
"MONOTONIC_COUNTER_HELLO_WORLD".to_string(),
"hello_world".to_string(),
InstrumentKind::Counter,
NumberKind::U64,
Number::from(1_u64),
);

tracing::collect::with_default(subscriber, || {
tracing::info!(MONOTONIC_COUNTER_HELLO_WORLD = 1_u64);
tracing::info!(monotonic_counter.hello_world = 1_u64);
});
}

#[tokio::test]
async fn u64_counter_is_exported_i64_at_instrumentation_point() {
let subscriber = init_subscriber(
"MONOTONIC_COUNTER_HELLO_WORLD2".to_string(),
"hello_world2".to_string(),
InstrumentKind::Counter,
NumberKind::U64,
Number::from(1_u64),
);

tracing::collect::with_default(subscriber, || {
tracing::info!(MONOTONIC_COUNTER_HELLO_WORLD2 = 1_i64);
tracing::info!(monotonic_counter.hello_world2 = 1_i64);
});
}

#[tokio::test]
async fn f64_counter_is_exported() {
let subscriber = init_subscriber(
"MONOTONIC_COUNTER_FLOAT_HELLO_WORLD".to_string(),
"float_hello_world".to_string(),
InstrumentKind::Counter,
NumberKind::F64,
Number::from(1.000000123_f64),
);

tracing::collect::with_default(subscriber, || {
tracing::info!(MONOTONIC_COUNTER_FLOAT_HELLO_WORLD = 1.000000123_f64);
tracing::info!(monotonic_counter.float_hello_world = 1.000000123_f64);
});
}

#[tokio::test]
async fn i64_up_down_counter_is_exported() {
let subscriber = init_subscriber(
"COUNTER_PEBCAK".to_string(),
"pebcak".to_string(),
InstrumentKind::UpDownCounter,
NumberKind::I64,
Number::from(-5_i64),
);

tracing::collect::with_default(subscriber, || {
tracing::info!(COUNTER_PEBCAK = -5_i64);
tracing::info!(counter.pebcak = -5_i64);
});
}

#[tokio::test]
async fn i64_up_down_counter_is_exported_u64_at_instrumentation_point() {
let subscriber = init_subscriber(
"COUNTER_PEBCAK2".to_string(),
"pebcak2".to_string(),
InstrumentKind::UpDownCounter,
NumberKind::I64,
Number::from(5_i64),
);

tracing::collect::with_default(subscriber, || {
tracing::info!(COUNTER_PEBCAK2 = 5_u64);
tracing::info!(counter.pebcak2 = 5_u64);
});
}

#[tokio::test]
async fn f64_up_down_counter_is_exported() {
let subscriber = init_subscriber(
"COUNTER_PEBCAK_BLAH".to_string(),
"pebcak_blah".to_string(),
InstrumentKind::UpDownCounter,
NumberKind::F64,
Number::from(99.123_f64),
);

tracing::collect::with_default(subscriber, || {
tracing::info!(COUNTER_PEBCAK_BLAH = 99.123_f64);
tracing::info!(counter.pebcak_blah = 99.123_f64);
});
}

#[tokio::test]
async fn u64_value_is_exported() {
let subscriber = init_subscriber(
"VALUE_ABCDEFG".to_string(),
"abcdefg".to_string(),
InstrumentKind::ValueRecorder,
NumberKind::U64,
Number::from(9_u64),
);

tracing::collect::with_default(subscriber, || {
tracing::info!(VALUE_ABCDEFG = 9_u64);
tracing::info!(value.abcdefg = 9_u64);
});
}

#[tokio::test]
async fn i64_value_is_exported() {
let subscriber = init_subscriber(
"VALUE_ABCDEFG_AUENATSOU".to_string(),
"abcdefg_auenatsou".to_string(),
InstrumentKind::ValueRecorder,
NumberKind::I64,
Number::from(-19_i64),
);

tracing::collect::with_default(subscriber, || {
tracing::info!(VALUE_ABCDEFG_AUENATSOU = -19_i64);
tracing::info!(value.abcdefg_auenatsou = -19_i64);
});
}

#[tokio::test]
async fn f64_value_is_exported() {
let subscriber = init_subscriber(
"VALUE_ABCDEFG_RACECAR".to_string(),
"abcdefg_racecar".to_string(),
InstrumentKind::ValueRecorder,
NumberKind::F64,
Number::from(777.0012_f64),
);

tracing::collect::with_default(subscriber, || {
tracing::info!(VALUE_ABCDEFG_RACECAR = 777.0012_f64);
tracing::info!(value.abcdefg_racecar = 777.0012_f64);
});
}

Expand Down

0 comments on commit 1e26171

Please sign in to comment.