Skip to content

Commit 45f0235

Browse files
authored
Connect async storage with async instruments (#1388)
1 parent 3fd5ca3 commit 45f0235

File tree

7 files changed

+191
-145
lines changed

7 files changed

+191
-145
lines changed

api/include/opentelemetry/metrics/meter.h

+25-31
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,15 @@ class Meter
5656
* @param callback the function to be observed by the instrument.
5757
* @return a shared pointer to the created Observable Counter.
5858
*/
59-
virtual nostd::shared_ptr<ObservableCounter<long>> CreateLongObservableCounter(
60-
nostd::string_view name,
61-
void (*callback)(ObserverResult<long> &),
62-
nostd::string_view description = "",
63-
nostd::string_view unit = "") noexcept = 0;
59+
virtual void CreateLongObservableCounter(nostd::string_view name,
60+
void (*callback)(ObserverResult<long> &),
61+
nostd::string_view description = "",
62+
nostd::string_view unit = "") noexcept = 0;
6463

65-
virtual nostd::shared_ptr<ObservableCounter<double>> CreateDoubleObservableCounter(
66-
nostd::string_view name,
67-
void (*callback)(ObserverResult<double> &),
68-
nostd::string_view description = "",
69-
nostd::string_view unit = "") noexcept = 0;
64+
virtual void CreateDoubleObservableCounter(nostd::string_view name,
65+
void (*callback)(ObserverResult<double> &),
66+
nostd::string_view description = "",
67+
nostd::string_view unit = "") noexcept = 0;
7068

7169
/**
7270
* Creates a Histogram with the passed characteristics and returns a shared_ptr to that Histogram.
@@ -96,17 +94,15 @@ class Meter
9694
* @param callback the function to be observed by the instrument.
9795
* @return a shared pointer to the created Observable Gauge.
9896
*/
99-
virtual nostd::shared_ptr<ObservableGauge<long>> CreateLongObservableGauge(
100-
nostd::string_view name,
101-
void (*callback)(ObserverResult<long> &),
102-
nostd::string_view description = "",
103-
nostd::string_view unit = "") noexcept = 0;
97+
virtual void CreateLongObservableGauge(nostd::string_view name,
98+
void (*callback)(ObserverResult<long> &),
99+
nostd::string_view description = "",
100+
nostd::string_view unit = "") noexcept = 0;
104101

105-
virtual nostd::shared_ptr<ObservableGauge<double>> CreateDoubleObservableGauge(
106-
nostd::string_view name,
107-
void (*callback)(ObserverResult<double> &),
108-
nostd::string_view description = "",
109-
nostd::string_view unit = "") noexcept = 0;
102+
virtual void CreateDoubleObservableGauge(nostd::string_view name,
103+
void (*callback)(ObserverResult<double> &),
104+
nostd::string_view description = "",
105+
nostd::string_view unit = "") noexcept = 0;
110106

111107
/**
112108
* Creates an UpDownCounter with the passed characteristics and returns a shared_ptr to that
@@ -137,17 +133,15 @@ class Meter
137133
* @param callback the function to be observed by the instrument.
138134
* @return a shared pointer to the created Observable UpDownCounter.
139135
*/
140-
virtual nostd::shared_ptr<ObservableUpDownCounter<long>> CreateLongObservableUpDownCounter(
141-
nostd::string_view name,
142-
void (*callback)(ObserverResult<long> &),
143-
nostd::string_view description = "",
144-
nostd::string_view unit = "") noexcept = 0;
145-
146-
virtual nostd::shared_ptr<ObservableUpDownCounter<double>> CreateDoubleObservableUpDownCounter(
147-
nostd::string_view name,
148-
void (*callback)(ObserverResult<double> &),
149-
nostd::string_view description = "",
150-
nostd::string_view unit = "") noexcept = 0;
136+
virtual void CreateLongObservableUpDownCounter(nostd::string_view name,
137+
void (*callback)(ObserverResult<long> &),
138+
nostd::string_view description = "",
139+
nostd::string_view unit = "") noexcept = 0;
140+
141+
virtual void CreateDoubleObservableUpDownCounter(nostd::string_view name,
142+
void (*callback)(ObserverResult<double> &),
143+
nostd::string_view description = "",
144+
nostd::string_view unit = "") noexcept = 0;
151145
};
152146
} // namespace metrics
153147
OPENTELEMETRY_END_NAMESPACE

api/include/opentelemetry/metrics/noop.h

+30-54
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,17 @@ class NoopMeter final : public Meter
137137
return nostd::shared_ptr<Counter<double>>{new NoopCounter<double>(name, description, unit)};
138138
}
139139

