Skip to content
Open
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
8 changes: 8 additions & 0 deletions google/cloud/internal/populate_common_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Options PopulateCommonOptions(Options opts, std::string const& endpoint_env_var,
if (e && !e->empty()) {
opts.set<OpenTelemetryTracingOption>(true);
}
e = GetEnv("GOOGLE_CLOUD_CPP_OPENTELEMETRY_METRICS");
if (e && !e->empty()) {
opts.set<OpenTelemetryMetricsOption>(true);
}
if (!opts.has<LoggingComponentsOption>()) {
opts.set<LoggingComponentsOption>(DefaultTracingComponents());
}
Expand Down Expand Up @@ -102,6 +106,10 @@ Options MakeAuthOptions(Options const& options) {
opts.set<OpenTelemetryTracingOption>(
options.get<OpenTelemetryTracingOption>());
}
if (options.has<OpenTelemetryMetricsOption>()) {
opts.set<OpenTelemetryMetricsOption>(
options.get<OpenTelemetryMetricsOption>());
}
if (options.has<LoggingComponentsOption>()) {
opts.set<LoggingComponentsOption>(options.get<LoggingComponentsOption>());
}
Expand Down
24 changes: 24 additions & 0 deletions google/cloud/internal/populate_common_options_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@ TEST(PopulateCommonOptions, OpenTelemetryTracing) {
}
}

TEST(PopulateCommonOptions, OpenTelemetryMetrics) {
struct TestCase {
std::optional<std::string> env;
bool value;
};
std::vector<TestCase> tests = {
{std::nullopt, false},
{"", false},
{"ON", true},
};
auto const input = Options{}.set<OpenTelemetryMetricsOption>(false);
for (auto const& test : tests) {
ScopedEnvironment env("GOOGLE_CLOUD_CPP_OPENTELEMETRY_METRICS", test.env);
auto options = PopulateCommonOptions(input, {}, {}, {}, {});
EXPECT_EQ(options.get<OpenTelemetryMetricsOption>(), test.value);
}
}

TEST(DefaultTracingComponents, NoEnvironment) {
ScopedEnvironment env("GOOGLE_CLOUD_CPP_ENABLE_TRACING", std::nullopt);
auto const actual = DefaultTracingComponents();
Expand Down Expand Up @@ -299,6 +317,12 @@ TEST(MakeAuthOptions, WithTracing) {
EXPECT_TRUE(auth_options.get<OpenTelemetryTracingOption>());
}

TEST(MakeAuthOptions, WithMetrics) {
auto options = Options{}.set<OpenTelemetryMetricsOption>(true);
auto auth_options = MakeAuthOptions(options);
EXPECT_TRUE(auth_options.get<OpenTelemetryMetricsOption>());
}

TEST(MakeAuthOptions, WithoutLoggingComponents) {
auto options = Options{}.set<EndpointOption>("endpoint_option");
auto auth_options = MakeAuthOptions(options);
Expand Down
21 changes: 21 additions & 0 deletions google/cloud/opentelemetry_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ struct OpenTelemetryTracingOption {
using Type = bool;
};

/**
* Enables metrics collection with [OpenTelemetry]
*
* Setting this option enables the generation of SDK metrics by the client
* library. The library uses the global meter provider to generate metrics.
*
* @par Prerequisites
* The library must be compiled with OpenTelemetry in order for this option to
* take effect.
*
* @par Environment variable
* This option is controlled by the `GOOGLE_CLOUD_CPP_OPENTELEMETRY_METRICS`
* environment variable. If the environment variable is set to a non-empty
* value, metrics collection with OpenTelemetry is enabled.
*
* @ingroup options
*/
struct OpenTelemetryMetricsOption {
using Type = bool;
};

namespace experimental {
/// @deprecated Use google::cloud::OpenTelemetryTracingOption instead.
using OpenTelemetryTracingOption = ::google::cloud::OpenTelemetryTracingOption;
Expand Down
5 changes: 5 additions & 0 deletions google/cloud/storage/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,11 @@ Options DefaultOptions(Options opts) {
o.set<OpenTelemetryTracingOption>(true);
}

auto metrics = GetEnv("GOOGLE_CLOUD_CPP_OPENTELEMETRY_METRICS");
if (metrics && !metrics->empty()) {
o.set<OpenTelemetryMetricsOption>(true);
}

auto project_id = GetEnv("GOOGLE_CLOUD_PROJECT");
if (project_id.has_value()) {
o.set<ProjectIdOption>(std::move(*project_id));
Expand Down
17 changes: 17 additions & 0 deletions google/cloud/storage/client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,23 @@ TEST_F(ClientTest, TracingWithEnv) {
EXPECT_TRUE(options.get<OpenTelemetryTracingOption>());
}

TEST_F(ClientTest, MetricsWithoutEnv) {
ScopedEnvironment env("GOOGLE_CLOUD_CPP_OPENTELEMETRY_METRICS", std::nullopt);
auto options = internal::DefaultOptions();
EXPECT_FALSE(options.get<OpenTelemetryMetricsOption>());

options =
internal::DefaultOptions(Options{}.set<OpenTelemetryMetricsOption>(true));
EXPECT_TRUE(options.get<OpenTelemetryMetricsOption>());
}

TEST_F(ClientTest, MetricsWithEnv) {
ScopedEnvironment env("GOOGLE_CLOUD_CPP_OPENTELEMETRY_METRICS", "ON");
auto const options = internal::DefaultOptions(
Options{}.set<OpenTelemetryMetricsOption>(false));
EXPECT_TRUE(options.get<OpenTelemetryMetricsOption>());
}

TEST_F(ClientTest, ProjectIdWithoutEnv) {
ScopedEnvironment env("GOOGLE_CLOUD_PROJECT", std::nullopt);
auto const options = internal::DefaultOptions();
Expand Down
4 changes: 3 additions & 1 deletion google/cloud/storage/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "google/cloud/backoff_policy.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/rest_options.h"
#include "google/cloud/opentelemetry_options.h"
#include "google/cloud/options.h"
#include <chrono>
#include <cstdint>
Expand Down Expand Up @@ -392,7 +393,8 @@ using ClientOptionList = ::google::cloud::OptionList<
IdempotencyPolicyOption, CARootsFilePathOption,
UploadChecksumValidationOption, DownloadChecksumValidationOption,
PrecomputedChecksumsOption, storage_experimental::HttpVersionOption,
storage_experimental::OTelSpanEnrichmentOption>;
storage_experimental::OTelSpanEnrichmentOption, OpenTelemetryTracingOption,
OpenTelemetryMetricsOption>;

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
Expand Down
Loading