1 // 2 // 3 // Copyright 2023 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPCPP_EXT_CSM_OBSERVABILITY_H 20 #define GRPCPP_EXT_CSM_OBSERVABILITY_H 21 22 #include <memory> 23 24 #include "absl/functional/any_invocable.h" 25 #include "absl/status/statusor.h" 26 #include "absl/strings/string_view.h" 27 #include "opentelemetry/metrics/meter_provider.h" 28 29 #include <grpc/support/port_platform.h> 30 #include <grpcpp/ext/otel_plugin.h> 31 32 namespace grpc { 33 34 namespace internal { 35 class OpenTelemetryPluginBuilderImpl; 36 } // namespace internal 37 38 // This object maintains state around the registered CsmObservability plugin. 39 // The application is responsible for retaining this object until it has closed 40 // all channels and servers that are recording metrics. 41 class CsmObservability { 42 public: 43 CsmObservability() = default; 44 ~CsmObservability(); 45 // Disable copy constructor and copy-assignment operator. 46 CsmObservability(const CsmObservability&) = delete; 47 CsmObservability& operator=(const CsmObservability&) = delete; 48 CsmObservability(CsmObservability&&) noexcept; 49 CsmObservability& operator=(CsmObservability&&) noexcept; 50 51 private: 52 bool valid_ = true; 53 }; 54 55 // CsmObservabilityBuilder configures observability for all service mesh traffic 56 // for a binary running on CSM. 57 class CsmObservabilityBuilder { 58 public: 59 CsmObservabilityBuilder(); 60 ~CsmObservabilityBuilder(); 61 CsmObservabilityBuilder& SetMeterProvider( 62 std::shared_ptr<opentelemetry::metrics::MeterProvider> meter_provider); 63 // If set, \a target_attribute_filter is called per channel to decide whether 64 // to record the target attribute on client or to replace it with "other". 65 // This helps reduce the cardinality on metrics in cases where many channels 66 // are created with different targets in the same binary (which might happen 67 // for example, if the channel target string uses IP addresses directly). 68 CsmObservabilityBuilder& SetTargetAttributeFilter( 69 absl::AnyInvocable<bool(absl::string_view /*target*/) const> 70 target_attribute_filter); 71 // If set, \a generic_method_attribute_filter is called per call with a 72 // generic method type to decide whether to record the method name or to 73 // replace it with "other". Non-generic or pre-registered methods remain 74 // unaffected. If not set, by default, generic method names are replaced with 75 // "other" when recording metrics. 76 CsmObservabilityBuilder& SetGenericMethodAttributeFilter( 77 absl::AnyInvocable<bool(absl::string_view /*generic_method*/) const> 78 generic_method_attribute_filter); 79 // Builds the CsmObservability plugin. The return status shows whether 80 // CsmObservability was successfully enabled or not. 81 // 82 // The most common way to use this API is - 83 // 84 // auto observability = 85 // CsmObservabilityBuilder().SetMeterProvider(provider).BuildAndRegister(); 86 // 87 // The set of instruments available are - 88 // grpc.client.attempt.started 89 // grpc.client.attempt.duration 90 // grpc.client.attempt.sent_total_compressed_message_size 91 // grpc.client.attempt.rcvd_total_compressed_message_size 92 // grpc.server.call.started 93 // grpc.server.call.duration 94 // grpc.server.call.sent_total_compressed_message_size 95 // grpc.server.call.rcvd_total_compressed_message_size 96 absl::StatusOr<CsmObservability> BuildAndRegister(); 97 98 private: 99 std::unique_ptr<grpc::internal::OpenTelemetryPluginBuilderImpl> builder_; 100 }; 101 102 namespace experimental { 103 // TODO(yashykt): Remove this once no longer needed. 104 using CsmObservability GRPC_DEPRECATED("Use grpc::CsmObservability instead.") = 105 grpc::CsmObservability; 106 using CsmObservabilityBuilder GRPC_DEPRECATED( 107 "Use grpc::CsmObservabilityBuilder instead.") = 108 grpc::CsmObservabilityBuilder; 109 } // namespace experimental 110 111 } // namespace grpc 112 113 #endif // GRPCPP_EXT_CSM_OBSERVABILITY_H 114