Skip to content

Commit 99d0301

Browse files
authored
Merge pull request #84 from cuiy0006/add-tracing-connection
impl: Add TracingConnection decorator
2 parents 20266d6 + f91f91f commit 99d0301

22 files changed

+1476
-12
lines changed

.bazelrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ common:macos --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1
5555
test --test_env=GTEST_SHUFFLE --test_env=GTEST_RANDOM_SEED
5656

5757
# By default, build the library with OpenTelemetry
58-
#build --//:enable_opentelemetry
58+
# Enable the config when the bool flag is visible.
59+
# https://github.com/googleapis/google-cloud-cpp-bigquery/issues/98
60+
# build --@com_google_googleapis_google_cloud_cpp//:enable_opentelemetry
5961

6062
# Don't show warnings when building external dependencies. This still shows
6163
# warnings when using these dependencies (say in headers).

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ project(
2626
LANGUAGES CXX)
2727
set(PROJECT_VERSION_PRE_RELEASE "rc")
2828

29+
set(GOOGLE_CLOUD_CPP_BIGQUERY_ENABLE_OPENTELEMETRY
30+
OFF
31+
CACHE BOOL "Enable OpenTelemetry in google-cloud-cpp-bigquery.")
32+
2933
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
3034

3135
include(GoogleCloudCppFeatures)

ci/cloudbuild/builds/cmake-install.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ io::run cmake "${cmake_args[@]}" \
3939
-DGOOGLE_CLOUD_CPP_ENABLE_CLANG_ABI_COMPAT_17=ON \
4040
-DBUILD_TESTING=OFF \
4141
-DGOOGLE_CLOUD_CPP_ENABLE_EXAMPLES=OFF \
42-
-DGOOGLE_CLOUD_CPP_ENABLE="${ENABLED_FEATURES}"
42+
-DGOOGLE_CLOUD_CPP_ENABLE="${ENABLED_FEATURES}" \
43+
-DGOOGLE_CLOUD_CPP_BIGQUERY_ENABLE_OPENTELEMETRY=ON
4344
io::run cmake --build cmake-out
4445
io::run cmake --install cmake-out --component google_cloud_cpp_bigquery_development
4546

ci/cloudbuild/dockerfiles/fedora-latest-cmake.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ RUN curl -fsSL https://github.com/googleapis/google-cloud-cpp/archive/v2.35.0.ta
198198
-DCMAKE_CXX_STANDARD=17 \
199199
-DGOOGLE_CLOUD_CPP_ENABLE_CLANG_ABI_COMPAT_17=ON \
200200
-DBUILD_SHARED_LIBS=yes \
201-
-DGOOGLE_CLOUD_CPP_ENABLE="bigquerycontrol,bigquery" \
201+
-DGOOGLE_CLOUD_CPP_ENABLE="bigquerycontrol,bigquery,opentelemetry" \
202202
-GNinja -S . -B cmake-out && \
203203
cmake --build cmake-out --target install && \
204204
ldconfig && cd /var/tmp && rm -fr build

google/cloud/bigquery_unified/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ package(default_visibility = ["//visibility:private"])
2121

2222
licenses(["notice"]) # Apache 2.0
2323

