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/support/port_platform.h>
20
21 #include "src/cpp/ext/filters/census/context.h"
22
23 namespace grpc {
24
25 using ::opencensus::trace::Span;
26 using ::opencensus::trace::SpanContext;
27
GenerateServerContext(absl::string_view tracing,absl::string_view stats,absl::string_view primary_role,absl::string_view method,CensusContext * context)28 void GenerateServerContext(absl::string_view tracing, absl::string_view stats,
29 absl::string_view primary_role,
30 absl::string_view method, CensusContext* context) {
31 GrpcTraceContext trace_ctxt;
32 TraceContextEncoding::Decode(tracing, &trace_ctxt);
33 SpanContext parent_ctx = trace_ctxt.ToSpanContext();
34 new (context) CensusContext(method, parent_ctx);
35 }
36
GenerateClientContext(absl::string_view method,CensusContext * ctxt,CensusContext * parent_ctxt)37 void GenerateClientContext(absl::string_view method, CensusContext* ctxt,
38 CensusContext* parent_ctxt) {
39 if (parent_ctxt != nullptr) {
40 SpanContext span_ctxt = parent_ctxt->Context();
41 Span span = parent_ctxt->Span();
42 if (span_ctxt.IsValid()) {
43 new (ctxt) CensusContext(method, &span);
44 return;
45 }
46 }
47 new (ctxt) CensusContext(method);
48 }
49
TraceContextSerialize(const::opencensus::trace::SpanContext & context,char * tracing_buf,size_t tracing_buf_size)50 size_t TraceContextSerialize(const ::opencensus::trace::SpanContext& context,
51 char* tracing_buf, size_t tracing_buf_size) {
52 GrpcTraceContext trace_ctxt(context);
53 return TraceContextEncoding::Encode(trace_ctxt, tracing_buf,
54 tracing_buf_size);
55 }
56
StatsContextSerialize(size_t max_tags_len,grpc_slice * tags)57 size_t StatsContextSerialize(size_t max_tags_len, grpc_slice* tags) {
58 // TODO: Add implementation. Waiting on stats tagging to be added.
59 return 0;
60 }
61
ServerStatsSerialize(uint64_t server_elapsed_time,char * buf,size_t buf_size)62 size_t ServerStatsSerialize(uint64_t server_elapsed_time, char* buf,
63 size_t buf_size) {
64 return RpcServerStatsEncoding::Encode(server_elapsed_time, buf, buf_size);
65 }
66
ServerStatsDeserialize(const char * buf,size_t buf_size,uint64_t * server_elapsed_time)67 size_t ServerStatsDeserialize(const char* buf, size_t buf_size,
68 uint64_t* server_elapsed_time) {
69 return RpcServerStatsEncoding::Decode(absl::string_view(buf, buf_size),
70 server_elapsed_time);
71 }
72
GetIncomingDataSize(const grpc_call_final_info * final_info)73 uint64_t GetIncomingDataSize(const grpc_call_final_info* final_info) {
74 return final_info->stats.transport_stream_stats.incoming.data_bytes;
75 }
76
GetOutgoingDataSize(const grpc_call_final_info * final_info)77 uint64_t GetOutgoingDataSize(const grpc_call_final_info* final_info) {
78 return final_info->stats.transport_stream_stats.outgoing.data_bytes;
79 }
80
SpanContextFromCensusContext(const census_context * ctxt)81 SpanContext SpanContextFromCensusContext(const census_context* ctxt) {
82 return reinterpret_cast<const CensusContext*>(ctxt)->Context();
83 }
84
SpanFromCensusContext(const census_context * ctxt)85 Span SpanFromCensusContext(const census_context* ctxt) {
86 return reinterpret_cast<const CensusContext*>(ctxt)->Span();
87 }
88
StatusCodeToString(grpc_status_code code)89 absl::string_view StatusCodeToString(grpc_status_code code) {
90 switch (code) {
91 case GRPC_STATUS_OK:
92 return "OK";
93 case GRPC_STATUS_CANCELLED:
94 return "CANCELLED";
95 case GRPC_STATUS_UNKNOWN:
96 return "UNKNOWN";
97 case GRPC_STATUS_INVALID_ARGUMENT:
98 return "INVALID_ARGUMENT";
99 case GRPC_STATUS_DEADLINE_EXCEEDED:
100 return "DEADLINE_EXCEEDED";
101 case GRPC_STATUS_NOT_FOUND:
102 return "NOT_FOUND";
103 case GRPC_STATUS_ALREADY_EXISTS:
104 return "ALREADY_EXISTS";
105 case GRPC_STATUS_PERMISSION_DENIED:
106 return "PERMISSION_DENIED";
107 case GRPC_STATUS_UNAUTHENTICATED:
108 return "UNAUTHENTICATED";
109 case GRPC_STATUS_RESOURCE_EXHAUSTED:
110 return "RESOURCE_EXHAUSTED";
111 case GRPC_STATUS_FAILED_PRECONDITION:
112 return "FAILED_PRECONDITION";
113 case GRPC_STATUS_ABORTED:
114 return "ABORTED";
115 case GRPC_STATUS_OUT_OF_RANGE:
116 return "OUT_OF_RANGE";
117 case GRPC_STATUS_UNIMPLEMENTED:
118 return "UNIMPLEMENTED";
119 case GRPC_STATUS_INTERNAL:
120 return "INTERNAL";
121 case GRPC_STATUS_UNAVAILABLE:
122 return "UNAVAILABLE";
123 case GRPC_STATUS_DATA_LOSS:
124 return "DATA_LOSS";
125 default:
126 // gRPC wants users of this enum to include a default branch so that
127 // adding values is not a breaking change.
128 return "UNKNOWN_STATUS";
129 }
130 }
131
132 } // namespace grpc
133