-
Notifications
You must be signed in to change notification settings - Fork 28
/
06-downstream-spans.sql
224 lines (219 loc) · 5.26 KB
/
06-downstream-spans.sql
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
-- nodes
WITH RECURSIVE x AS
(
SELECT
trace_id,
span_id,
parent_span_id,
service_name,
span_name
FROM ps_trace.span
WHERE $__timeFilter(start_time)
AND service_name = '${service}'
AND span_name = '${operation}'
UNION ALL
SELECT
s.trace_id,
s.span_id,
s.parent_span_id,
s.service_name,
s.span_name
FROM x
INNER JOIN ps_trace.span s
ON (x.trace_id = s.trace_id
AND x.span_id = s.parent_span_id)
)
SELECT
md5(service_name || '-' || span_name) as id,
span_name as title,
service_name as "subTitle",
count(*) as "mainStat"
FROM x
GROUP BY service_name, span_name
;
-- edges
WITH RECURSIVE x AS
(
SELECT
trace_id,
span_id,
parent_span_id,
service_name,
span_name,
null::text as id,
null::text as source,
null::text as target
FROM ps_trace.span
WHERE $__timeFilter(start_time)
AND service_name = '${service}'
AND span_name = '${operation}'
UNION ALL
SELECT
s.trace_id,
s.span_id,
s.parent_span_id,
s.service_name,
s.span_name,
md5(s.service_name || '-' || s.span_name || '-' || x.service_name || '-' || x.span_name) as id,
md5(x.service_name || '-' || x.span_name) as source,
md5(s.service_name || '-' || s.span_name) as target
FROM x
INNER JOIN ps_trace.span s
ON (x.trace_id = s.trace_id
AND x.span_id = s.parent_span_id)
)
SELECT DISTINCT
x.id,
x.source,
x.target
FROM x
WHERE id is not null
;
-- pie chart
WITH RECURSIVE x AS
(
SELECT
s.trace_id,
s.span_id,
s.parent_span_id,
s.service_name,
s.span_name,
s.duration_ms - coalesce(
(
SELECT sum(z.duration_ms)
FROM ps_trace.span z
WHERE s.trace_id = z.trace_id
AND s.span_id = z.parent_span_id
), 0.0) as duration_ms
FROM ps_trace.span s
WHERE $__timeFilter(s.start_time)
AND s.service_name = '${service}'
AND s.span_name = '${operation}'
UNION ALL
SELECT
s.trace_id,
s.span_id,
s.parent_span_id,
s.service_name,
s.span_name,
s.duration_ms - coalesce(
(
SELECT sum(z.duration_ms)
FROM ps_trace.span z
WHERE s.trace_id = z.trace_id
AND s.span_id = z.parent_span_id
), 0.0) as duration_ms
FROM x
INNER JOIN ps_trace.span s
ON (x.trace_id = s.trace_id
AND x.span_id = s.parent_span_id)
)
SELECT
service_name,
span_name,
sum(duration_ms) as total_exec_time
FROM x
GROUP BY 1, 2
ORDER BY 3 DESC
;
-- time series graph
WITH RECURSIVE x AS
(
SELECT
time_bucket('15 seconds', s.start_time) as time,
s.trace_id,
s.span_id,
s.parent_span_id,
s.service_name,
s.span_name,
s.duration_ms - coalesce(
(
SELECT sum(z.duration_ms)
FROM ps_trace.span z
WHERE s.trace_id = z.trace_id
AND s.span_id = z.parent_span_id
), 0.0) as duration_ms
FROM ps_trace.span s
WHERE $__timeFilter(s.start_time)
AND s.service_name = '${service}'
AND s.span_name = '${operation}'
UNION ALL
SELECT
time_bucket('15 seconds', s.start_time) as time,
s.trace_id,
s.span_id,
s.parent_span_id,
s.service_name,
s.span_name,
s.duration_ms - coalesce(
(
SELECT sum(z.duration_ms)
FROM ps_trace.span z
WHERE s.trace_id = z.trace_id
AND s.span_id = z.parent_span_id
), 0.0) as duration_ms
FROM x
INNER JOIN ps_trace.span s
ON (x.trace_id = s.trace_id
AND x.span_id = s.parent_span_id)
)
SELECT
time,
service_name || ' ' || span_name as series,
sum(duration_ms) as exec_ms
FROM x
GROUP BY 1, 2
ORDER BY 1
;
-- table
WITH RECURSIVE x AS
(
SELECT
s.trace_id,
s.span_id,
s.parent_span_id,
s.service_name,
s.span_name,
s.duration_ms - coalesce(
(
SELECT sum(z.duration_ms)
FROM ps_trace.span z
WHERE s.trace_id = z.trace_id
AND s.span_id = z.parent_span_id
), 0.0) as duration_ms,
s.status_code
FROM ps_trace.span s
WHERE $__timeFilter(s.start_time)
AND s.service_name = '${service}'
AND s.span_name = '${operation}'
UNION ALL
SELECT
s.trace_id,
s.span_id,
s.parent_span_id,
s.service_name,
s.span_name,
s.duration_ms - coalesce(
(
SELECT sum(z.duration_ms)
FROM ps_trace.span z
WHERE s.trace_id = z.trace_id
AND s.span_id = z.parent_span_id
), 0.0) as duration_ms,
s.status_code
FROM x
INNER JOIN ps_trace.span s
ON (x.trace_id = s.trace_id
AND x.span_id = s.parent_span_id)
)
SELECT
service_name,
span_name as operation,
sum(duration_ms) as total_exec_time,
approx_percentile(0.5, percentile_agg(duration_ms)) as p50,
approx_percentile(0.95, percentile_agg(duration_ms)) as p95,
approx_percentile(0.99, percentile_agg(duration_ms)) as p99
FROM x
GROUP BY 1, 2
ORDER BY 3 DESC
;