3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_CONTEXT_H
20 #define GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_CONTEXT_H
22 #include <grpc/support/port_platform.h>
24 #include <grpc/status.h>
26 #include "absl/memory/memory.h"
27 #include "absl/strings/string_view.h"
28 #include "absl/strings/strip.h"
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"
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"
41 // This is needed because grpc has hardcoded CensusContext with a
42 // forward declaration of 'struct census_context;'
43 struct census_context;
50 CensusContext() : span_(::opencensus::trace::Span::BlankSpan()), tags_({}) {}
52 explicit CensusContext(absl::string_view name,
53 const ::opencensus::tags::TagMap& tags)
54 : span_(::opencensus::trace::Span::StartSpan(name)), tags_(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)),
61 CensusContext(absl::string_view name,
62 const ::opencensus::trace::SpanContext& parent_ctxt)
63 : span_(::opencensus::trace::Span::StartSpanWithRemoteParent(
67 const ::opencensus::trace::Span& Span() const { return span_; }
68 const ::opencensus::tags::TagMap& tags() const { return tags_; }
70 ::opencensus::trace::SpanContext Context() const { return Span().context(); }
71 void EndSpan() { Span().End(); }
74 ::opencensus::trace::Span span_;
75 ::opencensus::tags::TagMap tags_;
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);
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);
88 // Serialize outgoing server stats. Returns the number of bytes serialized.
89 size_t ServerStatsSerialize(uint64_t server_elapsed_time, char* buf,
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);
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);
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);
110 // Returns the incoming data size from the grpc call final info.
111 uint64_t GetIncomingDataSize(const grpc_call_final_info* final_info);
113 // Returns the outgoing data size from the grpc call final info.
114 uint64_t GetOutgoingDataSize(const grpc_call_final_info* final_info);
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);
123 // Returns a string representation of the StatusCode enum.
124 absl::string_view StatusCodeToString(grpc_status_code code);
126 inline absl::string_view GetMethod(const grpc_slice* path) {
127 if (GRPC_SLICE_IS_EMPTY(*path)) {
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)),
139 #endif /* GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_CONTEXT_H */