Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions google/cloud/storage/grpc_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
* CPU (a single RPC every few minutes) or memory (a few KiB to batch the
* telemetry).
*
* The exporter connects to Cloud Monitoring using the client's credentials, CA
* certificates file, and proxy configuration. Telemetry is exported only when
* the client's endpoint host belongs to the client's universe domain, the
* client does not use `google::cloud::MakeInsecureCredentials()`,
* `google::cloud::MakeApiKeyCredentials()`, or
* `google::cloud::MakeGDCHServiceAccountCredentials()`, and any credentials
* file's universe domain matches the client's universe domain. A credentials
* file without a `universe_domain` field defaults to `googleapis.com`; export
* is disabled if the file's universe domain cannot be determined. Credential
* types without a file (such as access tokens, metadata-server credentials, or
* `google::cloud::GrpcCredentialOption`) skip the universe-domain check. These
* requirements apply to `EnableGrpcMetricsOption`, `EnableOTelMetricsOption`,
* and `EnableOTelDebugMetricsOption`; setting any of those options to `true`
* does not override them.
*
* Clients with the same `google::cloud::AuthorityOption` (by default,
* `storage.` followed by the universe domain) share one Cloud Monitoring
* exporter for the life of the process. The first qualifying client creates
* the exporter, and subsequent clients sharing that authority reuse it without
* re-checking these requirements.
*
* Monitored resource attributes from the host (project, zone, and instance)
* are used when the client authenticates through the host's metadata server.
* For other credential types, the monitoring project comes from
* `google::cloud::storage::ProjectIdOption` or the credentials, and metrics
* export is skipped when neither specifies a project.
*
* [gRFC/66]: https://github.com/grpc/proposal/blob/master/A66-otel-stats.md
* [gRFC/78]:
* https://github.com/grpc/proposal/blob/master/A78-grpc-metrics-wrr-pf-xds.md
Expand Down
27 changes: 22 additions & 5 deletions google/cloud/storage/internal/grpc/enable_metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,36 @@
#include "google/cloud/storage/grpc_plugin.h"
#include "google/cloud/storage/internal/grpc/default_options.h"
#include "google/cloud/storage/internal/grpc/metrics_exporter_impl.h"
#include "google/cloud/storage/internal/grpc/metrics_exporter_options.h"
#include <optional>
#include <utility>

namespace google {
namespace cloud {
namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

void EnableGrpcMetrics(Options const& storage_options) {
auto options = DefaultOptionsGrpc(storage_options);
// Avoid running the resource detector if all metrics options are disabled.
EnableGrpcMetrics(storage_options,
[] { return otel::MakeResourceDetector()->Detect(); });
}

void EnableGrpcMetrics(
Options const& storage_options,
std::function<opentelemetry::sdk::resource::Resource()> const&
detect_resource) {
Options const options = DefaultOptionsGrpc(storage_options);
// Avoid querying the metadata server for GCP resource attributes when metrics
// export is disabled or does not use host resource attributes.
if (!AnyMetricsEnabled(options)) return;
auto detector = otel::MakeResourceDetector();
auto resources = detector->Detect();
auto config = MakeMeterProviderConfig(resources, options);
MetricsExportMode const mode = DetectMetricsExportMode(options);
Comment thread
kalragauri marked this conversation as resolved.
Resolved
if (mode == MetricsExportMode::kDisabled) return;
opentelemetry::sdk::resource::Resource const resource =
mode == MetricsExportMode::kWithHostResource
? detect_resource()
: opentelemetry::sdk::resource::Resource::GetEmpty();
std::optional<ExporterConfig> config =
MakeMeterProviderConfig(resource, options, mode);
if (!config) return;
EnableGrpcMetricsImpl(std::move(*config));
}
Expand Down
19 changes: 19 additions & 0 deletions google/cloud/storage/internal/grpc/enable_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#include "google/cloud/options.h"
#include "google/cloud/version.h"
#ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS
#include <opentelemetry/sdk/resource/resource.h>
#include <functional>
#endif // GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS

namespace google {
namespace cloud {
Expand All @@ -25,6 +29,21 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

void EnableGrpcMetrics(Options const& storage_options);

#ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS
/**
* Overload of `EnableGrpcMetrics` that takes an explicit resource detector for
* testing.
*
* Invokes @p detect_resource only when `DetectMetricsExportMode()` returns
* `MetricsExportMode::kWithHostResource`, avoiding an unnecessary metadata
* server query otherwise.
*/
void EnableGrpcMetrics(
Options const& storage_options,
std::function<opentelemetry::sdk::resource::Resource()> const&
detect_resource);
#endif // GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
Expand Down
23 changes: 18 additions & 5 deletions google/cloud/storage/internal/grpc/metrics_exporter_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,28 @@ bool AnyMetricsEnabled(Options const& options) {
std::optional<ExporterConfig> MakeMeterProviderConfig(
opentelemetry::sdk::resource::Resource const& resource,
Options const& options) {
if (!AnyMetricsEnabled(options)) {
return std::nullopt;
}
if (!AnyMetricsEnabled(options)) return std::nullopt;
return MakeMeterProviderConfig(resource, options,
DetectMetricsExportMode(options));
}

std::optional<ExporterConfig> MakeMeterProviderConfig(
opentelemetry::sdk::resource::Resource const& resource,
Options const& options, MetricsExportMode mode) {
if (!AnyMetricsEnabled(options)) return std::nullopt;
if (mode == MetricsExportMode::kDisabled) return std::nullopt;
bool const enable_grpc =
options.get<storage_experimental::EnableGrpcMetricsOption>();
auto project = MonitoringProject(resource, options);
// Attach host resource attributes only when the client authenticates via the
// host's metadata server; otherwise use default client resource labels.
opentelemetry::sdk::resource::Resource const& client_resource =
mode == MetricsExportMode::kWithHostResource
? resource
: opentelemetry::sdk::resource::Resource::GetEmpty();
std::optional<Project> project = MonitoringProject(client_resource, options);
if (!project) return std::nullopt;

auto exporter_options = MetricsExporterOptions(*project, resource);
Options exporter_options = MetricsExporterOptions(*project, client_resource);
if (options.has<storage_experimental::GrpcMetricsExcludedLabelsOption>()) {
exporter_options.set<otel_internal::ResourceFilterDataFnOption>(
options.get<storage_experimental::GrpcMetricsExcludedLabelsOption>());
Expand Down
19 changes: 19 additions & 0 deletions google/cloud/storage/internal/grpc/metrics_exporter_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS

#include "google/cloud/storage/internal/grpc/metrics_exporter_options.h"
#include "google/cloud/options.h"
#include "google/cloud/project.h"
#include "google/cloud/version.h"
Expand Down Expand Up @@ -46,10 +47,28 @@ struct ExporterConfig {
/// Returns true if any gRPC or OpenTelemetry storage metrics option is enabled.
bool AnyMetricsEnabled(Options const& options);

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

/**
* Overload of `MakeMeterProviderConfig` that accepts a precomputed
* `MetricsExportMode` so callers that already inspected @p options do not call
* `DetectMetricsExportMode()` a second time.
*/
std::optional<ExporterConfig> MakeMeterProviderConfig(
opentelemetry::sdk::resource::Resource const& resource,
Options const& options, MetricsExportMode mode);

void EnableGrpcMetricsImpl(ExporterConfig config);

/// Test-only overload allowing callers to intercept gRPC plugin registration.
Expand Down
Loading
Loading