140-
nostd::shared_ptr<ObservableCounter<long>> CreateLongObservableCounter(
141-
nostd::string_view name,
142-
void (*callback)(ObserverResult<long> &),
143-
nostd::string_view description = "",
144-
nostd::string_view unit = "") noexcept override
145-
{
146-
return nostd::shared_ptr<ObservableCounter<long>>{
147-
new NoopObservableCounter<long>(name, callback, description, unit)};
148-
}
140+
void CreateLongObservableCounter(nostd::string_view name,
141+
void (*callback)(ObserverResult<long> &),
142+
nostd::string_view description = "",
143+
nostd::string_view unit = "") noexcept override
144+
{}
149145

150-
nostd::shared_ptr<ObservableCounter<double>> CreateDoubleObservableCounter(
151-
nostd::string_view name,
152-
void (*callback)(ObserverResult<double> &),
153-
nostd::string_view description = "",
154-
nostd::string_view unit = "") noexcept override
155-
{
156-
return nostd::shared_ptr<ObservableCounter<double>>{
157-
new NoopObservableCounter<double>(name, callback, description, unit)};
158-
}
146+
void CreateDoubleObservableCounter(nostd::string_view name,
147+
void (*callback)(ObserverResult<double> &),
148+
nostd::string_view description = "",
149+
nostd::string_view unit = "") noexcept override
150+
{}
159151

160152
nostd::shared_ptr<Histogram<long>> CreateLongHistogram(
161153
nostd::string_view name,
@@ -173,25 +165,17 @@ class NoopMeter final : public Meter
173165
return nostd::shared_ptr<Histogram<double>>{new NoopHistogram<double>(name, description, unit)};
174166
}
175167

176-
nostd::shared_ptr<ObservableGauge<long>> CreateLongObservableGauge(
177-
nostd::string_view name,
178-
void (*callback)(ObserverResult<long> &),
179-
nostd::string_view description = "",
180-
nostd::string_view unit = "") noexcept override
181-
{
182-
return nostd::shared_ptr<ObservableGauge<long>>{
183-
new NoopObservableGauge<long>(name, callback, description, unit)};
184-
}
168+
void CreateLongObservableGauge(nostd::string_view name,
169+
void (*callback)(ObserverResult<long> &),
170+
nostd::string_view description = "",
171+
nostd::string_view unit = "") noexcept override
172+
{}
185173

186-
nostd::shared_ptr<ObservableGauge<double>> CreateDoubleObservableGauge(
187-
nostd::string_view name,
188-
void (*callback)(ObserverResult<double> &),
189-
nostd::string_view description = "",
190-
nostd::string_view unit = "") noexcept override
191-
{
192-
return nostd::shared_ptr<ObservableGauge<double>>{
193-
new NoopObservableGauge<double>(name, callback, description, unit)};
194-
}
174+
void CreateDoubleObservableGauge(nostd::string_view name,
175+
void (*callback)(ObserverResult<double> &),
176+
nostd::string_view description = "",
177+
nostd::string_view unit = "") noexcept override
178+
{}
195179

196180
nostd::shared_ptr<UpDownCounter<long>> CreateLongUpDownCounter(
197181
nostd::string_view name,
@@ -211,25 +195,17 @@ class NoopMeter final : public Meter
211195
new NoopUpDownCounter<double>(name, description, unit)};
212196
}
213197

214-
nostd::shared_ptr<ObservableUpDownCounter<long>> CreateLongObservableUpDownCounter(
215-
nostd::string_view name,
216-
void (*callback)(ObserverResult<long> &),
217-
nostd::string_view description = "",
218-
nostd::string_view unit = "") noexcept override
219-
{
220-
return nostd::shared_ptr<ObservableUpDownCounter<long>>{
221-
new NoopObservableUpDownCounter<long>(name, callback, description, unit)};
222-
}
198+
void CreateLongObservableUpDownCounter(nostd::string_view name,
199+
void (*callback)(ObserverResult<long> &),
200+
nostd::string_view description = "",
201+
nostd::string_view unit = "") noexcept override
202+
{}
223203

224-
nostd::shared_ptr<ObservableUpDownCounter<double>> CreateDoubleObservableUpDownCounter(
225-
nostd::string_view name,
226-
void (*callback)(ObserverResult<double> &),
227-
nostd::string_view description = "",
228-
nostd::string_view unit = "") noexcept override
229-
{
230-
return nostd::shared_ptr<ObservableUpDownCounter<double>>{
231-
new NoopObservableUpDownCounter<double>(name, callback, description, unit)};
232-
}
204+
void CreateDoubleObservableUpDownCounter(nostd::string_view name,
205+
void (*callback)(ObserverResult<double> &),
206+
nostd::string_view description = "",
207+
nostd::string_view unit = "") noexcept override
208+
{}
233209
};
234210

