-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_conditions.cc
202 lines (187 loc) · 7.1 KB
/
query_conditions.cc
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
202
#include "query_conditions.h"
std::string get_value_as_string(const ot::Span* sp,
const get_value_func val_func, const property_type prop_type) {
switch (prop_type) {
case string_value:
return (sp->*val_func.string_func)();
case bool_value: {
bool val = (sp->*val_func.bool_func)();
std::string span_val;
if (val) {
span_val = "false";
} else {
span_val = "true";
}
return span_val;
}
case int_value: {
int val = ((sp->*val_func.int_func)());
return std::to_string(val);
}
case double_value: {
double val = ((sp->*val_func.double_func)());
return std::to_string(val);
}
case bytes_value: {
std::string bytes_str = ((sp->*val_func.bytes_func)());
return hex_str(bytes_str, bytes_str.size());
}
default:
return "";
}
}
bool does_attribute_condition_hold(const ot::Span* sp, const query_condition condition) {
const opentelemetry::proto::common::v1::KeyValue* attribute;
for (int64_t i=0; i < sp->attributes_size(); i++) {
attribute = &(sp->attributes(i));
const opentelemetry::proto::common::v1::AnyValue* val = &(attribute->value());
auto curr_attr_key = attribute->key();
std::string curr_attr_val = "";
switch (val->value_case()) {
case 1:
curr_attr_val = val->string_value();
break;
case 2:
curr_attr_val = val->bool_value() ? "true" : "false";
break;
case 3:
curr_attr_val = std::to_string(val->int_value());
break;
case 4:
curr_attr_val = std::to_string(val->double_value());
break;
default:
std::cerr << "Not supported attr type." << std::endl;
exit(1);
break;
}
if (condition.property_name == curr_attr_key) {
return does_value_satisfy_condition(curr_attr_val, condition);
}
}
// couldn't find the attribute, so probably not true for this span
return false;
}
bool does_condition_hold(const ot::Span* sp, const query_condition condition) {
if (condition.is_latency_condition) {
return does_latency_condition_hold(sp, condition);
} else if (condition.is_attribute_condition) {
return does_attribute_condition_hold(sp, condition);
}
switch (condition.type) {
case string_value: {
std::cout << "In string val" << std::endl;
const std::string span_value = (sp->*condition.func.string_func)();
std::cout << "In2 string val" << std::endl;
switch (condition.comp) {
case Equal_to:
return span_value.compare(condition.node_property_value) == 0;
default:
return span_value.compare(condition.node_property_value);
}
}
case bool_value: {
const bool val = (sp->*condition.func.bool_func)();
std::string span_val;
if (val) {
span_val = "false";
} else {
span_val = "true";
}
switch (condition.comp) {
case Equal_to:
return span_val.compare(condition.node_property_value) == 0;
default:
return false; // it is undefined to be "less than" or "greater than" a bool
}
}
case int_value: {
const int span_val = (sp->*condition.func.int_func)();
const int cond_val = std::stoi(condition.node_property_value);
switch (condition.comp) {
case Equal_to: return span_val == cond_val;
case Less_than: return span_val < cond_val;
case Greater_than: return span_val > cond_val;
}
}
case double_value: {
const double span_val = (sp->*condition.func.double_func)();
const double cond_val = std::stod(condition.node_property_value);
switch (condition.comp) {
case Equal_to: return span_val == cond_val;
case Less_than: return span_val < cond_val;
case Greater_than: return span_val > cond_val;
}
}
case bytes_value: {
// for bytes, we use a string representation
const std::string span_value = (sp->*condition.func.bytes_func)();
switch (condition.comp) {
case Equal_to:
return hex_str(span_value, span_value.size()).compare(condition.node_property_value) == 0;
default:
return hex_str(span_value, span_value.size()).compare(condition.node_property_value);
}
}
}
return false;
}
/**
* TODO: There got to be a concise way to do all this stuff.. directly getting function name
* from `condition` and invoking that using `reflection` maybe???
*
*/
bool does_latency_condition_hold(const ot::Span* sp, const query_condition condition) {
const __int64_t latency = sp->end_time_unix_nano() - sp->start_time_unix_nano();
switch (condition.comp) {
case Equal_to:
return latency == std::stoul(condition.node_property_value);
case Less_than:
return latency < std::stoul(condition.node_property_value);
case Greater_than:
return latency > std::stoul(condition.node_property_value);
default:
return false;
}
return false;
}
bool does_start_time_condition_hold(const ot::Span* sp, const query_condition condition) {
auto start_time = sp->start_time_unix_nano();
switch (condition.comp) {
case Equal_to:
return start_time == std::stoul(condition.node_property_value);
case Less_than:
return start_time < std::stoul(condition.node_property_value);
case Greater_than:
return start_time > std::stoul(condition.node_property_value);
default:
return false;
}
return false;
}
bool does_end_time_condition_hold(const ot::Span* sp, const query_condition condition) {
auto end_time = sp->end_time_unix_nano();
switch (condition.comp) {
case Equal_to:
return end_time == std::stoul(condition.node_property_value);
case Less_than:
return end_time < std::stoul(condition.node_property_value);
case Greater_than:
return end_time > std::stoul(condition.node_property_value);
default:
return false;
}
return false;
}
bool does_value_satisfy_condition(std::string value, query_condition condition) {
switch (condition.comp) {
case Equal_to:
return value.compare(condition.node_property_value) == 0;
case Less_than:
return value < condition.node_property_value;
case Greater_than:
return value > condition.node_property_value;
default:
return false;
}
}