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: upgrade otel #17

Merged
merged 3 commits into from
Apr 16, 2024
Merged
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
5 changes: 3 additions & 2 deletions crates/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ edition = "2021"
[dependencies]
pin-project = "1"
prometheus = "0.13"
opentelemetry = { version = "0.19", features = ["metrics", "rt-tokio"] }
opentelemetry-prometheus = "0.12"
opentelemetry = { version = "0.22.0", features = ["metrics"] }
opentelemetry_sdk = { version = "0.22.1", features = ["metrics", "rt-tokio"] }
opentelemetry-prometheus = "0.15"
once_cell = "1.17"
smallvec = "1.11"
47 changes: 16 additions & 31 deletions crates/metrics/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
pub use {future::*, once_cell::sync::Lazy, opentelemetry as otel, task::*};
use {
opentelemetry::{
metrics::{Meter, MeterProvider},
sdk::{
export::metrics::aggregation,
metrics::{processors, selectors},
},
},
opentelemetry_prometheus::PrometheusExporter,
prometheus::{Error as PrometheusError, TextEncoder},
opentelemetry_sdk::metrics::SdkMeterProvider,
otel::metrics::{Meter, MeterProvider},
prometheus::{Error as PrometheusError, Registry, TextEncoder},
std::{
sync::{Arc, Mutex},
time::Duration,
Expand All @@ -26,35 +20,26 @@ static SERVICE_NAME: Mutex<Option<&str>> = Mutex::new(None);
static METRICS_CORE: Lazy<Arc<ServiceMetrics>> = Lazy::new(|| {
let service_name = SERVICE_NAME.lock().unwrap().unwrap_or(DEFAULT_SERVICE_NAME);

let controller = otel::sdk::metrics::controllers::basic(processors::factory(
selectors::simple::histogram(vec![]),
aggregation::cumulative_temporality_selector(),
))
.with_resource(otel::sdk::Resource::new(vec![otel::KeyValue::new(
"service_name",
service_name,
)]))
.build();

let prometheus_exporter = opentelemetry_prometheus::exporter(controller).init();
let meter = prometheus_exporter
.meter_provider()
.unwrap()
.meter(service_name);

Arc::new(ServiceMetrics {
meter,
prometheus_exporter,
})
let registry = Registry::new();
let prometheus_exporter = opentelemetry_prometheus::exporter()
.with_registry(registry.clone())
.build()
.unwrap();
let provider = SdkMeterProvider::builder()
.with_reader(prometheus_exporter)
.build();
let meter = provider.meter(service_name);

Arc::new(ServiceMetrics { registry, meter })
});

/// Global application metrics access.
///
/// The main functionality is to provide global access to opentelemetry's
/// [`Meter`].
pub struct ServiceMetrics {
registry: Registry,
meter: Meter,
prometheus_exporter: PrometheusExporter,
}

impl ServiceMetrics {
Expand All @@ -81,7 +66,7 @@ impl ServiceMetrics {

/// Generates export data in Prometheus format, serialized into string.
pub fn export() -> Result<String, PrometheusError> {
let data = Self::get().prometheus_exporter.registry().gather();
let data = Self::get().registry.gather();
TextEncoder::new().encode_to_string(&data)
}

Expand Down
6 changes: 3 additions & 3 deletions crates/metrics/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ macro_rules! gauge {
}};

($name:expr, $value:expr, $tags:expr) => {{
$crate::gauge!($name).observe(&$crate::otel::Context::new(), $value as u64, $tags);
$crate::gauge!($name).observe($value as u64, $tags);
}};
}

Expand All @@ -39,7 +39,7 @@ macro_rules! histogram {
}};

($name:expr, $value:expr, $tags:expr) => {{
$crate::histogram!($name).record(&$crate::otel::Context::new(), $value as f64, $tags);
$crate::histogram!($name).record($value as f64, $tags);
}};
}

Expand All @@ -59,6 +59,6 @@ macro_rules! counter {
}};

($name:expr, $value:expr, $tags:expr) => {{
$crate::counter!($name).add(&$crate::otel::Context::new(), $value as u64, $tags);
$crate::counter!($name).add($value as u64, $tags);
}};
}
18 changes: 5 additions & 13 deletions crates/metrics/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ impl OtelTaskMetricsRecorder {

impl TaskMetricsRecorder for OtelTaskMetricsRecorder {
fn record_task_started(&self) {
self.inner
.tasks_started
.add(&otel::Context::new(), 1, &self.combine_attributes());
self.inner.tasks_started.add(1, &self.combine_attributes());
}

fn record_task_finished(
Expand All @@ -117,18 +115,12 @@ impl TaskMetricsRecorder for OtelTaskMetricsRecorder {
let mut attrs = self.combine_attributes();
attrs.push(otel::KeyValue::new("completed", completed));

let ctx = otel::Context::new();

self.inner
.total_duration
.record(&ctx, total_duration_ms, &attrs);
self.inner.total_duration.record(total_duration_ms, &attrs);

self.inner
.poll_duration
.record(&ctx, poll_duration_ms, &attrs);
self.inner.poll_duration.record(poll_duration_ms, &attrs);

self.inner.poll_entries.add(&ctx, poll_entries, &attrs);
self.inner.tasks_finished.add(&ctx, 1, &attrs);
self.inner.poll_entries.add(poll_entries, &attrs);
self.inner.tasks_finished.add(1, &attrs);
}
}

Expand Down
Loading