]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | #ifdef ENABLE_METRICS_PREVIEW | |
6 | ||
7 | # include <iostream> | |
8 | # include <string> | |
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" | |
14 | ||
15 | OPENTELEMETRY_BEGIN_NAMESPACE | |
16 | namespace exporter | |
17 | { | |
18 | namespace metrics | |
19 | { | |
20 | ||
21 | /** | |
22 | * The OStreamMetricsExporter exports record data through an ostream | |
23 | */ | |
24 | class OStreamMetricsExporter final : public opentelemetry::sdk::metrics::MetricsExporter | |
25 | { | |
26 | public: | |
27 | /** | |
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 | |
31 | */ | |
32 | explicit OStreamMetricsExporter(std::ostream &sout = std::cout) noexcept; | |
33 | ||
34 | sdk::common::ExportResult Export( | |
35 | const std::vector<opentelemetry::sdk::metrics::Record> &records) noexcept override; | |
36 | ||
37 | private: | |
38 | std::ostream &sout_; | |
39 | ||
40 | /** | |
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 | |
43 | * custom printing. | |
44 | */ | |
45 | template <typename T> | |
46 | void PrintAggregatorVariant(opentelemetry::sdk::metrics::AggregatorVariant value) | |
47 | { | |
48 | auto agg = nostd::get<std::shared_ptr<opentelemetry::sdk::metrics::Aggregator<T>>>(value); | |
49 | auto aggKind = agg->get_aggregator_kind(); | |
50 | ||
51 | if (!agg) | |
52 | return; | |
53 | switch (aggKind) | |
54 | { | |
55 | case opentelemetry::sdk::metrics::AggregatorKind::Counter: { | |
56 | sout_ << "\n sum : " << agg->get_checkpoint()[0]; | |
57 | } | |
58 | break; | |
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]; | |
63 | } | |
64 | break; | |
65 | case opentelemetry::sdk::metrics::AggregatorKind::Gauge: { | |
66 | auto timestamp = agg->get_checkpoint_timestamp(); | |
67 | ||
68 | sout_ << "\n last value : " << agg->get_checkpoint()[0] | |
69 | << "\n timestamp : " << std::to_string(timestamp.time_since_epoch().count()); | |
70 | } | |
71 | break; | |
72 | case opentelemetry::sdk::metrics::AggregatorKind::Exact: { | |
73 | // TODO: Find better way to print quantiles | |
74 | if (agg->get_quant_estimation()) | |
75 | { | |
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) << ']'; | |
82 | } | |
83 | else | |
84 | { | |
85 | auto vec = agg->get_checkpoint(); | |
86 | size_t size = vec.size(); | |
87 | size_t i = 1; | |
88 | ||
89 | sout_ << "\n values : " << '['; | |
90 | ||
91 | for (auto val : vec) | |
92 | { | |
93 | sout_ << val; | |
94 | if (i != size) | |
95 | sout_ << ", "; | |
96 | i++; | |
97 | } | |
98 | sout_ << ']'; | |
99 | } | |
100 | } | |
101 | break; | |
102 | case opentelemetry::sdk::metrics::AggregatorKind::Histogram: { | |
103 | auto boundaries = agg->get_boundaries(); | |
104 | auto counts = agg->get_counts(); | |
105 | ||
106 | size_t boundaries_size = boundaries.size(); | |
107 | size_t counts_size = counts.size(); | |
108 | ||
109 | sout_ << "\n buckets : " << '['; | |
110 | ||
111 | for (size_t i = 0; i < boundaries_size; i++) | |
112 | { | |
113 | sout_ << boundaries[i]; | |
114 | ||
115 | if (i != boundaries_size - 1) | |
116 | sout_ << ", "; | |
117 | } | |
118 | sout_ << ']'; | |
119 | ||
120 | sout_ << "\n counts : " << '['; | |
121 | for (size_t i = 0; i < counts_size; i++) | |
122 | { | |
123 | sout_ << counts[i]; | |
124 | ||
125 | if (i != counts_size - 1) | |
126 | sout_ << ", "; | |
127 | } | |
128 | sout_ << ']'; | |
129 | } | |
130 | break; | |
131 | case opentelemetry::sdk::metrics::AggregatorKind::Sketch: { | |
132 | auto boundaries = agg->get_boundaries(); | |
133 | auto counts = agg->get_counts(); | |
134 | ||
135 | size_t boundaries_size = boundaries.size(); | |
136 | size_t counts_size = counts.size(); | |
137 | ||
138 | sout_ << "\n buckets : " << '['; | |
139 | ||
140 | for (size_t i = 0; i < boundaries_size; i++) | |
141 | { | |
142 | sout_ << boundaries[i]; | |
143 | ||
144 | if (i != boundaries_size - 1) | |
145 | sout_ << ", "; | |
146 | } | |
147 | sout_ << ']'; | |
148 | ||
149 | sout_ << "\n counts : " << '['; | |
150 | for (size_t i = 0; i < counts_size; i++) | |
151 | { | |
152 | sout_ << counts[i]; | |
153 | ||
154 | if (i != counts_size - 1) | |
155 | sout_ << ", "; | |
156 | } | |
157 | sout_ << ']'; | |
158 | } | |
159 | break; | |
160 | } | |
161 | } | |
162 | }; | |
163 | } // namespace metrics | |
164 | } // namespace exporter | |
165 | OPENTELEMETRY_END_NAMESPACE | |
166 | #endif |