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