forked from qdrant/qdrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests_telemetry.rs
201 lines (176 loc) · 6.08 KB
/
requests_telemetry.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::collections::HashMap;
use std::sync::Arc;
use common::types::TelemetryDetail;
use parking_lot::Mutex;
use schemars::JsonSchema;
use segment::common::anonymize::Anonymize;
use segment::common::operation_time_statistics::{
OperationDurationStatistics, OperationDurationsAggregator, ScopeDurationMeasurer,
};
use serde::Serialize;
pub type HttpStatusCode = u16;
#[derive(Serialize, Clone, Default, Debug, JsonSchema)]
pub struct WebApiTelemetry {
pub responses: HashMap<String, HashMap<HttpStatusCode, OperationDurationStatistics>>,
}
#[derive(Serialize, Clone, Default, Debug, JsonSchema)]
pub struct GrpcTelemetry {
pub responses: HashMap<String, OperationDurationStatistics>,
}
pub struct ActixTelemetryCollector {
pub workers: Vec<Arc<Mutex<ActixWorkerTelemetryCollector>>>,
}
#[derive(Default)]
pub struct ActixWorkerTelemetryCollector {
methods: HashMap<String, HashMap<HttpStatusCode, Arc<Mutex<OperationDurationsAggregator>>>>,
}
pub struct TonicTelemetryCollector {
pub workers: Vec<Arc<Mutex<TonicWorkerTelemetryCollector>>>,
}
#[derive(Default)]
pub struct TonicWorkerTelemetryCollector {
methods: HashMap<String, Arc<Mutex<OperationDurationsAggregator>>>,
}
impl ActixTelemetryCollector {
pub fn create_web_worker_telemetry(&mut self) -> Arc<Mutex<ActixWorkerTelemetryCollector>> {
let worker: Arc<Mutex<_>> = Default::default();
self.workers.push(worker.clone());
worker
}
pub fn get_telemetry_data(&self, detail: TelemetryDetail) -> WebApiTelemetry {
let mut result = WebApiTelemetry::default();
for web_data in &self.workers {
let lock = web_data.lock().get_telemetry_data(detail);
result.merge(&lock);
}
result
}
}
impl TonicTelemetryCollector {
#[allow(dead_code)]
pub fn create_grpc_telemetry_collector(&mut self) -> Arc<Mutex<TonicWorkerTelemetryCollector>> {
let worker: Arc<Mutex<_>> = Default::default();
self.workers.push(worker.clone());
worker
}
pub fn get_telemetry_data(&self, detail: TelemetryDetail) -> GrpcTelemetry {
let mut result = GrpcTelemetry::default();
for grpc_data in &self.workers {
let lock = grpc_data.lock().get_telemetry_data(detail);
result.merge(&lock);
}
result
}
}
impl TonicWorkerTelemetryCollector {
#[allow(dead_code)]
pub fn add_response(&mut self, method: String, instant: std::time::Instant) {
let aggregator = self
.methods
.entry(method)
.or_insert_with(OperationDurationsAggregator::new);
ScopeDurationMeasurer::new_with_instant(aggregator, instant);
}
pub fn get_telemetry_data(&self, detail: TelemetryDetail) -> GrpcTelemetry {
let mut responses = HashMap::new();
for (method, aggregator) in self.methods.iter() {
responses.insert(method.clone(), aggregator.lock().get_statistics(detail));
}
GrpcTelemetry { responses }
}
}
impl ActixWorkerTelemetryCollector {
pub fn add_response(
&mut self,
method: String,
status_code: HttpStatusCode,
instant: std::time::Instant,
) {
let aggregator = self
.methods
.entry(method)
.or_default()
.entry(status_code)
.or_insert_with(OperationDurationsAggregator::new);
ScopeDurationMeasurer::new_with_instant(aggregator, instant);
}
pub fn get_telemetry_data(&self, detail: TelemetryDetail) -> WebApiTelemetry {
let mut responses = HashMap::new();
for (method, status_codes) in &self.methods {
let mut status_codes_map = HashMap::new();
for (status_code, aggregator) in status_codes {
status_codes_map.insert(*status_code, aggregator.lock().get_statistics(detail));
}
responses.insert(method.clone(), status_codes_map);
}
WebApiTelemetry { responses }
}
}
impl GrpcTelemetry {
pub fn merge(&mut self, other: &GrpcTelemetry) {
for (method, other_statistics) in &other.responses {
let entry = self.responses.entry(method.clone()).or_default();
*entry = entry.clone() + other_statistics.clone();
}
}
}
impl WebApiTelemetry {
pub fn merge(&mut self, other: &WebApiTelemetry) {
for (method, status_codes) in &other.responses {
let status_codes_map = self.responses.entry(method.clone()).or_default();
for (status_code, statistics) in status_codes {
let entry = status_codes_map.entry(*status_code).or_default();
*entry = entry.clone() + statistics.clone();
}
}
}
}
#[derive(Serialize, Clone, Debug, JsonSchema)]
pub struct RequestsTelemetry {
pub rest: WebApiTelemetry,
pub grpc: GrpcTelemetry,
}
impl RequestsTelemetry {
pub fn collect(
actix_collector: &ActixTelemetryCollector,
tonic_collector: &TonicTelemetryCollector,
detail: TelemetryDetail,
) -> Self {
let rest = actix_collector.get_telemetry_data(detail);
let grpc = tonic_collector.get_telemetry_data(detail);
Self { rest, grpc }
}
}
impl Anonymize for RequestsTelemetry {
fn anonymize(&self) -> Self {
let rest = self.rest.anonymize();
let grpc = self.grpc.anonymize();
Self { rest, grpc }
}
}
impl Anonymize for WebApiTelemetry {
fn anonymize(&self) -> Self {
let responses = self
.responses
.iter()
.map(|(key, value)| {
let value: HashMap<_, _> = value
.iter()
.map(|(key, value)| (*key, value.anonymize()))
.collect();
(key.clone(), value)
})
.collect();
WebApiTelemetry { responses }
}
}
impl Anonymize for GrpcTelemetry {
fn anonymize(&self) -> Self {
let responses = self
.responses
.iter()
.map(|(key, value)| (key.clone(), value.anonymize()))
.collect();
GrpcTelemetry { responses }
}
}