Debian Codegraph @

src/cpp/server/orca/orca_interceptor.cc

raw
1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "src/cpp/server/orca/orca_interceptor.h"
18
19 #include <algorithm>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25
26 #include "absl/strings/string_view.h"
27 #include "absl/types/optional.h"
28
29 #include <grpcpp/ext/call_metric_recorder.h>
30 #include <grpcpp/server_builder.h>
31 #include <grpcpp/server_context.h>
32
33 #include "src/core/lib/transport/metadata_batch.h"
34
35 namespace grpc {
36 namespace experimental {
37
38 void OrcaServerInterceptor::Intercept(InterceptorBatchMethods* methods) {
39 if (methods->QueryInterceptionHookPoint(
40 InterceptionHookPoints::POST_RECV_INITIAL_METADATA)) {
41 auto context = info_->server_context();
42 context->CreateCallMetricRecorder();
43 } else if (methods->QueryInterceptionHookPoint(
44 InterceptionHookPoints::PRE_SEND_STATUS)) {
45 auto trailers = methods->GetSendTrailingMetadata();
46 if (trailers != nullptr) {
47 auto context = info_->server_context();
48 auto* recorder = context->call_metric_recorder_;
49 auto serialized = recorder->CreateSerializedReport();
50 if (serialized.has_value() && !serialized->empty()) {
51 std::string key =
52 std::string(grpc_core::EndpointLoadMetricsBinMetadata::key());
53 trailers->emplace(
54 std::make_pair(std::move(key), std::move(serialized.value())));
55 }
56 }
57 }
58 methods->Proceed();
59 }
60
61 Interceptor* OrcaServerInterceptorFactory::CreateServerInterceptor(
62 ServerRpcInfo* info) {
63 return new OrcaServerInterceptor(info);
64 }
65
66 void OrcaServerInterceptorFactory::Register(grpc::ServerBuilder* builder) {
67 builder->internal_interceptor_creators_.push_back(
68 std::make_unique<OrcaServerInterceptorFactory>());
69 }
70
71 void EnableCallMetricRecording(grpc::ServerBuilder* builder) {
72 OrcaServerInterceptorFactory::Register(builder);
73 }
74
75 } // namespace experimental
76 } // namespace grpc