Debian Codegraph @

src/core/lib/surface/call_trace.cc

raw
1 // Copyright 2022 gRPC authors.
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 // http://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 <grpc/support/port_platform.h>
16
17 #include "src/core/lib/surface/call_trace.h"
18
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <utility>
23
24 #include "absl/base/thread_annotations.h"
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/meta/type_traits.h"
27 #include "absl/status/status.h"
28 #include "absl/types/variant.h"
29
30 #include <grpc/support/log.h>
31
32 #include "src/core/lib/channel/channel_stack.h"
33 #include "src/core/lib/gprpp/no_destruct.h"
34 #include "src/core/lib/gprpp/sync.h"
35 #include "src/core/lib/iomgr/closure.h"
36 #include "src/core/lib/promise/activity.h"
37 #include "src/core/lib/promise/arena_promise.h"
38 #include "src/core/lib/transport/metadata_batch.h"
39 #include "src/core/lib/transport/transport.h"
40
41 namespace grpc_core {
42
43 const grpc_channel_filter* PromiseTracingFilterFor(
44 const grpc_channel_filter* filter) {
45 struct DerivedFilter : public grpc_channel_filter {
46 explicit DerivedFilter(const grpc_channel_filter* filter)
47 : grpc_channel_filter{
48 /* start_transport_stream_op_batch: */ grpc_call_next_op,
49 /* make_call_promise: */
50 [](grpc_channel_element* elem, CallArgs call_args,
51 NextPromiseFactory next_promise_factory)
52 -> ArenaPromise<ServerMetadataHandle> {
53 auto* source_filter =
54 static_cast<const DerivedFilter*>(elem->filter)->filter;
55 gpr_log(
56 GPR_DEBUG,
57 "%sCreateCallPromise[%s]: client_initial_metadata=%s",
58 Activity::current()->DebugTag().c_str(),
59 source_filter->name,
60 call_args.client_initial_metadata->DebugString().c_str());
61 return [source_filter, child = next_promise_factory(
62 std::move(call_args))]() mutable {
63 gpr_log(GPR_DEBUG, "%sPollCallPromise[%s]: begin",
64 Activity::current()->DebugTag().c_str(),
65 source_filter->name);
66 auto r = child();
67 if (auto* p = absl::get_if<ServerMetadataHandle>(&r)) {
68 gpr_log(GPR_DEBUG, "%sPollCallPromise[%s]: done: %s",
69 Activity::current()->DebugTag().c_str(),
70 source_filter->name, (*p)->DebugString().c_str());
71 } else {
72 gpr_log(GPR_DEBUG, "%sPollCallPromise[%s]: <<pending>",
73 Activity::current()->DebugTag().c_str(),
74 source_filter->name);
75 }
76 return r;
77 };
78 },
79 grpc_channel_next_op, /* sizeof_call_data: */ 0,
80 /* init_call_elem: */
81 [](grpc_call_element*, const grpc_call_element_args*) {
82 return absl::OkStatus();
83 },
84 grpc_call_stack_ignore_set_pollset_or_pollset_set,
85 /* destroy_call_elem: */
86 [](grpc_call_element*, const grpc_call_final_info*,
87 grpc_closure*) {},
88 /* sizeof_channel_data: */ 0, /* init_channel_elem: */
89 [](grpc_channel_element*, grpc_channel_element_args*) {
90 return absl::OkStatus();
91 },
92 /* post_init_channel_elem: */
93 [](grpc_channel_stack*, grpc_channel_element*) {},
94 /* destroy_channel_elem: */ [](grpc_channel_element*) {},
95 grpc_channel_next_get_info, filter->name},
96 filter(filter) {}
97 const grpc_channel_filter* const filter;
98 };
99 struct Globals {
100 Mutex mu;
101 absl::flat_hash_map<const grpc_channel_filter*,
102 std::unique_ptr<DerivedFilter>>
103 map ABSL_GUARDED_BY(mu);
104 };
105 auto* globals = NoDestructSingleton<Globals>::Get();
106 MutexLock lock(&globals->mu);
107 auto it = globals->map.find(filter);
108 if (it != globals->map.end()) return it->second.get();
109 return globals->map.emplace(filter, std::make_unique<DerivedFilter>(filter))
110 .first->second.get();
111 }
112
113 } // namespace grpc_core