Skip to content
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

Refactor/metric exporters non plural #2255

Merged
Merged
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
Prev Previous commit
Next Next commit
refactor: make InMemoryMetricsExporter and Builder non-plural
Make metrics more consistent with other signals
  • Loading branch information
pitoniak32 committed Oct 26, 2024
commit 3e1dc855c4524cbac9a8765a040d17b3d1dbd9e5
22 changes: 11 additions & 11 deletions opentelemetry-sdk/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ mod tests {
use self::data::{DataPoint, HistogramDataPoint, ScopeMetrics};
use super::*;
use crate::metrics::data::{ResourceMetrics, Temporality};
use crate::testing::metrics::InMemoryMetricsExporterBuilder;
use crate::{runtime, testing::metrics::InMemoryMetricsExporter};
use crate::testing::metrics::InMemoryMetricExporterBuilder;
use crate::{runtime, testing::metrics::InMemoryMetricExporter};
use opentelemetry::metrics::{Counter, Meter, UpDownCounter};
use opentelemetry::InstrumentationScope;
use opentelemetry::{metrics::MeterProvider as _, KeyValue};
Expand Down Expand Up @@ -491,7 +491,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn counter_duplicate_instrument_merge() {
// Arrange
let exporter = InMemoryMetricsExporter::default();
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();

Expand Down Expand Up @@ -542,7 +542,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn counter_duplicate_instrument_different_meter_no_merge() {
// Arrange
let exporter = InMemoryMetricsExporter::default();
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();

Expand Down Expand Up @@ -631,7 +631,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn instrumentation_scope_identity_test() {
// Arrange
let exporter = InMemoryMetricsExporter::default();
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let meter_provider = SdkMeterProvider::builder().with_reader(reader).build();

Expand Down Expand Up @@ -713,7 +713,7 @@ mod tests {
// cargo test histogram_aggregation_with_invalid_aggregation_should_proceed_as_if_view_not_exist --features=testing -- --nocapture

// Arrange
let exporter = InMemoryMetricsExporter::default();
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let criteria = Instrument::new().name("test_histogram");
let stream_invalid_aggregation = Stream::new()
Expand Down Expand Up @@ -763,7 +763,7 @@ mod tests {
// cargo test metrics::tests::spatial_aggregation_when_view_drops_attributes_observable_counter --features=testing

// Arrange
let exporter = InMemoryMetricsExporter::default();
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let criteria = Instrument::new().name("my_observable_counter");
// View drops all attributes.
Expand Down Expand Up @@ -838,7 +838,7 @@ mod tests {
// cargo test spatial_aggregation_when_view_drops_attributes_counter --features=testing

// Arrange
let exporter = InMemoryMetricsExporter::default();
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let criteria = Instrument::new().name("my_counter");
// View drops all attributes.
Expand Down Expand Up @@ -2345,7 +2345,7 @@ mod tests {
}

struct TestContext {
exporter: InMemoryMetricsExporter,
exporter: InMemoryMetricExporter,
meter_provider: SdkMeterProvider,

// Saving this on the test context for lifetime simplicity
Expand All @@ -2354,7 +2354,7 @@ mod tests {

impl TestContext {
fn new(temporality: Temporality) -> Self {
let exporter = InMemoryMetricsExporterBuilder::new().with_temporality(temporality);
let exporter = InMemoryMetricExporterBuilder::new().with_temporality(temporality);

let exporter = exporter.build();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
Expand Down Expand Up @@ -2411,7 +2411,7 @@ mod tests {
let resource_metrics = self
.exporter
.get_finished_metrics()
.expect("metrics expected to be exported"); // TODO: Need to fix InMemoryMetricsExporter to return None.
.expect("metrics expected to be exported"); // TODO: Need to fix InMemoryMetricExporter to return None.

assert!(resource_metrics.is_empty(), "no metrics should be exported");
}
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-sdk/src/metrics/periodic_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ mod tests {
use super::PeriodicReader;
use crate::{
metrics::data::ResourceMetrics, metrics::reader::MetricReader, metrics::SdkMeterProvider,
runtime, testing::metrics::InMemoryMetricsExporter, Resource,
runtime, testing::metrics::InMemoryMetricExporter, Resource,
};
use opentelemetry::metrics::{MeterProvider, MetricError};
use std::sync::mpsc;
Expand Down Expand Up @@ -437,7 +437,7 @@ mod tests {
#[test]
fn unregistered_collect() {
// Arrange
let exporter = InMemoryMetricsExporter::default();
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime::Tokio).build();
let mut rm = ResourceMetrics {
resource: Resource::empty(),
Expand All @@ -458,7 +458,7 @@ mod tests {
RT: crate::runtime::Runtime,
{
let interval = std::time::Duration::from_millis(1);
let exporter = InMemoryMetricsExporter::default();
let exporter = InMemoryMetricExporter::default();
let reader = PeriodicReader::builder(exporter.clone(), runtime)
.with_interval(interval)
.build();
Expand Down
58 changes: 29 additions & 29 deletions opentelemetry-sdk/src/testing/metrics/in_memory_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use std::sync::{Arc, Mutex};
///# use opentelemetry_sdk::{metrics, runtime};
///# use opentelemetry::{KeyValue};
///# use opentelemetry::metrics::MeterProvider;
///# use opentelemetry_sdk::testing::metrics::InMemoryMetricsExporter;
///# use opentelemetry_sdk::testing::metrics::InMemoryMetricExporter;
///# use opentelemetry_sdk::metrics::PeriodicReader;
///
///# #[tokio::main]
///# async fn main() {
/// // Create an InMemoryMetricsExporter
/// let exporter = InMemoryMetricsExporter::default();
/// // Create an InMemoryMetricExporter
/// let exporter = InMemoryMetricExporter::default();
///
/// // Create a MeterProvider and register the exporter
/// let meter_provider = metrics::SdkMeterProvider::builder()
Expand All @@ -55,58 +55,58 @@ use std::sync::{Arc, Mutex};
/// }
///# }
/// ```
pub struct InMemoryMetricsExporter {
pub struct InMemoryMetricExporter {
metrics: Arc<Mutex<VecDeque<ResourceMetrics>>>,
temporality: Temporality,
}

impl Clone for InMemoryMetricsExporter {
impl Clone for InMemoryMetricExporter {
fn clone(&self) -> Self {
InMemoryMetricsExporter {
InMemoryMetricExporter {
metrics: self.metrics.clone(),
temporality: self.temporality,
}
}
}

impl fmt::Debug for InMemoryMetricsExporter {
impl fmt::Debug for InMemoryMetricExporter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("InMemoryMetricsExporter").finish()
f.debug_struct("InMemoryMetricExporter").finish()
}
}

impl Default for InMemoryMetricsExporter {
impl Default for InMemoryMetricExporter {
fn default() -> Self {
InMemoryMetricsExporterBuilder::new().build()
InMemoryMetricExporterBuilder::new().build()
}
}

/// Builder for [`InMemoryMetricsExporter`].
/// Builder for [`InMemoryMetricExporter`].
/// # Example
///
/// ```
/// # use opentelemetry_sdk::testing::metrics::{InMemoryMetricsExporter, InMemoryMetricsExporterBuilder};
/// # use opentelemetry_sdk::testing::metrics::{InMemoryMetricExporter, InMemoryMetricExporterBuilder};
///
/// let exporter = InMemoryMetricsExporterBuilder::new().build();
/// let exporter = InMemoryMetricExporterBuilder::new().build();
/// ```
pub struct InMemoryMetricsExporterBuilder {
pub struct InMemoryMetricExporterBuilder {
temporality: Option<Temporality>,
}

impl fmt::Debug for InMemoryMetricsExporterBuilder {
impl fmt::Debug for InMemoryMetricExporterBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("InMemoryMetricsExporterBuilder").finish()
f.debug_struct("InMemoryMetricExporterBuilder").finish()
}
}

impl Default for InMemoryMetricsExporterBuilder {
impl Default for InMemoryMetricExporterBuilder {
fn default() -> Self {
Self::new()
}
}

impl InMemoryMetricsExporterBuilder {
/// Creates a new instance of the `InMemoryMetricsExporterBuilder`.
impl InMemoryMetricExporterBuilder {
/// Creates a new instance of the `InMemoryMetricExporterBuilder`.
pub fn new() -> Self {
Self { temporality: None }
}
Expand All @@ -117,17 +117,17 @@ impl InMemoryMetricsExporterBuilder {
self
}

/// Creates a new instance of the `InMemoryMetricsExporter`.
/// Creates a new instance of the `InMemoryMetricExporter`.
///
pub fn build(self) -> InMemoryMetricsExporter {
InMemoryMetricsExporter {
pub fn build(self) -> InMemoryMetricExporter {
InMemoryMetricExporter {
metrics: Arc::new(Mutex::new(VecDeque::new())),
temporality: self.temporality.unwrap_or_default(),
}
}
}

impl InMemoryMetricsExporter {
impl InMemoryMetricExporter {
/// Returns the finished metrics as a vector of `ResourceMetrics`.
///
/// # Errors
Expand All @@ -137,9 +137,9 @@ impl InMemoryMetricsExporter {
/// # Example
///
/// ```
/// # use opentelemetry_sdk::testing::metrics::InMemoryMetricsExporter;
/// # use opentelemetry_sdk::testing::metrics::InMemoryMetricExporter;
///
/// let exporter = InMemoryMetricsExporter::default();
/// let exporter = InMemoryMetricExporter::default();
/// let finished_metrics = exporter.get_finished_metrics().unwrap();
/// ```
pub fn get_finished_metrics(&self) -> MetricResult<Vec<ResourceMetrics>> {
Expand All @@ -154,9 +154,9 @@ impl InMemoryMetricsExporter {
/// # Example
///
/// ```
/// # use opentelemetry_sdk::testing::metrics::InMemoryMetricsExporter;
/// # use opentelemetry_sdk::testing::metrics::InMemoryMetricExporter;
///
/// let exporter = InMemoryMetricsExporter::default();
/// let exporter = InMemoryMetricExporter::default();
/// exporter.reset();
/// ```
pub fn reset(&self) {
Expand Down Expand Up @@ -244,12 +244,12 @@ impl InMemoryMetricsExporter {
}

#[async_trait]
impl PushMetricsExporter for InMemoryMetricsExporter {
impl PushMetricsExporter for InMemoryMetricExporter {
async fn export(&self, metrics: &mut ResourceMetrics) -> MetricResult<()> {
self.metrics
.lock()
.map(|mut metrics_guard| {
metrics_guard.push_back(InMemoryMetricsExporter::clone_metrics(metrics))
metrics_guard.push_back(InMemoryMetricExporter::clone_metrics(metrics))
})
.map_err(MetricError::from)
}
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/testing/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// The `in_memory_exporter` module provides in-memory metrics exporter.
/// For detailed usage and examples, see `in_memory_exporter`.
pub mod in_memory_exporter;
pub use in_memory_exporter::{InMemoryMetricsExporter, InMemoryMetricsExporterBuilder};
pub use in_memory_exporter::{InMemoryMetricExporter, InMemoryMetricExporterBuilder};

#[doc(hidden)]
pub mod metric_reader;
Expand Down