Repository navigation
impl(storage): add PayloadMetrics recorder for OTel body size histograms #16712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cpriti-os
wants to merge
1
commit into
googleapis:main
Choose a base branch
from
cpriti-os:impl-storage-payload-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+456
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| PayloadMetrics::PayloadMetrics( | ||
| opentelemetry::nostd::shared_ptr< | ||
| opentelemetry::metrics::MeterProvider> const& provider, | ||
| std::string server_address, std::string rpc_system) | ||
| : client_version_(SemanticVersionString()), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify the code and avoid manual string formatting by using
google::cloud::version_string()directly instead of defining a customSemanticVersionString()helper. This is also more consistent with how the client version is recorded in other parts of the SDK.