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
6874LabelMap 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+
180250GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
181251} // namespace bigtable_internal
182252} // namespace cloud
0 commit comments