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
39 changes: 37 additions & 2 deletions google/cloud/storage/internal/attempt_metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <opentelemetry/nostd/span.h>
#include <opentelemetry/nostd/string_view.h>
#include <array>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
Expand Down Expand Up @@ -57,13 +58,16 @@ AttemptMetrics::AttemptMetrics(
provider->GetMeter(kStorageMeterName, google::cloud::version_string());
if (!meter_) return;
attempts_ = meter_->CreateUInt64Counter(
"gcp.storage.client.attempts",
ToNostd(kAttemptsMetric),
"Total number of individual backend attempts made during operations.",
"{attempt}");
errors_ = meter_->CreateUInt64Counter(
"gcp.storage.client.errors",
ToNostd(kErrorsMetric),
"Number of failed backend attempts encountered by the client.",
"{error}");
retries_ = meter_->CreateUInt64Counter(
ToNostd(kRetriesMetric),
"Number of retry attempts initiated by the client.", "{retry}");
}

void AttemptMetrics::RecordGrpcAttempt(
Expand Down Expand Up @@ -115,6 +119,37 @@ void AttemptMetrics::RecordAttemptImpl(
}
}

void AttemptMetrics::RecordGrpcRetry(
RetryLabels const& labels,
opentelemetry::context::Context const& ctx) const {
RecordRetryImpl("grpc", labels, ctx);
}

void AttemptMetrics::RecordHttpRetry(
RetryLabels const& labels,
opentelemetry::context::Context const& ctx) const {
RecordRetryImpl("http", labels, ctx);
}

void AttemptMetrics::RecordRetryImpl(
std::string_view rpc_system, RetryLabels const& labels,
opentelemetry::context::Context const& ctx) const {
if (!retries_) return;
std::array<Attr, 8> const attrs{{
{"gcp.client.version", ToNostd(client_version_)},
{"gcp.client.artifact",
opentelemetry::nostd::string_view("google-cloud-cpp")},
{"gcp.client.service", opentelemetry::nostd::string_view("storage")},
{"server.address", ToNostd(server_address_)},
{"rpc.system.name", ToNostd(rpc_system)},
{"gcp.storage.operation", ToNostd(labels.operation)},
{"error.type", ToNostd(labels.error_type)},
{"network.path", ToNostd(labels.network_path)},
}};
retries_->Add(std::uint64_t{1},
opentelemetry::common::MakeKeyValueIterableView(attrs), ctx);
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
Expand Down
29 changes: 28 additions & 1 deletion google/cloud/storage/internal/attempt_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ struct HttpAttemptLabels {
std::string_view network_path;
};

/// Labels for recording a client-initiated retry attempt
/// (`gcp.storage.client.retries`).
struct RetryLabels {
std::string_view operation;
std::string_view error_type;
std::string_view network_path;
};

/// Thread-safe recorder for attempt-scoped OpenTelemetry Storage counters
/// (`attempts` and `errors`). No-op when `provider` is null.
/// (`attempts`, `errors`, and `retries`). No-op when `provider` is null.
class AttemptMetrics {
public:
AttemptMetrics(opentelemetry::nostd::shared_ptr<
Expand All @@ -62,6 +70,19 @@ class AttemptMetrics {
void RecordHttpAttempt(HttpAttemptLabels const& labels,
opentelemetry::context::Context const& ctx) const;

/// Records a client-initiated gRPC retry (`rpc.system.name = "grpc"`).
/// Callers should invoke this only for actual retries (`attempt > 1`) or
/// proactive write stream rotations, setting `labels.error_type` to the
/// prior failed attempt's error type (or `"SLOW_STREAM"`).
void RecordGrpcRetry(RetryLabels const& labels,
opentelemetry::context::Context const& ctx) const;

/// Records a client-initiated HTTP retry (`rpc.system.name = "http"`).
/// Callers should invoke this only for actual retries (`attempt > 1`),
/// setting `labels.error_type` to the prior failed attempt's error type.
void RecordHttpRetry(RetryLabels const& labels,
opentelemetry::context::Context const& ctx) const;

private:
void RecordAttemptImpl(std::string_view rpc_system,
std::string_view operation,
Expand All @@ -70,6 +91,9 @@ class AttemptMetrics {
opentelemetry::common::AttributeValue status_value,
opentelemetry::context::Context const& ctx) const;

void RecordRetryImpl(std::string_view rpc_system, RetryLabels const& labels,
opentelemetry::context::Context const& ctx) const;

std::string client_version_;
std::string server_address_;
opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Meter> meter_;
Expand All @@ -79,6 +103,9 @@ class AttemptMetrics {
opentelemetry::nostd::unique_ptr<
opentelemetry::metrics::Counter<std::uint64_t>>
errors_;
opentelemetry::nostd::unique_ptr<
opentelemetry::metrics::Counter<std::uint64_t>>
retries_;
};

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
71 changes: 70 additions & 1 deletion google/cloud/storage/internal/attempt_metrics_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ TEST(AttemptMetricsTest, RecordsGrpcAndHttpAttributes) {
EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.errors"), _,
Eq("{error}")))
.WillOnce(Return(ByMove(CounterPtr(errors.release()))));
EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.retries"), _,
Eq("{retry}")))
.WillOnce(Return(ByMove(CounterPtr(nullptr))));

auto provider = std::make_shared<MockMeterProvider>();
EXPECT_CALL(*provider, GetMeter)
Expand Down Expand Up @@ -223,6 +226,8 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) {
no_provider.RecordGrpcAttempt({"ReadObject", "OK", "OK", "directpath"}, ctx);
no_provider.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"},
ctx);
no_provider.RecordGrpcRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx);
no_provider.RecordHttpRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx);

