Skip to content

Commit b46d384

Browse files
author
maladetska
committed
fix semconv
1 parent aa80faa commit b46d384

File tree

17 files changed

+440
-166
lines changed

17 files changed

+440
-166
lines changed

include/ydb-cpp-sdk/client/metrics/metrics.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,25 @@ class IMetricRegistry {
3333
public:
3434
virtual ~IMetricRegistry() = default;
3535

36-
virtual std::shared_ptr<ICounter> Counter(const std::string& name, const TLabels& labels = {}) = 0;
37-
virtual std::shared_ptr<IGauge> Gauge(const std::string& name, const TLabels& labels = {}) = 0;
38-
virtual std::shared_ptr<IHistogram> Histogram(const std::string& name, const std::vector<double>& buckets, const TLabels& labels = {}) = 0;
36+
virtual std::shared_ptr<ICounter> Counter(
37+
const std::string& name,
38+
const TLabels& labels = {},
39+
const std::string& description = {},
40+
const std::string& unit = {}
41+
) = 0;
42+
virtual std::shared_ptr<IGauge> Gauge(
43+
const std::string& name,
44+
const TLabels& labels = {},
45+
const std::string& description = {},
46+
const std::string& unit = {}
47+
) = 0;
48+
virtual std::shared_ptr<IHistogram> Histogram(
49+
const std::string& name,
50+
const std::vector<double>& buckets,
51+
const TLabels& labels = {},
52+
const std::string& description = {},
53+
const std::string& unit = {}
54+
) = 0;
3955
};
4056

4157
} // namespace NYdb::NMetrics

