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 @@ -98,6 +98,7 @@ google_cloud_cpp_storage_hdrs = [
"internal/object_write_streambuf.h",
"internal/patch_builder.h",
"internal/patch_builder_details.h",
"internal/payload_metrics.h",
"internal/policy_document_request.h",
"internal/request_project_id.h",
"internal/rest/object_read_source.h",
Expand Down Expand Up @@ -214,6 +215,7 @@ google_cloud_cpp_storage_srcs = [
"internal/openssl/hash_function_impl.cc",
"internal/patch_builder.cc",
"internal/patch_builder_details.cc",
"internal/payload_metrics.cc",
"internal/policy_document_request.cc",
"internal/request_project_id.cc",
"internal/rest/object_read_source.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 @@ -164,6 +164,8 @@ add_library(
internal/patch_builder.h
internal/patch_builder_details.cc
internal/patch_builder_details.h
internal/payload_metrics.cc
internal/payload_metrics.h
internal/policy_document_request.cc
internal/policy_document_request.h
internal/request_project_id.cc
Expand Down Expand Up @@ -476,6 +478,7 @@ if (BUILD_TESTING)
internal/object_requests_test.cc
internal/object_write_streambuf_test.cc
internal/patch_builder_test.cc
internal/payload_metrics_test.cc
internal/policy_document_request_test.cc
internal/request_project_id_test.cc
internal/rest/object_read_source_test.cc
Expand Down
105 changes: 105 additions & 0 deletions google/cloud/storage/internal/payload_metrics.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// 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/payload_metrics.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 {

constexpr char const* kStorageMeterName = "gl-cpp";
constexpr char const* kRequestBodySizeMetric =
"gcp.storage.client.request.body.size";
constexpr char const* kResponseBodySizeMetric =
"gcp.storage.client.response.body.size";

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

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 simplify the code and avoid manual string formatting by using google::cloud::version_string() directly instead of defining a custom SemanticVersionString() helper. This is also more consistent with how the client version is recorded in other parts of the SDK.


} // namespace

PayloadMetrics::PayloadMetrics(
opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address, std::string rpc_system)
: 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 here to initialize client_version_.

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

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;
request_body_size_ = meter_->CreateUInt64Histogram(
kRequestBodySizeMetric, "Total logical payload bytes uploaded.", "By");
response_body_size_ = meter_->CreateUInt64Histogram(
kResponseBodySizeMetric, "Total logical payload bytes downloaded.", "By");
}

void PayloadMetrics::RecordRequestBodySize(
PayloadLabels const& labels, std::uint64_t bytes,
opentelemetry::context::Context const& ctx) const {
RecordBodySize(request_body_size_.get(), labels, bytes, ctx);
}

void PayloadMetrics::RecordResponseBodySize(
PayloadLabels const& labels, std::uint64_t bytes,
opentelemetry::context::Context const& ctx) const {
RecordBodySize(response_body_size_.get(), labels, bytes, ctx);
}

void PayloadMetrics::RecordBodySize(
opentelemetry::metrics::Histogram<std::uint64_t>* histogram,
PayloadLabels const& labels, std::uint64_t bytes,
opentelemetry::context::Context const& ctx) const {
if (!histogram) return;
std::array<Attr, 7> 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)},
}};
histogram->Record(
bytes, opentelemetry::common::MakeKeyValueIterableView(attrs), ctx);
}

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage_internal
} // namespace cloud
} // namespace google
79 changes: 79 additions & 0 deletions google/cloud/storage/internal/payload_metrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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_PAYLOAD_METRICS_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_PAYLOAD_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 request or response payload body sizes.
struct PayloadLabels {
std::string_view operation;
std::string_view client_method;
};

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

void RecordRequestBodySize(PayloadLabels const& labels, std::uint64_t bytes,
opentelemetry::context::Context const& ctx) const;

void RecordResponseBodySize(PayloadLabels const& labels, std::uint64_t bytes,
opentelemetry::context::Context const& ctx) const;

private:
void RecordBodySize(
opentelemetry::metrics::Histogram<std::uint64_t>* histogram,
PayloadLabels const& labels, std::uint64_t bytes,
opentelemetry::context::Context const& ctx) const;

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::Histogram<std::uint64_t>>
request_body_size_;
opentelemetry::nostd::unique_ptr<
opentelemetry::metrics::Histogram<std::uint64_t>>
response_body_size_;
};

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

#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_PAYLOAD_METRICS_H
Loading
Loading