Skip to content

feat: add methods to get Resource set on in memory exporters #3070

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions opentelemetry-sdk/src/logs/in_memory_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ impl InMemoryLogExporter {
.map(|mut logs_guard| logs_guard.clear())
.map_err(|e| OTelSdkError::InternalFailure(format!("Failed to reset logs: {e}")));
}

/// Returns a clone of the current resource set for the exporter.
pub fn resource(&self) -> Resource {
self.resource
.lock()
.map(|res_guard| res_guard.clone())
.expect("Resource lock poisoned")
}
}

impl LogExporter for InMemoryLogExporter {
Expand Down Expand Up @@ -220,3 +228,23 @@ impl LogExporter for InMemoryLogExporter {
*res_guard = resource.clone();
}
}

#[cfg(test)]
mod tests {
use opentelemetry::KeyValue;

use super::*;

#[test]
fn test_in_memory_log_exporter_resource() {
let mut exporter = InMemoryLogExporter::default();
let custom_resource = Resource::builder()
.with_attribute(KeyValue::new("key", "value"))
.build();

assert_ne!(exporter.resource(), custom_resource);

exporter.set_resource(&custom_resource);
assert_eq!(exporter.resource(), custom_resource);
}
}
28 changes: 28 additions & 0 deletions opentelemetry-sdk/src/trace/in_memory_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ impl InMemorySpanExporter {
pub fn reset(&self) {
let _ = self.spans.lock().map(|mut spans_guard| spans_guard.clear());
}

/// Returns a clone of the current resource set for the exporter.
pub fn resource(&self) -> Resource {
self.resource
.lock()
.map(|res_guard| res_guard.clone())
.expect("Resource lock poisoned")
}
}

impl SpanExporter for InMemorySpanExporter {
Expand All @@ -151,3 +159,23 @@ impl SpanExporter for InMemorySpanExporter {
.expect("Resource lock poisoned");
}
}

#[cfg(test)]
mod tests {
use opentelemetry::KeyValue;

use super::*;

#[test]
fn test_in_memory_span_exporter_resource() {
let mut exporter = InMemorySpanExporter::default();
let custom_resource = Resource::builder()
.with_attribute(KeyValue::new("key", "value"))
.build();

assert_ne!(exporter.resource(), custom_resource);

exporter.set_resource(&custom_resource);
assert_eq!(exporter.resource(), custom_resource);
}
}
9 changes: 5 additions & 4 deletions opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## vNext

- Add `get_all` method to `opentelemetry::propagation::Extractor` to return all values of the given propagation key and provide a default implementation.
- Add `InMemorySpanExporter::resource` and `InMemoryLogExporter::resource` accessor methods.

## 0.30.0

Expand Down Expand Up @@ -104,10 +105,10 @@ let counter = meter.u64_counter("my_counter").build();
- Replaced `global::meter_with_version` with `global::meter_with_scope`
- Added `global::tracer_with_scope`
- Refer to PR description for migration guide.
- **Breaking change**: replaced `InstrumentationScope` public attributes by getters [#2275](https://github.com/open-telemetry/opentelemetry-rust/pull/2275)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the whitespace stripped in this file by my editor, let me know if this needs reverting.

- **Breaking change**: replaced `InstrumentationScope` public attributes by getters [#2275](https://github.com/open-telemetry/opentelemetry-rust/pull/2275)

- **Breaking change**: [#2260](https://github.com/open-telemetry/opentelemetry-rust/pull/2260)
- Removed `global::set_error_handler` and `global::handle_error`.
- Removed `global::set_error_handler` and `global::handle_error`.
- `global::handle_error` usage inside the opentelemetry crates has been replaced with `global::otel_info`, `otel_warn`, `otel_debug` and `otel_error` macros based on the severity of the internal logs.
- The default behavior of `global::handle_error` was to log the error using `eprintln!`. With otel macros, the internal logs get emitted via `tracing` macros of matching severity. Users now need to configure a `tracing` layer/subscriber to capture these logs.
- Refer to PR description for migration guide. Also refer to [self-diagnostics](https://github.com/open-telemetry/opentelemetry-rust/tree/main/examples/self-diagnostics) example to learn how to view internal logs in stdout using `tracing::fmt` layer.
Expand Down Expand Up @@ -207,7 +208,7 @@ to learn how to provide Observable callbacks.
opaque string. Migration: Replace `.with_unit(Unit::new("myunit"))` with
`.with_unit("myunit")`.

- [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) Introduced the `LogRecord::set_target()` method in the log bridge API.
- [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) Introduced the `LogRecord::set_target()` method in the log bridge API.
This method allows appenders to set the target/component emitting the logs.

## v0.23.0
Expand All @@ -228,7 +229,7 @@ This method allows appenders to set the target/component emitting the logs.
- opentelemetry::global::shutdown_logger_provider
- opentelemetry::global::logger_provider
- opentelemetry::global::GlobalLoggerProvider
- opentelemetry::global::ObjectSafeLoggerProvider
- opentelemetry::global::ObjectSafeLoggerProvider
For creating appenders using Logging bridge API, refer to the opentelemetry-tracing-appender [example](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-appender-tracing/examples/basic.rs)

### Changed
Expand Down
Loading