// Copyright 2023 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef GRPC_PYTHON_OPENCENSUS_H #define GRPC_PYTHON_OPENCENSUS_H #include #include #include #include #include #include #include "absl/strings/string_view.h" #include "absl/strings/strip.h" #include "absl/time/clock.h" #include "absl/time/time.h" #include "constants.h" #include "sampler.h" #include #include "src/core/lib/channel/channel_stack.h" namespace grpc_observability { namespace { std::atomic g_python_census_stats_enabled(false); std::atomic g_python_census_tracing_enabled(false); } // namespace // Enables/Disables Python census stats/tracing. It's only safe to do at the // start of a program, before any channels/servers are built. void EnablePythonCensusStats(bool enable); void EnablePythonCensusTracing(bool enable); // Gets the current status of Python OpenCensus stats/tracing bool PythonCensusStatsEnabled(); bool PythonCensusTracingEnabled(); static constexpr size_t kTraceIdSize = 16; static constexpr size_t kSpanIdSize = 8; constexpr uint8_t kVersionId = 0; constexpr uint8_t kTraceIdField = 0; constexpr uint8_t kSpanIdField = 1; constexpr uint8_t kTraceOptionsField = 2; constexpr int kVersionLen = 1; constexpr int kTraceIdLen = 16; constexpr int kSpanIdLen = 8; constexpr int kTraceOptionsLen = 1; constexpr int kVersionOfs = 0; constexpr int kTraceIdOfs = 1; constexpr int kSpanIdOfs = kTraceIdOfs + 1 + kTraceIdLen; constexpr int kTraceOptionsOfs = kSpanIdOfs + 1 + kSpanIdLen; static constexpr size_t kSizeTraceID = 16; static constexpr size_t kSizeSpanID = 8; static constexpr size_t kSizeTraceOptions = 1; // The length of the grpc-trace-bin value: // 1 (version) // + 1 (trace_id field) // + 16 (length of trace_id) // + 1 (span_id field) // + 8 (span_id length) // + 1 (trace_options field) // + 1 (trace_options length) // ---- // 29 constexpr int kGrpcTraceBinHeaderLen = kVersionLen + 1 + kTraceIdLen + 1 + kSpanIdLen + 1 + kTraceOptionsLen; struct Tag { std::string key; std::string value; }; struct Label { Label() {} Label(std::string k, std::string v) : key(k), value(v) {} std::string key; std::string value; }; union MeasurementValue { double value_double; int64_t value_int; }; struct Measurement { MetricsName name; MeasurementType type; MeasurementValue value; }; struct Annotation { std::string time_stamp; std::string description; }; struct SpanCensusData { std::string name; std::string start_time; std::string end_time; std::string trace_id; std::string span_id; std::string parent_span_id; std::string status; std::vector