xref: /aosp_15_r20/external/grpc-grpc/src/cpp/ext/otel/otel_client_call_tracer.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 GRPC_SRC_CPP_EXT_OTEL_OTEL_CLIENT_CALL_TRACER_H
20 #define GRPC_SRC_CPP_EXT_OTEL_OTEL_CLIENT_CALL_TRACER_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stdint.h>
25 
26 #include <memory>
27 #include <string>
28 
29 #include "absl/base/thread_annotations.h"
30 #include "absl/status/status.h"
31 #include "absl/strings/string_view.h"
32 #include "absl/time/time.h"
33 
34 #include <grpc/support/time.h>
35 
36 #include "src/core/lib/channel/call_tracer.h"
37 #include "src/core/lib/channel/tcp_tracer.h"
38 #include "src/core/lib/gprpp/sync.h"
39 #include "src/core/lib/iomgr/error.h"
40 #include "src/core/lib/resource_quota/arena.h"
41 #include "src/core/lib/slice/slice.h"
42 #include "src/core/lib/slice/slice_buffer.h"
43 #include "src/core/lib/transport/metadata_batch.h"
44 #include "src/core/lib/transport/transport.h"
45 #include "src/cpp/ext/otel/otel_plugin.h"
46 
47 namespace grpc {
48 namespace internal {
49 
50 class OpenTelemetryPlugin::ClientCallTracer
51     : public grpc_core::ClientCallTracer {
52  public:
53   class CallAttemptTracer
54       : public grpc_core::ClientCallTracer::CallAttemptTracer {
55    public:
56     CallAttemptTracer(const OpenTelemetryPlugin::ClientCallTracer* parent,
57                       bool arena_allocated);
58 
TraceId()59     std::string TraceId() override {
60       // Not implemented
61       return "";
62     }
63 
SpanId()64     std::string SpanId() override {
65       // Not implemented
66       return "";
67     }
68 
IsSampled()69     bool IsSampled() override {
70       // Not implemented
71       return false;
72     }
73 
74     void RecordSendInitialMetadata(
75         grpc_metadata_batch* send_initial_metadata) override;
RecordSendTrailingMetadata(grpc_metadata_batch *)76     void RecordSendTrailingMetadata(
77         grpc_metadata_batch* /*send_trailing_metadata*/) override {}
78     void RecordSendMessage(const grpc_core::SliceBuffer& send_message) override;
79     void RecordSendCompressedMessage(
80         const grpc_core::SliceBuffer& send_compressed_message) override;
81     void RecordReceivedInitialMetadata(
82         grpc_metadata_batch* recv_initial_metadata) override;
83     void RecordReceivedMessage(
84         const grpc_core::SliceBuffer& recv_message) override;
85     void RecordReceivedDecompressedMessage(
86         const grpc_core::SliceBuffer& recv_decompressed_message) override;
87     void RecordReceivedTrailingMetadata(
88         absl::Status status, grpc_metadata_batch* /*recv_trailing_metadata*/,
89         const grpc_transport_stream_stats* transport_stream_stats) override;
90     void RecordCancel(grpc_error_handle cancel_error) override;
91     void RecordEnd(const gpr_timespec& /*latency*/) override;
92     void RecordAnnotation(absl::string_view /*annotation*/) override;
93     void RecordAnnotation(const Annotation& /*annotation*/) override;
94     std::shared_ptr<grpc_core::TcpTracerInterface> StartNewTcpTrace() override;
95     void SetOptionalLabel(OptionalLabelKey key,
96                           grpc_core::RefCountedStringValue value) override;
97 
98    private:
99     const ClientCallTracer* parent_;
100     const bool arena_allocated_;
101     // Start time (for measuring latency).
102     absl::Time start_time_;
103     std::unique_ptr<LabelsIterable> injected_labels_;
104     // Avoid std::map to avoid per-call allocations.
105     std::array<grpc_core::RefCountedStringValue,
106                static_cast<size_t>(OptionalLabelKey::kSize)>
107         optional_labels_;
108     std::vector<std::unique_ptr<LabelsIterable>>
109         injected_labels_from_plugin_options_;
110   };
111 
112   ClientCallTracer(
113       const grpc_core::Slice& path, grpc_core::Arena* arena,
114       bool registered_method, OpenTelemetryPlugin* otel_plugin,
115       std::shared_ptr<OpenTelemetryPlugin::ClientScopeConfig> scope_config);
116   ~ClientCallTracer() override;
117 
TraceId()118   std::string TraceId() override {
119     // Not implemented
120     return "";
121   }
122 
SpanId()123   std::string SpanId() override {
124     // Not implemented
125     return "";
126   }
127 
IsSampled()128   bool IsSampled() override {
129     // Not implemented
130     return false;
131   }
132 
133   CallAttemptTracer* StartNewAttempt(bool is_transparent_retry) override;
134   void RecordAnnotation(absl::string_view /*annotation*/) override;
135   void RecordAnnotation(const Annotation& /*annotation*/) override;
136 
137  private:
138   absl::string_view MethodForStats() const;
139 
140   // Client method.
141   grpc_core::Slice path_;
142   grpc_core::Arena* arena_;
143   const bool registered_method_;
144   OpenTelemetryPlugin* otel_plugin_;
145   std::shared_ptr<OpenTelemetryPlugin::ClientScopeConfig> scope_config_;
146   grpc_core::Mutex mu_;
147   // Non-transparent attempts per call
148   uint64_t retries_ ABSL_GUARDED_BY(&mu_) = 0;
149   // Transparent retries per call
150   uint64_t transparent_retries_ ABSL_GUARDED_BY(&mu_) = 0;
151 };
152 
153 }  // namespace internal
154 }  // namespace grpc
155 
156 #endif  // GRPC_SRC_CPP_EXT_OTEL_OTEL_CLIENT_CALL_TRACER_H
157