Skip to content

Commit e4ef2a9

Browse files
authored
New metrics SDK (#1000)
This patch updates the metrics SDK to the latest spec. The following breaking changes are introduced. Metrics API changes: * Move `AttributeSet` to SDK as it's not mentioned in the spec or used in the api * Consolidate `AsyncCounter`, `AsyncUpDownCounter`, and `AsyncGauge` into `AsyncInstrument` trait and add downcasting for observer callbacks. * Add `AsyncInstrumentBuilder` to allow per-instrument callback configuration. * Allow metric `name` and `description` fields to be `Cow<'static, str>` * Warn on metric misconfiguration when using instrument builder `init` rather than returning error * Update `Meter::register_callback` to take a list of async instruments and validate they are registered in the callback through the associated `Observer` * Allow registered callbacks to be unregistered. Metrics SDK changes: * Introduce `Scope` as type alias for `InstrumentationLibrary` * Update `Aggregation` to match aggregation spec * Refactor `BasicController` to spec compliant `ManualReader` * Refactor `PushController` to spec compliant `PeriodicReader` * Update metric data fields to match spec, including exemplars. * Split `MetricsExporter` into `Reader`s and `PushMetricExporter`s * Add `View` implementation * Remove `AtomicNumber`s * Refactor `Processor`s into `Pipeline` Metrics exporter changes: * Update otlp exporter to match new metrics data * Update otlp exporter configuration to allow aggregation and temporality selectors to be optional. * Update prometheus exporter to match new metrics data Example changes: * Update otlp metrics and prometheus examples. * Remove basic example as we should be focusing on the OTLP variants
1 parent 879d6ff commit e4ef2a9

File tree

123 files changed

+7150
-6797
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+7150
-6797
lines changed

.github/codecov.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ coverage:
1212
threshold: 0.5%
1313

1414
ignore:
15+
- "examples"
1516
- "opentelemetry/src/testing" # test harnesses
1617
- "opentelemetry-jaeger/src/testing" # test harness
1718
- "opentelemetry-jaeger/src/exporter/thrift" # auto generated files
1819
- "opentelemetry-otlp/src/proto" # auto generated files
20+
- "opentelemetry-proto/src/proto" # auto generated files

.github/workflows/ci.yml

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,23 @@ jobs:
107107
continue-on-error: true
108108
runs-on: ubuntu-latest
109109
steps:
110-
- uses: actions/checkout@v1
111-
with:
112-
submodules: true
113-
- uses: actions-rs/toolchain@v1
114-
with:
115-
toolchain: nightly
116-
components: rustfmt
117-
override: true
118-
- uses: actions-rs/cargo@v1
119-
with:
120-
command: test
121-
args: -p opentelemetry -p opentelemetry_api -p opentelemetry_sdk -p opentelemetry-aws -p opentelemetry-jaeger -p opentelemetry-datadog -p opentelemetry-dynatrace -p opentelemetry-zipkin --all-features --no-fail-fast
122-
env:
123-
CARGO_INCREMENTAL: '0'
124-
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
125-
RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
126-
- uses: actions-rs/grcov@v0.1
127-
id: coverage
128-
- uses: codecov/codecov-action@v1
129-
with:
130-
file: ${{ steps.coverage.outputs.report }}
110+
- uses: actions/checkout@v3
111+
with:
112+
submodules: true
113+
- uses: actions-rs/toolchain@v1
114+
with:
115+
toolchain: stable
116+
components: rustfmt,llvm-tools-preview
117+
override: true
118+
- uses: arduino/setup-protoc@v1
119+
- name: cargo install cargo-llvm-cov
120+
uses: taiki-e/install-action@cargo-llvm-cov
121+
- name: cargo generate-lockfile
122+
if: hashFiles('Cargo.lock') == ''
123+
run: cargo generate-lockfile
124+
- name: cargo llvm-cov
125+
run: cargo llvm-cov --locked --all-features --workspace --lcov --output-path lcov.info
126+
- name: Upload to codecov.io
127+
uses: codecov/codecov-action@v3
128+
with:
129+
fail_ci_if_error: true

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ members = [
2222
"examples/actix-udp",
2323
"examples/async",
2424
"examples/aws-xray",
25-
"examples/basic",
2625
"examples/basic-otlp",
2726
"examples/basic-otlp-http",
2827
"examples/datadog",

examples/basic-otlp-http/src/main.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use opentelemetry::{
66
};
77
use opentelemetry_otlp::WithExportConfig;
88
use std::error::Error;
9-
use std::time::Duration;
109

1110
fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
1211
opentelemetry_otlp::new_pipeline()
@@ -44,9 +43,6 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
4443
});
4544
});
4645

47-
// wait for 1 minutes so that we could see metrics being pushed via OTLP every 10 seconds.
48-
tokio::time::sleep(Duration::from_secs(60)).await;
49-
5046
global::shutdown_tracer_provider();
5147

5248
Ok(())

examples/basic-otlp/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ publish = false
66

77
[dependencies]
88
futures-util = { version = "0.3", default-features = false, features = ["std"] }
9-
lazy_static = "1.4"
10-
opentelemetry = { path = "../../opentelemetry", features = ["rt-tokio", "metrics"] }
9+
once_cell = "1.17"
10+
opentelemetry_api = { path = "../../opentelemetry-api", features = ["metrics"] }
11+
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["rt-tokio"] }
1112
opentelemetry-otlp = { path = "../../opentelemetry-otlp", features = ["tonic", "metrics"] }
1213
opentelemetry-semantic-conventions = { path = "../../opentelemetry-semantic-conventions" }
1314
serde_json = "1.0"

examples/basic-otlp/src/main.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
use opentelemetry::global::shutdown_tracer_provider;
2-
use opentelemetry::runtime;
3-
use opentelemetry::sdk::export::metrics::aggregation::cumulative_temporality_selector;
4-
use opentelemetry::sdk::metrics::controllers::BasicController;
5-
use opentelemetry::sdk::metrics::selectors;
6-
use opentelemetry::sdk::Resource;
7-
use opentelemetry::trace::TraceError;
8-
use opentelemetry::{global, sdk::trace as sdktrace};
9-
use opentelemetry::{
1+
use once_cell::sync::Lazy;
2+
use opentelemetry_api::global;
3+
use opentelemetry_api::global::shutdown_tracer_provider;
4+
use opentelemetry_api::trace::TraceError;
5+
use opentelemetry_api::{
106
metrics,
117
trace::{TraceContextExt, Tracer},
128
Context, Key, KeyValue,
139
};
1410
use opentelemetry_otlp::{ExportConfig, WithExportConfig};
11+
use opentelemetry_sdk::{metrics::MeterProvider, runtime, trace as sdktrace, Resource};
1512
use std::error::Error;
16-
use std::time::Duration;
1713

1814
fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
1915
opentelemetry_otlp::new_pipeline()
@@ -29,20 +25,16 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
2925
"trace-demo",
3026
)])),
3127
)
32-
.install_batch(opentelemetry::runtime::Tokio)
28+
.install_batch(runtime::Tokio)
3329
}
3430

35-
fn init_metrics() -> metrics::Result<BasicController> {
31+
fn init_metrics() -> metrics::Result<MeterProvider> {
3632
let export_config = ExportConfig {
3733
endpoint: "http://localhost:4317".to_string(),
3834
..ExportConfig::default()
3935
};
4036
opentelemetry_otlp::new_pipeline()
41-
.metrics(
42-
selectors::simple::inexpensive(),
43-
cumulative_temporality_selector(),
44-
runtime::Tokio,
45-
)
37+
.metrics(runtime::Tokio)
4638
.with_exporter(
4739
opentelemetry_otlp::new_exporter()
4840
.tonic()
@@ -54,22 +46,22 @@ fn init_metrics() -> metrics::Result<BasicController> {
5446
const LEMONS_KEY: Key = Key::from_static_str("lemons");
5547
const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another");
5648

57-
lazy_static::lazy_static! {
58-
static ref COMMON_ATTRIBUTES: [KeyValue; 4] = [
49+
static COMMON_ATTRIBUTES: Lazy<[KeyValue; 4]> = Lazy::new(|| {
50+
[
5951
LEMONS_KEY.i64(10),
6052
KeyValue::new("A", "1"),
6153
KeyValue::new("B", "2"),
6254
KeyValue::new("C", "3"),
63-
];
64-
}
55+
]
56+
});
6557

6658
#[tokio::main]
6759
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
6860
// By binding the result to an unused variable, the lifetime of the variable
6961
// matches the containing block, reporting traces and metrics during the whole
7062
// execution.
7163
let _ = init_tracer()?;
72-
let metrics_controller = init_metrics()?;
64+
let meter_provider = init_metrics()?;
7365
let cx = Context::new();
7466

7567
let tracer = global::tracer("ex.com/basic");
@@ -79,7 +71,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
7971
.f64_observable_gauge("ex.com.one")
8072
.with_description("A gauge set to 1.0")
8173
.init();
82-
meter.register_callback(move |cx| gauge.observe(cx, 1.0, COMMON_ATTRIBUTES.as_ref()))?;
74+
75+
meter.register_callback(&[gauge.as_any()], move |observer| {
76+
observer.observe_f64(&gauge, 1.0, COMMON_ATTRIBUTES.as_ref())
77+
})?;
8378

8479
let histogram = meter.f64_histogram("ex.com.two").init();
8580
histogram.record(&cx, 5.5, COMMON_ATTRIBUTES.as_ref());
@@ -102,11 +97,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
10297
});
10398
});
10499

105-
// wait for 1 minutes so that we could see metrics being pushed via OTLP every 10 seconds.
106-
tokio::time::sleep(Duration::from_secs(60)).await;
107-
108100
shutdown_tracer_provider();
109-
metrics_controller.stop(&cx)?;
101+
meter_provider.shutdown()?;
110102

111103
Ok(())
112104
}

examples/basic/Cargo.toml

Lines changed: 0 additions & 13 deletions
This file was deleted.

examples/basic/README.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

examples/basic/src/main.rs

Lines changed: 0 additions & 109 deletions
This file was deleted.

examples/basic/trace.png

-150 KB
Binary file not shown.

0 commit comments

Comments
 (0)