Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics/gauge #1

Open
wants to merge 5 commits into
base: metrics/gauge
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ jobs:
- name: install cargo-nextest
uses: taiki-e/install-action@nextest
- name: Run tests
run: cargo nextest run --profile ci --workspace
run: cargo nextest run --profile ci --workspace --all-features
# TODO(eliza): punt on this for now because the generated JUnit report is
# missing some fields that this action needs to give good output.
# - name: Publish Test Report
Expand All @@ -146,7 +146,7 @@ jobs:
# check_name: "cargo test (Rust ${{ matrix.rust }} on ${{ matrix.os }})"
# check_title_template: "{{SUITE_NAME}}::{{TEST_NAME}}"
- name: Run doctests
run: cargo test --doc --workspace
run: cargo test --doc --workspace --all-features

test-build-wasm:
name: build tests (wasm)
Expand All @@ -158,7 +158,7 @@ jobs:
with:
target: wasm32-unknown-unknown
- name: build all tests
run: cargo test --no-run
run: cargo test --no-run --all-features

# all required checks except for the main test run (which we only require
# specific matrix combinations from)
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ rust-version = "1.65.0"
default = ["tracing-log", "metrics"]
# Enables support for exporting OpenTelemetry metrics
metrics = ["opentelemetry/metrics","opentelemetry_sdk/metrics", "smallvec"]
# Enables support for unstable OpenTelemetry metric instruments
otel_unstable = ["opentelemetry/otel_unstable"]
# Enables experimental support for OpenTelemetry gauge metrics
metrics_gauge_unstable = ["opentelemetry/otel_unstable"]

[dependencies]
opentelemetry = { version = "0.22.0", default-features = false, features = ["trace"] }
Expand Down
102 changes: 45 additions & 57 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, fmt, sync::RwLock};
use tracing::{field::Visit, Subscriber};
use tracing_core::{Field, Interest, Metadata};

#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
use opentelemetry::metrics::Gauge;
use opentelemetry::{
metrics::{Counter, Histogram, Meter, MeterProvider, UpDownCounter},
Expand All @@ -23,7 +23,7 @@ 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_HISTOGRAM: &str = "histogram.";
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
const METRIC_PREFIX_GAUGE: &str = "gauge.";

const I64_MAX: u64 = i64::MAX as u64;
Expand All @@ -36,12 +36,12 @@ pub(crate) struct Instruments {
f64_up_down_counter: MetricsMap<UpDownCounter<f64>>,
u64_histogram: MetricsMap<Histogram<u64>>,
f64_histogram: MetricsMap<Histogram<f64>>,
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
u64_gauge: MetricsMap<Gauge<u64>>,
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
i64_gauge: MetricsMap<Gauge<i64>>,
#[cfg(feature = "metrics_gauge_unstable")]
f64_gauge: MetricsMap<Gauge<f64>>,
#[cfg(feature="otel_unstable")]
i64_gauge: MetricsMap<Gauge<i64>>
}

type MetricsMap<T> = RwLock<HashMap<&'static str, T>>;
Expand All @@ -54,12 +54,12 @@ pub(crate) enum InstrumentType {
UpDownCounterF64(f64),
HistogramU64(u64),
HistogramF64(f64),
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
GaugeU64(u64),
#[cfg(feature="otel_unstable")]
GaugeF64(f64),
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
GaugeI64(i64),
#[cfg(feature = "metrics_gauge_unstable")]
GaugeF64(f64),
}

impl Instruments {
Expand Down Expand Up @@ -142,16 +142,7 @@ impl Instruments {
|rec| rec.record(value, attributes),
);
}
#[cfg(feature="otel_unstable")]
InstrumentType::GaugeF64(value) => {
update_or_insert(
&self.f64_gauge,
metric_name,
|| meter.f64_gauge(metric_name).init(),
|rec| rec.record(value, attributes),
);
}
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
InstrumentType::GaugeU64(value) => {
update_or_insert(
&self.u64_gauge,
Expand All @@ -160,7 +151,7 @@ impl Instruments {
|rec| rec.record(value, attributes),
);
}
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
InstrumentType::GaugeI64(value) => {
update_or_insert(
&self.i64_gauge,
Expand All @@ -169,7 +160,15 @@ impl Instruments {
|rec| rec.record(value, attributes),
);
}

#[cfg(feature = "metrics_gauge_unstable")]
InstrumentType::GaugeF64(value) => {
update_or_insert(
&self.f64_gauge,
metric_name,
|| meter.f64_gauge(metric_name).init(),
|rec| rec.record(value, attributes),
);
}
};
}
}
Expand All @@ -186,7 +185,7 @@ impl<'a> Visit for MetricVisitor<'a> {
}

