Skip to content

Commit d0562f1

Browse files
authored
impl(bigtable): add Metrics interface (#15273)
1 parent ba3de7b commit d0562f1

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

google/cloud/bigtable/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ add_library(
193193
internal/legacy_row_reader.h
194194
internal/logging_data_client.cc
195195
internal/logging_data_client.h
196+
internal/metrics.cc
197+
internal/metrics.h
196198
internal/mutate_rows_limiter.cc
197199
internal/mutate_rows_limiter.h
198200
internal/operation_context.cc

google/cloud/bigtable/google_cloud_cpp_bigtable.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ google_cloud_cpp_bigtable_hdrs = [
9696
"internal/legacy_async_row_sampler.h",
9797
"internal/legacy_row_reader.h",
9898
"internal/logging_data_client.h",
99+
"internal/metrics.h",
99100
"internal/mutate_rows_limiter.h",
100101
"internal/operation_context.h",
101102
"internal/prefix_range_end.h",
@@ -198,6 +199,7 @@ google_cloud_cpp_bigtable_srcs = [
198199
"internal/legacy_async_row_sampler.cc",
199200
"internal/legacy_row_reader.cc",
200201
"internal/logging_data_client.cc",
202+
"internal/metrics.cc",
201203
"internal/mutate_rows_limiter.cc",
202204
"internal/operation_context.cc",
203205
"internal/prefix_range_end.cc",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifdef GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS
16+
17+
#include "google/cloud/bigtable/internal/metrics.h"
18+
#include "google/cloud/bigtable/version.h"
19+
20+
namespace google {
21+
namespace cloud {
22+
namespace bigtable_internal {
23+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
24+
25+
LabelMap IntoMap(ResourceLabels const& r, DataLabels const& d) {
26+
return {
27+
{"project_id", r.project_id},
28+
{"instance", r.instance},
29+
{"table", r.table},
30+
{"cluster", r.cluster},
31+
{"zone", r.zone},
32+
{"method", d.method},
33+
{"streaming", d.streaming},
34+
{"client_name", d.client_name},
35+
{"client_uid", d.client_uid},
36+
{"app_profile", d.app_profile},
37+
{"status", d.status},
38+
};
39+
}
40+
41+
Metric::~Metric() = default;
42+
43+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
44+
} // namespace bigtable_internal
45+
} // namespace cloud
46+
} // namespace google
47+
48+
#endif // GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_METRICS_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_METRICS_H
17+
18+
#ifdef GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS
19+
20+
#include "google/cloud/bigtable/internal/operation_context.h"
21+
#include "google/cloud/bigtable/version.h"
22+
#include "google/cloud/status.h"
23+
#include <grpcpp/grpcpp.h>
24+
#include <opentelemetry/context/context.h>
25+
#include <memory>
26+
#include <string>
27+
#include <unordered_map>
28+
29+
namespace google {
30+
namespace cloud {
31+
namespace bigtable_internal {
32+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
33+
34+
struct ResourceLabels {
35+
std::string project_id;
36+
std::string instance;
37+
std::string table;
38+
std::string cluster;
39+
std::string zone;
40+
};
41+
42+
struct DataLabels {
43+
std::string method;
44+
std::string streaming;
45+
std::string client_name;
46+
std::string client_uid;
47+
std::string app_profile;
48+
std::string status;
49+
};
50+
51+
using LabelMap = std::unordered_map<std::string, std::string>;
52+
LabelMap IntoMap(ResourceLabels const& r, DataLabels const& d);
53+
54+
struct PreCallParams {
55+
OperationContext::Clock::time_point attempt_start;
56+
bool first_attempt;
57+
};
58+
59+
struct PostCallParams {
60+
OperationContext::Clock::time_point attempt_end;
61+
google::cloud::Status attempt_status;
62+
};
63+
64+
struct OnDoneParams {
65+
OperationContext::Clock::time_point operation_end;
66+
google::cloud::Status operation_status;
67+
};
68+
69+
struct ElementRequestParams {
70+
OperationContext::Clock::time_point element_request;
71+
};
72+
73+
struct ElementDeliveryParams {
74+
OperationContext::Clock::time_point element_delivery;
75+
bool first_response;
76+
};
77+
78+
class Metric {
79+
public:
80+
using LatencyDuration = std::chrono::duration<double, std::milli>;
81+
82+
virtual ~Metric() = 0;
83+
virtual void PreCall(opentelemetry::context::Context const&,
84+
PreCallParams const&) {}
85+
virtual void PostCall(opentelemetry::context::Context const&,
86+
grpc::ClientContext const&, PostCallParams const&) {}
87+
virtual void OnDone(opentelemetry::context::Context const&,
88+
OnDoneParams const&) {}
89+
virtual void ElementRequest(opentelemetry::context::Context const&,
90+
ElementRequestParams const&) {}
91+
virtual void ElementDelivery(opentelemetry::context::Context const&,
92+
ElementDeliveryParams const&) {}
93+
virtual std::unique_ptr<Metric> clone(ResourceLabels resource_labels,
94+
DataLabels data_labels) const = 0;
95+
};
96+
97+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
98+
} // namespace bigtable_internal
99+
} // namespace cloud
100+
} // namespace google
101+
102+
#endif // GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS
103+
104+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_METRICS_H

google/cloud/bigtable/internal/operation_context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_OPERATION_CONTEXT_H
1717

1818
#include "google/cloud/bigtable/version.h"
19+
#include "google/cloud/internal/clock.h"
1920
#include <grpcpp/grpcpp.h>
2021
#include <map>
2122
#include <string>
@@ -51,6 +52,8 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
5152
*/
5253
class OperationContext {
5354
public:
55+
using Clock = ::google::cloud::internal::SteadyClock;
56+
5457
// Adds stored bigtable cookies as client metadata.
5558
void PreCall(grpc::ClientContext& context);
5659
// Stores bigtable cookies returned as server metadata.

0 commit comments

Comments
 (0)