Skip to content

Commit f30ab92

Browse files
uuzaylalitbmarcalff
authored
[API] Add synchronous gauge (open-telemetry#3029)
* Add synchronous gauge * Add ABI Version macro, update tests * Fix function description * Fix formatting * Remove ABI macros from SDK. * Add error log for gauge delta temporality. * Fix formatting * Apply suggestions from code review Move kGauge to the end, for better ABI compatibility. --------- Co-authored-by: Lalit Kumar Bhasin <[email protected]> Co-authored-by: Marc Alff <[email protected]>
1 parent 6292a6a commit f30ab92

File tree

20 files changed

+664
-2
lines changed

20 files changed

+664
-2
lines changed

api/include/opentelemetry/metrics/meter.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class Histogram;
2121
template <typename T>
2222
class UpDownCounter;
2323

24+
template <typename T>
25+
class Gauge;
26+
2427
class ObservableInstrument;
2528

2629
/**
@@ -91,6 +94,27 @@ class Meter
9194
nostd::string_view description = "",
9295
nostd::string_view unit = "") noexcept = 0;
9396

97+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
98+
/**
99+
* Creates a Gauge with the passed characteristics and returns a unique_ptr to that Gauge.
100+
*
101+
* @param name the name of the new Gauge.
102+
* @param description a brief description of what the Gauge is used for.
103+
* @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html.
104+
* @return a unique pointer to the created Gauge.
105+
*/
106+
107+
virtual nostd::unique_ptr<Gauge<int64_t>> CreateInt64Gauge(
108+
nostd::string_view name,
109+
nostd::string_view description = "",
110+
nostd::string_view unit = "") noexcept = 0;
111+
112+
virtual nostd::unique_ptr<Gauge<double>> CreateDoubleGauge(
113+
nostd::string_view name,
114+
nostd::string_view description = "",
115+
nostd::string_view unit = "") noexcept = 0;
116+
#endif
117+
94118
/**
95119
* Creates a Asynchronous (Observable) Gauge with the passed characteristics and returns a
96120
* shared_ptr to that Observable Gauge

api/include/opentelemetry/metrics/noop.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ class NoopUpDownCounter : public UpDownCounter<T>
7171
{}
7272
};
7373

74+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
75+
template <class T>
76+
class NoopGauge : public Gauge<T>
77+
{
78+
public:
79+
NoopGauge(nostd::string_view /* name */,
80+
nostd::string_view /* description */,
81+
nostd::string_view /* unit */) noexcept
82+
{}
83+
~NoopGauge() override = default;
84+
void Record(T /* value */) noexcept override {}
85+
void Record(T /* value */, const context::Context & /* context */) noexcept override {}
86+
void Record(T /* value */, const common::KeyValueIterable & /* attributes */) noexcept override {}
87+
void Record(T /* value */,
88+
const common::KeyValueIterable & /* attributes */,
89+
const context::Context & /* context */) noexcept override
90+
{}
91+
};
92+
#endif
93+
7494
class NoopObservableInstrument : public ObservableInstrument
7595
{
7696
public:
@@ -140,6 +160,22 @@ class NoopMeter final : public Meter
140160
return nostd::unique_ptr<Histogram<double>>{new NoopHistogram<double>(name, description, unit)};
141161
}
142162

163+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
164+
nostd::unique_ptr<Gauge<int64_t>> CreateInt64Gauge(nostd::string_view name,
165+
nostd::string_view description = "",
166+
nostd::string_view unit = "") noexcept override
167+
{
168+
return nostd::unique_ptr<Gauge<int64_t>>{new NoopGauge<int64_t>(name, description, unit)};
169+
}
170+
171+
nostd::unique_ptr<Gauge<double>> CreateDoubleGauge(nostd::string_view name,
172+
nostd::string_view description = "",
173+
nostd::string_view unit = "") noexcept override
174+
{
175+
return nostd::unique_ptr<Gauge<double>>{new NoopGauge<double>(name, description, unit)};
176+
}
177+
#endif
178+
143179
nostd::shared_ptr<ObservableInstrument> CreateInt64ObservableGauge(
144180
nostd::string_view name,
145181
nostd::string_view description = "",

api/include/opentelemetry/metrics/sync_instruments.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,82 @@ class UpDownCounter : public SynchronousInstrument
247247
}
248248
};
249249

