Skip to content

Commit d2bb2bb

Browse files
authored
impl(bigtable): add directpath metric (#16370)
1 parent e575603 commit d2bb2bb

4 files changed

Lines changed: 308 additions & 29 deletions

File tree

google/cloud/bigtable/internal/client_schema_metrics.cc

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#include <opentelemetry/semconv/incubating/cloud_attributes.h>
2323
#include <opentelemetry/semconv/incubating/faas_attributes.h>
2424
#include <opentelemetry/semconv/incubating/host_attributes.h>
25-
#include <algorithm>
26-
#include <map>
2725
#include <set>
2826
#include <string_view>
2927

@@ -63,33 +61,54 @@ std::string_view IsStreamingAsString(RpcType type) {
6361
}
6462
return "false";
6563
}
64+
65+
LabelMap BaseLabels(ClientResourceLabels const& r) {
66+
return {{"project_id", r.project_id}, {"instance", r.instance},
67+
{"app_profile", r.app_profile}, {"client_name", r.client_name},
68+
{"client_uid", r.client_uid}, {"client_project", r.client_project},
69+
{"location", r.location}, {"cloud_platform", r.cloud_platform},
70+
{"host_id", r.host_id}, {"hostname", r.hostname}};
71+
}
6672
} // namespace
6773

