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 <stdint.h>
19 
20 #include <algorithm>
21 #include <condition_variable>
22 #include <mutex>
23 #include <queue>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include "absl/strings/string_view.h"
29 #include "constants.h"
30 #include "python_census_context.h"
31 
32 #include <grpc/status.h>
33 
34 namespace grpc_observability {
35 
36 struct CensusData {
37   DataType type;
38   std::vector<Label> labels;
39   // TODO(xuanwn): We can use union here
40   SpanCensusData span_data;
41   Measurement measurement_data;
CensusDataCensusData42   CensusData() {}
CensusDataCensusData43   CensusData(const Measurement& mm, const std::vector<Label>& labels)
44       : type(kMetricData), labels(std::move(labels)), measurement_data(mm) {}
CensusDataCensusData45   CensusData(const SpanCensusData& sd) : type(kSpanData), span_data(sd) {}
46 };
47 
48 // extern is required for Cython
49 extern std::queue<CensusData>* g_census_data_buffer;
50 extern std::mutex g_census_data_buffer_mutex;
51 extern std::condition_variable g_census_data_buffer_cv;
52 
53 void* CreateClientCallTracer(const char* method, const char* target,
54                              const char* trace_id, const char* parent_span_id);
55 
56 void* CreateServerCallTracerFactory();
57 
58 void NativeObservabilityInit();
59 
60 void AwaitNextBatchLocked(std::unique_lock<std::mutex>& lock, int timeout_ms);
61 
62 void AddCensusDataToBuffer(const CensusData& buffer);
63 
64 void RecordIntMetric(MetricsName name, int64_t value,
65                      const std::vector<Label>& labels);
66 
67 void RecordDoubleMetric(MetricsName name, double value,
68                         const std::vector<Label>& labels);
69 
70 void RecordSpan(const SpanCensusData& span_census_data);
71 
72 absl::string_view StatusCodeToString(grpc_status_code code);
73 
74 }  // namespace grpc_observability
75 
76 #endif  // OBSERVABILITY_MAIN_H
77