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_SRC_CPP_EXT_FILTERS_CENSUS_CONTEXT_H
20 #define GRPC_SRC_CPP_EXT_FILTERS_CENSUS_CONTEXT_H
21
22 #include <grpc/grpc.h>
23 #include <grpc/slice.h>
24 #include <grpc/status.h>
25 #include <grpc/support/port_platform.h>
26 #include <grpcpp/opencensus.h>
27 #include <stddef.h>
28 #include <stdint.h>
29
30 #include "absl/strings/string_view.h"
31 #include "absl/strings/strip.h"
32 #include "opencensus/trace/span.h"
33 #include "opencensus/trace/span_context.h"
34 #include "src/core/lib/channel/channel_stack.h"
35 #include "src/core/lib/slice/slice.h"
36
37 namespace grpc {
38
39 using experimental::CensusContext;
40
41 // Serializes the outgoing trace context. tracing_buf must be
42 // opencensus::trace::propagation::kGrpcTraceBinHeaderLen bytes long.
43 size_t TraceContextSerialize(const ::opencensus::trace::SpanContext& context,
44 char* tracing_buf, size_t tracing_buf_size);
45
46 // Serializes the outgoing stats context. Field IDs are 1 byte followed by
47 // field data. A 1 byte version ID is always encoded first. Tags are directly
48 // serialized into the given grpc_slice.
49 size_t StatsContextSerialize(size_t max_tags_len, grpc_slice* tags);
50
51 // Serialize outgoing server stats. Returns the number of bytes serialized.
52 size_t ServerStatsSerialize(uint64_t server_elapsed_time, char* buf,
53 size_t buf_size);
54
55 // Deserialize incoming server stats. Returns the number of bytes deserialized.
56 size_t ServerStatsDeserialize(const char* buf, size_t buf_size,
57 uint64_t* server_elapsed_time);
58
59 // Deserialize the incoming SpanContext and generate a new server context based
60 // on that. This new span will never be a root span. This should only be called
61 // with a blank CensusContext as it overwrites it.
62 void GenerateServerContext(absl::string_view tracing, absl::string_view method,
63 CensusContext* context);
64
65 // Creates a new client context that is by default a new root context.
66 // If the current context is the default context then the newly created
67 // span automatically becomes a root span. This should only be called with a
68 // blank CensusContext as it overwrites it.
69 void GenerateClientContext(absl::string_view method, CensusContext* ctxt,
70 CensusContext* parent_ctx);
71
72 // Returns the incoming data size from the grpc call final info.
73 uint64_t GetIncomingDataSize(const grpc_call_final_info* final_info);
74
75 // Returns the outgoing data size from the grpc call final info.
76 uint64_t GetOutgoingDataSize(const grpc_call_final_info* final_info);
77
78 // These helper functions return the SpanContext and Span, respectively
79 // associated with the census_context* stored by grpc. The user will need to
80 // call this for manual propagation of tracing data.
81 ::opencensus::trace::SpanContext SpanContextFromCensusContext(
82 const census_context* ctxt);
83 ::opencensus::trace::Span SpanFromCensusContext(const census_context* ctxt);
84
85 // Returns a string representation of the StatusCode enum.
86 absl::string_view StatusCodeToString(grpc_status_code code);
87
GetMethod(const grpc_core::Slice & path)88 inline absl::string_view GetMethod(const grpc_core::Slice& path) {
89 if (path.empty()) {
90 return "";
91 }
92 // Check for leading '/' and trim it if present.
93 return absl::StripPrefix(path.as_string_view(), "/");
94 }
95
96 } // namespace grpc
97
98 #endif // GRPC_SRC_CPP_EXT_FILTERS_CENSUS_CONTEXT_H
99