Imported Upstream version 1.33.0
[platform/upstream/grpc.git] / src / cpp / ext / filters / census / context.h
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:
50   CensusContext() : span_(::opencensus::trace::Span::BlankSpan()), tags_({}) {}
51
52   explicit CensusContext(absl::string_view name,
53                          const ::opencensus::tags::TagMap& tags)
54       : span_(::opencensus::trace::Span::StartSpan(name)), tags_(tags) {}
55
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
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
67   const ::opencensus::trace::Span& Span() const { return span_; }
68   const ::opencensus::tags::TagMap& tags() const { return tags_; }
69
70   ::opencensus::trace::SpanContext Context() const { return Span().context(); }
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 stats,
100                            absl::string_view primary_role,
101                            absl::string_view method, CensusContext* context);
102
103 // Creates a new client context that is by default a new root context.
104 // If the current context is the default context then the newly created
105 // span automatically becomes a root span. This should only be called with a
106 // blank CensusContext as it overwrites it.
107 void GenerateClientContext(absl::string_view method, CensusContext* ctxt,
108                            CensusContext* parent_ctx);
109
110 // Returns the incoming data size from the grpc call final info.
111 uint64_t GetIncomingDataSize(const grpc_call_final_info* final_info);
112
113 // Returns the outgoing data size from the grpc call final info.
114 uint64_t GetOutgoingDataSize(const grpc_call_final_info* final_info);
115
116 // These helper functions return the SpanContext and Span, respectively
117 // associated with the census_context* stored by grpc. The user will need to
118 // call this for manual propagation of tracing data.
119 ::opencensus::trace::SpanContext SpanContextFromCensusContext(
120     const census_context* ctxt);
121 ::opencensus::trace::Span SpanFromCensusContext(const census_context* ctxt);
122
123 // Returns a string representation of the StatusCode enum.
124 absl::string_view StatusCodeToString(grpc_status_code code);
125
126 inline absl::string_view GetMethod(const grpc_slice* path) {
127   if (GRPC_SLICE_IS_EMPTY(*path)) {
128     return "";
129   }
130   // Check for leading '/' and trim it if present.
131   return absl::StripPrefix(absl::string_view(reinterpret_cast<const char*>(
132                                                  GRPC_SLICE_START_PTR(*path)),
133                                              GRPC_SLICE_LENGTH(*path)),
134                            "/");
135 }
136
137 }  // namespace grpc
138
139 #endif /* GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_CONTEXT_H */