6874
LabelMap IntoLabelMap(ClientResourceLabels const& r,
6975
ClientOutstandingRpcLabels const& d,
7076
std::set<std::string> const& filtered_data_labels) {
71-
LabelMap labels = {
72-
{"project_id", r.project_id}, {"instance", r.instance},
73-
{"app_profile", r.app_profile}, {"client_name", r.client_name},
74-
{"client_uid", r.client_uid}, {"client_project", r.client_project},
75-
{"location", r.location}, {"cloud_platform", r.cloud_platform},
76-
{"host_id", r.host_id}, {"hostname", r.hostname}};
77-
78-
struct {
79-
std::string key;
80-
std::string value;
81-
} data[] = {
82-
{"transport_type", std::string(ToString(d.transport_type))},
83-
{"channel_pool_lb_policy",
84-
std::string(ToString(d.channel_pool_lb_policy))},
85-
{"streaming", std::string(IsStreamingAsString(d.streaming))},
77+
LabelMap labels = BaseLabels(r);
78+
79+
auto emplace_if_not_filtered = [&](std::string_view key,
80+
std::string_view value) {
81+
if (filtered_data_labels.empty() ||
82+
filtered_data_labels.find(std::string(key)) ==
83+
filtered_data_labels.end()) {
84+
labels.emplace(key, value);
85+
}
8686
};
8787

88-
for (auto& item : data) {
89-
if (filtered_data_labels.find(item.key) == filtered_data_labels.end()) {
90-
labels.emplace(std::move(item.key), std::move(item.value));
88+
emplace_if_not_filtered("transport_type", ToString(d.transport_type));
89+
emplace_if_not_filtered("channel_pool_lb_policy",
90+
ToString(d.channel_pool_lb_policy));
91+
emplace_if_not_filtered("streaming", IsStreamingAsString(d.streaming));
92+
93+
return labels;
94+
}
95+
96+
LabelMap IntoLabelMap(ClientResourceLabels const& r,
97+
DirectAccessCompatibilityLabels const& d,
98+
std::set<std::string> const& filtered_data_labels) {
99+
LabelMap labels = BaseLabels(r);
100+
101+
auto emplace_if_not_filtered = [&](std::string_view key,
102+
std::string_view value) {
103+
if (filtered_data_labels.empty() ||
104+
filtered_data_labels.find(std::string(key)) ==
105+
filtered_data_labels.end()) {
106+
labels.emplace(key, value);
91107
}
92-
}
108+
};
109+
110+
emplace_if_not_filtered("ip_preference", d.ip_preference);
111+
emplace_if_not_filtered("reason", d.reason);
93112

94113
return labels;
95114
}
@@ -109,10 +128,8 @@ ClientResourceLabels MakeClientResourceLabels(
109128
return opentelemetry::nostd::get<std::string>(l->second);
110129
};
111130

112-
if (project_id.empty() &&
113-
options.has<bigtable_internal::InstanceChannelAffinityOption>()) {
114-
auto const& instances =
115-
options.get<bigtable_internal::InstanceChannelAffinityOption>();
131+
if (project_id.empty() && options.has<InstanceChannelAffinityOption>()) {
132+
auto const& instances = options.get<InstanceChannelAffinityOption>();
116133
if (!instances.empty()) {
117134
project_id = instances[0].project_id();
118135
}
@@ -121,6 +138,17 @@ ClientResourceLabels MakeClientResourceLabels(
121138
project_id = by_name(sc::cloud::kCloudAccountId);
122139
}
123140

141+
if (instance.empty() && options.has<InstanceChannelAffinityOption>()) {
142+
auto const& instances = options.get<InstanceChannelAffinityOption>();
143+
if (!instances.empty()) {
144+
instance = instances[0].instance_id();
145+
}
146+
}
147+
148+
if (app_profile.empty() && options.has<bigtable::AppProfileIdOption>()) {
149+
app_profile = options.get<bigtable::AppProfileIdOption>();
150+
}
151+
124152
std::string client_project = by_name(sc::cloud::kCloudAccountId);
125153
if (client_project.empty()) {
126154
client_project = project_id;
@@ -166,7 +194,7 @@ void OutstandingRpcs::StubSelection(
166194
ClientOutstandingRpcLabels data_labels{p.transport_type,
167195
p.channel_pool_lb_policy, p.streaming};
168196
outstanding_rpcs_->Record(static_cast<double>(p.outstanding_rpcs),
169-
IntoLabelMap(resource_labels_, data_labels),
197+
IntoLabelMap(resource_labels_, data_labels, {}),
170198
context);
171199
}
172200

@@ -177,6 +205,48 @@ std::unique_ptr<ClientSchemaMetric> OutstandingRpcs::clone(
177205
return m;
178206
}
179207

208+
DirectAccessCompatibility::DirectAccessCompatibility(
209+
std::string const& instrumentation_scope,
210+
opentelemetry::nostd::shared_ptr<
211+
opentelemetry::metrics::MeterProvider> const& provider)
212+
: gauge_(
213+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
214+
provider
215+
->GetMeter(instrumentation_scope,
216+
kMeterInstrumentationScopeVersion)
217+
->CreateInt64Gauge(
218+
"direct_access/compatible",
219+
"Compatibility check result for Bigtable DirectPath.", "1")
220+
#else
221+
provider
222+
->GetMeter(instrumentation_scope,
223+
kMeterInstrumentationScopeVersion)
224+
->CreateDoubleHistogram(
225+
"direct_access/compatible",
226+
"Compatibility check result for Bigtable DirectPath.", "1")
227+
#endif
228+
) {
229+
}
230+
231+
void DirectAccessCompatibility::Record(
232+
opentelemetry::context::Context const& context, std::int64_t value,
233+
DirectAccessCompatibilityLabels const& data_labels) {
234+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
235+
gauge_->Record(value, IntoLabelMap(resource_labels_, data_labels, {}),
236+
context);
237+
#else
238+
gauge_->Record(static_cast<double>(value),
239+
IntoLabelMap(resource_labels_, data_labels, {}), context);
240+
#endif
241+
}
242+
243+
std::unique_ptr<ClientSchemaMetric> DirectAccessCompatibility::clone(
244+
ClientResourceLabels const& resource_labels) const {
245+
auto m = std::make_unique<DirectAccessCompatibility>(*this);
246+
m->resource_labels_ = resource_labels;
247+
return m;
248+
}
249+
180250
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
181251
} // namespace bigtable_internal
182252
} // namespace cloud

google/cloud/bigtable/internal/client_schema_metrics.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ struct ClientOutstandingRpcLabels {
5454
using LabelMap = std::unordered_map<std::string, std::string>;
5555
LabelMap IntoLabelMap(ClientResourceLabels const& r,
5656
ClientOutstandingRpcLabels const& d,
57-
std::set<std::string> const& filtered_data_labels = {});
57+
std::set<std::string> const& filtered_data_labels);
58+
59+
struct DirectAccessCompatibilityLabels {
60+
std::string ip_preference;
61+
std::string reason;
62+
};
63+
64+
LabelMap IntoLabelMap(ClientResourceLabels const& r,
65+
DirectAccessCompatibilityLabels const& d,
66+
std::set<std::string> const& filtered_data_labels);
5867

5968
ClientResourceLabels MakeClientResourceLabels(
6069
std::string project_id, std::string instance, std::string app_profile,
@@ -84,6 +93,31 @@ class OutstandingRpcs : public ClientSchemaMetric {
8493
outstanding_rpcs_;
8594
};
8695

96+
class DirectAccessCompatibility : public ClientSchemaMetric {
97+
public:
98+
DirectAccessCompatibility(
99+
std::string const& instrumentation_scope,
100+
opentelemetry::nostd::shared_ptr<
101+
opentelemetry::metrics::MeterProvider> const& provider);
102+
103+
void Record(opentelemetry::context::Context const& context,
104+
std::int64_t value,
105+
DirectAccessCompatibilityLabels const& data_labels);
106+
107+
std::unique_ptr<ClientSchemaMetric> clone(
108+
ClientResourceLabels const& resource_labels) const override;
109+
110+
private:
111+
ClientResourceLabels resource_labels_;
112+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
113+
opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Gauge<std::int64_t>>
114+
gauge_;
115+
#else
116+
opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Histogram<double>>
117+
gauge_;
118+
#endif
119+
};
120+
87121
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
88122
} // namespace bigtable_internal
89123
} // namespace cloud

0 commit comments

Comments
 (0)