]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/opentelemetry-cpp/examples/metrics_simple/main.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / examples / metrics_simple / main.cc
CommitLineData
1e59de90
TL
1// Copyright The OpenTelemetry Authors
2// SPDX-License-Identifier: Apache-2.0
3
4#ifdef ENABLE_METRICS_PREVIEW
5# include "opentelemetry/_metrics/provider.h"
6# include "opentelemetry/exporters/ostream/metrics_exporter.h"
7# include "opentelemetry/sdk/_metrics/controller.h"
8# include "opentelemetry/sdk/_metrics/meter.h"
9# include "opentelemetry/sdk/_metrics/meter_provider.h"
10# include "opentelemetry/sdk/_metrics/ungrouped_processor.h"
11
12namespace metric_sdk = opentelemetry::sdk::metrics;
13namespace nostd = opentelemetry::nostd;
14namespace common = opentelemetry::common;
15namespace exportermetrics = opentelemetry::exporter::metrics;
16namespace metrics_api = opentelemetry::metrics;
17
18int main()
19{
20 // Initialize and set the global MeterProvider
21 auto provider = nostd::shared_ptr<metrics_api::MeterProvider>(new metric_sdk::MeterProvider);
22 metrics_api::Provider::SetMeterProvider(provider);
23
24 // Get the Meter from the MeterProvider
25 nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter("Test", "0.1.0");
26
27 // Create the controller with Stateless Metrics Processor
28 metric_sdk::PushController ControllerStateless(
29 meter,
30 std::unique_ptr<metric_sdk::MetricsExporter>(new exportermetrics::OStreamMetricsExporter),
31 std::shared_ptr<metric_sdk::MetricsProcessor>(
32 new metric_sdk::UngroupedMetricsProcessor(false)),
33 .05);
34
35 // Create and instrument
36 auto intupdowncounter = meter->NewIntUpDownCounter("UpDownCounter", "None", "none", true);
37 auto intcounter = meter->NewIntCounter("Counter", "none", "none", true);
38
39 // Create a labelset
40 std::map<std::string, std::string> labels = {{"key", "value"}};
41 auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels};
42
43 // Create arrays of instrument and values to add to them
44 metrics_api::SynchronousInstrument<int> *iinstr_arr[] = {intupdowncounter.get(),
45 intcounter.get()};
46 int ivalues_arr[] = {10, 5};
47
48 // Change the arrays to be nostd::spans
49 nostd::span<metrics_api::SynchronousInstrument<int> *> instrument_span{iinstr_arr};
50 nostd::span<const int, 2> instrument_values{ivalues_arr};
51
52 /**
53 * First way of updating an instrument, RecordBatch. We can update multiple instruments at once by
54 * using a span of instruments and a span of values. This RecordBatch will update the ith
55 * instrument with the ith value.
56 **/
57 std::cout << "Example 1" << std::endl;
58 ControllerStateless.start();
59
60 // Updating multiple instruments with the same labelset
61 meter->RecordIntBatch(labelkv, instrument_span, instrument_values);
62
63 ControllerStateless.stop();
64 /**
65 * Second way of updating an instrument, bind then add. In this method the user binds an
66 *instrument to a labelset Then add to the bounded instrument, then unbind.
67 **/
68 std::cout << "Example 2" << std::endl;
69 ControllerStateless.start();
70
71 auto boundintupdowncounter = intupdowncounter->bindUpDownCounter(labelkv);
72 boundintupdowncounter->add(50);
73 boundintupdowncounter->unbind();
74
75 ControllerStateless.stop();
76 /**
77 * The Third and final way is to add a value with a labelset at the same time. This also shows
78 * The difference between using a Stateless and Stateful Processor
79 */
80
81 // Start exporting from the Controller with Stateless Processor
82 std::cout << "-----"
83 << " Stateless Processor "
84 << "-----" << std::endl;
85 ControllerStateless.start();
86 for (int i = 0; i < 20; i++)
87 {
88 intupdowncounter->add(i, labelkv);
89 std::this_thread::sleep_for(std::chrono::milliseconds(10));
90 }
91 ControllerStateless.stop();
92
93 // Do the same thing for stateful to see the difference
94 metric_sdk::PushController ControllerStateful(
95 meter,
96 std::unique_ptr<metric_sdk::MetricsExporter>(new exportermetrics::OStreamMetricsExporter),
97 std::shared_ptr<metric_sdk::MetricsProcessor>(
98 new metric_sdk::UngroupedMetricsProcessor(true)),
99 .05);
100
101 // Start exporting from the Controller with Stateful Processor
102 std::cout << "-----"
103 << " Stateful Processor "
104 << "-----" << std::endl;
105 ControllerStateful.start();
106 for (int i = 0; i < 20; i++)
107 {
108 intupdowncounter->add(i, labelkv);
109 std::this_thread::sleep_for(std::chrono::milliseconds(10));
110 }
111 ControllerStateful.stop();
112}
113#else
114int main()
115{
116 // empty
117}
118#endif