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
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 @@ -107,6 +107,7 @@ google_cloud_cpp_storage_hdrs = [
"internal/rest/stub.h",
"internal/retry_logging.h",
"internal/retry_object_read_source.h",
"internal/server_unreached_metrics.h",
"internal/service_account_parser.h",
"internal/service_account_requests.h",
"internal/sign_blob_requests.h",
Expand Down Expand Up @@ -224,6 +225,7 @@ google_cloud_cpp_storage_srcs = [
"internal/rest/stub.cc",
"internal/retry_logging.cc",
"internal/retry_object_read_source.cc",
"internal/server_unreached_metrics.cc",
"internal/service_account_parser.cc",
"internal/service_account_requests.cc",
"internal/sign_blob_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 @@ -181,6 +181,8 @@ add_library(
internal/retry_logging.h
internal/retry_object_read_source.cc
internal/retry_object_read_source.h
internal/server_unreached_metrics.cc
internal/server_unreached_metrics.h
internal/service_account_parser.cc
internal/service_account_parser.h
internal/service_account_requests.cc
Expand Down Expand Up @@ -487,6 +489,7 @@ if (BUILD_TESTING)
internal/rest/stub_test.cc
internal/retry_logging_test.cc
internal/retry_object_read_source_test.cc
internal/server_unreached_metrics_test.cc
internal/service_account_requests_test.cc
internal/sign_blob_requests_test.cc
internal/signed_url_requests_test.cc
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/storage/internal/metrics_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ 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 kServerUnreachedMetric =
"gcp.storage.client.server.unreached";

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
Expand Down
102 changes: 102 additions & 0 deletions google/cloud/storage/internal/server_unreached_metrics.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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/server_unreached_metrics.h"
#include "google/cloud/storage/internal/metrics_constants.h"
#include "google/cloud/version.h"
#include <opentelemetry/common/attribute_value.h>
#include <opentelemetry/common/key_value_iterable_view.h>
#include <opentelemetry/context/context.h>
#include <opentelemetry/nostd/string_view.h>
#include <array>
#include <cstdint>
#include <string>
#include <string_view>
#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) {

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.

Same point as my comment on #16712: ToNostd and SemanticVersionString are now in three places, here, operation_metrics.cc and attempt_metrics.cc.

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.

I moved these into shared metrics_attributes.{h,cc} in #16712. Once #16712 lands, I will rebase this PR onto main and reuse them here.


Replied by cpriti-o11y-agent

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());
}
Comment on lines +41 to +45

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

We can remove the redundant SemanticVersionString() helper function and use google::cloud::version_string() directly. This simplifies the code and ensures any pre-release or development suffixes are correctly preserved in the metrics.

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.

Same point as my reply on #16712: I would keep SemanticVersionString() here. version_string() returns v<major>.<minor>.<patch> plus any pre-release and build metadata, so gcp.client.version would no longer match what OperationMetrics and AttemptMetrics emit.


} // namespace

ServerUnreachedMetrics::ServerUnreachedMetrics(
opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address)
: client_version_(SemanticVersionString()),

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

Use google::cloud::version_string() directly instead of SemanticVersionString().

    : client_version_(google::cloud::version_string()),

server_address_(std::move(server_address)) {
if (!provider) return;
meter_ =
provider->GetMeter(kStorageMeterName, google::cloud::version_string());
if (!meter_) return;
server_unreached_ = meter_->CreateUInt64Counter(
ToNostd(kServerUnreachedMetric),
"Number of failed attempts proven not to have reached Google Cloud "
"Storage (pre-response failures such as DNS, TLS, connection, or auth "
"errors; excludes ambiguous aborts like timeouts or cancellations).",
"{request}");
}

void ServerUnreachedMetrics::RecordGrpcServerUnreached(
ServerUnreachedLabels const& labels,
opentelemetry::context::Context const& ctx) const {
RecordServerUnreachedImpl("grpc", labels, ctx);
}

void ServerUnreachedMetrics::RecordHttpServerUnreached(
ServerUnreachedLabels const& labels,
opentelemetry::context::Context const& ctx) const {
RecordServerUnreachedImpl("http", labels, ctx);
}

void ServerUnreachedMetrics::RecordServerUnreachedImpl(
std::string_view rpc_system, ServerUnreachedLabels const& labels,
opentelemetry::context::Context const& ctx) const {
if (!server_unreached_) 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)},
}};
server_unreached_->Add(std::uint64_t{1},
opentelemetry::common::MakeKeyValueIterableView(attrs),
ctx);
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
} // namespace google
94 changes: 94 additions & 0 deletions google/cloud/storage/internal/server_unreached_metrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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_SERVER_UNREACHED_METRICS_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_SERVER_UNREACHED_METRICS_H

#include "google/cloud/storage/version.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 failed attempt that did not reach the server
/// (`gcp.storage.client.server.unreached`).
struct ServerUnreachedLabels {
std::string_view operation;
std::string_view error_type;
std::string_view network_path;
};

/// Thread-safe recorder for the `gcp.storage.client.server.unreached`
/// OpenTelemetry Storage counter. A single instance can be shared across gRPC
/// and HTTP transports via `RecordGrpcServerUnreached` and
/// `RecordHttpServerUnreached`.
/// When constructed with a null provider, all methods are no-ops.
class ServerUnreachedMetrics {
public:
ServerUnreachedMetrics(
opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address);

/// Records a failed gRPC attempt (`rpc.system.name = "grpc"`) that is
/// positively proven not to have reached Google Cloud Storage (no server
/// response headers or trailers were received and the failure is a proven
/// pre-response error such as `DNS_FAILURE`, `TLS_FAILURE`,
/// `CONNECTION_ERROR`, or `AUTHENTICATION_ERROR`). Callers must not invoke
/// this for ambiguous client-side aborts such as `TIMEOUT`,
/// `DEADLINE_EXCEEDED`, or `CANCELLED`.
void RecordGrpcServerUnreached(
ServerUnreachedLabels const& labels,
opentelemetry::context::Context const& ctx) const;

/// Records a failed HTTP attempt (`rpc.system.name = "http"`) that is
/// positively proven not to have reached Google Cloud Storage (no server
/// response headers were received and the failure is a proven pre-response
/// error such as `DNS_FAILURE`, `TLS_FAILURE`, `CONNECTION_ERROR`, or
/// `AUTHENTICATION_ERROR`). Callers must not invoke this for ambiguous
/// client-side aborts such as `TIMEOUT`, `DEADLINE_EXCEEDED`, or
/// `CANCELLED`.
void RecordHttpServerUnreached(
ServerUnreachedLabels const& labels,
opentelemetry::context::Context const& ctx) const;

private:
void RecordServerUnreachedImpl(
std::string_view rpc_system, ServerUnreachedLabels const& labels,
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>>
server_unreached_;
};

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

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_SERVER_UNREACHED_METRICS_H
Loading
Loading