• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/cpp/ext/filters/census/context.h"
20 
21 #include <grpc/support/port_platform.h>
22 
23 #include <new>
24 
25 #include "opencensus/tags/context_util.h"
26 #include "opencensus/tags/tag_map.h"
27 #include "opencensus/trace/context_util.h"
28 #include "opencensus/trace/propagation/grpc_trace_bin.h"
29 #include "src/core/lib/transport/transport.h"
30 #include "src/cpp/ext/filters/census/grpc_plugin.h"
31 #include "src/cpp/ext/filters/census/rpc_encoding.h"
32 
33 namespace grpc {
34 
35 using ::opencensus::tags::TagMap;
36 using ::opencensus::trace::Span;
37 using ::opencensus::trace::SpanContext;
38 
GenerateServerContext(absl::string_view tracing,absl::string_view method,CensusContext * context)39 void GenerateServerContext(absl::string_view tracing, absl::string_view method,
40                            CensusContext* context) {
41   // Destruct the current CensusContext to free the Span memory before
42   // overwriting it below.
43   context->~CensusContext();
44   if (method.empty()) {
45     new (context) CensusContext(grpc::internal::OpenCensusRegistry::Get()
46                                     .PopulateTagMapWithConstantLabels({}));
47     return;
48   }
49   SpanContext parent_ctx =
50       opencensus::trace::propagation::FromGrpcTraceBinHeader(tracing);
51   if (parent_ctx.IsValid()) {
52     new (context) CensusContext(method, parent_ctx,
53                                 grpc::internal::OpenCensusRegistry::Get()
54                                     .PopulateTagMapWithConstantLabels({}));
55   } else {
56     new (context)
57         CensusContext(method, grpc::internal::OpenCensusRegistry::Get()
58                                   .PopulateTagMapWithConstantLabels({}));
59   }
60   grpc::internal::OpenCensusRegistry::Get()
61       .PopulateCensusContextWithConstantAttributes(context);
62 }
63 
GenerateClientContext(absl::string_view method,CensusContext * ctxt,CensusContext * parent_ctxt)64 void GenerateClientContext(absl::string_view method, CensusContext* ctxt,
65                            CensusContext* parent_ctxt) {
66   // Destruct the current CensusContext to free the Span memory before
67   // overwriting it below.
68   ctxt->~CensusContext();
69   if (method.empty()) {
70     new (ctxt) CensusContext(grpc::internal::OpenCensusRegistry::Get()
71                                  .PopulateTagMapWithConstantLabels({}));
72     return;
73   }
74   if (parent_ctxt != nullptr) {
75     SpanContext span_ctxt = parent_ctxt->Context();
76     Span span = parent_ctxt->Span();
77     if (span_ctxt.IsValid()) {
78       new (ctxt) CensusContext(method, &span,
79                                grpc::internal::OpenCensusRegistry::Get()
80                                    .PopulateTagMapWithConstantLabels({}));
81       grpc::internal::OpenCensusRegistry::Get()
82           .PopulateCensusContextWithConstantAttributes(ctxt);
83       return;
84     }
85   }
86   const Span& span = opencensus::trace::GetCurrentSpan();
87   const TagMap& tags = grpc::internal::OpenCensusRegistry::Get()
88                            .PopulateTagMapWithConstantLabels(
89                                opencensus::tags::GetCurrentTagMap());
90   if (span.context().IsValid()) {
91     // Create span with parent.
92     new (ctxt) CensusContext(method, &span, tags);
93   } else {
94     // Create span without parent.
95     new (ctxt) CensusContext(method, tags);
96   }
97   grpc::internal::OpenCensusRegistry::Get()
98       .PopulateCensusContextWithConstantAttributes(ctxt);
99 }
100 
TraceContextSerialize(const::opencensus::trace::SpanContext & context,char * tracing_buf,size_t tracing_buf_size)101 size_t TraceContextSerialize(const ::opencensus::trace::SpanContext& context,
102                              char* tracing_buf, size_t tracing_buf_size) {
103   if (tracing_buf_size <
104       opencensus::trace::propagation::kGrpcTraceBinHeaderLen) {
105     return 0;
106   }
107   opencensus::trace::propagation::ToGrpcTraceBinHeader(
108       context, reinterpret_cast<uint8_t*>(tracing_buf));
109   return opencensus::trace::propagation::kGrpcTraceBinHeaderLen;
110 }
111 
StatsContextSerialize(size_t,grpc_slice *)112 size_t StatsContextSerialize(size_t /*max_tags_len*/, grpc_slice* /*tags*/) {
113   // TODO(unknown): Add implementation. Waiting on stats tagging to be added.
114   return 0;
115 }
116 
ServerStatsSerialize(uint64_t server_elapsed_time,char * buf,size_t buf_size)117 size_t ServerStatsSerialize(uint64_t server_elapsed_time, char* buf,
118                             size_t buf_size) {
119   return internal::RpcServerStatsEncoding::Encode(server_elapsed_time, buf,
120                                                   buf_size);
121 }
122 
ServerStatsDeserialize(const char * buf,size_t buf_size,uint64_t * server_elapsed_time)123 size_t ServerStatsDeserialize(const char* buf, size_t buf_size,
124                               uint64_t* server_elapsed_time) {
125   return internal::RpcServerStatsEncoding::Decode(
126       absl::string_view(buf, buf_size), server_elapsed_time);
127 }
128 
GetIncomingDataSize(const grpc_call_final_info * final_info)129 uint64_t GetIncomingDataSize(const grpc_call_final_info* final_info) {
130   return final_info->stats.transport_stream_stats.incoming.data_bytes;
131 }
132 
GetOutgoingDataSize(const grpc_call_final_info * final_info)133 uint64_t GetOutgoingDataSize(const grpc_call_final_info* final_info) {
134   return final_info->stats.transport_stream_stats.outgoing.data_bytes;
135 }
136 
SpanContextFromCensusContext(const census_context * ctxt)137 SpanContext SpanContextFromCensusContext(const census_context* ctxt) {
138   return reinterpret_cast<const CensusContext*>(ctxt)->Context();
139 }
140 
SpanFromCensusContext(const census_context * ctxt)141 Span SpanFromCensusContext(const census_context* ctxt) {
142   return reinterpret_cast<const CensusContext*>(ctxt)->Span();
143 }
144 
StatusCodeToString(grpc_status_code code)145 absl::string_view StatusCodeToString(grpc_status_code code) {
146   switch (code) {
147     case GRPC_STATUS_OK:
148       return "OK";
149     case GRPC_STATUS_CANCELLED:
150       return "CANCELLED";
151     case GRPC_STATUS_UNKNOWN:
152       return "UNKNOWN";
153     case GRPC_STATUS_INVALID_ARGUMENT:
154       return "INVALID_ARGUMENT";
155     case GRPC_STATUS_DEADLINE_EXCEEDED:
156       return "DEADLINE_EXCEEDED";
157     case GRPC_STATUS_NOT_FOUND:
158       return "NOT_FOUND";
159     case GRPC_STATUS_ALREADY_EXISTS:
160       return "ALREADY_EXISTS";
161     case GRPC_STATUS_PERMISSION_DENIED:
162       return "PERMISSION_DENIED";
163     case GRPC_STATUS_UNAUTHENTICATED:
164       return "UNAUTHENTICATED";
165     case GRPC_STATUS_RESOURCE_EXHAUSTED:
166       return "RESOURCE_EXHAUSTED";
167     case GRPC_STATUS_FAILED_PRECONDITION:
168       return "FAILED_PRECONDITION";
169     case GRPC_STATUS_ABORTED:
170       return "ABORTED";
171     case GRPC_STATUS_OUT_OF_RANGE:
172       return "OUT_OF_RANGE";
173     case GRPC_STATUS_UNIMPLEMENTED:
174       return "UNIMPLEMENTED";
175     case GRPC_STATUS_INTERNAL:
176       return "INTERNAL";
177     case GRPC_STATUS_UNAVAILABLE:
178       return "UNAVAILABLE";
179     case GRPC_STATUS_DATA_LOSS:
180       return "DATA_LOSS";
181     default:
182       // gRPC wants users of this enum to include a default branch so that
183       // adding values is not a breaking change.
184       return "UNKNOWN_STATUS";
185   }
186 }
187 
188 }  // namespace grpc
189