-
Notifications
You must be signed in to change notification settings - Fork 458
feat(storage): add stream open latency metrics and trace annotations #16316
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
base: feat/storage-experimental-metrics
Are you sure you want to change the base?
Changes from all commits
d54ed9f
73bfe50
68e2792
e3106e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright 2024 Google LLC | ||
|
Contributor
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. nit. Copyright year should be 2026 |
||
| // | ||
| // 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/async/open_object_metrics.h" | ||
|
|
||
| #ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS | ||
| #include <opentelemetry/metrics/provider.h> | ||
| #endif | ||
|
|
||
| namespace google { | ||
| namespace cloud { | ||
| namespace storage_internal { | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
|
|
||
| #ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS | ||
|
Contributor
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. This file should ideally be invoked only when |
||
| namespace { | ||
| struct StreamOpenMetrics { | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Histogram<double>> | ||
| network_handshake; | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Histogram<double>> | ||
| server_metadata_latency; | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Histogram<double>> | ||
| stream_open_latency; | ||
|
|
||
| static StreamOpenMetrics const& Instance() { | ||
| static auto const metrics = [] { | ||
| auto meter = | ||
| opentelemetry::metrics::Provider::GetMeterProvider()->GetMeter( | ||
| "storage", "v1"); | ||
| return StreamOpenMetrics{ | ||
| meter->CreateDoubleHistogram("gl-cpp.latency.network_handshake", | ||
| "Network Handshake", "us"), | ||
| meter->CreateDoubleHistogram("gl-cpp.latency.server_metadata", | ||
| "Server Metadata Latency", "us"), | ||
| meter->CreateDoubleHistogram("gl-cpp.latency.stream_open", | ||
| "End-to-End Stream Open", "us")}; | ||
| }(); | ||
| return metrics; | ||
| } | ||
| }; | ||
| } // namespace | ||
| #endif | ||
|
|
||
| void OpenObjectMetrics::RecordCall() { | ||
| #if defined(GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS) || \ | ||
| defined(GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY) | ||
| t0_ = std::chrono::steady_clock::now(); | ||
| #endif | ||
| } | ||
|
|
||
| void OpenObjectMetrics::RecordStart() { | ||
| #if defined(GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS) || \ | ||
| defined(GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY) | ||
| t1_ = std::chrono::steady_clock::now(); | ||
| #endif | ||
| } | ||
|
|
||
| void OpenObjectMetrics::RecordWrite() { | ||
| #if defined(GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS) || \ | ||
| defined(GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY) | ||
| t2_ = std::chrono::steady_clock::now(); | ||
| #endif | ||
| } | ||
|
kalragauri marked this conversation as resolved.
|
||
|
|
||
| #ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS | ||
| void OpenObjectMetrics::RecordMetrics( | ||
| std::string const& bucket, std::chrono::steady_clock::time_point t3) { | ||
| auto const& metrics = StreamOpenMetrics::Instance(); | ||
| auto p1 = static_cast<double>( | ||
| std::chrono::duration_cast<std::chrono::microseconds>(t1_ - t0_).count()); | ||
| auto p2 = static_cast<double>( | ||
| std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2_).count()); | ||
| auto p3 = static_cast<double>( | ||
| std::chrono::duration_cast<std::chrono::microseconds>(t3 - t0_).count()); | ||
|
|
||
| metrics.network_handshake->Record(p1, {{"gcp.storage.bucket", bucket}}, | ||
| opentelemetry::context::Context{}); | ||
| metrics.server_metadata_latency->Record(p2, {{"gcp.storage.bucket", bucket}}, | ||
| opentelemetry::context::Context{}); | ||
| metrics.stream_open_latency->Record(p3, {{"gcp.storage.bucket", bucket}}, | ||
| opentelemetry::context::Context{}); | ||
| } | ||
| #endif | ||
|
|
||
| #ifdef GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY | ||
| void OpenObjectMetrics::RecordRead( | ||
| std::string const& bucket, | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> const& span) { | ||
| auto t3 = std::chrono::steady_clock::now(); | ||
| #ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS | ||
| RecordMetrics(bucket, t3); | ||
| #else | ||
| (void)bucket; | ||
| #endif | ||
|
|
||
| if (span && span->GetContext().IsValid()) { | ||
| auto p1 = static_cast<double>( | ||
| std::chrono::duration_cast<std::chrono::microseconds>(t1_ - t0_) | ||
| .count()); | ||
| auto p2 = static_cast<double>( | ||
| std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2_) | ||
| .count()); | ||
| auto p3 = static_cast<double>( | ||
| std::chrono::duration_cast<std::chrono::microseconds>(t3 - t0_) | ||
| .count()); | ||
| span->AddEvent("gl-cpp.stream_open.latency", | ||
| {{"gl-cpp.latency.network_handshake", p1}, | ||
| {"gl-cpp.latency.server_metadata", p2}, | ||
| {"gl-cpp.latency.stream_open", p3}}); | ||
| } | ||
| } | ||
|
kalragauri marked this conversation as resolved.
|
||
| #else | ||
| void OpenObjectMetrics::RecordRead(std::string const& bucket) { | ||
| #ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS | ||
| RecordMetrics(bucket, std::chrono::steady_clock::now()); | ||
| #else | ||
| (void)bucket; | ||
| #endif | ||
| } | ||
| #endif | ||
|
|
||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
| } // namespace storage_internal | ||
| } // namespace cloud | ||
| } // namespace google | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Copyright 2024 Google LLC | ||
|
Contributor
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. nit. Copyright year should be 2026 |
||
| // | ||
|
Contributor
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. Can we also add unit tests for metrics? |
||
| // 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_ASYNC_OPEN_OBJECT_METRICS_H | ||
| #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ASYNC_OPEN_OBJECT_METRICS_H | ||
|
|
||
| #include "google/cloud/version.h" | ||
| #include <chrono> | ||
| #include <string> | ||
|
|
||
| #ifdef GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY | ||
| #include <opentelemetry/trace/span.h> | ||
| #endif | ||
|
|
||
| namespace google { | ||
| namespace cloud { | ||
| namespace storage_internal { | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
|
|
||
| class OpenObjectMetrics { | ||
| public: | ||
| void RecordCall(); | ||
| void RecordStart(); | ||
| void RecordWrite(); | ||
|
|
||
| #ifdef GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY | ||
| void RecordRead( | ||
| std::string const& bucket, | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> const& span); | ||
| #else | ||
| void RecordRead(std::string const& bucket); | ||
| #endif | ||
|
|
||
| private: | ||
| #ifdef GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS | ||
| void RecordMetrics(std::string const& bucket, | ||
| std::chrono::steady_clock::time_point t3); | ||
| #endif | ||
|
|
||
| #if defined(GOOGLE_CLOUD_CPP_STORAGE_WITH_OTEL_METRICS) || \ | ||
| defined(GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY) | ||
| std::chrono::steady_clock::time_point t0_; | ||
| std::chrono::steady_clock::time_point t1_; | ||
| std::chrono::steady_clock::time_point t2_; | ||
| #endif | ||
|
kalragauri marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
| } // namespace storage_internal | ||
| } // namespace cloud | ||
| } // namespace google | ||
|
|
||
| #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ASYNC_OPEN_OBJECT_METRICS_H | ||
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.
So, we're mixing tracing changes and metrics changes in one file.
I think these should be segregated in two separate files ( one for metrics and one for tracing ).
That'd totally avoid the if...else blocks.
But we'll need to see how we're going to invoke these classes when both metrics and tracing are enabled.
We can discuss this offline.