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
15from libcpp.string cimport string
16from libcpp.vector cimport vector
17
18ctypedef   signed long long int64_t
19
20cdef extern from "<queue>" namespace "std" nogil:
21  cdef cppclass queue[T]:
22    bint empty()
23    T& front()
24    void pop()
25
26cdef extern from "<mutex>" namespace "std" nogil:
27  cdef cppclass mutex:
28    mutex()
29
30  cdef cppclass unique_lock[Mutex]:
31    unique_lock(Mutex&)
32
33cdef extern from "<condition_variable>" namespace "std" nogil:
34  cdef cppclass condition_variable:
35    void notify_all()
36
37cdef extern from "src/core/lib/channel/call_tracer.h" namespace "grpc_core":
38  cdef cppclass ClientCallTracer:
39    pass
40
41cdef extern from "python_census_context.h" namespace "grpc_observability":
42  cdef void EnablePythonCensusStats(bint enable) nogil
43  cdef void EnablePythonCensusTracing(bint enable) nogil
44
45  union MeasurementValue:
46    double value_double
47    int64_t value_int
48
49  ctypedef struct Label:
50    string key
51    string value
52
53  ctypedef struct Annotation:
54    string time_stamp
55    string description
56
57  ctypedef struct Measurement:
58    cMetricsName name
59    MeasurementType type
60    MeasurementValue value
61
62  ctypedef struct SpanCensusData:
63    string name
64    string start_time
65    string end_time
66    string trace_id
67    string span_id
68    string parent_span_id
69    string status
70    vector[Label] span_labels
71    vector[Annotation] span_annotations
72    int64_t child_span_count
73    bint should_sample
74
75cdef extern from "observability_util.h" namespace "grpc_observability":
76  cdef cGcpObservabilityConfig ReadAndActivateObservabilityConfig() nogil
77  cdef void NativeObservabilityInit() except +
78  cdef void* CreateClientCallTracer(const char* method,
79                                    const char* target,
80                                    const char* trace_id,
81                                    const char* parent_span_id) except +
82  cdef void* CreateServerCallTracerFactory() except +
83  cdef queue[NativeCensusData]* g_census_data_buffer
84  cdef void AwaitNextBatchLocked(unique_lock[mutex]&, int) nogil
85  cdef bint PythonCensusStatsEnabled() nogil
86  cdef bint PythonCensusTracingEnabled() nogil
87  cdef mutex g_census_data_buffer_mutex
88  cdef condition_variable g_census_data_buffer_cv
89
90  cppclass NativeCensusData "::grpc_observability::CensusData":
91    DataType type
92    Measurement measurement_data
93    SpanCensusData span_data
94    vector[Label] labels
95
96  ctypedef struct CloudMonitoring:
97    pass
98
99  ctypedef struct CloudTrace:
100    float sampling_rate
101
102  ctypedef struct CloudLogging:
103    pass
104
105  ctypedef struct cGcpObservabilityConfig "::grpc_observability::GcpObservabilityConfig":
106    CloudMonitoring cloud_monitoring
107    CloudTrace cloud_trace
108    CloudLogging cloud_logging
109    string project_id
110    vector[Label] labels
111    bint is_valid
112
113cdef extern from "constants.h" namespace "grpc_observability":
114  ctypedef enum DataType:
115    kSpanData
116    kMetricData
117
118  ctypedef enum MeasurementType:
119    kMeasurementDouble
120    kMeasurementInt
121
122  ctypedef enum cMetricsName "::grpc_observability::MetricsName":
123    # Client
124    kRpcClientApiLatencyMeasureName
125    kRpcClientSentMessagesPerRpcMeasureName
126    kRpcClientSentBytesPerRpcMeasureName
127    kRpcClientReceivedMessagesPerRpcMeasureName
128    kRpcClientReceivedBytesPerRpcMeasureName
129    kRpcClientRoundtripLatencyMeasureName
130    kRpcClientCompletedRpcMeasureName
131    kRpcClientServerLatencyMeasureName
132    kRpcClientStartedRpcsMeasureName
133    kRpcClientRetriesPerCallMeasureName
134    kRpcClientTransparentRetriesPerCallMeasureName
135    kRpcClientRetryDelayPerCallMeasureName
136    kRpcClientTransportLatencyMeasureName
137
138    # Server
139    kRpcServerSentMessagesPerRpcMeasureName
140    kRpcServerSentBytesPerRpcMeasureName
141    kRpcServerReceivedMessagesPerRpcMeasureName
142    kRpcServerReceivedBytesPerRpcMeasureName
143    kRpcServerServerLatencyMeasureName
144    kRpcServerCompletedRpcMeasureName
145    kRpcServerStartedRpcsMeasureName
146
147  string kClientMethod
148  string kClientTarget
149  string kClientStatus
150
151cdef extern from "sampler.h" namespace "grpc_observability":
152  cdef cppclass ProbabilitySampler:
153    @staticmethod
154    ProbabilitySampler& Get()
155
156    void SetThreshold(double sampling_rate)
157