Repository navigation
impl(storage): add ServerUnreachedMetrics recorder for OTel server.unreached counter #16714
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-server-unreached-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.
+348
−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,91 @@ | ||
| // 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/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* kServerUnreachedMetric = | ||
| "gcp.storage.client.server.unreached"; | ||
|
|
||
| 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 | ||
|
|
||
| ServerUnreachedMetrics::ServerUnreachedMetrics( | ||
| opentelemetry::nostd::shared_ptr< | ||
| opentelemetry::metrics::MeterProvider> const& provider, | ||
| std::string server_address) | ||
| : 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)) { | ||
| if (!provider) return; | ||
| meter_ = | ||
| provider->GetMeter(kStorageMeterName, google::cloud::version_string()); | ||
| if (!meter_) return; | ||
| server_unreached_ = meter_->CreateUInt64Counter( | ||
| kServerUnreachedMetric, | ||
| "Number of failed requests that did not reach Google Cloud Storage.", | ||
| "{request}"); | ||
| } | ||
|
|
||
| void ServerUnreachedMetrics::RecordServerUnreached( | ||
| 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(labels.rpc_system)}, | ||
| {"gcp.storage.operation", ToNostd(labels.operation)}, | ||
| {"network.path", ToNostd(labels.network_path)}, | ||
| {"error.type", ToNostd(labels.error_type)}, | ||
| }}; | ||
| 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 | ||
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,70 @@ | ||
| // 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 rpc_system; | ||
| std::string_view operation; | ||
| std::string_view network_path; | ||
| std::string_view error_type; | ||
| }; | ||
|
|
||
| /// Thread-safe recorder for the `gcp.storage.client.server.unreached` | ||
| /// OpenTelemetry Storage counter. | ||
| /// 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); | ||
|
|
||
| void RecordServerUnreached(ServerUnreachedLabels const& labels, | ||
| opentelemetry::context::Context const& ctx) const; | ||
|
|
||
| private: | ||
| 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 |
181 changes: 181 additions & 0 deletions
181
google/cloud/storage/internal/server_unreached_metrics_test.cc
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,181 @@ | ||
| // 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/testing_util/mock_opentelemetry_metrics.h" | ||
| #include "google/cloud/version.h" | ||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
| #include <opentelemetry/common/attribute_value.h> | ||
| #include <opentelemetry/common/key_value_iterable.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/string_view.h> | ||
| #include <opentelemetry/nostd/unique_ptr.h> | ||
| #include <opentelemetry/nostd/variant.h> | ||
| #include <opentelemetry/version.h> | ||
| #include <cstdint> | ||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| namespace google { | ||
| namespace cloud { | ||
| namespace storage_internal { | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
| namespace { | ||
|
|
||
| using ::google::cloud::testing_util::MockCounter; | ||
| using ::google::cloud::testing_util::MockMeter; | ||
| using ::google::cloud::testing_util::MockMeterProvider; | ||
| using ::testing::_; | ||
| using ::testing::ByMove; | ||
| using ::testing::Eq; | ||
| using ::testing::Pair; | ||
| using ::testing::Return; | ||
| using ::testing::UnorderedElementsAre; | ||
|
|
||
| using CounterPtr = opentelemetry::nostd::unique_ptr< | ||
| opentelemetry::metrics::Counter<std::uint64_t>>; | ||
|
|
||
| std::map<std::string, std::string> ExtractStringAttrs( | ||
| opentelemetry::common::KeyValueIterable const& iterable) { | ||
| std::map<std::string, std::string> result; | ||
| iterable.ForEachKeyValue( | ||
| [&](opentelemetry::nostd::string_view key, | ||
| opentelemetry::common::AttributeValue value) noexcept { | ||
| if (auto const* sv = | ||
| opentelemetry::nostd::get_if<opentelemetry::nostd::string_view>( | ||
| &value)) { | ||
| result.emplace(std::string(key.data(), key.size()), | ||
| std::string(sv->data(), sv->size())); | ||
| } else { | ||
| ADD_FAILURE() << "Unexpected non-string attribute for key: " | ||
| << std::string(key.data(), key.size()); | ||
| } | ||
| return true; | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| TEST(ServerUnreachedMetricsTest, RecordsServerUnreachedWithAttributes) { | ||
| std::map<std::string, std::string> recorded_attrs; | ||
| auto counter = std::make_unique<MockCounter<std::uint64_t>>(); | ||
| EXPECT_CALL(*counter, Add(std::uint64_t{1}, _, _)) | ||
| .WillOnce([&](std::uint64_t, | ||
| opentelemetry::common::KeyValueIterable const& kv, | ||
| opentelemetry::context::Context const& ctx) { | ||
| opentelemetry::context::ContextValue const val = | ||
| ctx.GetValue("trace_marker"); | ||
| auto const* marker = opentelemetry::nostd::get_if<std::int64_t>(&val); | ||
| ASSERT_NE(marker, nullptr); | ||
| EXPECT_EQ(*marker, 42); | ||
| recorded_attrs = ExtractStringAttrs(kv); | ||
| }); | ||
|
|
||
| opentelemetry::nostd::shared_ptr<MockMeter> meter = | ||
| std::make_shared<MockMeter>(); | ||
| EXPECT_CALL(*meter, | ||
| CreateUInt64Counter(Eq("gcp.storage.client.server.unreached"), _, | ||
| Eq("{request}"))) | ||
| .WillOnce(Return(ByMove(CounterPtr(counter.release())))); | ||
|
|
||
| auto provider = std::make_shared<MockMeterProvider>(); | ||
| EXPECT_CALL(*provider, GetMeter) | ||
| #if OPENTELEMETRY_ABI_VERSION_NO >= 2 | ||
| .WillOnce([&](opentelemetry::nostd::string_view scope, | ||
| opentelemetry::nostd::string_view scope_version, | ||
| opentelemetry::nostd::string_view, | ||
| opentelemetry::common::KeyValueIterable const*) { | ||
| #else | ||
| .WillOnce([&](opentelemetry::nostd::string_view scope, | ||
| opentelemetry::nostd::string_view scope_version, | ||
| opentelemetry::nostd::string_view) { | ||
| #endif | ||
| EXPECT_EQ(scope, "gl-cpp"); | ||
| EXPECT_EQ(scope_version, google::cloud::version_string()); | ||
| return opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Meter>( | ||
| meter); | ||
| }); | ||
|
|
||
| ServerUnreachedMetrics metrics( | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::MeterProvider>( | ||
| provider), | ||
| "private.googleapis.com"); | ||
|
|
||
| opentelemetry::context::Context const ctx("trace_marker", std::int64_t{42}); | ||
| metrics.RecordServerUnreached( | ||
| {"http", "ReadObject", "cloudpath", "DNS_FAILURE"}, ctx); | ||
|
|
||
| std::string const expected_version = | ||
| std::to_string(google::cloud::version_major()) + "." + | ||
| std::to_string(google::cloud::version_minor()) + "." + | ||
| std::to_string(google::cloud::version_patch()); | ||
|
Comment on lines
+124
to
+127
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. |
||
|
|
||
| EXPECT_THAT( | ||
| recorded_attrs, | ||
| UnorderedElementsAre(Pair("gcp.client.version", expected_version), | ||
| Pair("gcp.client.artifact", "google-cloud-cpp"), | ||
| Pair("gcp.client.service", "storage"), | ||
| Pair("server.address", "private.googleapis.com"), | ||
| Pair("rpc.system.name", "http"), | ||
| Pair("gcp.storage.operation", "ReadObject"), | ||
| Pair("network.path", "cloudpath"), | ||
| Pair("error.type", "DNS_FAILURE"))); | ||
| } | ||
|
|
||
| TEST(ServerUnreachedMetricsTest, | ||
| NullProviderNullMeterAndNullInstrumentAreNoOps) { | ||
| opentelemetry::context::Context const ctx; | ||
|
|
||
| ServerUnreachedMetrics null_provider(nullptr, "storage.googleapis.com"); | ||
| null_provider.RecordServerUnreached( | ||
| {"grpc", "WriteObject", "directpath", "CONNECTION_ERROR"}, ctx); | ||
|
|
||
| auto null_meter_provider = std::make_shared<MockMeterProvider>(); | ||
| EXPECT_CALL(*null_meter_provider, GetMeter) | ||
| .WillOnce(Return( | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Meter>{})); | ||
| ServerUnreachedMetrics null_meter( | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::MeterProvider>( | ||
| null_meter_provider), | ||
| "storage.googleapis.com"); | ||
| null_meter.RecordServerUnreached( | ||
| {"grpc", "WriteObject", "directpath", "CONNECTION_ERROR"}, ctx); | ||
|
|
||
| opentelemetry::nostd::shared_ptr<MockMeter> empty_meter = | ||
| std::make_shared<MockMeter>(); | ||
| EXPECT_CALL(*empty_meter, CreateUInt64Counter) | ||
| .WillOnce(Return(ByMove(CounterPtr{}))); | ||
| auto empty_provider = std::make_shared<MockMeterProvider>(); | ||
| EXPECT_CALL(*empty_provider, GetMeter) | ||
| .WillOnce(Return( | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Meter>( | ||
| empty_meter))); | ||
| ServerUnreachedMetrics null_instrument( | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::MeterProvider>( | ||
| empty_provider), | ||
| "storage.googleapis.com"); | ||
| null_instrument.RecordServerUnreached( | ||
| {"grpc", "WriteObject", "directpath", "CONNECTION_ERROR"}, ctx); | ||
| } | ||
|
|
||
| } // namespace | ||
| 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
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 remove the redundant
SemanticVersionString()helper function and usegoogle::cloud::version_string()directly. This simplifies the code and ensures any pre-release or development suffixes are correctly preserved in the metrics.