plugins/metrics/otel/src/metrics.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,37 @@ class TOtelMetricRegistry : public IMetricRegistry {
8181
, Meter_(MeterProvider_->GetMeter("ydb-cpp-sdk", GetSdkSemver()))
8282
{}
8383

84-
std::shared_ptr<ICounter> Counter(const std::string& name, const TLabels& labels) override {
85-
auto counter = Meter_->CreateUInt64Counter(name);
84+
std::shared_ptr<ICounter> Counter(const std::string& name
85+
, const TLabels& labels
86+
, const std::string& description
87+
, const std::string& unit
88+
) override {
89+
auto counter = Meter_->CreateUInt64Counter(name, description, unit);
8690
return std::make_shared<TOtelCounter>(std::move(counter), labels);
8791
}
8892

89-
std::shared_ptr<IGauge> Gauge(const std::string& name, const TLabels& labels) override {
90-
auto counter = Meter_->CreateDoubleUpDownCounter(name);
93+
std::shared_ptr<IGauge> Gauge(const std::string& name
94+
, const TLabels& labels
95+
, const std::string& description
96+
, const std::string& unit
97+
) override {
98+
auto counter = Meter_->CreateDoubleUpDownCounter(name, description, unit);
9199
return std::make_shared<TOtelUpDownCounterGauge>(std::move(counter), labels);
92100
}
93101

94-
std::shared_ptr<IHistogram> Histogram(const std::string& name, const std::vector<double>& buckets, const TLabels& labels) override {
95-
ConfigureHistogramBuckets(name, buckets);
96-
auto histogram = Meter_->CreateDoubleHistogram(name);
102+
std::shared_ptr<IHistogram> Histogram(const std::string& name
103+
, const std::vector<double>& buckets
104+
, const TLabels& labels
105+
, const std::string& description
106+
, const std::string& unit
107+
) override {
108+
ConfigureHistogramBuckets(name, unit, buckets);
109+
auto histogram = Meter_->CreateDoubleHistogram(name, description, unit);
97110
return std::make_shared<TOtelHistogram>(std::move(histogram), labels);
98111
}
99112

100113
private:
101-
void ConfigureHistogramBuckets(const std::string& name, const std::vector<double>& buckets) {
114+
void ConfigureHistogramBuckets(const std::string& name, const std::string& unit, const std::vector<double>& buckets) {
102115
if (buckets.empty()) {
103116
return;
104117
}
@@ -118,7 +131,7 @@ class TOtelMetricRegistry : public IMetricRegistry {
118131
auto selector = std::make_unique<sdk::metrics::InstrumentSelector>(
119132
sdk::metrics::InstrumentType::kHistogram,
120133
name,
121-
""
134+
unit
122135
);
123136
auto meterSelector = std::make_unique<sdk::metrics::MeterSelector>(
124137
"ydb-cpp-sdk",

src/client/impl/observability/client_metrics.cpp

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "client_metrics.h"
22

33
#include <exception>
4+
#include <util/string/cast.h>
45

56
namespace NYdb::inline V3::NObservability {
67

@@ -18,73 +19,40 @@ void SafeLogMetricsError(const char* /*message*/) noexcept {
1819
}
1920
}
2021

21-
std::string StatusToString(EStatus status) {
22-
switch (status) {
23-
case EStatus::SUCCESS: return "SUCCESS";
24-
case EStatus::BAD_REQUEST: return "BAD_REQUEST";
25-
case EStatus::UNAUTHORIZED: return "UNAUTHORIZED";
26-
case EStatus::INTERNAL_ERROR: return "INTERNAL_ERROR";
27-
case EStatus::ABORTED: return "ABORTED";
28-
case EStatus::UNAVAILABLE: return "UNAVAILABLE";
29-
case EStatus::OVERLOADED: return "OVERLOADED";
30-
case EStatus::SCHEME_ERROR: return "SCHEME_ERROR";
31-
case EStatus::GENERIC_ERROR: return "GENERIC_ERROR";
32-
case EStatus::TIMEOUT: return "TIMEOUT";
33-
case EStatus::BAD_SESSION: return "BAD_SESSION";
34-
case EStatus::PRECONDITION_FAILED: return "PRECONDITION_FAILED";
35-
case EStatus::ALREADY_EXISTS: return "ALREADY_EXISTS";
36-
case EStatus::NOT_FOUND: return "NOT_FOUND";
37-
case EStatus::SESSION_EXPIRED: return "SESSION_EXPIRED";
38-
case EStatus::CANCELLED: return "CANCELLED";
39-
case EStatus::UNDETERMINED: return "UNDETERMINED";
40-
case EStatus::UNSUPPORTED: return "UNSUPPORTED";
41-
case EStatus::SESSION_BUSY: return "SESSION_BUSY";
42-
case EStatus::EXTERNAL_ERROR: return "EXTERNAL_ERROR";
43-
case EStatus::TRANSPORT_UNAVAILABLE: return "TRANSPORT_UNAVAILABLE";
44-
case EStatus::CLIENT_RESOURCE_EXHAUSTED:return "CLIENT_RESOURCE_EXHAUSTED";
45-
case EStatus::CLIENT_DEADLINE_EXCEEDED: return "CLIENT_DEADLINE_EXCEEDED";
46-
case EStatus::CLIENT_INTERNAL_ERROR: return "CLIENT_INTERNAL_ERROR";
47-
case EStatus::CLIENT_CANCELLED: return "CLIENT_CANCELLED";
48-
case EStatus::CLIENT_UNAUTHENTICATED: return "CLIENT_UNAUTHENTICATED";
49-
case EStatus::CLIENT_CALL_UNIMPLEMENTED:return "CLIENT_CALL_UNIMPLEMENTED";
50-
case EStatus::CLIENT_OUT_OF_RANGE: return "CLIENT_OUT_OF_RANGE";
51-
case EStatus::CLIENT_DISCOVERY_FAILED: return "CLIENT_DISCOVERY_FAILED";
52-
case EStatus::CLIENT_LIMITS_REACHED: return "CLIENT_LIMITS_REACHED";
53-
default: return "STATUS_UNDEFINED";
54-
}
55-
}
56-
5722
} // namespace
5823

5924
static const std::vector<double> DurationBucketsSec = {
6025
0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10
6126
};
6227

63-
TClientMetrics::TClientMetrics(std::shared_ptr<NMetrics::IMetricRegistry> registry,
64-
const std::string& prefix, const std::string& operationName)
28+
static constexpr const char* RequestsDescription = "Number of database client operations started.";
29+
static constexpr const char* ErrorsDescription = "Number of database client operations that failed.";
30+
static constexpr const char* DurationDescription = "Duration of database client operations.";
31+
32+
TClientMetrics::TClientMetrics(std::shared_ptr<NMetrics::IMetricRegistry> registry
33+
, const std::string& operationName
34+
) : Registry_(std::move(registry))
35+
, OperationName_(operationName)
6536
{
66-
if (!registry) {
37+
if (!Registry_) {
6738
return;
6839
}
6940

7041
try {
71-
NMetrics::TLabels labels = {{"operation", operationName}};
72-
RequestCounter_ = registry->Counter(prefix + ".requests", labels);
73-
ErrorCounter_ = registry->Counter(prefix + ".errors", labels);
74-
75-
NMetrics::TLabels durationLabels = {
76-
{"db.system.name", "ydb"},
42+
NMetrics::TLabels labels = {
43+
{"db.system.name", "other_sql"},
7744
{"db.operation.name", operationName},
7845
};
79-
DurationHistogram_ = registry->Histogram("db.client.operation.duration", DurationBucketsSec, durationLabels);
46+
RequestCounter_ = Registry_->Counter("db.client.operation.requests", labels, RequestsDescription, "{operation}");
47+
ErrorCounter_ = Registry_->Counter("db.client.operation.errors", labels, ErrorsDescription, "{error}");
8048

8149
RequestCounter_->Inc();
8250
StartTime_ = std::chrono::steady_clock::now();
8351
} catch (...) {
8452
SafeLogMetricsError("failed to initialize metrics");
8553
RequestCounter_.reset();
8654
ErrorCounter_.reset();
87-
DurationHistogram_.reset();
55+
Registry_.reset();
8856
}
8957
}
9058

@@ -99,10 +67,27 @@ void TClientMetrics::End(EStatus status) noexcept {
9967
Ended_ = true;
10068

10169
try {
102-
if (DurationHistogram_) {
70+
const std::string statusCode = ToString(status);
71+
if (Registry_) {
10372
auto elapsed = std::chrono::steady_clock::now() - StartTime_;
10473
double durationSec = std::chrono::duration<double>(elapsed).count();
105-
DurationHistogram_->Record(durationSec);
74+
NMetrics::TLabels durationLabels = {
75+
{"db.system.name", "other_sql"},
76+
{"db.operation.name", OperationName_},
77+
{"db.response.status_code", statusCode},
78+
};
79+
if (status != EStatus::SUCCESS) {
80+
durationLabels["error.type"] = statusCode;
81+
}
82+
auto durationHistogram = Registry_->Histogram(
83+
"db.client.operation.duration",
84+
DurationBucketsSec,
85+
durationLabels,
86+
DurationDescription,
87+
"s");
88+
if (durationHistogram) {
89+
durationHistogram->Record(durationSec);
90+
}
10691
}
10792

10893
if (status != EStatus::SUCCESS && ErrorCounter_) {

src/client/impl/observability/client_metrics.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ namespace NYdb::inline V3::NObservability {
1111

1212
class TClientMetrics {
1313
public:
14-
TClientMetrics(std::shared_ptr<NMetrics::IMetricRegistry> registry,
15-
const std::string& prefix, const std::string& operationName);
14+
TClientMetrics(std::shared_ptr<NMetrics::IMetricRegistry> registry
15+
, const std::string& operationName
16+
);
1617
~TClientMetrics() noexcept;
1718

1819
void End(EStatus status) noexcept;
1920

2021
private:
22+
std::shared_ptr<NMetrics::IMetricRegistry> Registry_;
23+
std::string OperationName_;
2124
std::shared_ptr<NMetrics::ICounter> RequestCounter_;
2225
std::shared_ptr<NMetrics::ICounter> ErrorCounter_;
23-
std::shared_ptr<NMetrics::IHistogram> DurationHistogram_;
2426
std::chrono::steady_clock::time_point StartTime_;
2527
bool Ended_ = false;
2628
};

src/client/query/client.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class TQueryClient::TImpl: public TClientImplCommon<TQueryClient::TImpl>, public
103103
CollectQuerySize(query);
104104
CollectParamsSize(params ? &params->GetProtoMap() : nullptr);
105105

106-
auto span = std::make_shared<TQuerySpan>(Tracer_, "ExecuteQuery", DbDriverState_->DiscoveryEndpoint);
106+
auto span = std::make_shared<TQuerySpan>(Tracer_, "ExecuteQuery", DbDriverState_->DiscoveryEndpoint, DbDriverState_->Log);
107107
span->SetQueryText(query);
108108
auto metrics = std::make_shared<TQueryMetrics>(MetricRegistry_, "ExecuteQuery");
109109

@@ -188,7 +188,7 @@ class TQueryClient::TImpl: public TClientImplCommon<TQueryClient::TImpl>, public
188188

189189
auto promise = NThreading::NewPromise<TStatus>();
190190

191-
auto span = std::make_shared<TQuerySpan>(Tracer_, "Rollback", DbDriverState_->DiscoveryEndpoint);
191+
auto span = std::make_shared<TQuerySpan>(Tracer_, "Rollback", DbDriverState_->DiscoveryEndpoint, DbDriverState_->Log);
192192
auto metrics = std::make_shared<TQueryMetrics>(MetricRegistry_, "Rollback");
193193

194194
auto responseCb = [promise, session, span, metrics]
@@ -240,7 +240,7 @@ class TQueryClient::TImpl: public TClientImplCommon<TQueryClient::TImpl>, public
240240

241241
auto promise = NThreading::NewPromise<TCommitTransactionResult>();
242242

243-
auto span = std::make_shared<TQuerySpan>(Tracer_, "Commit", DbDriverState_->DiscoveryEndpoint);
243+
auto span = std::make_shared<TQuerySpan>(Tracer_, "Commit", DbDriverState_->DiscoveryEndpoint, DbDriverState_->Log);
244244
auto metrics = std::make_shared<TQueryMetrics>(MetricRegistry_, "Commit");
245245

246246
auto responseCb = [promise, session, span, metrics]
@@ -556,7 +556,7 @@ class TQueryClient::TImpl: public TClientImplCommon<TQueryClient::TImpl>, public
556556
std::shared_ptr<TQueryMetrics> Metrics;
557557
};
558558

559-
auto span = std::make_shared<TQuerySpan>(Tracer_, "CreateSession", DbDriverState_->DiscoveryEndpoint);
559+
auto span = std::make_shared<TQuerySpan>(Tracer_, "CreateSession", DbDriverState_->DiscoveryEndpoint, DbDriverState_->Log);
560560
auto metrics = std::make_shared<TQueryMetrics>(MetricRegistry_, "CreateSession");
561561
auto ctx = std::make_unique<TQueryClientGetSessionCtx>(shared_from_this(), settings, span, metrics);
562562
auto future = ctx->GetFuture();

src/client/query/impl/query_metrics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NYdb::inline V3::NQuery {
77
class TQueryMetrics : public NObservability::TClientMetrics {
88
public:
99
TQueryMetrics(std::shared_ptr<NMetrics::IMetricRegistry> registry, const std::string& operationName)
10-
: TClientMetrics(std::move(registry), "ydb.query", operationName)
10+
: TClientMetrics(std::move(registry), operationName)
1111
{}
1212
};
1313

src/client/query/impl/query_spans.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#include "query_spans.h"
22

3+
#include <src/client/topic/common/log_lazy.h>
4+
35
#include <util/string/cast.h>
46

7+
#include <exception>
8+
59
namespace NYdb::inline V3::NQuery {
610

711
namespace {
@@ -16,7 +20,6 @@ void ParseEndpoint(const std::string& endpoint, std::string& host, int& port) {
1620
return;
1721
}
1822

19-
// IPv6 bracket notation: [addr]:port
2023
if (endpoint.front() == '[') {
2124
auto bracketEnd = endpoint.find(']');
2225
if (bracketEnd != std::string::npos) {
@@ -41,21 +44,26 @@ void ParseEndpoint(const std::string& endpoint, std::string& host, int& port) {
4144
}
4245
}
4346

44-
void SafeLogSpanError(const char* message) noexcept {
47+
void SafeLogSpanError(TLog& log, const char* message) noexcept {
4548
try {
4649
try {
47-
std::cerr << "TQuerySpan: " << message << ": " << CurrentExceptionMessage() << std::endl;
50+
std::rethrow_exception(std::current_exception());
51+
} catch (const std::exception& e) {
52+
LOG_LAZY(log, TLOG_ERR, std::string("TQuerySpan: ") + message + ": " + e.what());
4853
return;
4954
} catch (...) {
5055
}
51-
std::cerr << "TQuerySpan: " << message << ": (unknown)" << std::endl;
56+
LOG_LAZY(log, TLOG_ERR, std::string("TQuerySpan: ") + message + ": (unknown)");
5257
} catch (...) {
5358
}
5459
}
5560

5661
} // namespace
5762

58-
TQuerySpan::TQuerySpan(std::shared_ptr<NMetrics::ITracer> tracer, const std::string& operationName, const std::string& endpoint) {
63+
TQuerySpan::TQuerySpan(std::shared_ptr<NMetrics::ITracer> tracer, const std::string& operationName,
64+
const std::string& endpoint, const TLog& log)
65+
: Log_(log)
66+
{
5967
if (!tracer) {
6068
return;
6169
}
@@ -65,15 +73,16 @@ TQuerySpan::TQuerySpan(std::shared_ptr<NMetrics::ITracer> tracer, const std::str
6573
ParseEndpoint(endpoint, host, port);
6674

6775
try {
68-
Span_ = tracer->StartSpan("ydb." + operationName, NMetrics::ESpanKind::CLIENT);
76+
Span_ = tracer->StartSpan(operationName, NMetrics::ESpanKind::CLIENT);
6977
if (!Span_) {
7078
return;
7179
}
72-
Span_->SetAttribute("db.system.name", "ydb");
80+
Span_->SetAttribute("db.system.name", "other_sql");
81+
Span_->SetAttribute("db.operation.name", operationName);
7382
Span_->SetAttribute("server.address", host);
7483
Span_->SetAttribute("server.port", static_cast<int64_t>(port));
7584
} catch (...) {
76-
SafeLogSpanError("failed to initialize span");
85+
SafeLogSpanError(Log_, "failed to initialize span");
7786
Span_.reset();
7887
}
7988
}
@@ -83,7 +92,7 @@ TQuerySpan::~TQuerySpan() noexcept {
8392
try {
8493
Span_->End();
8594
} catch (...) {
86-
SafeLogSpanError("failed to end span");
95+
SafeLogSpanError(Log_, "failed to end span");
8796
}
8897
}
8998
}
@@ -99,7 +108,7 @@ void TQuerySpan::SetPeerEndpoint(const std::string& endpoint) noexcept {
99108
Span_->SetAttribute("network.peer.address", host);
100109
Span_->SetAttribute("network.peer.port", static_cast<int64_t>(port));
101110
} catch (...) {
102-
SafeLogSpanError("failed to set peer endpoint");
111+
SafeLogSpanError(Log_, "failed to set peer endpoint");
103112
}
104113
}
105114

@@ -110,7 +119,7 @@ void TQuerySpan::SetQueryText(const std::string& query) noexcept {
110119
try {
111120
Span_->SetAttribute("db.query.text", query);
112121
} catch (...) {
113-
SafeLogSpanError("failed to set query text");
122+
SafeLogSpanError(Log_, "failed to set query text");
114123
}
115124
}
116125

@@ -121,20 +130,20 @@ void TQuerySpan::AddEvent(const std::string& name, const std::map<std::string, s
121130
try {
122131
Span_->AddEvent(name, attributes);
123132
} catch (...) {
124-
SafeLogSpanError("failed to add event");
133+
SafeLogSpanError(Log_, "failed to add event");
125134
}
126135
}
127136

128137
void TQuerySpan::End(EStatus status) noexcept {
129138
if (Span_) {
130139
try {
131-
Span_->SetAttribute("db.response.status_code", static_cast<int64_t>(status));
140+
Span_->SetAttribute("db.response.status_code", ToString(status));
132141
if (status != EStatus::SUCCESS) {
133142
Span_->SetAttribute("error.type", ToString(status));
134143
}
135144
Span_->End();
136145
} catch (...) {
137-
SafeLogSpanError("failed to finalize span");
146+
SafeLogSpanError(Log_, "failed to finalize span");
138147
}
139148
Span_.reset();
140149
}

0 commit comments

Comments
 (0)