Skip to content

Commit 415e23e

Browse files
committed
merged from upstream
2 parents 6e434e0 + f30ab92 commit 415e23e

File tree

21 files changed

+699
-3
lines changed

21 files changed

+699
-3
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

cmake/opentelemetry-cpp-config.cmake.in

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,39 @@ set(OPENTELEMETRY_VERSION
7070

7171
# ##############################################################################
7272

73-
find_package(Threads)
73+
include(CMakeFindDependencyMacro)
74+
75+
find_dependency(Threads)
76+
77+
if(@WITH_ABSEIL@)
78+
find_dependency(absl)
79+
endif()
80+
81+
if(@WITH_OTLP_GRPC@)
82+
find_dependency(gRPC)
83+
endif()
84+
85+
if("@OpenTracing_FOUND@")
86+
find_dependency(OpenTracing)
87+
endif()
88+
89+
if("@prometheus-cpp_FOUND@")
90+
find_dependency(prometheus-cpp)
91+
endif()
92+
93+
if("@Protobuf_FOUND@" OR "@PROTOBUF_FOUND@")
94+
find_dependency(Protobuf)
95+
endif()
96+
97+
if (@WITH_HTTP_CLIENT_CURL@ AND NOT @BUILD_SHARED_LIBS@)
98+
if("@CURL_FOUND@")
99+
find_dependency(CURL)
100+
endif()
101+
102+
if("@ZLIB_FOUND@")
103+
find_dependency(ZLIB)
104+
endif()
105+
endif()
74106

75107
set_and_check(OPENTELEMETRY_CPP_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIR@")
76108
set_and_check(OPENTELEMETRY_CPP_LIBRARY_DIRS "@PACKAGE_CMAKE_INSTALL_LIBDIR@")

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55

66
#include <string>
77

8+
#include "opentelemetry/version.h"
9+
810
class foo_library
911
{
1012
public:
1113
static void counter_example(const std::string &name);
1214
static void histogram_example(const std::string &name);
1315
static void observable_counter_example(const std::string &name);
16+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
17+
static void gauge_example(const std::string &name);
18+
#endif
1419
};

examples/metrics_simple/metrics_ostream.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,27 @@ int main(int argc, char **argv)
144144
{
145145
foo_library::histogram_example(name);
146146
}
147+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
148+
else if (example_type == "gauge")
149+
{
150+
foo_library::gauge_example(name);
151+
}
152+
#endif
147153
else
148154
{
149155
std::thread counter_example{&foo_library::counter_example, name};
150156
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
151157
std::thread histogram_example{&foo_library::histogram_example, name};
158+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
159+
std::thread gauge_example{&foo_library::gauge_example, name};
160+
#endif
152161

153162
counter_example.join();
154163
observable_counter_example.join();
155164
histogram_example.join();
165+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
166+
gauge_example.join();
167+
#endif
156168
}
157169

158170
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
@@ -89,15 +89,27 @@ int main(int argc, char *argv[])
8989
{
9090
foo_library::histogram_example(name);
9191
}
92+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
93+
else if (example_type == "gauge")
94+
{
95+
foo_library::gauge_example(name);
96+
}
97+
#endif
9298
else
9399
{
94100
std::thread counter_example{&foo_library::counter_example, name};
95101
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
96102
std::thread histogram_example{&foo_library::histogram_example, name};
103+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
104+
std::thread gauge_example{&foo_library::gauge_example, name};
105+
#endif
97106

98107
counter_example.join();
99108
observable_counter_example.join();
100109
histogram_example.join();
110+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
111+
gauge_example.join();
112+
#endif
101113
}
102114

103115
CleanupMetrics();

examples/otlp/http_metric_main.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,27 @@ int main(int argc, char *argv[])
129129
{
130130
foo_library::histogram_example(name);
131131
}
132+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
133+
else if (example_type == "gauge")
134+
{
135+
foo_library::gauge_example(name);
136+
}
137+
#endif
132138
else
133139
{
134140
std::thread counter_example{&foo_library::counter_example, name};
135141
std::thread observable_counter_example{&foo_library::observable_counter_example, name};
136142
std::thread histogram_example{&foo_library::histogram_example, name};
143+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
144+
std::thread gauge_example{&foo_library::gauge_example, name};
145+
#endif
137146

138147
counter_example.join();
139148
observable_counter_example.join();
140149
histogram_example.join();
150+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
151+
gauge_example.join();
152+
#endif
141153
}
142154

143155
CleanupMetrics();

0 commit comments

Comments
 (0)