1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
5 #ifdef ENABLE_METRICS_PREVIEW
9 # include "opentelemetry/sdk/_metrics/aggregator/exact_aggregator.h"
10 # include "opentelemetry/sdk/_metrics/aggregator/gauge_aggregator.h"
11 # include "opentelemetry/sdk/_metrics/aggregator/histogram_aggregator.h"
12 # include "opentelemetry/sdk/_metrics/exporter.h"
13 # include "opentelemetry/sdk/_metrics/record.h"
15 OPENTELEMETRY_BEGIN_NAMESPACE
22 * The OStreamMetricsExporter exports record data through an ostream
24 class OStreamMetricsExporter final
: public opentelemetry::sdk::metrics::MetricsExporter
28 * Create an OStreamMetricsExporter. This constructor takes in a reference to an ostream that the
29 * export() function will send span data into.
30 * The default ostream is set to stdout
32 explicit OStreamMetricsExporter(std::ostream
&sout
= std::cout
) noexcept
;
34 sdk::common::ExportResult
Export(
35 const std::vector
<opentelemetry::sdk::metrics::Record
> &records
) noexcept override
;
41 * Send specific data from the given AggregatorVariant based on what AggregatorKind
42 * it is holding. Each Aggregator holds data differently, so each have their own
46 void PrintAggregatorVariant(opentelemetry::sdk::metrics::AggregatorVariant value
)
48 auto agg
= nostd::get
<std::shared_ptr
<opentelemetry::sdk::metrics::Aggregator
<T
>>>(value
);
49 auto aggKind
= agg
->get_aggregator_kind();
55 case opentelemetry::sdk::metrics::AggregatorKind::Counter
: {
56 sout_
<< "\n sum : " << agg
->get_checkpoint()[0];
59 case opentelemetry::sdk::metrics::AggregatorKind::MinMaxSumCount
: {
60 auto mmsc
= agg
->get_checkpoint();
61 sout_
<< "\n min : " << mmsc
[0] << "\n max : " << mmsc
[1]
62 << "\n sum : " << mmsc
[2] << "\n count : " << mmsc
[3];
65 case opentelemetry::sdk::metrics::AggregatorKind::Gauge
: {
66 auto timestamp
= agg
->get_checkpoint_timestamp();
68 sout_
<< "\n last value : " << agg
->get_checkpoint()[0]
69 << "\n timestamp : " << std::to_string(timestamp
.time_since_epoch().count());
72 case opentelemetry::sdk::metrics::AggregatorKind::Exact
: {
73 // TODO: Find better way to print quantiles
74 if (agg
->get_quant_estimation())
76 sout_
<< "\n quantiles : "
77 << "[0: " << agg
->get_quantiles(0) << ", "
78 << ".25: " << agg
->get_quantiles(.25) << ", "
79 << ".50: " << agg
->get_quantiles(.50) << ", "
80 << ".75: " << agg
->get_quantiles(.75) << ", "
81 << "1: " << agg
->get_quantiles(1) << ']';
85 auto vec
= agg
->get_checkpoint();
86 size_t size
= vec
.size();
89 sout_
<< "\n values : " << '[';
102 case opentelemetry::sdk::metrics::AggregatorKind::Histogram
: {
103 auto boundaries
= agg
->get_boundaries();
104 auto counts
= agg
->get_counts();
106 size_t boundaries_size
= boundaries
.size();
107 size_t counts_size
= counts
.size();
109 sout_
<< "\n buckets : " << '[';
111 for (size_t i
= 0; i
< boundaries_size
; i
++)
113 sout_
<< boundaries
[i
];
115 if (i
!= boundaries_size
- 1)
120 sout_
<< "\n counts : " << '[';
121 for (size_t i
= 0; i
< counts_size
; i
++)
125 if (i
!= counts_size
- 1)
131 case opentelemetry::sdk::metrics::AggregatorKind::Sketch
: {
132 auto boundaries
= agg
->get_boundaries();
133 auto counts
= agg
->get_counts();
135 size_t boundaries_size
= boundaries
.size();
136 size_t counts_size
= counts
.size();
138 sout_
<< "\n buckets : " << '[';
140 for (size_t i
= 0; i
< boundaries_size
; i
++)
142 sout_
<< boundaries
[i
];
144 if (i
!= boundaries_size
- 1)
149 sout_
<< "\n counts : " << '[';
150 for (size_t i
= 0; i
< counts_size
; i
++)
154 if (i
!= counts_size
- 1)
163 } // namespace metrics
164 } // namespace exporter
165 OPENTELEMETRY_END_NAMESPACE