Skip to content

Commit 89e361a

Browse files
committed
2 parents 9ad4aee + 7b82473 commit 89e361a

File tree

7 files changed

+53
-33
lines changed

7 files changed

+53
-33
lines changed

exporters/prometheus/test/collector_test.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
#include <thread>
1414

1515
using opentelemetry::exporter::metrics::PrometheusCollector;
16+
using opentelemetry::sdk::metrics::MetricProducer;
1617
using opentelemetry::sdk::metrics::ResourceMetrics;
1718
namespace metric_api = opentelemetry::metrics;
1819
namespace metric_sdk = opentelemetry::sdk::metrics;
1920
namespace metric_exporter = opentelemetry::exporter::metrics;
2021

21-
class MockMetricProducer : public opentelemetry::sdk::metrics::MetricProducer
22+
class MockMetricProducer : public MetricProducer
2223
{
2324
TestDataPoints test_data_points_;
2425

@@ -27,13 +28,12 @@ class MockMetricProducer : public opentelemetry::sdk::metrics::MetricProducer
2728
: sleep_ms_{sleep_ms}
2829
{}
2930

30-
bool Collect(nostd::function_ref<bool(ResourceMetrics &)> callback) noexcept override
31+
MetricProducer::Result Produce() noexcept override
3132
{
3233
std::this_thread::sleep_for(sleep_ms_);
3334
data_sent_size_++;
3435
ResourceMetrics data = test_data_points_.CreateSumPointData();
35-
callback(data);
36-
return true;
36+
return {data, MetricProducer::Status::kSuccess};
3737
}
3838

3939
size_t GetDataCount() { return data_sent_size_; }
@@ -71,15 +71,13 @@ class MockMetricReader : public opentelemetry::sdk::metrics::MetricReader
7171
*/
7272
TEST(PrometheusCollector, BasicTests)
7373
{
74-
MockMetricReader *reader = new MockMetricReader();
75-
MockMetricProducer *producer = new MockMetricProducer();
76-
reader->SetMetricProducer(producer);
77-
PrometheusCollector collector(reader, true, false);
74+
MockMetricReader reader;
75+
MockMetricProducer producer;
76+
reader.SetMetricProducer(&producer);
77+
PrometheusCollector collector(&reader, true, false);
7878
auto data = collector.Collect();
7979

8080
// Collection size should be the same as the size
8181
// of the records collection produced by MetricProducer.
8282
ASSERT_EQ(data.size(), 2);
83-
delete reader;
84-
delete producer;
8583
}

sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "opentelemetry/version.h"
1010
#include "opentelemetry/nostd/function_ref.h"
11+
#include "opentelemetry/nostd/variant.h"
1112
#include "opentelemetry/sdk/metrics/data/metric_data.h"
1213

1314
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -70,27 +71,42 @@ struct ResourceMetrics
7071
};
7172

7273
/**
73-
* MetricProducer is the interface that is used to make metric data available to the
74-
* OpenTelemetry exporters. Implementations should be stateful, in that each call to
75-
* `Collect` will return any metric generated since the last call was made.
74+
* MetricProducer defines the interface which bridges to third-party metric sources MUST implement,
75+
* so they can be plugged into an OpenTelemetry MetricReader as a source of aggregated metric data.
7676
*
77-
* <p>Implementations must be thread-safe.
77+
* Implementations must be thread-safe, and should accept configuration for the
78+
* AggregationTemporality of produced metrics.
7879
*/
79-
8080
class MetricProducer
8181
{
8282
public:
8383
MetricProducer() = default;
8484
virtual ~MetricProducer() = default;
8585

86+
MetricProducer(const MetricProducer &) = delete;
87+
MetricProducer(const MetricProducer &&) = delete;
88+
void operator=(const MetricProducer &) = delete;
89+
void operator=(const MetricProducer &&) = delete;
90+
91+
enum class Status
92+
{
93+
kSuccess,
94+
kFailure,
95+
kTimeout,
96+
};
97+
98+
struct Result
99+
{
100+
ResourceMetrics points_;
101+
Status status_;
102+
};
103+
86104
/**
87-
* The callback to be called for each metric exporter. This will only be those
88-
* metrics that have been produced since the last time this method was called.
89-
*
90-
* @return a status of completion of method.
105+
* Produce returns a batch of Metric Points, with a single instrumentation scope that identifies
106+
* the MetricProducer. Implementations may return successfully collected points even if there is a
107+
* partial failure.
91108
*/
92-
virtual bool Collect(
93-
nostd::function_ref<bool(ResourceMetrics &metric_data)> callback) noexcept = 0;
109+
virtual Result Produce() noexcept = 0;
94110
};
95111

