Skip to content

Commit

Permalink
Add few internal logs for Metrics sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed Nov 26, 2024
1 parent c9388e4 commit f751ec0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
16 changes: 11 additions & 5 deletions examples/self-diagnostics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
let reader = PeriodicReader::builder(exporter, opentelemetry_sdk::runtime::Tokio).build();

let provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder()
.with_resource(opentelemetry_sdk::resource::Resource::new(vec![
KeyValue::new("service.name", "self-diagnostics"),
]))
.with_reader(reader)
.build();

Expand All @@ -26,7 +29,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// OpenTelemetry uses `tracing` crate for its internal logging. Unless a
// tracing subscriber is set, the logs will be discarded. In this example,
// we configure a `tracing` subscriber to:
// 1. Print logs of level INFO or higher to stdout using tracing's fmt layer.
// 1. Print logs of level DEBUG or higher to stdout using tracing's fmt layer.
// 2. Filter logs from OpenTelemetry's dependencies (like tonic, hyper,
// reqwest etc. which are commonly used by the OTLP exporter) to only print
// ERROR-level logs. This filtering helps reduce repetitive log messages
Expand All @@ -39,7 +42,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// Hence, one may use "add_directive("opentelemetry=off".parse().unwrap())"
// to turn off all logs from OpenTelemetry.

let filter = EnvFilter::new("info")
let filter = EnvFilter::new("debug")
.add_directive("hyper=error".parse().unwrap())
.add_directive("tonic=error".parse().unwrap())
.add_directive("h2=error".parse().unwrap())
Expand All @@ -54,11 +57,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
info!("Starting self-diagnostics example");

let meter = global::meter("example");
// Create a counter using an invalid name to trigger
// internal log about the same.
let counter = meter.u64_counter("my_counter with_space").build();
let counter = meter.u64_counter("my_counter").build();
counter.add(10, &[KeyValue::new("key", "value")]);

let _observable_counter = meter
.u64_observable_counter("my_observable_counter")
.with_callback(|observer| observer.observe(10, &[KeyValue::new("key", "value")]))
.build();

meter_provider.shutdown()?;
info!("Shutdown complete. Bye!");
Ok(())
Expand Down
35 changes: 28 additions & 7 deletions opentelemetry-sdk/src/metrics/meter_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ impl MeterProvider for SdkMeterProvider {

fn meter_with_scope(&self, scope: InstrumentationScope) -> Meter {
if self.inner.is_shutdown.load(Ordering::Relaxed) {
otel_debug!(

Check warning on line 177 in opentelemetry-sdk/src/metrics/meter_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/meter_provider.rs#L177

Added line #L177 was not covered by tests
name: "MeterProvider.NoOpMeterReturned",
meter_name = scope.name(),

Check warning on line 179 in opentelemetry-sdk/src/metrics/meter_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/meter_provider.rs#L179

Added line #L179 was not covered by tests
);
return Meter::new(Arc::new(NoopMeter::new()));
}

Expand All @@ -182,14 +186,26 @@ impl MeterProvider for SdkMeterProvider {
};

if let Ok(mut meters) = self.inner.meters.lock() {
let meter = meters
.entry(scope)
.or_insert_with_key(|scope| {
Arc::new(SdkMeter::new(scope.clone(), self.inner.pipes.clone()))
})
.clone();
Meter::new(meter)
if let Some(existing_meter) = meters.get(&scope) {
otel_debug!(
name: "MeterProvider.ExistingMeterReturned",
meter_name = scope.name(),

Check warning on line 192 in opentelemetry-sdk/src/metrics/meter_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/meter_provider.rs#L192

Added line #L192 was not covered by tests
);
Meter::new(existing_meter.clone())
} else {
let new_meter = Arc::new(SdkMeter::new(scope.clone(), self.inner.pipes.clone()));
meters.insert(scope.clone(), new_meter.clone());
otel_debug!(
name: "MeterProvider.NewMeterCreated",
meter_name = scope.name(),

Check warning on line 200 in opentelemetry-sdk/src/metrics/meter_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/meter_provider.rs#L200

Added line #L200 was not covered by tests
);
Meter::new(new_meter)
}
} else {
otel_debug!(

Check warning on line 205 in opentelemetry-sdk/src/metrics/meter_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/meter_provider.rs#L205

Added line #L205 was not covered by tests
name: "MeterProvider.NoOpMeterReturned",
meter_name = scope.name(),

Check warning on line 207 in opentelemetry-sdk/src/metrics/meter_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/meter_provider.rs#L207

Added line #L207 was not covered by tests
);
Meter::new(Arc::new(NoopMeter::new()))
}
}
Expand Down Expand Up @@ -242,6 +258,11 @@ impl MeterProviderBuilder {
/// Construct a new [MeterProvider] with this configuration.

pub fn build(self) -> SdkMeterProvider {
otel_debug!(
name: "MeterProvider.Building",
builder = format!("{:?}", &self),

Check warning on line 263 in opentelemetry-sdk/src/metrics/meter_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/meter_provider.rs#L263

Added line #L263 was not covered by tests
);

let meter_provider = SdkMeterProvider {
inner: Arc::new(SdkMeterProviderInner {
pipes: Arc::new(Pipelines::new(
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/src/metrics/periodic_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ where
name: "PeriodicReader.BuildCompleted",
message = "Periodic reader built.",
interval_in_secs = self.interval.as_secs(),
temporality = format!("{:?}", self.exporter.temporality()),

Check warning on line 135 in opentelemetry-sdk/src/metrics/periodic_reader.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/periodic_reader.rs#L135

Added line #L135 was not covered by tests
);

PeriodicReader {
Expand Down
8 changes: 8 additions & 0 deletions opentelemetry-sdk/src/metrics/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ impl Pipeline {
/// unique values.
fn add_sync(&self, scope: InstrumentationScope, i_sync: InstrumentSync) {
let _ = self.inner.lock().map(|mut inner| {
otel_debug!(
name : "InstrumentCreated",
instrument_name = i_sync.name.as_ref(),

Check warning on line 77 in opentelemetry-sdk/src/metrics/pipeline.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/pipeline.rs#L77

Added line #L77 was not covered by tests
);
inner.aggregations.entry(scope).or_default().push(i_sync);
});
}
Expand Down Expand Up @@ -99,6 +103,10 @@ impl SdkProducer for Pipeline {
/// Returns aggregated metrics from a single collection.
fn produce(&self, rm: &mut ResourceMetrics) -> MetricResult<()> {
let inner = self.inner.lock()?;
otel_debug!(
name: "MeterProviderInvokingObservableCallbacks",
count = inner.callbacks.len(),

Check warning on line 108 in opentelemetry-sdk/src/metrics/pipeline.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/pipeline.rs#L108

Added line #L108 was not covered by tests
);
for cb in &inner.callbacks {
// TODO consider parallel callbacks.
cb();
Expand Down

0 comments on commit f751ec0

Please sign in to comment.