|
| 1 | +// Copyright 2025 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_BIGTABLE_INTERNAL_METRICS_H |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_METRICS_H |
| 17 | + |
| 18 | +#ifdef GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS |
| 19 | + |
| 20 | +#include "google/cloud/bigtable/internal/operation_context.h" |
| 21 | +#include "google/cloud/bigtable/version.h" |
| 22 | +#include "google/cloud/status.h" |
| 23 | +#include <grpcpp/grpcpp.h> |
| 24 | +#include <opentelemetry/context/context.h> |
| 25 | +#include <memory> |
| 26 | +#include <string> |
| 27 | +#include <unordered_map> |
| 28 | + |
| 29 | +namespace google { |
| 30 | +namespace cloud { |
| 31 | +namespace bigtable_internal { |
| 32 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 33 | + |
| 34 | +struct ResourceLabels { |
| 35 | + std::string project_id; |
| 36 | + std::string instance; |
| 37 | + std::string table; |
| 38 | + std::string cluster; |
| 39 | + std::string zone; |
| 40 | +}; |
| 41 | + |
| 42 | +struct DataLabels { |
| 43 | + std::string method; |
| 44 | + std::string streaming; |
| 45 | + std::string client_name; |
| 46 | + std::string client_uid; |
| 47 | + std::string app_profile; |
| 48 | + std::string status; |
| 49 | +}; |
| 50 | + |
| 51 | +using LabelMap = std::unordered_map<std::string, std::string>; |
| 52 | +LabelMap IntoMap(ResourceLabels const& r, DataLabels const& d); |
| 53 | + |
| 54 | +struct PreCallParams { |
| 55 | + OperationContext::Clock::time_point attempt_start; |
| 56 | + bool first_attempt; |
| 57 | +}; |
| 58 | + |
| 59 | +struct PostCallParams { |
| 60 | + OperationContext::Clock::time_point attempt_end; |
| 61 | + google::cloud::Status attempt_status; |
| 62 | +}; |
| 63 | + |
| 64 | +struct OnDoneParams { |
| 65 | + OperationContext::Clock::time_point operation_end; |
| 66 | + google::cloud::Status operation_status; |
| 67 | +}; |
| 68 | + |
| 69 | +struct ElementRequestParams { |
| 70 | + OperationContext::Clock::time_point element_request; |
| 71 | +}; |
| 72 | + |
| 73 | +struct ElementDeliveryParams { |
| 74 | + OperationContext::Clock::time_point element_delivery; |
| 75 | + bool first_response; |
| 76 | +}; |
| 77 | + |
| 78 | +class Metric { |
| 79 | + public: |
| 80 | + using LatencyDuration = std::chrono::duration<double, std::milli>; |
| 81 | + |
| 82 | + virtual ~Metric() = 0; |
| 83 | + virtual void PreCall(opentelemetry::context::Context const&, |
| 84 | + PreCallParams const&) {} |
| 85 | + virtual void PostCall(opentelemetry::context::Context const&, |
| 86 | + grpc::ClientContext const&, PostCallParams const&) {} |
| 87 | + virtual void OnDone(opentelemetry::context::Context const&, |
| 88 | + OnDoneParams const&) {} |
| 89 | + virtual void ElementRequest(opentelemetry::context::Context const&, |
| 90 | + ElementRequestParams const&) {} |
| 91 | + virtual void ElementDelivery(opentelemetry::context::Context const&, |
| 92 | + ElementDeliveryParams const&) {} |
| 93 | + virtual std::unique_ptr<Metric> clone(ResourceLabels resource_labels, |
| 94 | + DataLabels data_labels) const = 0; |
| 95 | +}; |
| 96 | + |
| 97 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 98 | +} // namespace bigtable_internal |
| 99 | +} // namespace cloud |
| 100 | +} // namespace google |
| 101 | + |
| 102 | +#endif // GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS |
| 103 | + |
| 104 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_METRICS_H |
0 commit comments