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