use crate::client::Channel;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
macro_rules! metrics {
(
$pub_container:ty {
$field:ident: $internal_container:ident {
$(
$(#[$meta:meta])*
$metric:ident: $ty:ident -> $pub_ty:ident,
)*
}
}
) => {
#[derive(Default)]
pub(crate) struct $internal_container {
$(
$(#[$meta])*
pub(crate) $metric: $ty,
)*
}
impl $internal_container {
pub fn sample(&self) -> impl Iterator<Item = emit::metric::Metric<'static, emit::empty::Empty>> + 'static {
let $internal_container { $($metric),* } = self;
[$(
emit::metric::Metric::new(
emit::pkg!(),
stringify!($metric),
<$ty>::AGG,
emit::empty::Empty,
$metric.sample(),
emit::empty::Empty,
),
)*]
.into_iter()
}
}
impl $pub_container {
$(
$(#[$meta])*
pub fn $metric(&self) -> $pub_ty {
self.$field.$metric.sample()
}
)*
}
};
}
#[derive(Default)]
pub(crate) struct Counter(AtomicUsize);
impl Counter {
const AGG: &'static str = emit::well_known::METRIC_AGG_COUNT;
pub fn increment(&self) {
self.increment_by(1);
}
pub fn increment_by(&self, by: usize) {
self.0.fetch_add(by, Ordering::Relaxed);
}
pub fn sample(&self) -> usize {
self.0.load(Ordering::Relaxed)
}
}
metrics!(
OtlpMetrics {
metrics: InternalMetrics {
event_discarded: Counter -> usize,
transport_conn_established: Counter -> usize,
transport_conn_failed: Counter -> usize,
transport_conn_tls_handshake: Counter -> usize,
transport_conn_tls_failed: Counter -> usize,
transport_request_sent: Counter -> usize,
transport_request_failed: Counter -> usize,
transport_request_compress_gzip: Counter -> usize,
http_batch_sent: Counter -> usize,
http_batch_failed: Counter -> usize,
grpc_batch_sent: Counter -> usize,
grpc_batch_failed: Counter -> usize,
configuration_failed: Counter -> usize,
}
}
);
pub struct OtlpMetrics {
pub(crate) logs_channel_metrics: Option<emit_batcher::ChannelMetrics<Channel>>,
pub(crate) traces_channel_metrics: Option<emit_batcher::ChannelMetrics<Channel>>,
pub(crate) metrics_channel_metrics: Option<emit_batcher::ChannelMetrics<Channel>>,
pub(crate) metrics: Arc<InternalMetrics>,
}
impl emit::metric::Source for OtlpMetrics {
fn sample_metrics<S: emit::metric::sampler::Sampler>(&self, sampler: S) {
if let Some(ref channel_metrics) = self.logs_channel_metrics {
channel_metrics.sample_metrics(emit::metric::sampler::from_fn(|metric| {
sampler.metric(
metric
.by_ref()
.with_name(&format!("otlp_logs_{}", metric.name()))
.with_mdl(emit::pkg!().append(emit::path!("logs_channel"))),
);
}));
}
if let Some(ref channel_metrics) = self.traces_channel_metrics {
channel_metrics.sample_metrics(emit::metric::sampler::from_fn(|metric| {
sampler.metric(
metric
.by_ref()
.with_name(&format!("otlp_traces_{}", metric.name()))
.with_mdl(emit::pkg!().append(emit::path!("traces_channel"))),
);
}));
}
if let Some(ref channel_metrics) = self.metrics_channel_metrics {
channel_metrics.sample_metrics(emit::metric::sampler::from_fn(|metric| {
sampler.metric(
metric
.by_ref()
.with_name(&format!("otlp_metrics_{}", metric.name()))
.with_mdl(emit::pkg!().append(emit::path!("metrics_channel"))),
);
}));
}
for metric in self.metrics.sample() {
sampler.metric(metric);
}
}
}