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