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
3 changes: 3 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ google_cloud_cpp_storage_hdrs = [
"internal/make_jwt_assertion.h",
"internal/md5hash.h",
"internal/metadata_parser.h",
"internal/metrics_constants.h",
"internal/metrics_error_type.h",
"internal/notification_metadata_parser.h",
"internal/notification_requests.h",
Expand All @@ -95,6 +96,7 @@ google_cloud_cpp_storage_hdrs = [
"internal/object_read_streambuf.h",
"internal/object_requests.h",
"internal/object_write_streambuf.h",
"internal/operation_metrics.h",
"internal/patch_builder.h",
"internal/patch_builder_details.h",
"internal/policy_document_request.h",
Expand Down Expand Up @@ -210,6 +212,7 @@ google_cloud_cpp_storage_srcs = [
"internal/object_requests.cc",
"internal/object_write_streambuf.cc",
"internal/openssl/hash_function_impl.cc",
"internal/operation_metrics.cc",
"internal/patch_builder.cc",
"internal/patch_builder_details.cc",
"internal/policy_document_request.cc",
Expand Down
4 changes: 4 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ add_library(
internal/md5hash.h
internal/metadata_parser.cc
internal/metadata_parser.h
internal/metrics_constants.h
internal/metrics_error_type.cc
internal/metrics_error_type.h
internal/notification_metadata_parser.cc
Expand All @@ -158,6 +159,8 @@ add_library(
internal/object_write_streambuf.cc
internal/object_write_streambuf.h
internal/openssl/hash_function_impl.cc
internal/operation_metrics.cc
internal/operation_metrics.h
internal/patch_builder.cc
internal/patch_builder.h
internal/patch_builder_details.cc
Expand Down Expand Up @@ -472,6 +475,7 @@ if (BUILD_TESTING)
internal/object_read_streambuf_test.cc
internal/object_requests_test.cc
internal/object_write_streambuf_test.cc
internal/operation_metrics_test.cc
internal/patch_builder_test.cc
internal/policy_document_request_test.cc
internal/request_project_id_test.cc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "google/cloud/storage/internal/grpc/metrics_exporter_options.h"
#include "google/cloud/opentelemetry/monitoring_exporter.h"
#include "google/cloud/storage/internal/metrics_constants.h"
#include "google/cloud/common_options.h"
#include "google/cloud/internal/invocation_id_generator.h"
#include "google/cloud/universe_domain_options.h"
Expand Down Expand Up @@ -77,7 +78,7 @@ Options MetricsExporterOptions(
return Options{}
.set<otel::ServiceTimeSeriesOption>(true)
.set<otel::MetricNameFormatterOption>([](std::string s) -> std::string {
if (s == "gcp.client.request.duration") {
if (s == kRequestDurationMetric) {
return "storage.googleapis.com/client/request_duration";
}
// Storage SDK metrics under `gcp.storage.client.*` are registered in
Expand Down
12 changes: 2 additions & 10 deletions google/cloud/storage/internal/grpc/metrics_meter_provider.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/metrics_constants.h"
#include "google/cloud/version.h"
#include <opentelemetry/metrics/meter_provider.h>
#include <opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h>
Expand All @@ -31,15 +32,6 @@ namespace cloud {
namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

/**
* The instrumentation scope for instruments created by this library.
*
* gRPC's own instruments use the `grpc-c++` scope. Using the same scope name
* as the tracer (`gl-cpp`) keeps the library's telemetry identifiable and
* lets `MakeGrpcMeterProvider()` attach a latency view to it.
*/
auto constexpr kStorageMeterName = "gl-cpp";

/**
* The fully qualified name of the `channel_creation_latency` instrument.
*
Expand All @@ -56,7 +48,7 @@ std::string ChannelCreationLatencyInstrument();
/// Storage SDK latency histogram instruments registered with explicit buckets.
inline constexpr std::array<std::string_view, 9>
kStorageLatencyHistogramInstruments = {{
"gcp.client.request.duration",
kRequestDurationMetric,
"gcp.storage.client.ttfb",
"rpc.client.call.duration",
"gcp.storage.client.stall.duration",
Expand Down
47 changes: 47 additions & 0 deletions google/cloud/storage/internal/metrics_constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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_METRICS_CONSTANTS_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_METRICS_CONSTANTS_H

#include "google/cloud/storage/version.h"
#include <string_view>

namespace google {
namespace cloud {
namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

/**
* The instrumentation scope for instruments created by this library.
*
* gRPC's own instruments use the `grpc-c++` scope. Using the same scope name
* as the tracer (`gl-cpp`) keeps the library's telemetry identifiable and
* lets `MakeGrpcMeterProvider()` attach histogram views to it.
*/
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 kOperationsMetric =
"gcp.storage.client.operations";
inline constexpr std::string_view kRequestDurationMetric =
"gcp.client.request.duration";

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

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_METRICS_CONSTANTS_H
108 changes: 108 additions & 0 deletions google/cloud/storage/internal/operation_metrics.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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/operation_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/span.h>
#include <opentelemetry/nostd/string_view.h>
#include <array>
#include <chrono>
#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

OperationMetrics::OperationMetrics(
opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address, std::string rpc_system)
: client_version_(SemanticVersionString()),
server_address_(std::move(server_address)),
rpc_system_(std::move(rpc_system)) {
if (!provider) return;
meter_ =
provider->GetMeter(kStorageMeterName, google::cloud::version_string());
if (!meter_) return;
operations_ = meter_->CreateUInt64Counter(
ToNostd(kOperationsMetric),
"Number of logical GCS operations initiated by the client.",
"{operation}");
request_duration_ = meter_->CreateDoubleHistogram(
ToNostd(kRequestDurationMetric),
"Application-level end-to-end latency of logical GCS operations.", "s");
}

void OperationMetrics::RecordOperation(
OperationLabels const& labels, std::chrono::steady_clock::duration elapsed,
opentelemetry::context::Context const& ctx) const {
if (!operations_ && !request_duration_) return;
// `rpc.method` is placed last so `operations_` 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(labels.operation)},
{"gcp.client.method", ToNostd(labels.client_method)},
{"error.type", ToNostd(labels.error_type)},
{"rpc.method", ToNostd(labels.rpc_method)},
}};
if (operations_) {
opentelemetry::nostd::span<Attr const> const op_attrs(attrs.data(),
attrs.size() - 1);
operations_->Add(std::uint64_t{1},
opentelemetry::common::MakeKeyValueIterableView(op_attrs),
ctx);
}
if (request_duration_) {
double const duration_seconds =
elapsed < std::chrono::steady_clock::duration::zero()
? 0.0
: std::chrono::duration<double>(elapsed).count();
request_duration_->Record(

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.

Passing a raw double to request_duration_->Record() leaves edge values unguarded. The OpenTelemetry SDK drops negative values (causing operations_ and request_duration_ counts to diverge), while NaN passes the < 0 check and poisons the cumulative histogram sum.

Consider taking std::chrono::steady_clock::duration like MakeChannelMetricsCallback (and clamping negative values to zero), or guarding std::isfinite(duration_seconds) before recording.

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.

Switched RecordOperation to take std::chrono::steady_clock::duration and clamp negative durations to zero before converting to seconds, with a unit test covering negative durations.


Replied by cpriti-o11y-agent

duration_seconds,
opentelemetry::common::MakeKeyValueIterableView(attrs), ctx);
}
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
} // namespace google
73 changes: 73 additions & 0 deletions google/cloud/storage/internal/operation_metrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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_OPERATION_METRICS_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_OPERATION_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 <chrono>
#include <cstdint>
#include <string>
#include <string_view>

namespace google {
namespace cloud {
namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

/// Labels for recording a logical Storage operation.
struct OperationLabels {
std::string_view operation;
std::string_view client_method;
std::string_view rpc_method;
std::string_view error_type;
};

/// Thread-safe recorder for operation-scoped OpenTelemetry Storage metrics
/// (`gcp.storage.client.operations` and `gcp.client.request.duration`).
/// When constructed with a null provider, all methods are no-ops.
class OperationMetrics {
public:
OperationMetrics(opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address, std::string rpc_system);

void RecordOperation(OperationLabels const& labels,
std::chrono::steady_clock::duration elapsed,
opentelemetry::context::Context const& ctx) const;

private:
std::string client_version_;
std::string server_address_;
std::string rpc_system_;
opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Meter> meter_;
opentelemetry::nostd::unique_ptr<
opentelemetry::metrics::Counter<std::uint64_t>>
operations_;
opentelemetry::nostd::unique_ptr<opentelemetry::metrics::Histogram<double>>
request_duration_;
};

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

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_OPERATION_METRICS_H
Loading
Loading