Repository navigation
Conversation
…C duration histograms
There was a problem hiding this comment.
Code Review
This pull request introduces the AttemptTimingMetrics class to record OpenTelemetry metrics for storage attempt timings, specifically tracking time-to-first-byte (gcp.storage.client.ttfb) and RPC call duration (rpc.client.call.duration). It also includes corresponding unit tests and updates the Bazel and CMake build configurations. The review feedback suggests optimizing the retrieval of the semantic version string by caching it in a heap-allocated static variable. This avoids re-computation on every call and prevents static destruction order issues, allowing the removal of the client_version_ member variable from the class to reduce object size and avoid unnecessary copies.
| 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()); | ||
| } |
There was a problem hiding this comment.
To avoid re-computation on each function call, we should cache the semantic version string in a static variable. To prevent static destruction order issues with non-trivial destructors (like std::string), we can allocate it on the heap using new std::string and return a reference to it.
std::string const& SemanticVersionString() {
static auto const* const version = new std::string(
std::to_string(google::cloud::version_major()) + "." +
std::to_string(google::cloud::version_minor()) + "." +
std::to_string(google::cloud::version_patch()));
return *version;
}References
- To avoid re-computation on each function call, cache the result in a
staticvariable. This one-time cost is often preferable to repeated computation, even for seemingly quick operations. - Avoid using function-local static variables with non-trivial destructors (such as std::vector or std::string) to prevent static destruction order issues, in accordance with the Google C++ Style Guide.
| std::string client_version_; | ||
| std::string server_address_; |
There was a problem hiding this comment.
| AttemptTimingMetrics::AttemptTimingMetrics( | ||
| opentelemetry::nostd::shared_ptr< | ||
| opentelemetry::metrics::MeterProvider> const& provider, | ||
| std::string server_address) | ||
| : client_version_(SemanticVersionString()), | ||
| server_address_(std::move(server_address)) { |
There was a problem hiding this comment.
Remove the initialization of client_version_ from the constructor since we can use the cached static version directly.
AttemptTimingMetrics::AttemptTimingMetrics(
opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::MeterProvider> const& provider,
std::string server_address)
: server_address_(std::move(server_address)) {| 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(labels.rpc_system)}, | ||
| {"gcp.storage.operation", ToNostd(labels.operation)}, | ||
| {"network.path", ToNostd(labels.network_path)}, | ||
| }}; |
There was a problem hiding this comment.
Use the cached static SemanticVersionString() directly instead of the member variable client_version_.
std::array<Attr, 7> const attrs{{
{"gcp.client.version", ToNostd(SemanticVersionString())},
{"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)},
}};| 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", opentelemetry::nostd::string_view("grpc")}, | ||
| {"rpc.method", ToNostd(labels.rpc_method)}, | ||
| {"rpc.response.status_code", ToNostd(labels.status_code)}, | ||
| {"error.type", ToNostd(labels.error_type)}, | ||
| {"network.path", ToNostd(labels.network_path)}, | ||
| }}; |
There was a problem hiding this comment.
Use the cached static SemanticVersionString() directly instead of the member variable client_version_.
std::array<Attr, 9> const attrs{{
{"gcp.client.version", ToNostd(SemanticVersionString())},
{"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", opentelemetry::nostd::string_view("grpc")},
{"rpc.method", ToNostd(labels.rpc_method)},
{"rpc.response.status_code", ToNostd(labels.status_code)},
{"error.type", ToNostd(labels.error_type)},
{"network.path", ToNostd(labels.network_path)},
}};
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #16713 +/- ##
========================================
Coverage 92.78% 92.79%
========================================
Files 2275 2277 +2
Lines 227981 228172 +191
========================================
+ Hits 211537 211726 +189
- Misses 16444 16446 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Adds the internal
AttemptTimingMetricsrecorder for per-attempt OpenTelemetry timing histograms:gcp.storage.client.ttfb(DoubleHistogram, units) with base attributes (gcp.client.version,gcp.client.artifact,gcp.client.service,server.address) plusrpc.system.name,gcp.storage.operation, andnetwork.pathrpc.client.call.duration(DoubleHistogram, units) with base attributes plusrpc.system.name("grpc"),rpc.method,rpc.response.status_code,error.type, andnetwork.pathBoth methods accept
std::chrono::steady_clock::duration(clamping negative durations to0.0), propagate the explicitopentelemetry::context::Context, and safely no-op when the provider, meter, or individual instruments are null.Also registers
attempt_timing_metrics.{h,cc,_test.cc}in Bazel and CMake build files.Created by cpriti-o11y-agent