Repository navigation
Conversation
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.80% +0.02%
==========================================
Files 2279 2281 +2
Lines 228396 228610 +214
==========================================
+ Hits 211921 212170 +249
+ Misses 16475 16440 -35 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
6395641 to
e5b50c9
Compare
| opentelemetry::metrics::MeterProvider> const& provider, | ||
| std::string server_address); | ||
|
|
||
| void RecordTtfb(TtfbLabels const& labels, |
There was a problem hiding this comment.
RecordTtfb does not say where elapsed starts (the start of the attempt, or after the request is sent) or whether it is called for attempts that got no response. The histogram's meaning depends on both, and the instrument description (Time to first byte of a Storage response.) does not settle it either.
There was a problem hiding this comment.
Updated the doc comments and instrument description to specify that timing starts when the attempt begins and ends at the first response byte/header (or final response headers for writes), and that callers only invoke it when a server response is received.
Replied by cpriti-o11y-agent
| using Attr = std::pair<opentelemetry::nostd::string_view, | ||
| opentelemetry::common::AttributeValue>; | ||
|
|
||
| opentelemetry::nostd::string_view ToNostd(std::string_view sv) { |
There was a problem hiding this comment.
Same point as my comment on #16712: ToNostd and SemanticVersionString are now in three places, here, operation_metrics.cc and attempt_metrics.cc.
|
|
||
| /// Labels for recording time-to-first-byte (`gcp.storage.client.ttfb`). | ||
| struct TtfbLabels { | ||
| std::string_view rpc_system; |
There was a problem hiding this comment.
rpc_system accepts any string here, while AttemptMetrics fixes "grpc" and "http" inside per-transport methods. A typo at a call site would silently start a new rpc.system.name series. Would it make sense to split this into RecordGrpcTtfb() and RecordHttpTtfb() (or take an enum class), so callers cannot pass an arbitrary value?
There was a problem hiding this comment.
Split RecordTtfb into RecordGrpcTtfb and RecordHttpTtfb and dropped rpc_system from TtfbLabels, matching AttemptMetrics.
Replied by cpriti-o11y-agent
| /// (`rpc.client.call.duration`). | ||
| struct RpcCallDurationLabels { | ||
| std::string_view rpc_method; | ||
| std::string_view status_code; |
There was a problem hiding this comment.
status_code holds the same rpc.response.status_code value that GrpcAttemptLabels calls rpc_status_code. Both structs describe one gRPC attempt, so a single name for the field would be easier to follow. Would it make sense to rename this one to rpc_status_code?
There was a problem hiding this comment.
Renamed status_code to rpc_status_code in RpcCallDurationLabels.
Replied by cpriti-o11y-agent
| using HistPtr = | ||
| opentelemetry::nostd::unique_ptr<opentelemetry::metrics::Histogram<double>>; | ||
|
|
||
| std::map<std::string, std::string> ExtractStringAttrs( |
There was a problem hiding this comment.
Same point as my comment on #16712: ExtractStringAttrs() here is another verbatim copy of the helper in operation_metrics_test.cc. Could this test use whichever shared location that thread settles on?
…C duration histograms
…s_code, and clarify TTFB docs
e5b50c9 to
591e00b
Compare
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