Skip to content

Commit 423e92d

Browse files
committed
add ostream exporter and example
1 parent b03d915 commit 423e92d

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

examples/metrics_simple/metrics_ostream.cc

+32-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "opentelemetry/sdk/metrics/instruments.h"
1818
#include "opentelemetry/sdk/metrics/meter_provider.h"
1919
#include "opentelemetry/sdk/metrics/meter_provider_factory.h"
20+
#include "opentelemetry/sdk/metrics/meter_context_factory.h"
2021
#include "opentelemetry/sdk/metrics/metric_reader.h"
2122
#include "opentelemetry/sdk/metrics/provider.h"
2223
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
@@ -56,9 +57,9 @@ void InitMetrics(const std::string &name)
5657
auto reader =
5758
metrics_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options);
5859

59-
auto provider = opentelemetry::sdk::metrics::MeterProviderFactory::Create();
60-
61-
provider->AddMetricReader(std::move(reader));
60+
auto context = metrics_sdk::MeterContextFactory::Create();
61+
context->AddMetricReader(std::move(reader));
62+
auto provider = opentelemetry::sdk::metrics::MeterProviderFactory::Create(std::move(context));
6263

6364
// counter view
6465
std::string counter_name = name + "_counter";
@@ -112,6 +113,28 @@ void InitMetrics(const std::string &name)
112113
provider->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
113114
std::move(histogram_view));
114115

116+
// hisogram view with base2 exponential aggregation
117+
std::string histogram_base2_name = name + "_exponential_histogram";
118+
unit = "histogram-unit";
119+
auto histogram_base2_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
120+
metrics_sdk::InstrumentType::kHistogram, histogram_base2_name, unit);
121+
auto histogram_base2_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version,
122+
schema);
123+
auto histogram_base2_aggregation_config = std::unique_ptr<metrics_sdk::Base2ExponentialHistogramAggregationConfig>(
124+
new metrics_sdk::Base2ExponentialHistogramAggregationConfig);
125+
histogram_base2_aggregation_config->max_scale_ = 3;
126+
histogram_base2_aggregation_config->record_min_max_ = true;
127+
histogram_base2_aggregation_config->max_buckets_ = 100;
128+
129+
std::shared_ptr<metrics_sdk::AggregationConfig> base2_aggregation_config(
130+
std::move(histogram_base2_aggregation_config));
131+
132+
auto histogram_base2_view = metrics_sdk::ViewFactory::Create(
133+
name, "description", unit, metrics_sdk::AggregationType::kBase2ExponentialHistogram,
134+
base2_aggregation_config);
135+
136+
provider->AddView(std::move(histogram_base2_instrument_selector), std::move(histogram_base2_meter_selector), std::move(histogram_base2_view));
137+
115138
std::shared_ptr<opentelemetry::metrics::MeterProvider> api_provider(std::move(provider));
116139

117140
metrics_sdk::Provider::SetMeterProvider(api_provider);
@@ -147,6 +170,10 @@ int main(int argc, char **argv)
147170
{
148171
foo_library::histogram_example(name);
149172
}
173+
else if (example_type == "exponential_histogram")
174+
{
175+
foo_library::histogram_exp_example(name);
176+
}
150177
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
151178
else if (example_type == "gauge")
152179
{
@@ -170,6 +197,7 @@ int main(int argc, char **argv)
170197
std::thread counter_example{&foo_library::counter_example, name};
171198
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
172199
std::thread histogram_example{&foo_library::histogram_example, name};
200+
std::thread histogram_exp_example{&foo_library::histogram_exp_example, name};
173201
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
174202
std::thread gauge_example{&foo_library::gauge_example, name};
175203
#endif
@@ -181,6 +209,7 @@ int main(int argc, char **argv)
181209
counter_example.join();
182210
observable_counter_example.join();
183211
histogram_example.join();
212+
histogram_exp_example.join();
184213
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
185214
gauge_example.join();
186215
#endif

exporters/ostream/src/metric_exporter.cc

+31
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,37 @@ void OStreamMetricExporter::printPointData(const opentelemetry::sdk::metrics::Po
247247
sout_ << nostd::get<int64_t>(last_point_data.value_);
248248
}
249249
}
250+
else if (nostd::holds_alternative<sdk::metrics::Base2ExponentialHistogramPointData>(point_data))
251+
{
252+
auto histogram_point_data =
253+
nostd::get<sdk::metrics::Base2ExponentialHistogramPointData>(point_data);
254+
sout_ << "\n type: Base2ExponentialHistogramPointData";
255+
sout_ << "\n count: " << histogram_point_data.count_;
256+
sout_ << "\n sum: " << histogram_point_data.sum_;
257+
sout_ << "\n zero_count: " << histogram_point_data.zero_count_;
258+
if (histogram_point_data.record_min_max_)
259+
{
260+
sout_ << "\n min: " << histogram_point_data.min_;
261+
sout_ << "\n max: " << histogram_point_data.max_;
262+
}
263+
sout_ << "\n scale: " << histogram_point_data.scale_;
264+
sout_ << "\n positive buckets: ";
265+
if (!histogram_point_data.positive_buckets_.Empty())
266+
{
267+
for (auto i = histogram_point_data.positive_buckets_.StartIndex(); i <= histogram_point_data.positive_buckets_.EndIndex(); ++i)
268+
{
269+
sout_ << "\n\t" << i << ": " << histogram_point_data.positive_buckets_.Get(i);
270+
}
271+
}
272+
sout_ << "\n negative buckets: ";
273+
if (!histogram_point_data.negative_buckets_.Empty())
274+
{
275+
for (auto i = histogram_point_data.negative_buckets_.StartIndex(); i <= histogram_point_data.negative_buckets_.EndIndex(); ++i)
276+
{
277+
sout_ << "\n\t" << i << ": " << histogram_point_data.negative_buckets_.Get(i);
278+
}
279+
}
280+
}
250281
}
251282

252283
void OStreamMetricExporter::printPointAttributes(

0 commit comments

Comments
 (0)