250+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
251+
/* A Gauge instrument that records values. */
252+
template <class T>
253+
class Gauge : public SynchronousInstrument
254+
{
255+
256+
public:
257+
/**
258+
* Record a value
259+
*
260+
* @param value The measurement value. May be positive, negative or zero.
261+
*/
262+
virtual void Record(T value) noexcept = 0;
263+
264+
/**
265+
* Record a value
266+
*
267+
* @param value The measurement value. May be positive, negative or zero.
268+
* @param context The explicit context to associate with this measurement.
269+
*/
270+
virtual void Record(T value, const context::Context &context) noexcept = 0;
271+
272+
/**
273+
* Record a value with a set of attributes.
274+
*
275+
* @param value The measurement value. May be positive, negative or zero.
276+
* @param attributes A set of attributes to associate with the value.
277+
*/
278+
279+
virtual void Record(T value, const common::KeyValueIterable &attributes) noexcept = 0;
280+
281+
/**
282+
* Record a value with a set of attributes.
283+
*
284+
* @param value The measurement value. May be positive, negative or zero.
285+
* @param attributes A set of attributes to associate with the value.
286+
* @param context The explicit context to associate with this measurement.
287+
*/
288+
virtual void Record(T value,
289+
const common::KeyValueIterable &attributes,
290+
const context::Context &context) noexcept = 0;
291+
292+
template <class U,
293+
nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
294+
void Record(T value, const U &attributes) noexcept
295+
{
296+
this->Record(value, common::KeyValueIterableView<U>{attributes});
297+
}
298+
299+
template <class U,
300+
nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
301+
void Record(T value, const U &attributes, const context::Context &context) noexcept
302+
{
303+
this->Record(value, common::KeyValueIterableView<U>{attributes}, context);
304+
}
305+
306+
void Record(T value,
307+
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
308+
attributes) noexcept
309+
{
310+
this->Record(value, nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
311+
attributes.begin(), attributes.end()});
312+
}
313+
314+
void Record(
315+
T value,
316+
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
317+
const context::Context &context) noexcept
318+
{
319+
this->Record(value,
320+
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
321+
attributes.begin(), attributes.end()},
322+
context);
323+
}
324+
};
325+
#endif
326+
250327
} // namespace metrics
251328
OPENTELEMETRY_END_NAMESPACE

examples/common/metrics_foo_library/foo_library.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,22 @@ void foo_library::histogram_example(const std::string &name)
107107
std::this_thread::sleep_for(std::chrono::milliseconds(250));
108108
}
109109
}
110+
111+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
112+
void foo_library::gauge_example(const std::string &name)
113+
{
114+
std::string gauge_name = name + "_gauge";
115+
auto provider = metrics_api::Provider::GetMeterProvider();
116+
opentelemetry::nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
117+
auto gauge = meter->CreateInt64Gauge(gauge_name, "des", "unit");
118+
auto context = opentelemetry::context::Context{};
119+
for (uint32_t i = 0; i < 20; ++i)
120+
{
121+
int64_t val = (rand() % 100) + 100;
122+
std::map<std::string, std::string> labels = get_random_attr();
123+
auto labelkv = opentelemetry::common::KeyValueIterableView<decltype(labels)>{labels};
124+
gauge->Record(val, labelkv, context);
125+
std::this_thread::sleep_for(std::chrono::milliseconds(250));
126+
}
127+
}
128+
#endif

examples/common/metrics_foo_library/foo_library.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ class foo_library
1111
static void counter_example(const std::string &name);
1212
static void histogram_example(const std::string &name);
1313
static void observable_counter_example(const std::string &name);
14+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
15+
static void gauge_example(const std::string &name);
16+
#endif
1417
};

