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 #ifndef GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_CONTEXT_H 20 #define GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_CONTEXT_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <grpc/status.h> 25 #include "absl/memory/memory.h" 26 #include "absl/strings/string_view.h" 27 #include "absl/strings/strip.h" 28 #include "opencensus/trace/span.h" 29 #include "opencensus/trace/span_context.h" 30 #include "opencensus/trace/trace_params.h" 31 #include "src/core/lib/slice/slice_internal.h" 32 #include "src/cpp/common/channel_filter.h" 33 #include "src/cpp/ext/filters/census/rpc_encoding.h" 34 35 // This is needed because grpc has hardcoded CensusContext with a 36 // forward declaration of 'struct census_context;' 37 struct census_context; 38 39 namespace grpc { 40 41 // Thread compatible. 42 class CensusContext { 43 public: 44 CensusContext() : span_(::opencensus::trace::Span::BlankSpan()) {} 45 46 explicit CensusContext(absl::string_view name) 47 : span_(::opencensus::trace::Span::StartSpan(name)) {} 48 49 CensusContext(absl::string_view name, const ::opencensus::trace::Span* parent) 50 : span_(::opencensus::trace::Span::StartSpan(name, parent)) {} 51 52 CensusContext(absl::string_view name, 53 const ::opencensus::trace::SpanContext& parent_ctxt) 54 : span_(::opencensus::trace::Span::StartSpanWithRemoteParent( 55 name, parent_ctxt)) {} 56 57 ::opencensus::trace::SpanContext Context() const { return span_.context(); } 58 ::opencensus::trace::Span Span() const { return span_; } 59 void EndSpan() { span_.End(); } 60 61 private: 62 ::opencensus::trace::Span span_; 63 }; 64 65 // Serializes the outgoing trace context. Field IDs are 1 byte followed by 66 // field data. A 1 byte version ID is always encoded first. 67 size_t TraceContextSerialize(const ::opencensus::trace::SpanContext& context, 68 char* tracing_buf, size_t tracing_buf_size); 69 70 // Serializes the outgoing stats context. Field IDs are 1 byte followed by 71 // field data. A 1 byte version ID is always encoded first. Tags are directly 72 // serialized into the given grpc_slice. 73 size_t StatsContextSerialize(size_t max_tags_len, grpc_slice* tags); 74 75 // Serialize outgoing server stats. Returns the number of bytes serialized. 76 size_t ServerStatsSerialize(uint64_t server_elapsed_time, char* buf, 77 size_t buf_size); 78 79 // Deserialize incoming server stats. Returns the number of bytes deserialized. 80 size_t ServerStatsDeserialize(const char* buf, size_t buf_size, 81 uint64_t* server_elapsed_time); 82 83 // Deserialize the incoming SpanContext and generate a new server context based 84 // on that. This new span will never be a root span. This should only be called 85 // with a blank CensusContext as it overwrites it. 86 void GenerateServerContext(absl::string_view tracing, absl::string_view stats, 87 absl::string_view primary_role, 88 absl::string_view method, CensusContext* context); 89 90 // Creates a new client context that is by default a new root context. 91 // If the current context is the default context then the newly created 92 // span automatically becomes a root span. This should only be called with a 93 // blank CensusContext as it overwrites it. 94 void GenerateClientContext(absl::string_view method, CensusContext* ctxt, 95 CensusContext* parent_ctx); 96 97 // Returns the incoming data size from the grpc call final info. 98 uint64_t GetIncomingDataSize(const grpc_call_final_info* final_info); 99 100 // Returns the outgoing data size from the grpc call final info. 101 uint64_t GetOutgoingDataSize(const grpc_call_final_info* final_info); 102 103 // These helper functions return the SpanContext and Span, respectively 104 // associated with the census_context* stored by grpc. The user will need to 105 // call this for manual propagation of tracing data. 106 ::opencensus::trace::SpanContext SpanContextFromCensusContext( 107 const census_context* ctxt); 108 ::opencensus::trace::Span SpanFromCensusContext(const census_context* ctxt); 109 110 // Returns a string representation of the StatusCode enum. 111 absl::string_view StatusCodeToString(grpc_status_code code); 112 113 inline absl::string_view GetMethod(const grpc_slice* path) { 114 if (GRPC_SLICE_IS_EMPTY(*path)) { 115 return ""; 116 } 117 // Check for leading '/' and trim it if present. 118 return absl::StripPrefix(absl::string_view(reinterpret_cast<const char*>( 119 GRPC_SLICE_START_PTR(*path)), 120 GRPC_SLICE_LENGTH(*path)), 121 "/"); 122 } 123 124 } // namespace grpc 125 126 #endif /* GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_CONTEXT_H */ 127