24+
config_setting(
25+
name = "enable_opentelemetry",
26+
flag_values = {
27+
"@google_cloud_cpp//:enable_opentelemetry": "true",
28+
},
29+
)
30+
2431
filegroup(
2532
name = "common_public_hdrs",
2633
srcs = [h for h in google_cloud_cpp_bigquery_common_hdrs if not h.startswith("internal/")],
@@ -53,6 +60,15 @@ cc_library(
5360
name = "google_cloud_cpp_bigquery_bigquery_unified",
5461
srcs = google_cloud_cpp_bigquery_bigquery_unified_srcs,
5562
hdrs = google_cloud_cpp_bigquery_bigquery_unified_hdrs,
63+
# Enable the config when the bool flag is visible.
64+
# https://github.com/googleapis/google-cloud-cpp-bigquery/issues/98
65+
# defines = select({
66+
# ":enable_opentelemetry": [
67+
# # Enable OpenTelemetry features in google-cloud-cpp
68+
# "GOOGLE_CLOUD_CPP_BIGQUERY_HAVE_OPENTELEMETRY",
69+
# ],
70+
# "//conditions:default": [],
71+
# }),
5672
visibility = ["//:__pkg__"],
5773
deps = [
5874
":google_cloud_cpp_bigquery_common",

google/cloud/bigquery_unified/CMakeLists.txt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ set(bigquery_unified_library_files
147147
internal/default_options.cc
148148
internal/default_options.h
149149
internal/retry_traits.h
150+
internal/tracing_connection.cc
151+
internal/tracing_connection.h
150152
job_options.h
151153
read_arrow_response.h
152154
read_options.h
@@ -178,11 +180,29 @@ bigquery_unified_library(
178180
PKGCONFIG_DEPS
179181
${bigquery_unified_pkgconfig_deps})
180182

183+
if (GOOGLE_CLOUD_CPP_BIGQUERY_ENABLE_OPENTELEMETRY)
184+
find_package(google_cloud_cpp_opentelemetry CONFIG)
185+
if (TARGET google-cloud-cpp::opentelemetry)
186+
target_compile_definitions(
187+
google_cloud_cpp_bigquery_bigquery_unified
188+
PUBLIC # Enable OpenTelemetry features in google-cloud-cpp-bigquery
189+
GOOGLE_CLOUD_CPP_BIGQUERY_HAVE_OPENTELEMETRY)
190+
else ()
191+
message(
192+
FATAL_ERROR
193+
"Instrumentation with OpenTelemetry was requested "
194+
"for google-cloud-cpp-bigquery, but it was not enabled "
195+
"in google-cloud-cpp. Please build and install "
196+
"google-cloud-cpp with GOOGLE_CLOUD_CPP_ENABLE=opentelemetry "
197+
"if you want to use this feature.")
198+
endif ()
199+
endif ()
200+
181201
if (GOOGLE_CLOUD_CPP_BIGQUERY_WITH_MOCKS)
182202
add_library(google_cloud_cpp_bigquery_bigquery_unified_mocks INTERFACE)
183203
set(google_cloud_cpp_bigquery_bigquery_unified_mocks_hdrs
184204
# cmake-format: sort
185-
mocks/mock_connection.h)
205+
mocks/mock_connection.h mocks/mock_stream_range.h)
186206
export_list_to_bazel(
187207
"google_cloud_cpp_bigquery_bigquery_unified_mocks.bzl"
188208
"google_cloud_cpp_bigquery_bigquery_unified_mocks_hdrs" YEAR "2024")
@@ -236,8 +256,12 @@ function (bigquery_unified_client_define_tests)
236256
find_package(GTest CONFIG REQUIRED)
237257
set(bigquery_unified_client_unit_tests
238258
# cmake-format: sort
239-
client_test.cc internal/connection_impl_test.cc
240-
internal/default_options_test.cc)
259+
client_test.cc
260+
connection_test.cc
261+
internal/connection_impl_test.cc
262+
internal/default_options_test.cc
263+
internal/tracing_connection_test.cc
264+
mocks/mock_stream_range_test.cc)
241265

242266
# Export the list of unit tests to a .bzl file so we do not need to maintain
243267
# the list in two places.

google/cloud/bigquery_unified/bigquery_unified_client_unit_tests.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
bigquery_unified_client_unit_tests = [
2020
"client_test.cc",
21+
"connection_test.cc",
2122
"internal/connection_impl_test.cc",
2223
"internal/default_options_test.cc",
24+
"internal/tracing_connection_test.cc",
25+
"mocks/mock_stream_range_test.cc",
2326
]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
#include "google/cloud/bigquery_unified/connection.h"
16+
#include "google/cloud/bigquery_unified/testing_util/opentelemetry_matchers.h"
17+
#include "google/cloud/bigquery_unified/version.h"
18+
19+
namespace google::cloud::bigquery_unified {
20+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_BEGIN
21+
namespace {
22+
23+
#ifdef GOOGLE_CLOUD_CPP_BIGQUERY_HAVE_OPENTELEMETRY
24+
using ::google::cloud::bigquery_unified::testing_util::DisableTracing;
25+
using ::google::cloud::bigquery_unified::testing_util::EnableTracing;
26+
using ::google::cloud::bigquery_unified::testing_util::SpanNamed;
27+
using ::testing::Contains;
28+
using ::testing::Not;
29+
30+
TEST(BigQueryUnifiedConnectionTest, TracingEnabled) {
31+
auto span_catcher = testing_util::InstallSpanCatcher();
32+
33+
auto options = EnableTracing(Options{});
34+
auto conn = MakeConnection(std::move(options));
35+
google::cloud::internal::OptionsSpan span(
36+
google::cloud::internal::MergeOptions(Options{}, conn->options()));
37+
// Make a call, which should fail fast. The error itself is not important.
38+
google::cloud::bigquery::v2::GetJobRequest request;
39+
(void)conn->GetJob(request, Options{});
40+
41+
auto spans = span_catcher->GetSpans();
42+
EXPECT_THAT(spans,
43+
Contains(SpanNamed("bigquery_unified::Connection::GetJob")));
44+
}
45+
46+
TEST(BigQueryUnifiedConnectionTest, TracingDisabled) {
47+
auto span_catcher = testing_util::InstallSpanCatcher();
48+
49+
auto options = DisableTracing(Options{});
50+
auto conn = MakeConnection(std::move(options));
51+
google::cloud::internal::OptionsSpan span(
52+
google::cloud::internal::MergeOptions(Options{}, conn->options()));
53+
// Make a call, which should fail fast. The error itself is not important.
54+
google::cloud::bigquery::v2::GetJobRequest request;
55+
(void)conn->GetJob(request, Options{});
56+
57+
auto spans = span_catcher->GetSpans();
58+
EXPECT_THAT(spans,
59+
Not(Contains(SpanNamed("bigquery_unified::Connection::GetJob"))));
60+
}
61+
62+
#endif // GOOGLE_CLOUD_CPP_BIGQUERY_HAVE_OPENTELEMETRY
63+
64+
} // namespace
65+
GOOGLE_CLOUD_CPP_BIGQUERY_INLINE_NAMESPACE_END
66+
} // namespace google::cloud::bigquery_unified

google/cloud/bigquery_unified/google_cloud_cpp_bigquery_bigquery_unified.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ google_cloud_cpp_bigquery_bigquery_unified_hdrs = [
2525
"internal/connection_impl.h",
2626
"internal/default_options.h",
2727
"internal/retry_traits.h",
28+
"internal/tracing_connection.h",
2829
"job_options.h",
2930
"read_arrow_response.h",
3031
"read_options.h",
@@ -38,4 +39,5 @@ google_cloud_cpp_bigquery_bigquery_unified_srcs = [
3839
"internal/arrow_reader.cc",
3940
"internal/connection_impl.cc",
4041
"internal/default_options.cc",
42+
"internal/tracing_connection.cc",
4143
]

google/cloud/bigquery_unified/google_cloud_cpp_bigquery_bigquery_unified_mocks.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818

1919
google_cloud_cpp_bigquery_bigquery_unified_mocks_hdrs = [
2020
"mocks/mock_connection.h",
21+
"mocks/mock_stream_range.h",
2122
]

0 commit comments

Comments
 (0)