Skip to content

Commit 31d888a

Browse files
authored
Enable metric collection from MetricReader (#1241)
1 parent 6bd2f96 commit 31d888a

27 files changed

+571
-240
lines changed

exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# include <string>
99
# include "opentelemetry/common/spin_lock_mutex.h"
1010
# include "opentelemetry/nostd/span.h"
11+
# include "opentelemetry/sdk/metrics/data/metric_data.h"
1112
# include "opentelemetry/sdk/metrics/instruments.h"
1213
# include "opentelemetry/sdk/metrics/metric_exporter.h"
1314
# include "opentelemetry/sdk/metrics/recordable.h"

sdk/include/opentelemetry/sdk/metrics/async_instruments.h

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
#ifndef ENABLE_METRICS_PREVIEW
66
# include "opentelemetry/metrics/async_instruments.h"
77
# include "opentelemetry/metrics/observer_result.h"
8-
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
9-
# include "opentelemetry/sdk/metrics/measurement_processor.h"
10-
118
# include "opentelemetry/nostd/string_view.h"
9+
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
1210
# include "opentelemetry/sdk/metrics/instruments.h"
1311
OPENTELEMETRY_BEGIN_NAMESPACE
1412
namespace sdk
@@ -23,13 +21,11 @@ class Asynchronous
2321
Asynchronous(nostd::string_view name,
2422
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
2523
*instrumentation_library,
26-
MeasurementProcessor *measurement_processor,
2724
void (*callback)(opentelemetry::metrics::ObserverResult<T> &),
2825
nostd::string_view description = "",
2926
nostd::string_view unit = "")
3027
: name_(name),
3128
instrumentation_library_{instrumentation_library},
32-
measurement_processor_{measurement_processor},
3329
callback_(callback),
3430
description_(description),
3531
unit_(unit)
@@ -39,7 +35,6 @@ class Asynchronous
3935
std::string name_;
4036
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
4137
*instrumentation_library_;
42-
const MeasurementProcessor *measurement_processor_;
4338
void (*callback_)(opentelemetry::metrics::ObserverResult<T> &);
4439
std::string description_;
4540
std::string unit_;
@@ -52,16 +47,10 @@ class LongObservableCounter : public opentelemetry::metrics::ObservableCounter<l
5247
LongObservableCounter(nostd::string_view name,
5348
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
5449
*instrumentation_library,
55-
MeasurementProcessor *measurement_processor,
5650
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
5751
nostd::string_view description = "",
5852
nostd::string_view unit = "")
59-
: Asynchronous(name,
60-
instrumentation_library,
61-
measurement_processor,
62-
callback,
63-
description,
64-
unit)
53+
: Asynchronous(name, instrumentation_library, callback, description, unit)
6554

6655
{}
6756
};
@@ -73,16 +62,10 @@ class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter
7362
DoubleObservableCounter(nostd::string_view name,
7463
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
7564
*instrumentation_library,
76-
MeasurementProcessor *measurement_processor,
7765
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
7866
nostd::string_view description = "",
7967
nostd::string_view unit = "")
80-
: Asynchronous(name,
81-
instrumentation_library,
82-
measurement_processor,
83-
callback,
84-
description,
85-
unit)
68+
: Asynchronous(name, instrumentation_library, callback, description, unit)
8669

8770
{}
8871
};
@@ -94,16 +77,10 @@ class LongObservableGauge : public opentelemetry::metrics::ObservableGauge<long>
9477
LongObservableGauge(nostd::string_view name,
9578
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
9679
*instrumentation_library,
97-
MeasurementProcessor *measurement_processor,
9880
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
9981
nostd::string_view description = "",
10082
nostd::string_view unit = "")
101-
: Asynchronous(name,
102-
instrumentation_library,
103-
measurement_processor,
104-
callback,
105-
description,
106-
unit)
83+
: Asynchronous(name, instrumentation_library, callback, description, unit)
10784

10885
{}
10986
};
@@ -115,16 +92,10 @@ class DoubleObservableGauge : public opentelemetry::metrics::ObservableGauge<dou
11592
DoubleObservableGauge(nostd::string_view name,
11693
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
11794
*instrumentation_library,
118-
MeasurementProcessor *measurement_processor,
11995
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
12096
nostd::string_view description = "",
12197
nostd::string_view unit = "")
122-
: Asynchronous(name,
123-
instrumentation_library,
124-
measurement_processor,
125-
callback,
126-
description,
127-
unit)
98+
: Asynchronous(name, instrumentation_library, callback, description, unit)
12899

