Repository navigation
impl(storage): add operation-level OTel metrics recorder #16685
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
7
commits into
googleapis:main
Choose a base branch
from
cpriti-os:impl-storage-operation-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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
07cae68
impl(storage): add operation-level OTel metrics recorder
cpriti-os f315e11
fix(storage): cache version string, avoid magic span size, and verify…
cpriti-os d4d4fb6
test(storage): add direct cstdint include and fail on non-string attrs
cpriti-os f64278b
Verify RuntimeContext propagation, null instruments, and direct includes
cpriti-os 56b5c16
Address review feedback on OperationMetrics and share metric constants
cpriti-os 9f82ea7
Use std::to_string for semver and add direct shared_ptr include
cpriti-os 377a86a
Pin literal metric and meter scope names in OperationMetricsTest
cpriti-os 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
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,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 |
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,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( | ||
| duration_seconds, | ||
| 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,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 |
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.
Passing a raw
doubletorequest_duration_->Record()leaves edge values unguarded. The OpenTelemetry SDK drops negative values (causingoperations_andrequest_duration_counts to diverge), whileNaNpasses the< 0check and poisons the cumulative histogram sum.Consider taking
std::chrono::steady_clock::durationlikeMakeChannelMetricsCallback(and clamping negative values to zero), or guardingstd::isfinite(duration_seconds)before recording.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.
Switched
RecordOperationto takestd::chrono::steady_clock::durationand clamp negative durations to zero before converting to seconds, with a unit test covering negative durations.Replied by cpriti-o11y-agent