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

feat: add otel_scope_info and scope labels #974

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: add test
  • Loading branch information
zengxilong committed Feb 26, 2023
commit d5f4cd0f9f73d18e35aff4057c3a3da632c2a6aa
17 changes: 10 additions & 7 deletions opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,18 @@
//! //
//! // # HELP a_counter Counts things
//! // # TYPE a_counter counter
//! // a_counter{R="V",key="value"} 100
//! // a_counter{R="V",key="value",otel_scope_name="my-app",otel_scope_version=""} 100
//! // # HELP a_histogram Records values
//! // # TYPE a_histogram histogram
//! // a_histogram_bucket{R="V",key="value",le="0.5"} 0
//! // a_histogram_bucket{R="V",key="value",le="0.9"} 0
//! // a_histogram_bucket{R="V",key="value",le="0.99"} 0
//! // a_histogram_bucket{R="V",key="value",le="+Inf"} 1
//! // a_histogram_sum{R="V",key="value"} 100
//! // a_histogram_count{R="V",key="value"} 1
//! // a_histogram_bucket{R="V",key="value",le="0.5",otel_scope_name="my-app",otel_scope_version=""} 0
//! // a_histogram_bucket{R="V",key="value",le="0.9",otel_scope_name="my-app",otel_scope_version=""} 0
//! // a_histogram_bucket{R="V",key="value",le="0.99",otel_scope_name="my-app",otel_scope_version=""} 0
//! // a_histogram_bucket{R="V",key="value",le="+Inf",otel_scope_name="my-app",otel_scope_version=""} 1
//! // a_histogram_sum{R="V",key="value",otel_scope_name="my-app",otel_scope_version=""} 100
//! // a_histogram_count{R="V",key="value",otel_scope_name="my-app",otel_scope_version=""} 1
//! // HELP otel_scope_info Instrumentation Scope metadata
//! // TYPE otel_scope_info gauge
//! // otel_scope_info{otel_scope_name="ex.com/B",otel_scope_version=""} 1
//! ```
#![warn(
future_incompatible,
Expand Down
71 changes: 66 additions & 5 deletions opentelemetry-prometheus/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use opentelemetry::sdk::metrics::{controllers, processors, selectors};
use opentelemetry::sdk::Resource;
use opentelemetry::Context;
use opentelemetry::{metrics::MeterProvider, KeyValue};
use opentelemetry_prometheus::PrometheusExporter;
use opentelemetry_prometheus::{ExporterConfig, PrometheusExporter};
use prometheus::{Encoder, TextEncoder};

#[test]
Expand All @@ -22,15 +22,16 @@ fn free_unused_instruments() {
let meter = exporter
.meter_provider()
.unwrap()
.versioned_meter("test", None, None);
.versioned_meter("test", Some("v0.1.0"), None);
let counter = meter.f64_counter("counter").init();

let attributes = vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")];

counter.add(&cx, 10.0, &attributes);
counter.add(&cx, 5.3, &attributes);

expected.push(r#"counter_total{A="B",C="D",R="V"} 15.3"#);
expected.push(r#"counter_total{A="B",C="D",R="V",otel_scope_name="test",otel_scope_version="v0.1.0"} 15.3"#);
expected.push(r#"otel_scope_info{otel_scope_name="test",otel_scope_version="v0.1.0"} 1"#);
}
// Standard export
compare_export(&exporter, expected.clone());
Expand All @@ -49,7 +50,8 @@ fn test_add() {
))
.with_resource(Resource::new(vec![KeyValue::new("R", "V")]))
.build();
let exporter = opentelemetry_prometheus::exporter(controller).init();
let exporter = opentelemetry_prometheus::exporter(controller)
.with_config(ExporterConfig::default().disable_scope_info()).init();

let meter = exporter
.meter_provider()
Expand Down Expand Up @@ -108,7 +110,8 @@ fn test_sanitization() {
"Test Service",
)]))
.build();
let exporter = opentelemetry_prometheus::exporter(controller).init();
let exporter = opentelemetry_prometheus::exporter(controller)
.with_config(ExporterConfig::default().disable_scope_info()).init();
let meter = exporter
.meter_provider()
.unwrap()
Expand All @@ -134,6 +137,64 @@ fn test_sanitization() {
compare_export(&exporter, expected)
}


#[test]
fn test_scope_info() {
let cx = Context::new();
let controller = controllers::basic(processors::factory(
selectors::simple::histogram(vec![-0.5, 1.0]),
aggregation::cumulative_temporality_selector(),
))
.with_resource(Resource::new(vec![KeyValue::new("R", "V")]))
.build();
let exporter = opentelemetry_prometheus::exporter(controller).init();

let meter = exporter
.meter_provider()
.unwrap()
.versioned_meter("test", Some("v0.1.0"), None);

let up_down_counter = meter.f64_up_down_counter("updowncounter").init();
let counter = meter.f64_counter("counter").init();
let histogram = meter.f64_histogram("my.histogram").init();

let attributes = vec![KeyValue::new("A", "B"), KeyValue::new("C", "D")];

let mut expected = Vec::new();

counter.add(&cx, 10.0, &attributes);
counter.add(&cx, 5.3, &attributes);

expected.push(r#"counter_total{A="B",C="D",R="V",otel_scope_name="test",otel_scope_version="v0.1.0"} 15.3"#);

let cb_attributes = attributes.clone();
let gauge = meter.i64_observable_gauge("intgauge").init();
meter
.register_callback(move |cx| gauge.observe(cx, 1, cb_attributes.as_ref()))
.unwrap();

expected.push(r#"intgauge{A="B",C="D",R="V",otel_scope_name="test",otel_scope_version="v0.1.0"} 1"#);

histogram.record(&cx, -0.6, &attributes);
histogram.record(&cx, -0.4, &attributes);
histogram.record(&cx, 0.6, &attributes);
histogram.record(&cx, 20.0, &attributes);

expected.push(r#"my_histogram_bucket{A="B",C="D",R="V",otel_scope_name="test",otel_scope_version="v0.1.0",le="+Inf"} 4"#);
expected.push(r#"my_histogram_bucket{A="B",C="D",R="V",otel_scope_name="test",otel_scope_version="v0.1.0",le="-0.5"} 1"#);
expected.push(r#"my_histogram_bucket{A="B",C="D",R="V",otel_scope_name="test",otel_scope_version="v0.1.0",le="1"} 3"#);
expected.push(r#"my_histogram_count{A="B",C="D",R="V",otel_scope_name="test",otel_scope_version="v0.1.0"} 4"#);
expected.push(r#"my_histogram_sum{A="B",C="D",R="V",otel_scope_name="test",otel_scope_version="v0.1.0"} 19.6"#);

up_down_counter.add(&cx, 10.0, &attributes);
up_down_counter.add(&cx, -3.2, &attributes);

expected.push(r#"updowncounter{A="B",C="D",R="V",otel_scope_name="test",otel_scope_version="v0.1.0"} 6.8"#);
expected.push(r#"otel_scope_info{otel_scope_name="test",otel_scope_version="v0.1.0"} 1"#);

compare_export(&exporter, expected)
}

fn compare_export(exporter: &PrometheusExporter, mut expected: Vec<&'static str>) {
let mut output = Vec::new();
let encoder = TextEncoder::new();
Expand Down