Skip to content

Commit aba976e

Browse files
DemetrisChravsej
authored andcommitted
CXXCBC-622: Update otel metrics integration to use GA Metrics API
1 parent 3d9e4bf commit aba976e

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

core/meta/features.hxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,9 @@
195195
* encode document content or decode results.
196196
*/
197197
#define COUCHBASE_CXX_CLIENT_PUBLIC_API_USES_TAO_JSON_ONLY_FOR_CONTENT
198+
199+
/**
200+
* couchbase::metrics::otel_meter uses the GA version of the OpenTelemetry Metrics API.
201+
* The Metrics API for OpenTelemetry was GA'd in version 1.7.0.
202+
*/
203+
#define COUCHBASE_CXX_CLIENT_OTEL_METER_USES_GA_METRICS_API 1

couchbase/metrics/otel_meter.hxx

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "opentelemetry/sdk/metrics/meter.h"
2121
#include <couchbase/metrics/meter.hxx>
2222

23+
#include <algorithm>
2324
#include <iostream>
2425
#include <thread>
2526
#include <utility>
@@ -37,58 +38,53 @@ namespace couchbase::metrics
3738
class otel_sync_histogram
3839
{
3940
public:
40-
otel_sync_histogram(nostd::shared_ptr<metrics_api::Histogram<long>> histogram_counter)
41+
otel_sync_histogram(nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter)
4142
: histogram_counter_(histogram_counter)
4243
{
4344
}
44-
void record(uint64_t value,
45+
46+
void record(std::uint64_t value,
4547
const opentelemetry::common::KeyValueIterable& tags,
4648
opentelemetry::context::Context& ctx)
4749
{
48-
// overflow
49-
if (value > LONG_MAX) {
50-
value = LONG_MAX;
51-
}
52-
long lvalue = static_cast<long>(value);
53-
histogram_counter_->Record(lvalue, tags, ctx);
50+
histogram_counter_->Record(value, tags, ctx);
5451
}
5552

5653
private:
57-
nostd::shared_ptr<metrics_api::Histogram<long>> histogram_counter_;
54+
nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter_;
5855
std::mutex mutex_;
5956
};
6057

6158
class otel_value_recorder : public couchbase::metrics::value_recorder
6259
{
6360
public:
64-
explicit otel_value_recorder(nostd::shared_ptr<metrics_api::Histogram<long>> histogram_counter,
65-
const std::map<std::string, std::string>& tags)
61+
explicit otel_value_recorder(
62+
nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter,
63+
const std::map<std::string, std::string>& tags)
6664
: histogram_counter_(histogram_counter)
6765
, tags_(tags)
6866
{
6967
}
7068
void record_value(std::int64_t value) override
7169
{
72-
if (value > LONG_MAX) {
73-
value = LONG_MAX;
74-
}
75-
long lvalue = static_cast<long>(value);
70+
value = std::max<int64_t>(value, 0);
71+
auto uvalue = static_cast<std::uint64_t>(value);
7672
histogram_counter_->Record(
77-
value, opentelemetry::common::KeyValueIterableView<decltype(tags_)>{ tags_ }, context_);
73+
uvalue, opentelemetry::common::KeyValueIterableView<decltype(tags_)>{ tags_ }, context_);
7874
}
7975

8076
const std::map<std::string, std::string> tags()
8177
{
8278
return tags_;
8379
}
8480

85-
nostd::shared_ptr<metrics_api::Histogram<long>> histogram_counter()
81+
nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter()
8682
{
8783
return histogram_counter_;
8884
}
8985

9086
private:
91-
nostd::shared_ptr<metrics_api::Histogram<long>> histogram_counter_;
87+
nostd::shared_ptr<metrics_api::Histogram<std::uint64_t>> histogram_counter_;
9288
const std::map<std::string, std::string> tags_;
9389
opentelemetry::context::Context context_{};
9490
std::mutex mutex_;
@@ -115,8 +111,8 @@ public:
115111
// api doesn't seem to allow this.
116112
return recorders_
117113
.insert({ name,
118-
std::make_shared<otel_value_recorder>(meter_->CreateLongHistogram(name, "", "us"),
119-
tags) })
114+
std::make_shared<otel_value_recorder>(
115+
meter_->CreateUInt64Histogram(name, "", "us"), tags) })
120116
->second;
121117
}
122118
// so it is already, lets see if we already have one with those tags, or need

0 commit comments

Comments
 (0)