| 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 |
#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 |
|
| 30 |
#include "src/core/lib/transport/transport.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 |
|
| 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 |
SpanContext parent_ctx = |
| 45 |
opencensus::trace::propagation::FromGrpcTraceBinHeader(tracing); |
| 46 |
if (parent_ctx.IsValid()) { |
| 47 |
new (context) CensusContext(method, parent_ctx); |
| 48 |
return; |
| 49 |
} |
| 50 |
new (context) CensusContext(method, TagMap{}); |
| 51 |
} |
| 52 |
|
| 53 |
void GenerateClientContext(absl::string_view method, CensusContext* ctxt, |
| 54 |
CensusContext* parent_ctxt) { |
| 55 |
// Destruct the current CensusContext to free the Span memory before |
| 56 |
// overwriting it below. |
| 57 |
ctxt->~CensusContext(); |
| 58 |
if (parent_ctxt != nullptr) { |
| 59 |
SpanContext span_ctxt = parent_ctxt->Context(); |
| 60 |
Span span = parent_ctxt->Span(); |
| 61 |
if (span_ctxt.IsValid()) { |
| 62 |
new (ctxt) CensusContext(method, &span, TagMap{}); |
| 63 |
return; |
| 64 |
} |
| 65 |
} |
| 66 |
const Span& span = opencensus::trace::GetCurrentSpan(); |
| 67 |
const TagMap& tags = opencensus::tags::GetCurrentTagMap(); |
| 68 |
if (span.context().IsValid()) { |
| 69 |
// Create span with parent. |
| 70 |
new (ctxt) CensusContext(method, &span, tags); |
| 71 |
return; |
| 72 |
} |
| 73 |
// Create span without parent. |
| 74 |
new (ctxt) CensusContext(method, tags); |
| 75 |
} |
| 76 |
|
| 77 |
size_t TraceContextSerialize(const ::opencensus::trace::SpanContext& context, |
| 78 |
char* tracing_buf, size_t tracing_buf_size) { |
| 79 |
if (tracing_buf_size < |
| 80 |
opencensus::trace::propagation::kGrpcTraceBinHeaderLen) { |
| 81 |
return 0; |
| 82 |
} |
| 83 |
opencensus::trace::propagation::ToGrpcTraceBinHeader( |
| 84 |
context, reinterpret_cast<uint8_t*>(tracing_buf)); |
| 85 |
return opencensus::trace::propagation::kGrpcTraceBinHeaderLen; |
| 86 |
} |
| 87 |
|
| 88 |
size_t StatsContextSerialize(size_t /*max_tags_len*/, grpc_slice* /*tags*/) { |
| 89 |
// TODO(unknown): Add implementation. Waiting on stats tagging to be added. |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
size_t ServerStatsSerialize(uint64_t server_elapsed_time, char* buf, |
| 94 |
size_t buf_size) { |
| 95 |
return RpcServerStatsEncoding::Encode(server_elapsed_time, buf, buf_size); |
| 96 |
} |
| 97 |
|
| 98 |
size_t ServerStatsDeserialize(const char* buf, size_t buf_size, |
| 99 |
uint64_t* server_elapsed_time) { |
| 100 |
return RpcServerStatsEncoding::Decode(absl::string_view(buf, buf_size), |
| 101 |
server_elapsed_time); |
| 102 |
} |
| 103 |
|
| 104 |
uint64_t GetIncomingDataSize(const grpc_call_final_info* final_info) { |
| 105 |
return final_info->stats.transport_stream_stats.incoming.data_bytes; |
| 106 |
} |
| 107 |
|
| 108 |
uint64_t GetOutgoingDataSize(const grpc_call_final_info* final_info) { |
| 109 |
return final_info->stats.transport_stream_stats.outgoing.data_bytes; |
| 110 |
} |
| 111 |
|
| 112 |
SpanContext SpanContextFromCensusContext(const census_context* ctxt) { |
| 113 |
return reinterpret_cast<const CensusContext*>(ctxt)->Context(); |
| 114 |
} |
| 115 |
|
| 116 |
Span SpanFromCensusContext(const census_context* ctxt) { |
| 117 |
return reinterpret_cast<const CensusContext*>(ctxt)->Span(); |
| 118 |
} |
| 119 |
|
| 120 |
absl::string_view StatusCodeToString(grpc_status_code code) { |
| 121 |
switch (code) { |
| 122 |
case GRPC_STATUS_OK: |
| 123 |
return "OK"; |
| 124 |
case GRPC_STATUS_CANCELLED: |
| 125 |
return "CANCELLED"; |
| 126 |
case GRPC_STATUS_UNKNOWN: |
| 127 |
return "UNKNOWN"; |
| 128 |
case GRPC_STATUS_INVALID_ARGUMENT: |
| 129 |
return "INVALID_ARGUMENT"; |
| 130 |
case GRPC_STATUS_DEADLINE_EXCEEDED: |
| 131 |
return "DEADLINE_EXCEEDED"; |
| 132 |
case GRPC_STATUS_NOT_FOUND: |
| 133 |
return "NOT_FOUND"; |
| 134 |
case GRPC_STATUS_ALREADY_EXISTS: |
| 135 |
return "ALREADY_EXISTS"; |
| 136 |
case GRPC_STATUS_PERMISSION_DENIED: |
| 137 |
return "PERMISSION_DENIED"; |
| 138 |
case GRPC_STATUS_UNAUTHENTICATED: |
| 139 |
return "UNAUTHENTICATED"; |
| 140 |
case GRPC_STATUS_RESOURCE_EXHAUSTED: |
| 141 |
return "RESOURCE_EXHAUSTED"; |
| 142 |
case GRPC_STATUS_FAILED_PRECONDITION: |
| 143 |
return "FAILED_PRECONDITION"; |
| 144 |
case GRPC_STATUS_ABORTED: |
| 145 |
return "ABORTED"; |
| 146 |
case GRPC_STATUS_OUT_OF_RANGE: |
| 147 |
return "OUT_OF_RANGE"; |
| 148 |
case GRPC_STATUS_UNIMPLEMENTED: |
| 149 |
return "UNIMPLEMENTED"; |
| 150 |
case GRPC_STATUS_INTERNAL: |
| 151 |
return "INTERNAL"; |
| 152 |
case GRPC_STATUS_UNAVAILABLE: |
| 153 |
return "UNAVAILABLE"; |
| 154 |
case GRPC_STATUS_DATA_LOSS: |
| 155 |
return "DATA_LOSS"; |
| 156 |
default: |
| 157 |
// gRPC wants users of this enum to include a default branch so that |
| 158 |
// adding values is not a breaking change. |
| 159 |
return "UNKNOWN_STATUS"; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
} // namespace grpc |