235211
/**

examples/common/metrics_foo_library/foo_library.cc

+27
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
# include <chrono>
77
# include <map>
88
# include <thread>
9+
# include <vector>
10+
# include "opentelemetry/context/context.h"
911
# include "opentelemetry/metrics/provider.h"
1012

1113
namespace nostd = opentelemetry::nostd;
1214
namespace metrics_api = opentelemetry::metrics;
1315

1416
namespace
1517
{
18+
1619
std::map<std::string, std::string> get_random_attr()
1720
{
1821
static const std::vector<std::pair<std::string, std::string>> labels = {{"key1", "value1"},
@@ -23,6 +26,18 @@ std::map<std::string, std::string> get_random_attr()
2326
return std::map<std::string, std::string>{labels[rand() % (labels.size() - 1)],
2427
labels[rand() % (labels.size() - 1)]};
2528
}
29+
30+
class MeasurementFetcher
31+
{
32+
public:
33+
static void Fetcher(opentelemetry::metrics::ObserverResult<double> &observer_result)
34+
{
35+
double val = (rand() % 700) + 1.1;
36+
std::map<std::string, std::string> labels = get_random_attr();
37+
auto labelkv = opentelemetry::common::KeyValueIterableView<decltype(labels)>{labels};
38+
observer_result.Observe(val /*, labelkv*/);
39+
}
40+
};
2641
} // namespace
2742

2843
void foo_library::counter_example(const std::string &name)
@@ -40,6 +55,18 @@ void foo_library::counter_example(const std::string &name)
4055
}
4156
}
4257

58+
void foo_library::observable_counter_example(const std::string &name)
59+
{
60+
std::string counter_name = name + "_observable_counter";
61+
auto provider = metrics_api::Provider::GetMeterProvider();
62+
nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
63+
meter->CreateDoubleObservableCounter(counter_name, MeasurementFetcher::Fetcher);
64+
while (true)
65+
{
66+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
67+
}
68+
}
69+
4370
void foo_library::histogram_example(const std::string &name)
4471
{
4572
std::string histogram_name = name + "_histogram";

examples/common/metrics_foo_library/foo_library.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ class foo_library
1010
public:
1111
static void counter_example(const std::string &name);
1212
static void histogram_example(const std::string &name);
13+
static void observable_counter_example(const std::string &name);
1314
};
1415
#endif

examples/metrics_simple/metrics_ostream.cc

+19
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ void initMetrics(const std::string &name)
5454
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}};
5555
p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));
5656

57+
// observable counter view
58+
std::string observable_counter_name = name + "_observable_counter";
59+
std::unique_ptr<metric_sdk::InstrumentSelector> observable_instrument_selector{
60+
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kObservableCounter,
61+
observable_counter_name)};
62+
std::unique_ptr<metric_sdk::MeterSelector> observable_meter_selector{
63+
new metric_sdk::MeterSelector(name, version, schema)};
64+
std::unique_ptr<metric_sdk::View> observable_sum_view{
65+
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}};
66+
p->AddView(std::move(observable_instrument_selector), std::move(observable_meter_selector),
67+
std::move(observable_sum_view));
68+
5769
// histogram view
5870
std::string histogram_name = name + "_histogram";
5971
std::unique_ptr<metric_sdk::InstrumentSelector> histogram_instrument_selector{
@@ -83,15 +95,22 @@ int main(int argc, char **argv)
8395
{
8496
foo_library::counter_example(name);
8597
}
98+
else if (example_type == "observable_counter")
99+
{
100+
foo_library::observable_counter_example(name);
101+
}
86102
else if (example_type == "histogram")
87103
{
88104
foo_library::histogram_example(name);
89105
}
90106
else
91107
{
92108
std::thread counter_example{&foo_library::counter_example, name};
109+
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
93110
std::thread histogram_example{&foo_library::histogram_example, name};
111+
94112
counter_example.join();
113+
observable_counter_example.join();
95114
histogram_example.join();
96115
}
97116
}

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

+35-18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h"
99
# include "opentelemetry/sdk/metrics/instruments.h"
1010
# include "opentelemetry/sdk/metrics/meter_context.h"
11+
# include "opentelemetry/sdk/metrics/state/async_metric_storage.h"
12+
1113
# include "opentelemetry/sdk/resource/resource.h"
1214
# include "opentelemetry/version.h"
1315