fn record_u64(&mut self, field: &Field, value: u64) {
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_GAUGE) {
self.visited_metrics
.push((metric_name, InstrumentType::GaugeU64(value)));
Expand All @@ -195,11 +194,10 @@ impl<'a> Visit for MetricVisitor<'a> {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.visited_metrics
.push((metric_name, InstrumentType::CounterU64(value)));
return;
}
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
if value <= I64_MAX {
self.visited_metrics.push((metric_name, InstrumentType::UpDownCounterI64(value as i64)));
self.visited_metrics
.push((metric_name, InstrumentType::UpDownCounterI64(value as i64)));
} else {
eprintln!(
"[tracing-opentelemetry]: Received Counter metric, but \
Expand All @@ -208,47 +206,39 @@ impl<'a> Visit for MetricVisitor<'a> {
value
);
}
return;
}
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
self.visited_metrics
.push((metric_name, InstrumentType::HistogramU64(value)));
return;
}

if value <= I64_MAX {
self.attributes.push(KeyValue::new(field.name(), Value::I64(value as i64)));
} else if value <= I64_MAX {
self.attributes
.push(KeyValue::new(field.name(), Value::I64(value as i64)));
}
}

fn record_f64(&mut self, field: &Field, value: f64) {
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_GAUGE) {
self.visited_metrics
.push((metric_name, InstrumentType::GaugeF64(value)));
return;
}
}
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.visited_metrics
.push((metric_name, InstrumentType::CounterF64(value)));
return;
}
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
self.visited_metrics
.push((metric_name, InstrumentType::UpDownCounterF64(value)));
return;
}
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
self.visited_metrics
.push((metric_name, InstrumentType::HistogramF64(value)));
return;
} else {
self.attributes
.push(KeyValue::new(field.name(), Value::F64(value)));
}

self.attributes.push(KeyValue::new(field.name(), Value::F64(value)));
}

fn record_i64(&mut self, field: &Field, value: i64) {
#[cfg(feature="otel_unstable")]
#[cfg(feature = "metrics_gauge_unstable")]
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_GAUGE) {
self.visited_metrics
.push((metric_name, InstrumentType::GaugeI64(value)));
Expand All @@ -257,15 +247,12 @@ impl<'a> Visit for MetricVisitor<'a> {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.visited_metrics
.push((metric_name, InstrumentType::CounterU64(value as u64)));
return;
}
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
self.visited_metrics
.push((metric_name, InstrumentType::UpDownCounterI64(value)));
return;
} else {
self.attributes.push(KeyValue::new(field.name(), value));
}

self.attributes.push(KeyValue::new(field.name(), value));
}