96112
} // namespace metrics

sdk/include/opentelemetry/sdk/metrics/state/metric_collector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class OPENTELEMETRY_EXPORT_TYPE MetricCollector : public MetricProducer, public
5353
*
5454
* @return a status of completion of method.
5555
*/
56-
bool Collect(nostd::function_ref<bool(ResourceMetrics &metric_data)> callback) noexcept override;
56+
Result Produce() noexcept override;
5757

5858
bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
5959

sdk/src/metrics/metric_reader.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool MetricReader::Collect(
2626
if (!metric_producer_)
2727
{
2828
OTEL_INTERNAL_LOG_WARN(
29-
"MetricReader::Collect Cannot invoke Collect(). No MetricProducer registered for "
29+
"MetricReader::Collect Cannot invoke Produce(). No MetricProducer registered for "
3030
"collection!")
3131
return false;
3232
}
@@ -36,7 +36,15 @@ bool MetricReader::Collect(
3636
OTEL_INTERNAL_LOG_WARN("MetricReader::Collect invoked while Shutdown in progress!");
3737
}
3838

39-
return metric_producer_->Collect(callback);
39+
auto result = metric_producer_->Produce();
40+
41+
// According to the spec,
42+
// When the Produce operation fails, the MetricProducer MAY return successfully collected
43+
// results and a failed reasons list to the caller.
44+
// So we invoke the callback with whatever points we get back, even if the overall operation may
45+
// have failed.
46+
auto success = callback(result.points_);
47+
return (result.status_ == MetricProducer::Status::kSuccess) && success;
4048
}
4149

4250
bool MetricReader::Shutdown(std::chrono::microseconds timeout) noexcept

sdk/src/metrics/state/metric_collector.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace sdk
2424
{
2525
namespace metrics
2626
{
27+
using opentelemetry::sdk::resource::Resource;
2728

2829
MetricCollector::MetricCollector(opentelemetry::sdk::metrics::MeterContext *context,
2930
std::shared_ptr<MetricReader> metric_reader)
@@ -38,14 +39,13 @@ AggregationTemporality MetricCollector::GetAggregationTemporality(
3839
return metric_reader_->GetAggregationTemporality(instrument_type);
3940
}
4041

41-
bool MetricCollector::Collect(
42-
nostd::function_ref<bool(ResourceMetrics &metric_data)> callback) noexcept
42+
MetricProducer::Result MetricCollector::Produce() noexcept
4343
{
4444
if (!meter_context_)
4545
{
4646
OTEL_INTERNAL_LOG_ERROR("[MetricCollector::Collect] - Error during collecting."
4747
<< "The metric context is invalid");
48-
return false;
48+
return {{}, MetricProducer::Status::kFailure};
4949
}
5050
ResourceMetrics resource_metrics;
5151
meter_context_->ForEachMeter([&](const std::shared_ptr<Meter> &meter) noexcept {
@@ -61,8 +61,7 @@ bool MetricCollector::Collect(
6161
return true;
6262
});
6363
resource_metrics.resource_ = &meter_context_->GetResource();
64-
callback(resource_metrics);
65-
return true;
64+
return {resource_metrics, MetricProducer::Status::kSuccess};
6665
}
6766

6867
bool MetricCollector::ForceFlush(std::chrono::microseconds timeout) noexcept

sdk/test/metrics/metric_reader_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ TEST(MetricReaderTest, BasicTests)
3131
std::shared_ptr<MeterContext> meter_context2(new MeterContext());
3232
std::shared_ptr<MetricProducer> metric_producer{
3333
new MetricCollector(meter_context2.get(), std::move(metric_reader2))};
34-
metric_producer->Collect([](ResourceMetrics & /* metric_data */) { return true; });
34+
metric_producer->Produce();
3535
}

sdk/test/metrics/periodic_exporting_metric_reader_test.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ class MockMetricProducer : public MetricProducer
6363
: sleep_ms_{sleep_ms}
6464
{}
6565

66-
bool Collect(nostd::function_ref<bool(ResourceMetrics &)> callback) noexcept override
66+
MetricProducer::Result Produce() noexcept override
6767
{
6868
std::this_thread::sleep_for(sleep_ms_);
6969
data_sent_size_++;
7070
ResourceMetrics data;
71-
callback(data);
72-
return true;
71+
return {data, MetricProducer::Status::kSuccess};
7372
}
7473

7574
size_t GetDataCount() { return data_sent_size_; }

0 commit comments

Comments
 (0)