examples/metrics_simple/metrics_ostream.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,27 @@ int main(int argc, char **argv)
148148
{
149149
foo_library::histogram_example(name);
150150
}
151+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
152+
else if (example_type == "gauge")
153+
{
154+
foo_library::gauge_example(name);
155+
}
156+
#endif
151157
else
152158
{
153159
std::thread counter_example{&foo_library::counter_example, name};
154160
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
155161
std::thread histogram_example{&foo_library::histogram_example, name};
162+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
163+
std::thread gauge_example{&foo_library::gauge_example, name};
164+
#endif
156165

157166
counter_example.join();
158167
observable_counter_example.join();
159168
histogram_example.join();
169+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
170+
gauge_example.join();
171+
#endif
160172
}
161173

162174
CleanupMetrics();

examples/otlp/file_metric_main.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,27 @@ int main(int argc, char *argv[])
9898
{
9999
foo_library::histogram_example(name);
100100
}
101+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
102+
else if (example_type == "gauge")
103+
{
104+
foo_library::gauge_example(name);
105+
}
106+
#endif
101107
else
102108
{
103109
std::thread counter_example{&foo_library::counter_example, name};
104110
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
105111
std::thread histogram_example{&foo_library::histogram_example, name};
112+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
113+
std::thread gauge_example{&foo_library::gauge_example, name};
114+
#endif
106115

107116
counter_example.join();
108117
observable_counter_example.join();
109118
histogram_example.join();
119+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
120+
gauge_example.join();
121+
#endif
110122
}
111123

112124
CleanupMetrics();

examples/otlp/grpc_metric_main.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,27 @@ int main(int argc, char *argv[])
9393
{
9494
foo_library::histogram_example(name);
9595
}
96+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
97+
else if (example_type == "gauge")
98+
{
99+
foo_library::gauge_example(name);
100+
}
101+
#endif
96102
else
97103
{
98104
std::thread counter_example{&foo_library::counter_example, name};
99105
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
100106
std::thread histogram_example{&foo_library::histogram_example, name};
107+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
108+
std::thread gauge_example{&foo_library::gauge_example, name};
109+
#endif
101110

102111
counter_example.join();
103112
observable_counter_example.join();
104113
histogram_example.join();
114+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
115+
gauge_example.join();
116+
#endif
105117
}
106118

107119
CleanupMetrics();

examples/otlp/http_metric_main.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,27 @@ int main(int argc, char *argv[])
133133
{
134134
foo_library::histogram_example(name);
135135
}
136+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
137+
else if (example_type == "gauge")
138+
{
139+
foo_library::gauge_example(name);
140+
}
141+
#endif
136142
else
137143
{
138144
std::thread counter_example{&foo_library::counter_example, name};
139145
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
140146
std::thread histogram_example{&foo_library::histogram_example, name};
147+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
148+
std::thread gauge_example{&foo_library::gauge_example, name};
149+
#endif
141150

142151
counter_example.join();
143152
observable_counter_example.join();
144153
histogram_example.join();
154+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
155+
gauge_example.join();
156+
#endif
145157
}
146158

147159
CleanupMetrics();

exporters/otlp/src/otlp_metric_utils.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ sdk::metrics::AggregationTemporality OtlpMetricUtils::DeltaTemporalitySelector(
297297
case sdk::metrics::InstrumentType::kObservableCounter:
298298
case sdk::metrics::InstrumentType::kHistogram:
299299
case sdk::metrics::InstrumentType::kObservableGauge:
300+
case sdk::metrics::InstrumentType::kGauge:
300301
return sdk::metrics::AggregationTemporality::kDelta;
301302
case sdk::metrics::InstrumentType::kUpDownCounter:
302303
case sdk::metrics::InstrumentType::kObservableUpDownCounter:
@@ -320,6 +321,7 @@ sdk::metrics::AggregationTemporality OtlpMetricUtils::LowMemoryTemporalitySelect
320321
case sdk::metrics::InstrumentType::kHistogram:
321322
return sdk::metrics::AggregationTemporality::kDelta;
322323
case sdk::metrics::InstrumentType::kObservableCounter:
324+
case sdk::metrics::InstrumentType::kGauge:
323325
case sdk::metrics::InstrumentType::kObservableGauge:
324326
case sdk::metrics::InstrumentType::kUpDownCounter:
325327
case sdk::metrics::InstrumentType::kObservableUpDownCounter:

0 commit comments

Comments
 (0)