fn record_str(&mut self, field: &Field, value: &str) {
Expand Down Expand Up @@ -435,12 +422,13 @@ impl MetricsFilter {
let name = field.name();

if name.starts_with(METRIC_PREFIX_COUNTER)
|| name.starts_with(METRIC_PREFIX_MONOTONIC_COUNTER)
|| name.starts_with(METRIC_PREFIX_HISTOGRAM) {
|| name.starts_with(METRIC_PREFIX_MONOTONIC_COUNTER)
|| name.starts_with(METRIC_PREFIX_HISTOGRAM)
{
return true;
}
#[cfg(feature="otel_unstable")]

#[cfg(feature = "metrics_gauge_unstable")]
if name.starts_with(METRIC_PREFIX_GAUGE) {
return true;
}
Expand Down
47 changes: 23 additions & 24 deletions tests/metrics_publishing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use opentelemetry::{metrics::MetricsError, KeyValue};
use opentelemetry_sdk::{
metrics::{
data::{self, Histogram, Sum, Gauge},
data::{self, Gauge, Histogram, Sum},
reader::{
AggregationSelector, DefaultAggregationSelector, DefaultTemporalitySelector,
MetricReader, TemporalitySelector,
Expand Down Expand Up @@ -115,49 +115,43 @@ async fn f64_up_down_counter_is_exported() {
exporter.export().unwrap();
}

#[cfg(feature = "metrics_gauge_unstable")]
#[tokio::test]
async fn u64_gauge_is_exported() {
let (subscriber, exporter) = init_subscriber(
"gygygy".to_string(),
InstrumentKind::Gauge,
1_u64,
None,
);
let (subscriber, exporter) =
init_subscriber("gygygy".to_string(), InstrumentKind::Gauge, 2_u64, None);

tracing::subscriber::with_default(subscriber, || {
tracing::info!(gauge.gygygy = 1_u64);
tracing::info!(gauge.gygygy = 2_u64);
});

exporter.export().unwrap();
}

#[cfg(feature = "metrics_gauge_unstable")]
#[tokio::test]
async fn f64_gauge_is_exported() {
let (subscriber, exporter) = init_subscriber(
"huitt".to_string(),
InstrumentKind::Gauge,
1_f64,
None,
);
let (subscriber, exporter) =
init_subscriber("huitt".to_string(), InstrumentKind::Gauge, 2_f64, None);

tracing::subscriber::with_default(subscriber, || {
tracing::info!(gauge.huitt = 1_f64);
tracing::info!(gauge.huitt = 2_f64);
});

exporter.export().unwrap();
}

#[cfg(feature = "metrics_gauge_unstable")]
#[tokio::test]
async fn i64_gauge_is_exported() {
let (subscriber, exporter) = init_subscriber(
"samsagaz".to_string(),
InstrumentKind::Gauge,
1_i64,
None,
);
let (subscriber, exporter) =
init_subscriber("samsagaz".to_string(), InstrumentKind::Gauge, 2_i64, None);

tracing::subscriber::with_default(subscriber, || {
tracing::info!(gauge.samsagaz = 1_i64);
tracing::info!(gauge.samsagaz = 2_i64);
});

exporter.export().unwrap();
Expand Down Expand Up @@ -323,11 +317,12 @@ async fn f64_up_down_counter_with_attributes_is_exported() {
exporter.export().unwrap();
}

#[cfg(feature = "metrics_gauge_unstable")]
#[tokio::test]
async fn f64_gauge_with_attributes_is_exported() {
let (subscriber, exporter) = init_subscriber(
"hello_world".to_string(),
InstrumentKind::Counter,
InstrumentKind::Gauge,
1_f64,
Some(AttributeSet::from(
[
Expand Down Expand Up @@ -355,11 +350,12 @@ async fn f64_gauge_with_attributes_is_exported() {
exporter.export().unwrap();
}

#[cfg(feature = "metrics_gauge_unstable")]
#[tokio::test]
async fn u64_gauge_with_attributes_is_exported() {
let (subscriber, exporter) = init_subscriber(
"hello_world".to_string(),
InstrumentKind::Counter,
InstrumentKind::Gauge,
1_u64,
Some(AttributeSet::from(
[
Expand Down Expand Up @@ -387,11 +383,12 @@ async fn u64_gauge_with_attributes_is_exported() {
exporter.export().unwrap();
}

#[cfg(feature = "metrics_gauge_unstable")]
#[tokio::test]
async fn i64_gauge_with_attributes_is_exported() {
let (subscriber, exporter) = init_subscriber(
"hello_world".to_string(),
InstrumentKind::Counter,
InstrumentKind::Gauge,
1_i64,
Some(AttributeSet::from(
[
Expand Down Expand Up @@ -665,10 +662,12 @@ where
let gauge = metric.data.as_any().downcast_ref::<Gauge<T>>().unwrap();
assert_eq!(
self.expected_value,
gauge.data_points
gauge
.data_points
.iter()
.map(|data_point| data_point.value)
.sum()
.last()
.unwrap()
);

if let Some(expected_attributes) = self.expected_attributes.as_ref() {
Expand Down