3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "opencensus/tags/context_util.h"
22 #include "opencensus/trace/context_util.h"
23 #include "src/cpp/ext/filters/census/context.h"
27 using ::opencensus::tags::TagMap;
28 using ::opencensus::trace::Span;
29 using ::opencensus::trace::SpanContext;
31 void GenerateServerContext(absl::string_view tracing, absl::string_view method,
32 CensusContext* context) {
33 // Destruct the current CensusContext to free the Span memory before
34 // overwriting it below.
35 context->~CensusContext();
36 GrpcTraceContext trace_ctxt;
37 if (TraceContextEncoding::Decode(tracing, &trace_ctxt) !=
38 TraceContextEncoding::kEncodeDecodeFailure) {
39 SpanContext parent_ctx = trace_ctxt.ToSpanContext();
40 if (parent_ctx.IsValid()) {
41 new (context) CensusContext(method, parent_ctx);
45 new (context) CensusContext(method, TagMap{});
48 void GenerateClientContext(absl::string_view method, CensusContext* ctxt,
49 CensusContext* parent_ctxt) {
50 // Destruct the current CensusContext to free the Span memory before
51 // overwriting it below.
52 ctxt->~CensusContext();
53 if (parent_ctxt != nullptr) {
54 SpanContext span_ctxt = parent_ctxt->Context();
55 Span span = parent_ctxt->Span();
56 if (span_ctxt.IsValid()) {
57 new (ctxt) CensusContext(method, &span, TagMap{});
61 const Span& span = opencensus::trace::GetCurrentSpan();
62 const TagMap& tags = opencensus::tags::GetCurrentTagMap();
63 if (span.context().IsValid()) {
64 // Create span with parent.
65 new (ctxt) CensusContext(method, &span, tags);
68 // Create span without parent.
69 new (ctxt) CensusContext(method, tags);
72 size_t TraceContextSerialize(const ::opencensus::trace::SpanContext& context,
73 char* tracing_buf, size_t tracing_buf_size) {
74 GrpcTraceContext trace_ctxt(context);
75 return TraceContextEncoding::Encode(trace_ctxt, tracing_buf,
79 size_t StatsContextSerialize(size_t /*max_tags_len*/, grpc_slice* /*tags*/) {
80 // TODO(unknown): Add implementation. Waiting on stats tagging to be added.
84 size_t ServerStatsSerialize(uint64_t server_elapsed_time, char* buf,
86 return RpcServerStatsEncoding::Encode(server_elapsed_time, buf, buf_size);
89 size_t ServerStatsDeserialize(const char* buf, size_t buf_size,
90 uint64_t* server_elapsed_time) {
91 return RpcServerStatsEncoding::Decode(absl::string_view(buf, buf_size),
95 uint64_t GetIncomingDataSize(const grpc_call_final_info* final_info) {
96 return final_info->stats.transport_stream_stats.incoming.data_bytes;
99 uint64_t GetOutgoingDataSize(const grpc_call_final_info* final_info) {
100 return final_info->stats.transport_stream_stats.outgoing.data_bytes;
103 SpanContext SpanContextFromCensusContext(const census_context* ctxt) {
104 return reinterpret_cast<const CensusContext*>(ctxt)->Context();
107 Span SpanFromCensusContext(const census_context* ctxt) {
108 return reinterpret_cast<const CensusContext*>(ctxt)->Span();
111 absl::string_view StatusCodeToString(grpc_status_code code) {
115 case GRPC_STATUS_CANCELLED:
117 case GRPC_STATUS_UNKNOWN:
119 case GRPC_STATUS_INVALID_ARGUMENT:
120 return "INVALID_ARGUMENT";
121 case GRPC_STATUS_DEADLINE_EXCEEDED:
122 return "DEADLINE_EXCEEDED";
123 case GRPC_STATUS_NOT_FOUND:
125 case GRPC_STATUS_ALREADY_EXISTS:
126 return "ALREADY_EXISTS";
127 case GRPC_STATUS_PERMISSION_DENIED:
128 return "PERMISSION_DENIED";
129 case GRPC_STATUS_UNAUTHENTICATED:
130 return "UNAUTHENTICATED";
131 case GRPC_STATUS_RESOURCE_EXHAUSTED:
132 return "RESOURCE_EXHAUSTED";
133 case GRPC_STATUS_FAILED_PRECONDITION:
134 return "FAILED_PRECONDITION";
135 case GRPC_STATUS_ABORTED:
137 case GRPC_STATUS_OUT_OF_RANGE:
138 return "OUT_OF_RANGE";
139 case GRPC_STATUS_UNIMPLEMENTED:
140 return "UNIMPLEMENTED";
141 case GRPC_STATUS_INTERNAL:
143 case GRPC_STATUS_UNAVAILABLE:
144 return "UNAVAILABLE";
145 case GRPC_STATUS_DATA_LOSS:
148 // gRPC wants users of this enum to include a default branch so that
149 // adding values is not a breaking change.
150 return "UNKNOWN_STATUS";