auto provider = std::make_shared<MockMeterProvider>();
EXPECT_CALL(*provider, GetMeter)
Expand All @@ -237,13 +242,16 @@ TEST(AttemptMetricsTest, NullProviderAndNullMeterAreNoOps) {
{"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx);
no_meter.RecordHttpAttempt({"ReadObject", "UNAVAILABLE", 503, "cloudpath"},
ctx);
no_meter.RecordGrpcRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx);
no_meter.RecordHttpRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx);
}

TEST(AttemptMetricsTest, NullAndPartialInstruments) {
opentelemetry::context::Context const ctx{};
opentelemetry::nostd::shared_ptr<MockMeter> empty_meter =
std::make_shared<MockMeter>();
EXPECT_CALL(*empty_meter, CreateUInt64Counter)
.WillOnce(Return(ByMove(CounterPtr(nullptr))))
.WillOnce(Return(ByMove(CounterPtr(nullptr))))
.WillOnce(Return(ByMove(CounterPtr(nullptr))));

Expand All @@ -253,14 +261,16 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) {
std::make_shared<MockMeter>();
EXPECT_CALL(*no_attempts_meter, CreateUInt64Counter)
.WillOnce(Return(ByMove(CounterPtr(nullptr))))
.WillOnce(Return(ByMove(CounterPtr(errors.release()))));
.WillOnce(Return(ByMove(CounterPtr(errors.release()))))
.WillOnce(Return(ByMove(CounterPtr(nullptr))));

auto attempts = std::make_unique<MockCounter<std::uint64_t>>();
EXPECT_CALL(*attempts, Add(std::uint64_t{1}, _, _)).Times(1);
opentelemetry::nostd::shared_ptr<MockMeter> no_errors_meter =
std::make_shared<MockMeter>();
EXPECT_CALL(*no_errors_meter, CreateUInt64Counter)
.WillOnce(Return(ByMove(CounterPtr(attempts.release()))))
.WillOnce(Return(ByMove(CounterPtr(nullptr))))
.WillOnce(Return(ByMove(CounterPtr(nullptr))));

auto provider = std::make_shared<MockMeterProvider>();
Expand All @@ -275,6 +285,8 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) {
AttemptMetrics none(mp, "storage.googleapis.com");
none.RecordGrpcAttempt(
{"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx);
none.RecordGrpcRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx);
none.RecordHttpRetry({"ReadObject", "UNAVAILABLE", "cloudpath"}, ctx);

AttemptMetrics no_attempts(mp, "storage.googleapis.com");
no_attempts.RecordGrpcAttempt({"ReadObject", "OK", "OK", "directpath"}, ctx);
Expand All @@ -286,6 +298,63 @@ TEST(AttemptMetricsTest, NullAndPartialInstruments) {
{"ReadObject", "UNAVAILABLE", "UNAVAILABLE", "cloudpath"}, ctx);
}

TEST(AttemptMetricsTest, RecordsGrpcAndHttpRetryAttributes) {
std::vector<CapturedAttrs> retry_attrs;
auto retries = std::make_unique<MockCounter<std::uint64_t>>();
EXPECT_CALL(*retries, Add(std::uint64_t{1}, _, _))
.Times(2)
.WillRepeatedly([&](std::uint64_t,
opentelemetry::common::KeyValueIterable const& kv,
opentelemetry::context::Context const& ctx) {
ExpectTraceMarker(ctx);
retry_attrs.push_back(ExtractAttrs(kv));
});

auto attempts =
std::make_unique<::testing::StrictMock<MockCounter<std::uint64_t>>>();
auto errors =
std::make_unique<::testing::StrictMock<MockCounter<std::uint64_t>>>();

opentelemetry::nostd::shared_ptr<MockMeter> meter =
std::make_shared<MockMeter>();
EXPECT_CALL(*meter,
CreateUInt64Counter(Eq("gcp.storage.client.attempts"), _, _))
.WillOnce(Return(ByMove(CounterPtr(attempts.release()))));
EXPECT_CALL(*meter,
CreateUInt64Counter(Eq("gcp.storage.client.errors"), _, _))
.WillOnce(Return(ByMove(CounterPtr(errors.release()))));
EXPECT_CALL(*meter, CreateUInt64Counter(Eq("gcp.storage.client.retries"), _,
Eq("{retry}")))
.WillOnce(Return(ByMove(CounterPtr(retries.release()))));

auto provider = std::make_shared<MockMeterProvider>();
EXPECT_CALL(*provider, GetMeter).WillOnce(Return(meter));
opentelemetry::nostd::shared_ptr<opentelemetry::metrics::MeterProvider> mp(
std::static_pointer_cast<opentelemetry::metrics::MeterProvider>(
provider));

AttemptMetrics metrics(mp, "storage.googleapis.com");
opentelemetry::context::Context const ctx("trace_marker", std::int64_t{42});
metrics.RecordHttpRetry({"DeleteObject", "UNAVAILABLE", "cloudpath"}, ctx);
metrics.RecordGrpcRetry({"WriteObject", "SLOW_STREAM", "directpath"}, ctx);

ASSERT_EQ(retry_attrs.size(), 2U);
EXPECT_THAT(retry_attrs[0].strings,
ContainerEq(ExpectedStringAttrs(
{{"rpc.system.name", "http"},
{"gcp.storage.operation", "DeleteObject"},
{"error.type", "UNAVAILABLE"},
{"network.path", "cloudpath"}})));
EXPECT_THAT(retry_attrs[0].ints, IsEmpty());
EXPECT_THAT(
retry_attrs[1].strings,
ContainerEq(ExpectedStringAttrs({{"rpc.system.name", "grpc"},
{"gcp.storage.operation", "WriteObject"},
{"error.type", "SLOW_STREAM"},
{"network.path", "directpath"}})));
EXPECT_THAT(retry_attrs[1].ints, IsEmpty());
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
Expand Down
4 changes: 4 additions & 0 deletions google/cloud/storage/internal/metrics_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ inline constexpr char const* kStorageMeterName = "gl-cpp";

/// Shared OpenTelemetry instrument names referenced by both recorders and
/// exporter/view registration so they cannot drift apart.
inline constexpr std::string_view kAttemptsMetric =
"gcp.storage.client.attempts";
inline constexpr std::string_view kErrorsMetric = "gcp.storage.client.errors";
inline constexpr std::string_view kOperationsMetric =
"gcp.storage.client.operations";
inline constexpr std::string_view kRequestDurationMetric =
"gcp.client.request.duration";
inline constexpr std::string_view kRetriesMetric = "gcp.storage.client.retries";

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
Expand Down
Loading