|
| 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 |
0 commit comments