1 // Copyright 2023 gRPC authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef OBSERVABILITY_MAIN_H 16 #define OBSERVABILITY_MAIN_H 17 18 #include <grpc/status.h> 19 #include <stdint.h> 20 21 #include <algorithm> 22 #include <condition_variable> 23 #include <mutex> 24 #include <queue> 25 #include <string> 26 #include <utility> 27 #include <vector> 28 29 #include "absl/strings/string_view.h" 30 #include "constants.h" 31 #include "python_observability_context.h" 32 33 namespace grpc_observability { 34 35 struct CensusData { 36 DataType type; 37 std::vector<Label> labels; 38 std::string identifier; 39 // TODO(xuanwn): We can use union for span_data and measurement_data 40 SpanCensusData span_data; 41 Measurement measurement_data; CensusDataCensusData42 CensusData() {} CensusDataCensusData43 CensusData(const Measurement& mm, const std::vector<Label>& labels, 44 std::string id) 45 : type(kMetricData), 46 labels(std::move(labels)), 47 identifier(id), 48 measurement_data(mm) {} CensusDataCensusData49 CensusData(const SpanCensusData& sd) : type(kSpanData), span_data(sd) {} 50 }; 51 52 // extern is required for Cython 53 extern std::queue<CensusData>* g_census_data_buffer; 54 extern std::mutex g_census_data_buffer_mutex; 55 extern std::condition_variable g_census_data_buffer_cv; 56 57 void* CreateClientCallTracer(const char* method, const char* target, 58 const char* trace_id, const char* parent_span_id, 59 const char* identifier, 60 const std::vector<Label> exchange_labels, 61 bool add_csm_optional_labels, 62 bool registered_method); 63 64 void* CreateServerCallTracerFactory(const std::vector<Label> exchange_labels, 65 const char* identifier); 66 67 void NativeObservabilityInit(); 68 69 void AwaitNextBatchLocked(std::unique_lock<std::mutex>& lock, int timeout_ms); 70 71 void AddCensusDataToBuffer(const CensusData& buffer); 72 73 void RecordIntMetric(MetricsName name, int64_t value, 74 const std::vector<Label>& labels, std::string identifier, 75 const bool registered_method, 76 const bool include_exchange_labels); 77 78 void RecordDoubleMetric(MetricsName name, double value, 79 const std::vector<Label>& labels, 80 std::string identifier, const bool registered_method, 81 const bool include_exchange_labels); 82 83 void RecordSpan(const SpanCensusData& span_census_data); 84 85 absl::string_view StatusCodeToString(grpc_status_code code); 86 87 } // namespace grpc_observability 88 89 #endif // OBSERVABILITY_MAIN_H 90