Skip to content
Open
2 changes: 2 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ google_cloud_cpp_storage_hdrs = [
"iam_policy.h",
"idempotency_policy.h",
"include_folders_as_prefixes.h",
"internal/attempt_metrics.h",
"internal/base64.h",
"internal/binary_data_as_debug_string.h",
"internal/bucket_access_control_parser.h",
Expand Down Expand Up @@ -168,6 +169,7 @@ google_cloud_cpp_storage_srcs = [
"hmac_key_metadata.cc",
"iam_policy.cc",
"idempotency_policy.cc",
"internal/attempt_metrics.cc",
"internal/base64.cc",
"internal/bucket_access_control_parser.cc",
"internal/bucket_acl_requests.cc",
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ add_library(
idempotency_policy.cc
idempotency_policy.h
include_folders_as_prefixes.h
internal/attempt_metrics.cc
internal/attempt_metrics.h
internal/base64.cc
internal/base64.h
internal/binary_data_as_debug_string.h
Expand Down Expand Up @@ -429,6 +431,7 @@ if (BUILD_TESTING)
hashing_options_test.cc
hmac_key_metadata_test.cc
idempotency_policy_test.cc
internal/attempt_metrics_test.cc
internal/base64_test.cc
internal/bucket_acl_requests_test.cc
internal/bucket_metadata_cache_test.cc
Expand Down
117 changes: 117 additions & 0 deletions google/cloud/storage/internal/attempt_metrics.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "google/cloud/storage/internal/attempt_metrics.h"
#include "google/cloud/version.h"
#include <opentelemetry/common/key_value_iterable_view.h>
#include <opentelemetry/context/context.h>
#include <opentelemetry/nostd/span.h>
#include <opentelemetry/nostd/string_view.h>
#include <array>
#include <string>
#include <utility>

namespace google {
namespace cloud {
namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using Attr = std::pair<opentelemetry::nostd::string_view,
opentelemetry::common::AttributeValue>;

opentelemetry::nostd::string_view ToNostd(std::string_view sv) {
return opentelemetry::nostd::string_view(sv.data(), sv.size());
}

std::string SemanticVersionString() {
return std::to_string(google::cloud::version_major()) + "." +
std::to_string(google::cloud::version_minor()) + "." +
std::to_string(google::cloud::version_patch());
}

} // namespace

AttemptMetrics::AttemptMetrics(
opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address)
: client_version_(SemanticVersionString()),
server_address_(std::move(server_address)) {
if (!provider) return;
meter_ = provider->GetMeter("gl-cpp", google::cloud::version_string());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hardcoding "gl-cpp" here repeats kStorageMeterName and HedgedReadMetrics. Once #16685 lands metrics_constants.h, consider referencing kStorageMeterName here (or in a follow-up) so the storage instrumentation scope stays in one place.

if (!meter_) return;
attempts_ = meter_->CreateUInt64Counter(
"gcp.storage.client.attempts",
"Total number of individual backend attempts made during operations.",
"{attempt}");
errors_ = meter_->CreateUInt64Counter(
"gcp.storage.client.errors",
"Number of failed backend attempts encountered by the client.",
"{error}");
}

void AttemptMetrics::RecordGrpcAttempt(
GrpcAttemptLabels const& labels,
opentelemetry::context::Context const& ctx) const {
RecordAttemptImpl("grpc", labels.operation, labels.error_type,
labels.network_path, "rpc.response.status_code",
ToNostd(labels.rpc_status_code), ctx);
}

void AttemptMetrics::RecordHttpAttempt(
HttpAttemptLabels const& labels,
opentelemetry::context::Context const& ctx) const {
RecordAttemptImpl("http", labels.operation, labels.error_type,
labels.network_path, "http.response.status_code",
labels.http_status_code, ctx);
}

void AttemptMetrics::RecordAttemptImpl(
std::string_view rpc_system, std::string_view operation,
std::string_view error_type, std::string_view network_path,
char const* status_key, opentelemetry::common::AttributeValue status_value,
opentelemetry::context::Context const& ctx) const {
if (!attempts_ && !errors_) return;
// The status code attribute is placed last so `errors_` can exclude it via a
// prefix span of size `attrs.size() - 1`.
std::array<Attr, 9> 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(operation)},
{"error.type", ToNostd(error_type)},
{"network.path", ToNostd(network_path)},
{status_key, status_value},
}};
if (attempts_) {
attempts_->Add(std::uint64_t{1},
opentelemetry::common::MakeKeyValueIterableView(attrs), ctx);
}
if (errors_ && error_type != "OK") {
opentelemetry::nostd::span<Attr const> const err_attrs(attrs.data(),
attrs.size() - 1);
errors_->Add(std::uint64_t{1},
Comment on lines +105 to +108

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

Using a hardcoded magic number 8 for the span size is fragile and can easily break if attributes are added or reordered in the future. Use attrs.size() - 1 instead to dynamically exclude the last element (the status code) in a more robust way.

  if (errors_ && error_type != "OK") {
    opentelemetry::nostd::span<Attr const> const err_attrs(attrs.data(),
                                                           attrs.size() - 1);
    errors_->Add(std::uint64_t{1},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced magic 8 with attrs.size() - 1 and added a brief comment explaining that the status code attribute is intentionally placed last.


Replied by cpriti-o11y-agent

opentelemetry::common::MakeKeyValueIterableView(err_attrs),
ctx);
}
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
} // namespace google
89 changes: 89 additions & 0 deletions google/cloud/storage/internal/attempt_metrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_METRICS_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_METRICS_H

#include "google/cloud/storage/version.h"
#include <opentelemetry/common/attribute_value.h>
#include <opentelemetry/context/context.h>
#include <opentelemetry/metrics/meter.h>
#include <opentelemetry/metrics/meter_provider.h>
#include <opentelemetry/metrics/sync_instruments.h>
#include <opentelemetry/nostd/shared_ptr.h>
#include <opentelemetry/nostd/unique_ptr.h>
#include <cstdint>
#include <string>
#include <string_view>

namespace google {
namespace cloud {
namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

/// Labels for recording a single gRPC backend attempt.
struct GrpcAttemptLabels {
std::string_view operation;
std::string_view error_type;
std::string_view rpc_status_code;
std::string_view network_path;
};

/// Labels for recording a single HTTP backend attempt.
struct HttpAttemptLabels {
std::string_view operation;
std::string_view error_type;
std::int64_t http_status_code = 0;
std::string_view network_path;
};

/// Thread-safe recorder for attempt-scoped OpenTelemetry Storage counters
/// (`attempts` and `errors`). No-op when `provider` is null.
class AttemptMetrics {
public:
AttemptMetrics(opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address);

void RecordGrpcAttempt(GrpcAttemptLabels const& labels,
opentelemetry::context::Context const& ctx) const;

void RecordHttpAttempt(HttpAttemptLabels const& labels,
opentelemetry::context::Context const& ctx) const;

private:
void RecordAttemptImpl(std::string_view rpc_system,
std::string_view operation,
std::string_view error_type,
std::string_view network_path, char const* status_key,
opentelemetry::common::AttributeValue status_value,
opentelemetry::context::Context const& ctx) const;

std::string client_version_;
std::string server_address_;
opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Meter> meter_;
opentelemetry::nostd::unique_ptr<
opentelemetry::metrics::Counter<std::uint64_t>>
attempts_;
opentelemetry::nostd::unique_ptr<
opentelemetry::metrics::Counter<std::uint64_t>>
errors_;
};

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
} // namespace google

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_METRICS_H
Loading
Loading