forked from lmangani/vLogQL
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vlogql.v
308 lines (275 loc) · 7.8 KB
/
vlogql.v
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
module main
import os
import json
import flag
import term
import time
import rand
import net.http
import net.websocket
import v.vmod
struct App {
mut:
counter string = '0'
last string = '0'
last_ts string = '0'
diff_ts string = '0'
api string = '0'
query string = '0'
limit int
start string = '0'
end string = '0'
labels bool
debug bool
ts bool
timer int
}
struct Response {
mut:
status string
data Data
}
struct Data {
mut:
res_type string [json: 'resultType']
result []Result
}
struct Result {
mut:
stream map[string]string
values [][]string
}
struct Values {
ts int
log string
}
fn fetch_logs(app App) {
data := http.get_text('$app.api/loki/api/v1/query_range?query=$app.query&limit=$app.limit&start=$app.start&end=$app.end')
res := json.decode(Response, data) or { exit(1) }
println('---------- Logs for: $app.query')
for row in res.data.result {
if app.labels {
print(term.gray('Log Labels: '))
print(term.bold('$row.stream\n'))
}
for log in row.values {
if app.ts {
ts_microseconds := ((log[0].i64())%1000000000)/1000
ts := time.unix2(log[0].i64()/1000000000,int(ts_microseconds))
println('${ts.format_ss_milli()}: ${log[1]}')
}
else{
println(log[1])
}
}
}
return
}
struct Labels {
status string
data []string
}
fn fetch_labels(api string, label string) {
if label.len_utf8() > 0 {
data := http.get_text('$api/loki/api/v1/label/$label/values')
res := json.decode(Labels, data) or { exit(1) }
println('---------- Values for: $label')
println(term.bold(res.data.str()))
return
} else {
data := http.get_text('$api/loki/api/v1/labels')
res := json.decode(Labels, data) or { exit(1) }
println('---------- Labels:')
println(term.bold(res.data.str()))
return
}
}
fn set_value(s string) ?string {
if s != '' {
return s
}
return none
}
fn now(diff int) string {
ts := time.utc()
subts := ts.unix_time_milli() - (diff * 1000)
return '${subts}000000'
}
fn now_iso() string {
mut timestamp := time.now().format_ss_micro().split(' ').join('T') + 'Z'
return timestamp
}
fn get_rand(len int) string {
return rand.string_from_set('abcdefghiklmnopqrestuvwzABCDEFGHIKLMNOPQRSTUVWWZX0123456789',
len)
}
struct Tail {
mut:
streams []Result
}
fn tail_logs(app App)! {
socket := app.api.replace('http', 'ws')
mut ws := websocket.new_client(socket + '/loki/api/v1/tail?query=' + app.query)!
// use on_open_ref if you want to send any reference object
ws.on_open(fn (mut ws websocket.Client)! {
println('---------- Tail Logs')
})
// use on_error_ref if you want to send any reference object
ws.on_error(fn (mut ws websocket.Client, err string)! {
eprintln('---------- Tail error: $err')
})
// use on_close_ref if you want to send any reference object
// ws.on_close(fn (mut ws websocket.Client, code int, reason string)! {
// eprintln('---------- Tail closed')
// })
// use on_message_ref if you want to send any reference object
ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, app &App)! {
if msg.payload.len > 0 {
message := msg.payload.bytestr()
res := json.decode(Tail, message) or { exit(1) }
for row in res.streams {
if app.labels {
print(term.gray('Log Labels: '))
print(term.bold('$row.stream\n'))
}
for log in row.values {
println(log[1])
}
}
}
}, app)
ws.connect() or { eprintln('error on connect: $err') }
ws.listen() or { eprintln('error on listen $err') }
unsafe {
ws.free()
}
}
fn canary_logs(mut app App, canary_string string)! {
query := '{canary="$canary_string"}'
socket := app.api.replace('http', 'ws')
mut ws := websocket.new_client(socket + '/loki/api/v1/tail?query=' + query)!
ws.on_open(fn (mut ws websocket.Client)! {
println('---------- Tail Canary Logs')
})
ws.on_error(fn (mut ws websocket.Client, err string)! {
eprintln('---------- Tail error: $err')
})
ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, mut app App)! {
if msg.payload.len > 0 {
diff_ts := now(0).i64() - app.last_ts.i64()
app.diff_ts = diff_ts.str()
message := msg.payload.bytestr()
res := json.decode(Tail, message) or { exit(1) }
for row in res.streams {
if app.labels {
print(term.gray('Log Labels: '))
print(term.bold('$row.stream\n'))
}
for log in row.values {
println(log[1])
if log[1] != app.last {
eprintln('>>>>>>>>>> Out of order log! log=$app.last')
}
if log[0] != app.last_ts {
eprintln('>>>>>>>>>> Out of order timestamp! last_ts=$app.last_ts')
}
if app.diff_ts.len > 9 {
eprintln('>>>>>>>>>> High latency! diff_ts=$app.diff_ts')
}
}
}
}
}, app)
ws.connect() or { eprintln('error on connect: $err') }
ws.listen() or { eprintln('error on listen $err') }
unsafe {
ws.free()
}
}
fn canary_emitter(mut app App, canary_string string, timer int, count int)! {
labels := '{"canary":"$canary_string","type":"canary"}'
timestamp := now(0)
mut log := 'ts=$timestamp count=$count type=canary tag=$canary_string delay=$app.diff_ts'
payload := '{"streams":[{"stream": $labels, "values":[ ["$timestamp", "$log"] ]}]}'
data := http.post_json('$app.api/loki/api/v1/push', payload) or { exit(1) }
if data.status_code != 204 {
eprintln('PUSH error: $data.status_code')
} else {
// println('PUSH successful: $data.status_code')
}
next := count + 1
app.counter = (app.counter.int() + 1).str()
app.last = log
app.last_ts = timestamp
// println('Sleeping for $timer seconds...')
time.sleep(timer * time.second)
go canary_emitter(mut app, canary_string, timer, next)
}
fn main() {
mut app := &App{}
app.counter = (0).str()
mut fp := flag.new_flag_parser(os.args)
vm := vmod.decode(@VMOD_FILE) or { panic(err.msg()) }
fp.application('$vm.name')
fp.description('$vm.description')
fp.version('$vm.version')
fp.skip_executable()
env_limit := set_value(os.getenv('LOGQL_LIMIT')) or { '5' }
logql_limit := fp.int('limit', `l`, env_limit.int(), 'logql query limit [LOGQL_LIMIT]')
app.limit = logql_limit
env_api := set_value(os.getenv('LOGQL_API')) or { 'http://localhost:3100' }
logql_api := fp.string('api', `a`, env_api, 'logql api [LOGQL_API]')
app.api = logql_api
env_query := set_value(os.getenv('LOGQL_QUERY')) or { '' }
logql_query := fp.string('query', `q`, env_query, 'logql query [LOGQL_QUERY]')
app.query = logql_query
logql_labels := fp.bool('labels', `t`, false, 'get labels')
logql_label := fp.string('label', `v`, '', 'get label values')
app.labels = logql_labels
logql_timestamp := fp.bool('timestamp', `z`, false, 'get timestamp')
app.ts = logql_timestamp
logql_start := fp.string('start', `s`, now(3600), 'start nanosec timestamp')
logql_end := fp.string('end', `e`, now(0), 'end nanosec timestamp')
app.start = logql_start
app.end = logql_end
logql_tail := fp.bool('tail', `x`, false, 'tail mode')
logql_canary := fp.bool('canary', `c`, false, 'canary mode')
env_canary_label := set_value(os.getenv('CANARY_LABEL')) or { '' }
env_canary_timer := set_value(os.getenv('CANARY_TIMER')) or { '10' }
fp.finalize() or {
eprintln(err)
println(fp.usage())
return
}
if logql_query.len_utf8() > 0 {
if logql_tail {
tail_logs(app) or { exit(1) }
} else if logql_canary {
mut tag := env_canary_label
tag = get_rand(12)
tag = 'canary_' + tag
go canary_emitter(mut app, tag, env_canary_timer.int(), 0)
canary_logs(mut app, tag) or { exit(1) }
} else {
fetch_logs(app)
}
return
} else if logql_canary {
mut tag := env_canary_label
if env_canary_label.len < 1 {
tag = get_rand(12)
}
tag = 'canary_' + tag
go canary_emitter(mut app, tag, env_canary_timer.int(), 0)
canary_logs(mut app, tag) or { exit(1) }
} else if logql_labels {
fetch_labels(logql_api, '')
return
} else if logql_label.len_utf8() > 0 {
fetch_labels(logql_api, logql_label)
return
} else {
println(fp.usage())
return
}
}