xref: /aosp_15_r20/external/grpc-grpc/test/core/channel/call_tracer_test.cc (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 #include "src/core/lib/channel/call_tracer.h"
20 
21 #include <vector>
22 
23 #include "gtest/gtest.h"
24 
25 #include <grpc/event_engine/memory_allocator.h>
26 #include <grpc/grpc.h>
27 
28 #include "src/core/lib/gprpp/ref_counted_ptr.h"
29 #include "src/core/lib/promise/context.h"
30 #include "src/core/lib/resource_quota/memory_quota.h"
31 #include "src/core/lib/resource_quota/resource_quota.h"
32 #include "test/core/util/fake_stats_plugin.h"
33 #include "test/core/util/test_config.h"
34 
35 namespace grpc_core {
36 namespace {
37 
38 class CallTracerTest : public ::testing::Test {
39  protected:
SetUp()40   void SetUp() override {
41     memory_allocator_ = new MemoryAllocator(
42         ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
43             "test"));
44     arena_ = Arena::Create(1024, memory_allocator_);
45   }
46 
TearDown()47   void TearDown() override {
48     arena_->Destroy();
49     delete memory_allocator_;
50   }
51 
52   MemoryAllocator* memory_allocator_ = nullptr;
53   Arena* arena_ = nullptr;
54   grpc_call_context_element context_[GRPC_CONTEXT_COUNT] = {};
55   std::vector<std::string> annotation_logger_;
56 };
57 
TEST_F(CallTracerTest,BasicClientCallTracer)58 TEST_F(CallTracerTest, BasicClientCallTracer) {
59   FakeClientCallTracer client_call_tracer(&annotation_logger_);
60   AddClientCallTracerToContext(context_, &client_call_tracer);
61   static_cast<CallTracerAnnotationInterface*>(
62       context_[GRPC_CONTEXT_CALL_TRACER_ANNOTATION_INTERFACE].value)
63       ->RecordAnnotation("Test");
64   EXPECT_EQ(annotation_logger_, std::vector<std::string>{"Test"});
65 }
66 
TEST_F(CallTracerTest,MultipleClientCallTracers)67 TEST_F(CallTracerTest, MultipleClientCallTracers) {
68   promise_detail::Context<Arena> arena_ctx(arena_);
69   FakeClientCallTracer client_call_tracer1(&annotation_logger_);
70   FakeClientCallTracer client_call_tracer2(&annotation_logger_);
71   FakeClientCallTracer client_call_tracer3(&annotation_logger_);
72   AddClientCallTracerToContext(context_, &client_call_tracer1);
73   AddClientCallTracerToContext(context_, &client_call_tracer2);
74   AddClientCallTracerToContext(context_, &client_call_tracer3);
75   static_cast<CallTracerAnnotationInterface*>(
76       context_[GRPC_CONTEXT_CALL_TRACER_ANNOTATION_INTERFACE].value)
77       ->RecordAnnotation("Test");
78   EXPECT_EQ(annotation_logger_,
79             std::vector<std::string>({"Test", "Test", "Test"}));
80 }
81 
TEST_F(CallTracerTest,MultipleClientCallAttemptTracers)82 TEST_F(CallTracerTest, MultipleClientCallAttemptTracers) {
83   promise_detail::Context<Arena> arena_ctx(arena_);
84   FakeClientCallTracer client_call_tracer1(&annotation_logger_);
85   FakeClientCallTracer client_call_tracer2(&annotation_logger_);
86   FakeClientCallTracer client_call_tracer3(&annotation_logger_);
87   AddClientCallTracerToContext(context_, &client_call_tracer1);
88   AddClientCallTracerToContext(context_, &client_call_tracer2);
89   AddClientCallTracerToContext(context_, &client_call_tracer3);
90   auto* attempt_tracer =
91       static_cast<ClientCallTracer*>(
92           context_[GRPC_CONTEXT_CALL_TRACER_ANNOTATION_INTERFACE].value)
93           ->StartNewAttempt(true /* is_transparent_retry */);
94   attempt_tracer->RecordAnnotation("Test");
95   EXPECT_EQ(annotation_logger_,
96             std::vector<std::string>({"Test", "Test", "Test"}));
97 }
98 
TEST_F(CallTracerTest,BasicServerCallTracerTest)99 TEST_F(CallTracerTest, BasicServerCallTracerTest) {
100   FakeServerCallTracer server_call_tracer(&annotation_logger_);
101   AddServerCallTracerToContext(context_, &server_call_tracer);
102   static_cast<CallTracerAnnotationInterface*>(
103       context_[GRPC_CONTEXT_CALL_TRACER].value)
104       ->RecordAnnotation("Test");
105   static_cast<CallTracerAnnotationInterface*>(
106       context_[GRPC_CONTEXT_CALL_TRACER_ANNOTATION_INTERFACE].value)
107       ->RecordAnnotation("Test");
108   EXPECT_EQ(annotation_logger_, std::vector<std::string>({"Test", "Test"}));
109 }
110 
TEST_F(CallTracerTest,MultipleServerCallTracers)111 TEST_F(CallTracerTest, MultipleServerCallTracers) {
112   promise_detail::Context<Arena> arena_ctx(arena_);
113   FakeServerCallTracer server_call_tracer1(&annotation_logger_);
114   FakeServerCallTracer server_call_tracer2(&annotation_logger_);
115   FakeServerCallTracer server_call_tracer3(&annotation_logger_);
116   AddServerCallTracerToContext(context_, &server_call_tracer1);
117   AddServerCallTracerToContext(context_, &server_call_tracer2);
118   AddServerCallTracerToContext(context_, &server_call_tracer3);
119   static_cast<CallTracerAnnotationInterface*>(
120       context_[GRPC_CONTEXT_CALL_TRACER_ANNOTATION_INTERFACE].value)
121       ->RecordAnnotation("Test");
122   EXPECT_EQ(annotation_logger_,
123             std::vector<std::string>({"Test", "Test", "Test"}));
124 }
125 
126 }  // namespace
127 }  // namespace grpc_core
128 
main(int argc,char ** argv)129 int main(int argc, char** argv) {
130   ::testing::InitGoogleTest(&argc, argv);
131   grpc::testing::TestEnvironment env(&argc, argv);
132   grpc_init();
133   auto r = RUN_ALL_TESTS();
134   grpc_shutdown();
135   return r;
136 }
137