opentelemetry_otlp/
metric.rs#[cfg(any(feature = "http-proto", feature = "http-json", feature = "grpc-tonic"))]
use crate::HasExportConfig;
#[cfg(any(feature = "http-proto", feature = "http-json"))]
use crate::{exporter::http::HttpExporterBuilder, HasHttpConfig, HttpExporterBuilderSet};
#[cfg(feature = "grpc-tonic")]
use crate::{exporter::tonic::TonicExporterBuilder, HasTonicConfig, TonicExporterBuilderSet};
use crate::NoExporterBuilderSet;
use async_trait::async_trait;
use core::fmt;
use opentelemetry_sdk::metrics::MetricResult;
use opentelemetry_sdk::metrics::{
data::ResourceMetrics, exporter::PushMetricExporter, Temporality,
};
use std::fmt::{Debug, Formatter};
pub const OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT";
pub const OTEL_EXPORTER_OTLP_METRICS_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_METRICS_TIMEOUT";
pub const OTEL_EXPORTER_OTLP_METRICS_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_METRICS_COMPRESSION";
pub const OTEL_EXPORTER_OTLP_METRICS_HEADERS: &str = "OTEL_EXPORTER_OTLP_METRICS_HEADERS";
#[derive(Debug, Default, Clone)]
pub struct MetricExporterBuilder<C> {
client: C,
temporality: Temporality,
}
impl MetricExporterBuilder<NoExporterBuilderSet> {
pub fn new() -> Self {
MetricExporterBuilder::default()
}
}
impl<C> MetricExporterBuilder<C> {
#[cfg(feature = "grpc-tonic")]
pub fn with_tonic(self) -> MetricExporterBuilder<TonicExporterBuilderSet> {
MetricExporterBuilder {
client: TonicExporterBuilderSet(TonicExporterBuilder::default()),
temporality: self.temporality,
}
}
#[cfg(any(feature = "http-proto", feature = "http-json"))]
pub fn with_http(self) -> MetricExporterBuilder<HttpExporterBuilderSet> {
MetricExporterBuilder {
client: HttpExporterBuilderSet(HttpExporterBuilder::default()),
temporality: self.temporality,
}
}
pub fn with_temporality(self, temporality: Temporality) -> MetricExporterBuilder<C> {
MetricExporterBuilder {
client: self.client,
temporality,
}
}
}
#[cfg(feature = "grpc-tonic")]
impl MetricExporterBuilder<TonicExporterBuilderSet> {
pub fn build(self) -> MetricResult<MetricExporter> {
let exporter = self.client.0.build_metrics_exporter(self.temporality)?;
Ok(exporter)
}
}
#[cfg(any(feature = "http-proto", feature = "http-json"))]
impl MetricExporterBuilder<HttpExporterBuilderSet> {
pub fn build(self) -> MetricResult<MetricExporter> {
let exporter = self.client.0.build_metrics_exporter(self.temporality)?;
Ok(exporter)
}
}
#[cfg(feature = "grpc-tonic")]
impl HasExportConfig for MetricExporterBuilder<TonicExporterBuilderSet> {
fn export_config(&mut self) -> &mut crate::ExportConfig {
&mut self.client.0.exporter_config
}
}
#[cfg(any(feature = "http-proto", feature = "http-json"))]
impl HasExportConfig for MetricExporterBuilder<HttpExporterBuilderSet> {
fn export_config(&mut self) -> &mut crate::ExportConfig {
&mut self.client.0.exporter_config
}
}
#[cfg(feature = "grpc-tonic")]
impl HasTonicConfig for MetricExporterBuilder<TonicExporterBuilderSet> {
fn tonic_config(&mut self) -> &mut crate::TonicConfig {
&mut self.client.0.tonic_config
}
}
#[cfg(any(feature = "http-proto", feature = "http-json"))]
impl HasHttpConfig for MetricExporterBuilder<HttpExporterBuilderSet> {
fn http_client_config(&mut self) -> &mut crate::exporter::http::HttpConfig {
&mut self.client.0.http_config
}
}
#[async_trait]
pub trait MetricsClient: fmt::Debug + Send + Sync + 'static {
async fn export(&self, metrics: &mut ResourceMetrics) -> MetricResult<()>;
fn shutdown(&self) -> MetricResult<()>;
}
pub struct MetricExporter {
client: Box<dyn MetricsClient>,
temporality: Temporality,
}
impl Debug for MetricExporter {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MetricExporter").finish()
}
}
#[async_trait]
impl PushMetricExporter for MetricExporter {
async fn export(&self, metrics: &mut ResourceMetrics) -> MetricResult<()> {
self.client.export(metrics).await
}
async fn force_flush(&self) -> MetricResult<()> {
Ok(())
}
fn shutdown(&self) -> MetricResult<()> {
self.client.shutdown()
}
fn temporality(&self) -> Temporality {
self.temporality
}
}
impl MetricExporter {
pub fn builder() -> MetricExporterBuilder<NoExporterBuilderSet> {
MetricExporterBuilder::default()
}
pub fn new(client: impl MetricsClient, temporality: Temporality) -> MetricExporter {
MetricExporter {
client: Box::new(client),
temporality,
}
}
}