@@ -40,18 +42,17 @@ class Meter final : public opentelemetry::metrics::Meter
4042
nostd::string_view description = "",
4143
nostd::string_view unit = "") noexcept override;
4244

43-
nostd::shared_ptr<opentelemetry::metrics::ObservableCounter<long>> CreateLongObservableCounter(
45+
void CreateLongObservableCounter(nostd::string_view name,
46+
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
47+
nostd::string_view description = "",
48+
nostd::string_view unit = "") noexcept override;
49+
50+
void CreateDoubleObservableCounter(
4451
nostd::string_view name,
45-
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
52+
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
4653
nostd::string_view description = "",
4754
nostd::string_view unit = "") noexcept override;
4855

49-
nostd::shared_ptr<opentelemetry::metrics::ObservableCounter<double>>
50-
CreateDoubleObservableCounter(nostd::string_view name,
51-
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
52-
nostd::string_view description = "",
53-
nostd::string_view unit = "1") noexcept override;
54-
5556
nostd::shared_ptr<opentelemetry::metrics::Histogram<long>> CreateLongHistogram(
5657
nostd::string_view name,
5758
nostd::string_view description = "",
@@ -62,13 +63,12 @@ class Meter final : public opentelemetry::metrics::Meter
6263
nostd::string_view description = "",
6364
nostd::string_view unit = "") noexcept override;
6465

65-
nostd::shared_ptr<opentelemetry::metrics::ObservableGauge<long>> CreateLongObservableGauge(
66-
nostd::string_view name,
67-
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
68-
nostd::string_view description = "",
69-
nostd::string_view unit = "") noexcept override;
66+
void CreateLongObservableGauge(nostd::string_view name,
67+
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
68+
nostd::string_view description = "",
69+
nostd::string_view unit = "") noexcept override;
7070

71-
nostd::shared_ptr<opentelemetry::metrics::ObservableGauge<double>> CreateDoubleObservableGauge(
71+
void CreateDoubleObservableGauge(
7272
nostd::string_view name,
7373
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
7474
nostd::string_view description = "",
@@ -84,15 +84,13 @@ class Meter final : public opentelemetry::metrics::Meter
8484
nostd::string_view description = "",
8585
nostd::string_view unit = "") noexcept override;
8686

87-
nostd::shared_ptr<opentelemetry::metrics::ObservableUpDownCounter<long>>
88-
CreateLongObservableUpDownCounter(
87+
void CreateLongObservableUpDownCounter(
8988
nostd::string_view name,
9089
void (*callback)(opentelemetry::metrics::ObserverResult<long> &),
9190
nostd::string_view description = "",
9291
nostd::string_view unit = "") noexcept override;
9392

94-
nostd::shared_ptr<opentelemetry::metrics::ObservableUpDownCounter<double>>
95-
CreateDoubleObservableUpDownCounter(
93+
void CreateDoubleObservableUpDownCounter(
9694
nostd::string_view name,
9795
void (*callback)(opentelemetry::metrics::ObserverResult<double> &),
9896
nostd::string_view description = "",
@@ -116,6 +114,25 @@ class Meter final : public opentelemetry::metrics::Meter
116114

117115
std::unique_ptr<WritableMetricStorage> RegisterMetricStorage(
118116
InstrumentDescriptor &instrument_descriptor);
117+
118+
template <class T>
119+
void RegisterAsyncMetricStorage(InstrumentDescriptor &instrument_descriptor,
120+
void (*callback)(opentelemetry::metrics::ObserverResult<T> &))
121+
{
122+
auto view_registry = meter_context_->GetViewRegistry();
123+
auto success = view_registry->FindViews(
124+
instrument_descriptor, *instrumentation_library_,
125+
[this, &instrument_descriptor, callback](const View &view) {
126+
auto view_instr_desc = instrument_descriptor;
127+
view_instr_desc.name_ = view.GetName();
128+
view_instr_desc.description_ = view.GetDescription();
129+
auto storage = std::shared_ptr<AsyncMetricStorage<T>>(
130+
new AsyncMetricStorage<T>(view_instr_desc, view.GetAggregationType(), callback,
131+
&view.GetAttributesProcessor()));
132+
storage_registry_[instrument_descriptor.name_] = storage;
133+
return true;
134+
});
135+
}
119136
};
120137
} // namespace metrics
121138
} // namespace sdk

0 commit comments

Comments
 (0)