Skip to content

Commit 2e55e5b

Browse files
committed
feat: Add configuration option for metric temporality
1 parent a33d5c0 commit 2e55e5b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

crates/apollo-mcp-server/src/runtime/telemetry.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::runtime::telemetry::sampler::SamplerOption;
77
use apollo_mcp_server::generated::telemetry::TelemetryAttribute;
88
use opentelemetry::{Key, KeyValue, global, trace::TracerProvider as _};
99
use opentelemetry_otlp::WithExportConfig;
10-
use opentelemetry_sdk::metrics::{Instrument, Stream};
10+
use opentelemetry_sdk::metrics::{Instrument, Stream, Temporality};
1111
use opentelemetry_sdk::{
1212
Resource,
1313
metrics::{MeterProviderBuilder, PeriodicReader, SdkMeterProvider},
@@ -44,17 +44,34 @@ pub struct MetricsExporters {
4444
omitted_attributes: Option<HashSet<TelemetryAttribute>>,
4545
}
4646

47+
#[derive(Debug, Deserialize, JsonSchema)]
48+
pub enum MetricTemporality {
49+
Cumulative,
50+
Delta,
51+
}
52+
53+
impl MetricTemporality {
54+
pub fn to_sdk(&self) -> Temporality {
55+
match self {
56+
MetricTemporality::Cumulative => Temporality::Cumulative,
57+
MetricTemporality::Delta => Temporality::Delta,
58+
}
59+
}
60+
}
61+
4762
#[derive(Debug, Deserialize, JsonSchema)]
4863
pub struct OTLPMetricExporter {
4964
endpoint: String,
5065
protocol: String,
66+
temporality: MetricTemporality,
5167
}
5268

5369
impl Default for OTLPMetricExporter {
5470
fn default() -> Self {
5571
Self {
5672
endpoint: "http://localhost:4317".into(),
5773
protocol: "grpc".into(),
74+
temporality: MetricTemporality::Cumulative,
5875
}
5976
}
6077
}
@@ -122,10 +139,12 @@ fn init_meter_provider(telemetry: &Telemetry) -> Result<SdkMeterProvider, anyhow
122139
"grpc" => opentelemetry_otlp::MetricExporter::builder()
123140
.with_tonic()
124141
.with_endpoint(otlp.endpoint.clone())
142+
.with_temporality(otlp.temporality.to_sdk())
125143
.build()?,
126144
"http/protobuf" => opentelemetry_otlp::MetricExporter::builder()
127145
.with_http()
128146
.with_endpoint(otlp.endpoint.clone())
147+
.with_temporality(otlp.temporality.to_sdk())
129148
.build()?,
130149
other => {
131150
return Err(anyhow::anyhow!(
@@ -331,6 +350,7 @@ mod tests {
331350
otlp: Some(OTLPMetricExporter {
332351
protocol: "bogus".to_string(),
333352
endpoint: "http://localhost:4317".to_string(),
353+
temporality: MetricTemporality::Cumulative,
334354
}),
335355
omitted_attributes: None,
336356
}),
@@ -354,6 +374,7 @@ mod tests {
354374
otlp: Some(OTLPMetricExporter {
355375
protocol: "http/protobuf".to_string(),
356376
endpoint: "http://localhost:4318/v1/metrics".to_string(),
377+
temporality: MetricTemporality::Delta,
357378
}),
358379
omitted_attributes: None,
359380
}),

0 commit comments

Comments
 (0)