Skip to content

Commit 43bdb07

Browse files
committed
fix(storage): propagate client options to the gRPC metrics exporter
1 parent 6f1c3c3 commit 43bdb07

8 files changed

Lines changed: 1472 additions & 13 deletions

‎google/cloud/storage/grpc_plugin.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
6565
* CPU (a single RPC every few minutes) or memory (a few KiB to batch the
6666
* telemetry).
6767
*
68+
* The exporter connects to Cloud Monitoring using the client's credentials, CA
69+
* certificates file, and proxy configuration. Telemetry is exported when the
70+
* client's endpoint host belongs to the client's universe domain, the client
71+
* does not use `google::cloud::MakeInsecureCredentials()`, and any universe
72+
* domain specified in the credentials matches the client's universe domain.
73+
* Setting this option to `true` does not override these requirements.
74+
*
75+
* Monitored resource attributes from the host (project, zone, and instance)
76+
* are used when the client authenticates through the host's metadata server.
77+
* For other credential types, the monitoring project comes from
78+
* `google::cloud::storage::ProjectIdOption` or the credentials, and metrics
79+
* export is skipped when neither specifies a project.
80+
*
6881
* [gRFC/66]: https://github.com/grpc/proposal/blob/master/A66-otel-stats.md
6982
* [gRFC/78]:
7083
* https://github.com/grpc/proposal/blob/master/A78-grpc-metrics-wrr-pf-xds.md

‎google/cloud/storage/internal/grpc/enable_metrics.cc‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,31 @@
1919
#include "google/cloud/storage/grpc_plugin.h"
2020
#include "google/cloud/storage/internal/grpc/default_options.h"
2121
#include "google/cloud/storage/internal/grpc/metrics_exporter_impl.h"
22+
#include "google/cloud/storage/internal/grpc/metrics_exporter_options.h"
23+
#include <optional>
24+
#include <utility>
2225

2326
namespace google {
2427
namespace cloud {
2528
namespace storage_internal {
2629
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2730

2831
void EnableGrpcMetrics(Options const& storage_options) {
32+
EnableGrpcMetrics(storage_options,
33+
[] { return otel::MakeResourceDetector()->Detect(); });
34+
}
35+
36+
void EnableGrpcMetrics(
37+
Options const& storage_options,
38+
std::function<opentelemetry::sdk::resource::Resource()> const&
39+
detect_resource) {
2940
auto options = DefaultOptionsGrpc(storage_options);
30-
// Avoid running the resource detector if all metrics options are disabled.
41+
// Avoid querying the metadata server for GCP resource attributes when metrics
42+
// export is disabled.
3143
if (!AnyMetricsEnabled(options)) return;
32-
auto detector = otel::MakeResourceDetector();
33-
auto resources = detector->Detect();
34-
auto config = MakeMeterProviderConfig(resources, options);
44+
if (DetectMetricsExportMode(options) == MetricsExportMode::kDisabled) return;
45+
std::optional<ExporterConfig> config =
46+
MakeMeterProviderConfig(detect_resource(), options);
3547
if (!config) return;
3648
EnableGrpcMetricsImpl(std::move(*config));
3749
}

‎google/cloud/storage/internal/grpc/enable_metrics.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
#include "google/cloud/options.h"
1919
#include "google/cloud/version.h"
20+
#ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS
21+
#include <opentelemetry/sdk/resource/resource.h>
22+
#include <functional>
23+
#endif // GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS
2024

2125
namespace google {
2226
namespace cloud {
@@ -25,6 +29,20 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2529

2630
void EnableGrpcMetrics(Options const& storage_options);
2731

32+
#ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS
33+
/**
34+
* Overload of `EnableGrpcMetrics` that takes an explicit resource detector for
35+
* testing.
36+
*
37+
* Invokes @p detect_resource only when metrics export is enabled for
38+
* @p storage_options, avoiding an unnecessary metadata server query otherwise.
39+
*/
40+
void EnableGrpcMetrics(
41+
Options const& storage_options,
42+
std::function<opentelemetry::sdk::resource::Resource()> const&
43+
detect_resource);
44+
#endif // GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS
45+
2846
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
2947
} // namespace storage_internal
3048
} // namespace cloud

‎google/cloud/storage/internal/grpc/metrics_exporter_impl.cc‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,20 @@ std::optional<ExporterConfig> MakeMeterProviderConfig(
162162
if (!AnyMetricsEnabled(options)) {
163163
return std::nullopt;
164164
}
165+
MetricsExportMode const mode = DetectMetricsExportMode(options);
166+
if (mode == MetricsExportMode::kDisabled) return std::nullopt;
165167
bool const enable_grpc =
166168
options.get<storage_experimental::EnableGrpcMetricsOption>();
167-
auto project = MonitoringProject(resource, options);
169+
// Attach host resource attributes only when the client authenticates via the
170+
// host's metadata server; otherwise use default client resource labels.
171+
opentelemetry::sdk::resource::Resource const& client_resource =
172+
mode == MetricsExportMode::kWithHostResource
173+
? resource
174+
: opentelemetry::sdk::resource::Resource::GetEmpty();
175+
std::optional<Project> project = MonitoringProject(client_resource, options);
168176
if (!project) return std::nullopt;
169177

170-
auto exporter_options = MetricsExporterOptions(*project, resource);
178+
Options exporter_options = MetricsExporterOptions(*project, client_resource);
171179
if (options.has<storage_experimental::GrpcMetricsExcludedLabelsOption>()) {
172180
exporter_options.set<otel_internal::ResourceFilterDataFnOption>(
173181
options.get<storage_experimental::GrpcMetricsExcludedLabelsOption>());

‎google/cloud/storage/internal/grpc/metrics_exporter_impl.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ struct ExporterConfig {
4646
/// Returns true if any gRPC or OpenTelemetry storage metrics option is enabled.
4747
bool AnyMetricsEnabled(Options const& options);
4848

49+
/**
50+
* Returns the exporter configuration for the resolved @p options, or
51+
* `std::nullopt` when metrics export is disabled or no monitoring project can
52+
* be determined.
53+
*
54+
* Uses the host @p resource for the monitoring project and resource labels only
55+
* when `DetectMetricsExportMode()` returns
56+
* `MetricsExportMode::kWithHostResource`.
57+
*/
4958
std::optional<ExporterConfig> MakeMeterProviderConfig(
5059
opentelemetry::sdk::resource::Resource const& resource,
5160
Options const& options);

0 commit comments

Comments
 (0)