Skip to content

Commit 174bb40

Browse files
committed
impl(storage): add AttemptTimingMetrics recorder for OTel TTFB and RPC duration histograms
1 parent fa26249 commit 174bb40

6 files changed

Lines changed: 497 additions & 0 deletions

‎google/cloud/storage/google_cloud_cpp_storage.bzl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ google_cloud_cpp_storage_hdrs = [
4545
"iam_policy.h",
4646
"idempotency_policy.h",
4747
"include_folders_as_prefixes.h",
48+
"internal/attempt_timing_metrics.h",
4849
"internal/base64.h",
4950
"internal/binary_data_as_debug_string.h",
5051
"internal/bucket_access_control_parser.h",
@@ -170,6 +171,7 @@ google_cloud_cpp_storage_srcs = [
170171
"hmac_key_metadata.cc",
171172
"iam_policy.cc",
172173
"idempotency_policy.cc",
174+
"internal/attempt_timing_metrics.cc",
173175
"internal/base64.cc",
174176
"internal/bucket_access_control_parser.cc",
175177
"internal/bucket_acl_requests.cc",

‎google/cloud/storage/google_cloud_cpp_storage.cmake‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ add_library(
6767
idempotency_policy.cc
6868
idempotency_policy.h
6969
include_folders_as_prefixes.h
70+
internal/attempt_timing_metrics.cc
71+
internal/attempt_timing_metrics.h
7072
internal/base64.cc
7173
internal/base64.h
7274
internal/binary_data_as_debug_string.h
@@ -433,6 +435,7 @@ if (BUILD_TESTING)
433435
hashing_options_test.cc
434436
hmac_key_metadata_test.cc
435437
idempotency_policy_test.cc
438+
internal/attempt_timing_metrics_test.cc
436439
internal/base64_test.cc
437440
internal/bucket_acl_requests_test.cc
438441
internal/bucket_metadata_cache_test.cc
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/storage/internal/attempt_timing_metrics.h"
16+
#include "google/cloud/version.h"
17+
#include <opentelemetry/common/attribute_value.h>
18+
#include <opentelemetry/common/key_value_iterable_view.h>
19+
#include <opentelemetry/context/context.h>
20+
#include <opentelemetry/nostd/string_view.h>
21+
#include <array>
22+
#include <chrono>
23+
#include <string>
24+
#include <string_view>
25+
#include <utility>
26+
27+
namespace google {
28+
namespace cloud {
29+
namespace storage_internal {
30+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
31+
namespace {
32+
33+
constexpr char const* kStorageMeterName = "gl-cpp";
34+
constexpr char const* kTtfbMetric = "gcp.storage.client.ttfb";
35+
constexpr char const* kRpcCallDurationMetric = "rpc.client.call.duration";
36+
37+
using Attr = std::pair<opentelemetry::nostd::string_view,
38+
opentelemetry::common::AttributeValue>;
39+
40+
opentelemetry::nostd::string_view ToNostd(std::string_view sv) {
41+
return opentelemetry::nostd::string_view(sv.data(), sv.size());
42+
}
43+
44+
std::string SemanticVersionString() {
45+
return std::to_string(google::cloud::version_major()) + "." +
46+
std::to_string(google::cloud::version_minor()) + "." +
47+
std::to_string(google::cloud::version_patch());
48+
}
49+
50+
double DurationToNonNegativeSeconds(
51+
std::chrono::steady_clock::duration elapsed) {
52+
return elapsed < std::chrono::steady_clock::duration::zero()
53+
? 0.0
54+
: std::chrono::duration<double>(elapsed).count();
55+
}
56+
57+
} // namespace
58+
59+
AttemptTimingMetrics::AttemptTimingMetrics(
60+
opentelemetry::nostd::shared_ptr<
61+
opentelemetry::metrics::MeterProvider> const& provider,
62+
std::string server_address)
63+
: client_version_(SemanticVersionString()),
64+
server_address_(std::move(server_address)) {
65+
if (!provider) return;
66+
meter_ =
67+
provider->GetMeter(kStorageMeterName, google::cloud::version_string());
68+
if (!meter_) return;
69+
ttfb_ = meter_->CreateDoubleHistogram(
70+
kTtfbMetric, "Time to first byte of a Storage response.", "s");
71+
rpc_call_duration_ = meter_->CreateDoubleHistogram(
72+
kRpcCallDurationMetric, "Duration of a single gRPC attempt.", "s");
73+
}
74+
75+
void AttemptTimingMetrics::RecordTtfb(
76+
TtfbLabels const& labels, std::chrono::steady_clock::duration elapsed,
77+
opentelemetry::context::Context const& ctx) const {
78+
if (!ttfb_) return;
79+
std::array<Attr, 7> const attrs{{
80+
{"gcp.client.version", ToNostd(client_version_)},
81+
{"gcp.client.artifact",
82+
opentelemetry::nostd::string_view("google-cloud-cpp")},
83+
{"gcp.client.service", opentelemetry::nostd::string_view("storage")},
84+
{"server.address", ToNostd(server_address_)},
85+
{"rpc.system.name", ToNostd(labels.rpc_system)},
86+
{"gcp.storage.operation", ToNostd(labels.operation)},
87+
{"network.path", ToNostd(labels.network_path)},
88+
}};
89+
ttfb_->Record(DurationToNonNegativeSeconds(elapsed),
90+
opentelemetry::common::MakeKeyValueIterableView(attrs), ctx);
91+
}
92+
93+
void AttemptTimingMetrics::RecordRpcCallDuration(
94+
RpcCallDurationLabels const& labels,
95+
std::chrono::steady_clock::duration elapsed,
96+
opentelemetry::context::Context const& ctx) const {
97+
if (!rpc_call_duration_) return;
98+
std::array<Attr, 9> const attrs{{
99+
{"gcp.client.version", ToNostd(client_version_)},
100+
{"gcp.client.artifact",
101+
opentelemetry::nostd::string_view("google-cloud-cpp")},
102+
{"gcp.client.service", opentelemetry::nostd::string_view("storage")},
103+
{"server.address", ToNostd(server_address_)},
104+
{"rpc.system.name", opentelemetry::nostd::string_view("grpc")},
105+
{"rpc.method", ToNostd(labels.rpc_method)},
106+
{"rpc.response.status_code", ToNostd(labels.status_code)},
107+
{"error.type", ToNostd(labels.error_type)},
108+
{"network.path", ToNostd(labels.network_path)},
109+
}};
110+
rpc_call_duration_->Record(
111+
DurationToNonNegativeSeconds(elapsed),
112+
opentelemetry::common::MakeKeyValueIterableView(attrs), ctx);
113+
}
114+
115+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
116+
} // namespace storage_internal
117+
} // namespace cloud
118+
} // namespace google
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_TIMING_METRICS_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_TIMING_METRICS_H
17+
18+
#include "google/cloud/storage/version.h"
19+
#include <opentelemetry/context/context.h>
20+
#include <opentelemetry/metrics/meter.h>
21+
#include <opentelemetry/metrics/meter_provider.h>
22+
#include <opentelemetry/metrics/sync_instruments.h>
23+
#include <opentelemetry/nostd/shared_ptr.h>
24+
#include <opentelemetry/nostd/unique_ptr.h>
25+
#include <chrono>
26+
#include <string>
27+
#include <string_view>
28+
29+
namespace google {
30+
namespace cloud {
31+
namespace storage_internal {
32+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
33+
34+
/// Labels for recording time-to-first-byte (`gcp.storage.client.ttfb`).
35+
struct TtfbLabels {
36+
std::string_view rpc_system;
37+
std::string_view operation;
38+
std::string_view network_path;
39+
};
40+
41+
/// Labels for recording per-attempt gRPC call duration
42+
/// (`rpc.client.call.duration`).
43+
struct RpcCallDurationLabels {
44+
std::string_view rpc_method;
45+
std::string_view status_code;
46+
std::string_view error_type;
47+
std::string_view network_path;
48+
};
49+
50+
/// Thread-safe recorder for per-attempt timing OpenTelemetry Storage metrics
51+
/// (`gcp.storage.client.ttfb` and `rpc.client.call.duration`).
52+
/// When constructed with a null provider, all methods are no-ops.
53+
class AttemptTimingMetrics {
54+
public:
55+
AttemptTimingMetrics(
56+
opentelemetry::nostd::shared_ptr<
57+
opentelemetry::metrics::MeterProvider> const& provider,
58+
std::string server_address);
59+
60+
void RecordTtfb(TtfbLabels const& labels,
61+
std::chrono::steady_clock::duration elapsed,
62+
opentelemetry::context::Context const& ctx) const;
63+
64+
void RecordRpcCallDuration(RpcCallDurationLabels const& labels,
65+
std::chrono::steady_clock::duration elapsed,
66+
opentelemetry::context::Context const& ctx) const;
67+
68+
private:
69+
std::string client_version_;
70+
std::string server_address_;
71+
opentelemetry::nostd::shared_ptr<opentelemetry::metrics::Meter> meter_;
72+
opentelemetry::nostd::unique_ptr<opentelemetry::metrics::Histogram<double>>
73+
ttfb_;
74+
opentelemetry::nostd::unique_ptr<opentelemetry::metrics::Histogram<double>>
75+
rpc_call_duration_;
76+
};
77+
78+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
79+
} // namespace storage_internal
80+
} // namespace cloud
81+
} // namespace google
82+
83+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_ATTEMPT_TIMING_METRICS_H

0 commit comments

Comments
 (0)