129100
{}
130101
};
@@ -137,16 +108,10 @@ class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpD
137108
nostd::string_view name,
138109
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
139110
*instrumentation_library,
140-
MeasurementProcessor *measurement_processor,
141111
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
142112
nostd::string_view description = "",
143113
nostd::string_view unit = "")
144-
: Asynchronous(name,
145-
instrumentation_library,
146-
measurement_processor,
147-
callback,
148-
description,
149-
unit)
114+
: Asynchronous(name, instrumentation_library, callback, description, unit)
150115

151116
{}
152117
};
@@ -160,16 +125,10 @@ class DoubleObservableUpDownCounter
160125
nostd::string_view name,
161126
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
162127
*instrumentation_library,
163-
MeasurementProcessor *measurement_processor,
164128
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
165129
nostd::string_view description = "",
166130
nostd::string_view unit = "")
167-
: Asynchronous(name,
168-
instrumentation_library,
169-
measurement_processor,
170-
callback,
171-
description,
172-
unit)
131+
: Asynchronous(name, instrumentation_library, callback, description, unit)
173132
{}
174133
};
175134

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
#ifndef ENABLE_METRICS_PREVIEW
6+
# include "opentelemetry/sdk/metrics/data/metric_data.h"
7+
OPENTELEMETRY_BEGIN_NAMESPACE
8+
namespace sdk
9+
{
10+
namespace metrics
11+
{
12+
13+
/**
14+
* MetricProducer is the interface that is used to make metric data available to the
15+
* OpenTelemetry exporters. Implementations should be stateful, in that each call to
16+
* `Collect` will return any metric generated since the last call was made.
17+
*
18+
* <p>Implementations must be thread-safe.
19+
*/
20+
21+
class MetricProducer
22+
{
23+
public:
24+
/**
25+
* The callback to be called for each metric exporter. This will only be those
26+
* metrics that have been produced since the last time this method was called.
27+
*
28+
* @return a status of completion of method.
29+
*/
30+
virtual bool Collect(nostd::function_ref<bool(MetricData)> callback) noexcept = 0;
31+
};
32+
33+
} // namespace metrics
34+
} // namespace sdk
35+
OPENTELEMETRY_END_NAMESPACE
36+
#endif

sdk/include/opentelemetry/sdk/metrics/instruments.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,6 @@ struct InstrumentDescriptor
5454

5555
using MetricAttributes = opentelemetry::sdk::common::OrderedAttributeMap;
5656

57-
// TBD -> Remove once MetricCollector is imoplemeted
58-
class MetricCollector
59-
{
60-
public:
61-
AggregationTemporarily aggregation_temporarily_;
62-
};
63-
6457
/*class InstrumentSelector {
6558
public:
6659
InstrumentSelector(opentelemetry::nostd::string_view name,

sdk/include/opentelemetry/sdk/metrics/meter.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# include "opentelemetry/metrics/meter.h"
88
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
99
# include "opentelemetry/sdk/metrics/instruments.h"
10-
# include "opentelemetry/sdk/metrics/measurement_processor.h"
1110
# include "opentelemetry/sdk/metrics/meter_context.h"
1211
# include "opentelemetry/sdk/resource/resource.h"
1312
# include "opentelemetry/version.h"
@@ -17,6 +16,10 @@ namespace sdk
1716
{
1817
namespace metrics
1918
{
19+
20+
class MetricStorage;
21+
class WritableMetricStorage;
22+
2023
class Meter final : public opentelemetry::metrics::Meter
2124
{
2225
public:
@@ -99,8 +102,10 @@ class Meter final : public opentelemetry::metrics::Meter
99102
const sdk::instrumentationlibrary::InstrumentationLibrary *GetInstrumentationLibrary()
100103
const noexcept;
101104

102-
/** Returns the associated measurement processor */
103-
MeasurementProcessor *GetMeasurementProcessor() const noexcept;
105+
/** collect metrics across all the meters **/
106+
bool collect(CollectorHandle *collector,
107+
opentelemetry::common::SystemTimestamp collect_ts,
108+
nostd::function_ref<bool(MetricData &)> callback) noexcept;
104109

105110
private:
106111
// order of declaration is important here - instrumentation library should destroy after

sdk/include/opentelemetry/sdk/metrics/meter_context.h

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,35 @@
33

