1 //
2 //
3 // Copyright 2018 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 <grpc/load_reporting.h>
20 #include <grpc/support/port_platform.h>
21 #include <grpcpp/ext/server_load_reporting.h>
22 #include <grpcpp/server_context.h>
23 #include <string.h>
24
25 #include <cmath>
26 #include <string>
27
28 #include "absl/log/log.h"
29
30 namespace grpc {
31 namespace load_reporter {
32 namespace experimental {
33
AddLoadReportingCost(grpc::ServerContext * ctx,const std::string & cost_name,double cost_value)34 void AddLoadReportingCost(grpc::ServerContext* ctx,
35 const std::string& cost_name, double cost_value) {
36 if (std::isnormal(cost_value)) {
37 std::string buf;
38 buf.resize(sizeof(cost_value) + cost_name.size());
39 memcpy(&(*buf.begin()), &cost_value, sizeof(cost_value));
40 memcpy(&(*buf.begin()) + sizeof(cost_value), cost_name.data(),
41 cost_name.size());
42 ctx->AddTrailingMetadata(GRPC_LB_COST_MD_KEY, buf);
43 } else {
44 LOG(ERROR) << "Call metric value is not normal.";
45 }
46 }
47
48 } // namespace experimental
49 } // namespace load_reporter
50 } // namespace grpc
51