-
Notifications
You must be signed in to change notification settings - Fork 497
Description
By refer to example code metrics_ostream.cc(https://github.com/open-telemetry/opentelemetry-cpp/blob/v1.11.0/examples/metrics_simple/metrics_ostream.cc) and foo_library.cc(https://github.com/open-telemetry/opentelemetry-cpp/blob/v1.11.0/examples/common/metrics_foo_library/foo_library.cc), my code just merge the two files into one file such as below(all the below code are copy and paste from above two files)
otel.cpp (warp by myself)
#include <memory>
#include <thread>
#include "opentelemetry/exporters/ostream/metric_exporter_factory.h"
#include "opentelemetry/metrics/provider.h"
#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
#include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h"
#include "opentelemetry/sdk/metrics/meter.h"
#include "opentelemetry/sdk/metrics/meter_provider.h"
#include "opentelemetry/sdk/metrics/meter_provider_factory.h"
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
#include "opentelemetry/sdk/metrics/view/instrument_selector_factory.h"
#include "opentelemetry/sdk/metrics/view/meter_selector_factory.h"
#include "opentelemetry/sdk/metrics/view/view_factory.h"
#include "opentelemetry/context/context.h"
#include "opentelemetry/nostd/shared_ptr.h"
namespace nostd = opentelemetry::nostd;
namespace metrics_sdk = opentelemetry::sdk::metrics;
namespace common = opentelemetry::common;
namespace metrics_api = opentelemetry::metrics;
namespace exportermetrics = opentelemetry::exporter::metrics;
namespace
{
std::map<std::string, std::string> get_random_attr()
{
const std::vector<std::pair<std::string, std::string> > labels = {{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"},
{"key4", "value4"},
{"key5", "value5"}};
return std::map<std::string, std::string>{labels[rand() % (labels.size() - 1)],
labels[rand() % (labels.size() - 1)]};
}
void InitMetrics(const std::string &name)
{
auto exporter = exportermetrics::OStreamMetricExporterFactory::Create();
std::string version{"1.2.0"};
std::string schema{"https://opentelemetry.io/schemas/1.2.0"};
// Initialize and set the global MeterProvider
metrics_sdk::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000);
options.export_timeout_millis = std::chrono::milliseconds(500);
auto reader = metrics_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options);
auto u_provider = metrics_sdk::MeterProviderFactory::Create();
auto *p = static_cast<metrics_sdk::MeterProvider *>(u_provider.get());
p->AddMetricReader(std::move(reader));
/*metrics_exporter::PrometheusExporterOptions opts;
if (!addr.empty())
{
opts.url = addr;
}
*/
// std::puts("PrometheusExporter example program running ...");
// auto prometheus_exporter = metrics_exporter::PrometheusExporterFactory::Create(opts);
// auto exporter = exportermetrics::OStreamMetricExporterFactory::Create();
// std::string version{"1.2.0"};
// std::string schema{"https://opentelemetry.io/schemas/1.2.0"};
// Initialize and set the global MeterProvider
// metrics_sdk::PeriodicExportingMetricReaderOptions options;
// options.export_interval_millis = std::chrono::milliseconds(1000);
// options.export_timeout_millis = std::chrono::milliseconds(500);
// auto reader =
// metrics_sdk::PeriodicExportingMetricReaderFactory::Create(std::move(exporter), options);
// auto u_provider = metrics_sdk::MeterProviderFactory::Create();
// auto *p = static_cast<metrics_sdk::MeterProvider *>(u_provider.get());
// p->AddMetricReader(std::move(prometheus_exporter));
// histogram view
std::string histogram_name = name + "_histogram";
std::string unit = "histogram-unit";
auto histogram_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kHistogram, histogram_name, unit);
auto histogram_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);
auto histogram_aggregation_config = std::unique_ptr<metrics_sdk::HistogramAggregationConfig>(
new metrics_sdk::HistogramAggregationConfig);
histogram_aggregation_config->boundaries_ = std::vector<double>{
0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0, 20000.0};
std::shared_ptr<metrics_sdk::AggregationConfig> aggregation_config(
std::move(histogram_aggregation_config));
auto histogram_view = metrics_sdk::ViewFactory::Create(
name, "description", unit, metrics_sdk::AggregationType::kHistogram, aggregation_config);
p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));
std::shared_ptr<opentelemetry::metrics::MeterProvider> provider(std::move(u_provider));
metrics_api::Provider::SetMeterProvider(provider);
}
void CleanupMetrics()
{
std::shared_ptr<metrics_api::MeterProvider> none;
metrics_api::Provider::SetMeterProvider(none);
}
} // namespace
void histogram_example(const std::string &name)
{
std::string histogram_name = name + "_histogram";
auto provider = metrics_api::Provider::GetMeterProvider();
nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
auto histogram_counter = meter->CreateDoubleHistogram(histogram_name, "des", "histogram-unit");
auto context = opentelemetry::context::Context{};
for (uint32_t i = 0; i < 20; ++i)
{
double val = (rand() % 700) + 1.1;
std::map<std::string, std::string> labels = get_random_attr();
auto labelkv = opentelemetry::common::KeyValueIterableView<decltype(labels)>{labels};
std::cout << "histogram val is " << val << std::endl;
histogram_counter->Record(val, labelkv, context);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
}
int main(int argc, char **argv)
{
std::string name{"ostream_metric_example"};
InitMetrics(name);
histogram_example(name);
CleanupMetrics();
return 0;
}
The program output on my local box such as:
[Error] File: /home/developer/opentelemetry-cpp-git/sdk/include/opentelemetry/sdk/metrics/view/predicate.h:38 PatternPredicate::Match - failed. std::regex not fully supported for this compiler.
histogram val is 184.1
histogram val is 216.1
histogram val is 487.1
histogram val is 522.1
{
scope name : ostream_metric_example
schema url :
version : 1.2.0
start time : Tue Aug 26 03:02:59 2025
end time : Tue Aug 26 03:03:00 2025
instrument name : ostream_metric_example_histogram
description : des
unit : histogram-unit
type : HistogramPointData
count : 1
sum : 216.1
min : 216.1
max : 216.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key4: value4
type : HistogramPointData
count : 1
sum : 487.1
min : 487.1
max : 487.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key2: value2
type : HistogramPointData
count : 1
sum : 184.1
min : 184.1
max : 184.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key3: value3
type : HistogramPointData
count : 1
sum : 522.1
min : 522.1
max : 522.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key3: value3
key4: value4
resources :
service.name: unknown_service
telemetry.sdk.language: cpp
telemetry.sdk.name: opentelemetry
telemetry.sdk.version: 1.11.0
}
histogram val is 91.1
histogram val is 127.1
histogram val is 73.1
histogram val is 169.1
{
scope name : ostream_metric_example
schema url :
version : 1.2.0
start time : Tue Aug 26 03:02:59 2025
end time : Tue Aug 26 03:03:01 2025
instrument name : ostream_metric_example_histogram
description : des
unit : histogram-unit
type : HistogramPointData
count : 1
sum : 487.1
min : 487.1
max : 487.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key2: value2
type : HistogramPointData
count : 1
sum : 522.1
min : 522.1
max : 522.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key3: value3
key4: value4
type : HistogramPointData
count : 1
sum : 184.1
min : 184.1
max : 184.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key3: value3
type : HistogramPointData
count : 1
sum : 91.1
min : 91.1
max : 91.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key4: value4
type : HistogramPointData
count : 1
sum : 127.1
min : 127.1
max : 127.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key3: value3
type : HistogramPointData
count : 1
sum : 73.1
min : 73.1
max : 73.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key4: value4
type : HistogramPointData
count : 2
sum : 385.2
min : 169.1
max : 216.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key4: value4
resources :
service.name: unknown_service
telemetry.sdk.language: cpp
telemetry.sdk.name: opentelemetry
telemetry.sdk.version: 1.11.0
}
histogram val is 283.1
histogram val is 224.1
histogram val is 230.1
histogram val is 559.1
{
scope name : ostream_metric_example
schema url :
version : 1.2.0
start time : Tue Aug 26 03:02:59 2025
end time : Tue Aug 26 03:03:02 2025
instrument name : ostream_metric_example_histogram
description : des
unit : histogram-unit
type : HistogramPointData
count : 1
sum : 73.1
min : 73.1
max : 73.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key4: value4
type : HistogramPointData
count : 1
sum : 127.1
min : 127.1
max : 127.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key3: value3
type : HistogramPointData
count : 1
sum : 487.1
min : 487.1
max : 487.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key2: value2
type : HistogramPointData
count : 1
sum : 184.1
min : 184.1
max : 184.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key3: value3
type : HistogramPointData
count : 1
sum : 522.1
min : 522.1
max : 522.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key3: value3
key4: value4
type : HistogramPointData
count : 2
sum : 315.2
min : 91.1
max : 224.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key4: value4
type : HistogramPointData
count : 2
sum : 513.2
min : 230.1
max : 283.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key3: value3
type : HistogramPointData
count : 3
sum : 944.3
min : 169.1
max : 559.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key4: value4
resources :
service.name: unknown_service
telemetry.sdk.language: cpp
telemetry.sdk.name: opentelemetry
telemetry.sdk.version: 1.11.0
}
histogram val is 394.1
histogram val is 143.1
histogram val is 622.1
histogram val is 438.1
{
scope name : ostream_metric_example
schema url :
version : 1.2.0
start time : Tue Aug 26 03:02:59 2025
end time : Tue Aug 26 03:03:03 2025
instrument name : ostream_metric_example_histogram
description : des
unit : histogram-unit
type : HistogramPointData
count : 3
sum : 944.3
min : 169.1
max : 559.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key4: value4
type : HistogramPointData
count : 2
sum : 513.2
min : 230.1
max : 283.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key3: value3
type : HistogramPointData
count : 2
sum : 315.2
min : 91.1
max : 224.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key4: value4
type : HistogramPointData
count : 1
sum : 522.1
min : 522.1
max : 522.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key3: value3
key4: value4
type : HistogramPointData
count : 1
sum : 184.1
min : 184.1
max : 184.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key3: value3
type : HistogramPointData
count : 1
sum : 487.1
min : 487.1
max : 487.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key2: value2
type : HistogramPointData
count : 2
sum : 565.2
min : 127.1
max : 438.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key3: value3
type : HistogramPointData
count : 1
sum : 143.1
min : 143.1
max : 143.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
type : HistogramPointData
count : 3
sum : 1089.3
min : 73.1
max : 622.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key4: value4
resources :
service.name: unknown_service
telemetry.sdk.language: cpp
telemetry.sdk.name: opentelemetry
telemetry.sdk.version: 1.11.0
}
histogram val is 416.1
histogram val is 27.1
histogram val is 457.1
histogram val is 271.1
{
scope name : ostream_metric_example
schema url :
version : 1.2.0
start time : Tue Aug 26 03:02:59 2025
end time : Tue Aug 26 03:03:04 2025
instrument name : ostream_metric_example_histogram
description : des
unit : histogram-unit
type : HistogramPointData
count : 1
sum : 143.1
min : 143.1
max : 143.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
type : HistogramPointData
count : 2
sum : 565.2
min : 127.1
max : 438.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key3: value3
type : HistogramPointData
count : 2
sum : 513.2
min : 230.1
max : 283.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key3: value3
type : HistogramPointData
count : 3
sum : 944.3
min : 169.1
max : 559.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key4: value4
type : HistogramPointData
count : 4
sum : 1116.4
min : 27.1
max : 622.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key4: value4
type : HistogramPointData
count : 1
sum : 522.1
min : 522.1
max : 522.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ]
attributes :
key3: value3
key4: value4
type : HistogramPointData
count : 2
sum : 315.2
min : 91.1
max : 224.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key4: value4
type : HistogramPointData
count : 3
sum : 1057.3
min : 184.1
max : 457.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key2: value2
key3: value3
type : HistogramPointData
count : 2
sum : 758.2
min : 271.1
max : 487.1
buckets : [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ]
counts : [0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, ]
attributes :
key1: value1
key2: value2
resources :
service.name: unknown_service
telemetry.sdk.language: cpp
telemetry.sdk.name: opentelemetry
telemetry.sdk.version: 1.11.0
}
The buckets is the default explicit aggregation buckets [0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000, ] instead of the custom bucket {
0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0, 20000.0}, counter also is caculated based on the default explicit aggregation buckets instead of the custom bucket{
0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0, 20000.0};
As for our project, we need use custom buckets for histogram instrument type. Above otel.cpp is for testing purpose.
Could you help me on why the example code from opentelmetry-cpp v1.11.0 not working for custom buckets of histogram?