44
#pragma once
55
#ifndef ENABLE_METRICS_PREVIEW
6-
# include <chrono>
7-
# include <memory>
8-
# include <vector>
9-
# include "opentelemetry/sdk/metrics/metric_exporter.h"
10-
# include "opentelemetry/sdk/metrics/metric_reader.h"
6+
7+
# include "opentelemetry/common/spin_lock_mutex.h"
8+
# include "opentelemetry/sdk/metrics/state/metric_collector.h"
119
# include "opentelemetry/sdk/metrics/view/instrument_selector.h"
1210
# include "opentelemetry/sdk/metrics/view/meter_selector.h"
1311
# include "opentelemetry/sdk/metrics/view/view_registry.h"
1412
# include "opentelemetry/sdk/resource/resource.h"
1513
# include "opentelemetry/version.h"
1614

15+
# include <chrono>
16+
# include <memory>
17+
# include <vector>
18+
1719
OPENTELEMETRY_BEGIN_NAMESPACE
1820
namespace sdk
1921
{
2022
namespace metrics
2123
{
24+
25+
// forward declaration
26+
class Meter;
27+
class MetricExporter;
28+
class MetricReader;
29+
2230
/**
2331
* A class which stores the MeterProvider context.
2432
2533
*/
26-
class MeterContext
34+
class MeterContext : public std::enable_shared_from_this<MeterContext>
2735
{
2836
public:
2937
/**
@@ -35,7 +43,6 @@ class MeterContext
3543
*/
3644
MeterContext(
3745
std::vector<std::unique_ptr<sdk::metrics::MetricExporter>> &&exporters,
38-
std::vector<std::unique_ptr<MetricReader>> &&readers,
3946
std::unique_ptr<ViewRegistry> views = std::unique_ptr<ViewRegistry>(new ViewRegistry()),
4047
opentelemetry::sdk::resource::Resource resource =
4148
opentelemetry::sdk::resource::Resource::Create({})) noexcept;
@@ -47,16 +54,28 @@ class MeterContext
4754
const opentelemetry::sdk::resource::Resource &GetResource() const noexcept;
4855

4956
/**
50-
* Obtain the reference of measurement_processor.
57+
* Obtain the View Registry configured
58+
* @return The reference to view registry
59+
*/
60+
ViewRegistry *GetViewRegistry() const noexcept;
61+
62+
/**
63+
* Obtain the configured meters.
5164
*
5265
*/
53-
MeasurementProcessor *GetMeasurementProcessor() const noexcept;
66+
nostd::span<std::shared_ptr<Meter>> GetMeters() noexcept;
5467

5568
/**
56-
* Obtain the View Registry configured
57-
* @return The reference to view registry
69+
* Obtain the configured collectors.
70+
*
5871
*/
59-
ViewRegistry *GetViewRegistry() const noexcept;
72+
nostd::span<std::shared_ptr<CollectorHandle>> GetCollectors() noexcept;
73+
74+
/**
75+
* GET SDK Start time
76+
*
77+
*/
78+
opentelemetry::common::SystemTimestamp GetSDKStartTime() noexcept;
6079

6180
/**
6281
* Attaches a metric exporter to list of configured exporters for this Meter context.
@@ -91,23 +110,36 @@ class MeterContext
91110
std::unique_ptr<View> view) noexcept;
92111

93112
/**
94-
* Force all active Exporters and Readers to flush any buffered meter data
113+
* Adds a meter to the list of configured meters.
114+
*
115+
* Note: This method is INTERNAL to sdk not thread safe.
116+
*
117+
* @param meter
118+
*/
119+
void AddMeter(std::shared_ptr<Meter> meter);
120+
121+
/**
122+
* Force all active Exporters and Collectors to flush any buffered meter data
95123
* within the given timeout.
96124
*/
97125

98126
bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
99127

100128
/**
101-
* Shutdown the Exporters and Readers associated with this meter provider.
129+
* Shutdown the Exporters and Collectors associated with this meter provider.
102130
*/
103131
bool Shutdown() noexcept;
104132

105133
private:
106134
opentelemetry::sdk::resource::Resource resource_;
107135
std::vector<std::unique_ptr<MetricExporter>> exporters_;
108-
std::vector<std::unique_ptr<MetricReader>> readers_;
136+
std::vector<std::shared_ptr<CollectorHandle>> collectors_;
109137
std::unique_ptr<ViewRegistry> views_;
110-
std::unique_ptr<MeasurementProcessor> measurement_processor_;
138+
opentelemetry::common::SystemTimestamp sdk_start_ts_;
139+
std::vector<std::shared_ptr<Meter>> meters_;
140+
141+
std::atomic_flag shutdown_latch_ = ATOMIC_FLAG_INIT;
142+
opentelemetry::common::SpinLockMutex forceflush_lock_;
111143
};
112144

113145
} // namespace metrics

0 commit comments

Comments
 (0)