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/strings/str_cat.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/promise/poll.h"
39 #include "src/core/lib/transport/metadata_batch.h"
40 #include "src/core/lib/transport/transport.h"
41 
42 namespace grpc_core {
43 
PromiseTracingFilterFor(const grpc_channel_filter * filter)44 const grpc_channel_filter* PromiseTracingFilterFor(
45     const grpc_channel_filter* filter) {
46   struct DerivedFilter : public grpc_channel_filter {
47     explicit DerivedFilter(const grpc_channel_filter* filter)
48         : grpc_channel_filter{
49               // start_transport_stream_op_batch:
50               grpc_call_next_op,
51               // make_call_promise:
52               [](grpc_channel_element* elem, CallArgs call_args,
53                  NextPromiseFactory next_promise_factory)
54                   -> ArenaPromise<ServerMetadataHandle> {
55                 auto* source_filter =
56                     static_cast<const DerivedFilter*>(elem->filter)->filter;
57                 gpr_log(
58                     GPR_DEBUG,
59                     "%s[%s] CreateCallPromise: client_initial_metadata=%s",
60                     Activity::current()->DebugTag().c_str(),
61                     source_filter->name,
62                     call_args.client_initial_metadata->DebugString().c_str());
63                 return [source_filter, child = next_promise_factory(
64                                            std::move(call_args))]() mutable {
65                   gpr_log(GPR_DEBUG, "%s[%s] PollCallPromise: begin",
66                           Activity::current()->DebugTag().c_str(),
67                           source_filter->name);
68                   auto r = child();
69                   if (auto* p = r.value_if_ready()) {
70                     gpr_log(GPR_DEBUG, "%s[%s] PollCallPromise: done: %s",
71                             Activity::current()->DebugTag().c_str(),
72                             source_filter->name, (*p)->DebugString().c_str());
73                   } else {
74                     gpr_log(GPR_DEBUG, "%s[%s] PollCallPromise: <<pending>>",
75                             Activity::current()->DebugTag().c_str(),
76                             source_filter->name);
77                   }
78                   return r;
79                 };
80               },
81               grpc_channel_next_op, /* sizeof_call_data: */ 0,
82               // init_call_elem:
83               [](grpc_call_element*, const grpc_call_element_args*) {
84                 return absl::OkStatus();
85               },
86               grpc_call_stack_ignore_set_pollset_or_pollset_set,
87               // destroy_call_elem:
88               [](grpc_call_element*, const grpc_call_final_info*,
89                  grpc_closure*) {},
90               // sizeof_channel_data:
91               0,
92               // init_channel_elem:
93               [](grpc_channel_element*, grpc_channel_element_args*) {
94                 return absl::OkStatus();
95               },
96               // post_init_channel_elem:
97               [](grpc_channel_stack*, grpc_channel_element*) {},
98               // destroy_channel_elem:
99               [](grpc_channel_element*) {}, grpc_channel_next_get_info,
100               // name:
101               nullptr},
102           filter(filter),
103           name_str(absl::StrCat(filter->name, ".trace")) {
104       this->name = name_str.c_str();
105     }
106     const grpc_channel_filter* const filter;
107     const std::string name_str;
108   };
109   struct Globals {
110     Mutex mu;
111     absl::flat_hash_map<const grpc_channel_filter*,
112                         std::unique_ptr<DerivedFilter>>
113         map ABSL_GUARDED_BY(mu);
114   };
115   auto* globals = NoDestructSingleton<Globals>::Get();
116   MutexLock lock(&globals->mu);
117   auto it = globals->map.find(filter);
118   if (it != globals->map.end()) return it->second.get();
119   return globals->map.emplace(filter, std::make_unique<DerivedFilter>(filter))
120       .first->second.get();
121 }
122 
123 }  // namespace grpc_core
124