Skip to content

Commit 172b94b

Browse files
authored
impl(bigtable): add OperationContextFactory (#15297)
1 parent 313620e commit 172b94b

14 files changed

+482
-7
lines changed

ci/cloudbuild/builds/cmake-oldest-deps.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ source module ci/cloudbuild/builds/lib/integration.sh
2727
source module ci/cloudbuild/builds/lib/vcpkg.sh
2828
source module ci/lib/io.sh
2929

30-
mapfile -t feature_list < <(features::always_build | grep -v "experimental-bigquery")
30+
# TODO(#14944): add bigtable back when we are able to update vcpkg version.
31+
mapfile -t feature_list < <(features::always_build | grep -v "experimental-bigquery" | grep -v "bigtable")
3132
ENABLED_FEATURES="$(printf ",%s" "${feature_list[@]}")"
3233
ENABLED_FEATURES="${ENABLED_FEATURES:1}"
3334
readonly ENABLED_FEATURES

cmake/GoogleCloudCppFeatures.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ export_libraries_bzl()
300300
# other feature, or even if no features are enabled.
301301
# ~~~
302302
macro (google_cloud_cpp_enable_deps)
303+
find_package(opentelemetry-cpp CONFIG)
303304
if (__ga_libraries__ IN_LIST GOOGLE_CLOUD_CPP_ENABLE)
304305
list(APPEND GOOGLE_CLOUD_CPP_ENABLE ${GOOGLE_CLOUD_CPP_GA_LIBRARIES})
305306
list(APPEND GOOGLE_CLOUD_CPP_ENABLE
@@ -338,6 +339,11 @@ macro (google_cloud_cpp_enable_deps)
338339
if (asset IN_LIST GOOGLE_CLOUD_CPP_ENABLE)
339340
list(INSERT GOOGLE_CLOUD_CPP_ENABLE 0 accesscontextmanager osconfig)
340341
endif ()
342+
if (bigtable IN_LIST GOOGLE_CLOUD_CPP_ENABLE)
343+
if (opentelemetry-cpp_FOUND)
344+
list(INSERT GOOGLE_CLOUD_CPP_ENABLE 0 opentelemetry)
345+
endif ()
346+
endif ()
341347
if (contentwarehouse IN_LIST GOOGLE_CLOUD_CPP_ENABLE)
342348
list(INSERT GOOGLE_CLOUD_CPP_ENABLE 0 documentai)
343349
endif ()

google/cloud/bigtable/CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ include(CTest)
4747
# Export the version information for Bazel.
4848
include(CreateBazelConfig)
4949

50+
find_package(opentelemetry-cpp CONFIG)
51+
5052
# the client library
5153
add_library(
5254
google_cloud_cpp_bigtable # cmake-format: sort
@@ -199,6 +201,8 @@ add_library(
199201
internal/mutate_rows_limiter.h
200202
internal/operation_context.cc
201203
internal/operation_context.h
204+
internal/operation_context_factory.cc
205+
internal/operation_context_factory.h
202206
internal/prefix_range_end.cc
203207
internal/prefix_range_end.h
204208
internal/rate_limiter.cc
@@ -261,6 +265,14 @@ target_link_libraries(
261265
gRPC::grpc++
262266
gRPC::grpc
263267
protobuf::libprotobuf)
268+
if (opentelemetry-cpp_FOUND)
269+
target_compile_definitions(
270+
google_cloud_cpp_bigtable
271+
PRIVATE GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS)
272+
target_link_libraries(google_cloud_cpp_bigtable
273+
PUBLIC google-cloud-cpp::opentelemetry)
274+
set(EXTRA_MODULES "google_cloud_cpp_opentelemetry" "opentelemetry_metrics")
275+
endif ()
264276
google_cloud_cpp_add_common_options(google_cloud_cpp_bigtable)
265277
target_include_directories(
266278
google_cloud_cpp_bigtable
@@ -305,7 +317,8 @@ google_cloud_cpp_add_pkgconfig(
305317
"Provides C++ APIs to access Google Cloud Bigtable."
306318
"google_cloud_cpp_grpc_utils"
307319
"google_cloud_cpp_common"
308-
"google_cloud_cpp_bigtable_protos")
320+
"google_cloud_cpp_bigtable_protos"
321+
${EXTRA_MODULES})
309322

310323
# Create and install the CMake configuration files.
311324
include(CMakePackageConfigHelpers)
@@ -501,6 +514,10 @@ if (BUILD_TESTING)
501514
gRPC::grpc++
502515
gRPC::grpc
503516
protobuf::libprotobuf)
517+
if (opentelemetry-cpp_FOUND)
518+
target_compile_definitions(
519+
${target} PRIVATE GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS)
520+
endif ()
504521
google_cloud_cpp_add_common_options(${target})
505522
add_test(NAME ${target} COMMAND ${target})
506523
endforeach ()

google/cloud/bigtable/config.cmake.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ include(CMakeFindDependencyMacro)
1717
find_dependency(google_cloud_cpp_googleapis)
1818
find_dependency(google_cloud_cpp_common)
1919
find_dependency(google_cloud_cpp_grpc_utils)
20+
find_dependency(google_cloud_cpp_opentelemetry)
2021
find_dependency(absl)
2122

2223
include("${CMAKE_CURRENT_LIST_DIR}/google_cloud_cpp_bigtable-targets.cmake")

google/cloud/bigtable/google_cloud_cpp_bigtable.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ google_cloud_cpp_bigtable_hdrs = [
9999
"internal/metrics.h",
100100
"internal/mutate_rows_limiter.h",
101101
"internal/operation_context.h",
102+
"internal/operation_context_factory.h",
102103
"internal/prefix_range_end.h",
103104
"internal/rate_limiter.h",
104105
"internal/readrowsparser.h",
@@ -202,6 +203,7 @@ google_cloud_cpp_bigtable_srcs = [
202203
"internal/metrics.cc",
203204
"internal/mutate_rows_limiter.cc",
204205
"internal/operation_context.cc",
206+
"internal/operation_context_factory.cc",
205207
"internal/prefix_range_end.cc",
206208
"internal/rate_limiter.cc",
207209
"internal/readrowsparser.cc",

google/cloud/bigtable/internal/data_connection_impl.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "google/cloud/idempotency.h"
2727
#include "google/cloud/internal/async_retry_loop.h"
2828
#include "google/cloud/internal/make_status.h"
29+
#include "google/cloud/internal/random.h"
2930
#include "google/cloud/internal/retry_loop.h"
3031
#include <memory>
3132
#include <string>
@@ -88,7 +89,26 @@ DataConnectionImpl::DataConnectionImpl(
8889
stub_(std::move(stub)),
8990
limiter_(std::move(limiter)),
9091
options_(internal::MergeOptions(std::move(options),
91-
DataConnection::options())) {}
92+
DataConnection::options())) {
93+
#ifdef GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS
94+
if (options_.get<bigtable::EnableMetricsOption>()) {
95+
// The client_uid is eventually used in conjunction with other data labels
96+
// to identify metric data points. This pseudorandom string is used to aid
97+
// in disambiguation.
98+
auto gen = internal::MakeDefaultPRNG();
99+
std::string client_uid =
100+
internal::Sample(gen, 16, "abcdefghijklmnopqrstuvwxyz0123456789");
101+
operation_context_factory_ =
102+
std::make_unique<MetricsOperationContextFactory>(std::move(client_uid));
103+
} else {
104+
operation_context_factory_ =
105+
std::make_unique<SimpleOperationContextFactory>();
106+
}
107+
#else
108+
operation_context_factory_ =
109+
std::make_unique<SimpleOperationContextFactory>();
110+
#endif
111+
}
92112

93113
Status DataConnectionImpl::Apply(std::string const& table_name,
94114
bigtable::SingleRowMutation mut) {

google/cloud/bigtable/internal/data_connection_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "google/cloud/bigtable/data_connection.h"
1919
#include "google/cloud/bigtable/internal/bigtable_stub.h"
2020
#include "google/cloud/bigtable/internal/mutate_rows_limiter.h"
21+
#include "google/cloud/bigtable/internal/operation_context_factory.h"
2122
#include "google/cloud/background_threads.h"
2223
#include "google/cloud/options.h"
2324
#include "google/cloud/status_or.h"
@@ -94,6 +95,7 @@ class DataConnectionImpl : public bigtable::DataConnection {
9495
bigtable::Filter filter) override;
9596

9697
private:
98+
std::unique_ptr<OperationContextFactory> operation_context_factory_;
9799
std::unique_ptr<BackgroundThreads> background_;
98100
std::shared_ptr<BigtableStub> stub_;
99101
std::shared_ptr<MutateRowsLimiter> limiter_;

google/cloud/bigtable/internal/defaults.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ Options DefaultOptions(Options opts) {
221221
products.insert(products.begin(),
222222
::google::cloud::internal::UserAgentPrefix());
223223

224+
if (!opts.has<EnableMetricsOption>()) {
225+
opts.set<EnableMetricsOption>(true);
226+
}
227+
228+
if (opts.get<EnableMetricsOption>()) {
229+
if (!opts.has<MetricsPeriodOption>() ||
230+
(opts.get<MetricsPeriodOption>() < std::chrono::seconds(5))) {
231+
opts.set<MetricsPeriodOption>(std::chrono::seconds(60));
232+
}
233+
}
234+
224235
return opts;
225236
}
226237

google/cloud/bigtable/internal/defaults_test.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,34 @@ TEST(OptionsTest, BigtableEndpointEnvVarsOverrideUniverseDomain) {
318318
"emulator-host:8000");
319319
}
320320

321+
TEST(OptionsTest, EnableMetrics) {
322+
Options options;
323+
options = DefaultOptions(std::move(options));
324+
EXPECT_TRUE(options.get<EnableMetricsOption>());
325+
326+
options = Options{}.set<EnableMetricsOption>(true);
327+
options = DefaultOptions(std::move(options));
328+
EXPECT_TRUE(options.get<EnableMetricsOption>());
329+
330+
options = Options{}.set<EnableMetricsOption>(false);
331+
options = DefaultOptions(std::move(options));
332+
EXPECT_FALSE(options.get<EnableMetricsOption>());
333+
}
334+
335+
TEST(OptionsTest, MetricsPeriod) {
336+
Options options;
337+
options = DefaultOptions(std::move(options));
338+
EXPECT_EQ(options.get<MetricsPeriodOption>(), std::chrono::seconds(60));
339+
340+
options.set<MetricsPeriodOption>(std::chrono::seconds(30));
341+
options = DefaultOptions(std::move(options));
342+
EXPECT_EQ(options.get<MetricsPeriodOption>(), std::chrono::seconds(30));
343+
344+
options.set<MetricsPeriodOption>(std::chrono::seconds(3));
345+
options = DefaultOptions(std::move(options));
346+
EXPECT_EQ(options.get<MetricsPeriodOption>(), std::chrono::seconds(60));
347+
}
348+
321349
TEST(EndpointEnvTest, EmulatorEnvOnly) {
322350
ScopedEnvironment emulator("BIGTABLE_EMULATOR_HOST", "emulator-host:8000");
323351

0 commit comments

Comments
 (0)