Skip to content

impl(bigtable): make MetricServiceConnection more shareable#16217

Open
scotthart wants to merge 1 commit into
googleapis:mainfrom
scotthart:bigtable_shared_metric_connection
Open

impl(bigtable): make MetricServiceConnection more shareable#16217
scotthart wants to merge 1 commit into
googleapis:mainfrom
scotthart:bigtable_shared_metric_connection

Conversation

@scotthart

@scotthart scotthart commented Jun 29, 2026

Copy link
Copy Markdown
Member

Refactor the MetricServiceConnection from inside the OperationContextFactory to DataConnectionImpl. This enables sharing of the MetricServiceConnection across exporters in the future.

@scotthart scotthart requested a review from a team as a code owner June 29, 2026 22:02
@product-auto-label product-auto-label Bot added the api: bigtable Issues related to the Bigtable API. label Jun 29, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request integrates OpenTelemetry metrics into the Bigtable data connection by creating and passing a "MetricServiceConnection" to the "MetricsOperationContextFactory". The review feedback suggests several improvements to align with the style guide and optimize compilation: forward-declaring "MetricServiceConnection" in the header to reduce header pollution, wrapping helper functions in an anonymous namespace in the source file, and replacing default parameters with overloads for functions in the internal namespace.

#include "google/cloud/bigtable/prepared_query.h"
#include "google/cloud/bigtable/result_source_interface.h"
#include "google/cloud/background_threads.h"
#include "google/cloud/monitoring/v3/metric_connection.h"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of including "google/cloud/monitoring/v3/metric_connection.h" in the header file, we can use a forward declaration of MetricServiceConnection. This reduces header pollution and avoids unnecessary dependencies when GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS is not defined.

namespace google::cloud::monitoring_v3 {
class MetricServiceConnection;
}  // namespace google::cloud::monitoring_v3

Comment on lines +223 to +232
#ifdef GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS
Options MetricsExporterConnectionOptions(Options options) {
// We start with a copy of the client options to preserve credentials and
// universe domain, but we must unset Bigtable-specific endpoints/authorities
// to allow default Monitoring defaults.
options.unset<EndpointOption>();
options.unset<AuthorityOption>();
return options;
}
#endif // GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since we forward-declared MetricServiceConnection in the header, we should include "google/cloud/monitoring/v3/metric_connection.h" here inside the #ifdef GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS block. Additionally, we should wrap MetricsExporterConnectionOptions in an anonymous namespace to give it internal linkage and avoid polluting the namespace, as recommended by the Google C++ Style Guide.

#ifdef GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS
#include "google/cloud/monitoring/v3/metric_connection.h"

namespace {
Options MetricsExporterConnectionOptions(Options options) {
  // We start with a copy of the client options to preserve credentials and
  // universe domain, but we must unset Bigtable-specific endpoints/authorities
  // to allow default Monitoring defaults.
  options.unset<EndpointOption>();
  options.unset<AuthorityOption>();
  return options;
}
}  // namespace
#endif  // GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS
References
  1. Use of anonymous namespaces is encouraged for all code that does not need to be referenced from outside that file. Do not use static for functions or variables in namespace scope; use anonymous namespaces instead. (link)

Comment on lines +91 to +100
MetricsOperationContextFactory(
std::string client_uid,
std::shared_ptr<monitoring_v3::MetricServiceConnection> conn,
Options options = {});

// Used for injecting a MockMetricsServiceConnection for testing.
MetricsOperationContextFactory(
std::string client_uid,
std::shared_ptr<monitoring_v3::MetricServiceConnection> conn,
std::shared_ptr<OperationContext::Clock> clock =
std::make_shared<OperationContext::Clock>(),
Options options = {});
std::shared_ptr<OperationContext::Clock> clock, Options options = {});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The repository style guide discourages default parameters for functions in an internal namespace. We should avoid using default parameters like Options options = {} for these constructors. Instead, we can provide overloads to maintain the same convenience without violating the style guide.

  MetricsOperationContextFactory(
      std::string client_uid,
      std::shared_ptr<monitoring_v3::MetricServiceConnection> conn)
      : MetricsOperationContextFactory(std::move(client_uid), std::move(conn),
                                       Options{}) {}

  MetricsOperationContextFactory(
      std::string client_uid,
      std::shared_ptr<monitoring_v3::MetricServiceConnection> conn,
      Options options);

  // Used for injecting a MockMetricsServiceConnection for testing.
  MetricsOperationContextFactory(
      std::string client_uid,
      std::shared_ptr<monitoring_v3::MetricServiceConnection> conn,
      std::shared_ptr<OperationContext::Clock> clock)
      : MetricsOperationContextFactory(std::move(client_uid), std::move(conn),
                                       std::move(clock), Options{}) {}

  MetricsOperationContextFactory(
      std::string client_uid,
      std::shared_ptr<monitoring_v3::MetricServiceConnection> conn,
      std::shared_ptr<OperationContext::Clock> clock, Options options);
References
  1. Discourage default parameters for functions in an internal namespace. (link)

@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.24%. Comparing base (f1a8e57) to head (56a1bc4).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16217   +/-   ##
=======================================
  Coverage   92.23%   92.24%           
=======================================
  Files        2265     2265           
  Lines      210205   210211    +6     
=======================================
+ Hits       193888   193904   +16     
+ Misses      16317    16307   -10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigtable Issues related to the Bigtable API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant