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