-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e.writer.js
434 lines (401 loc) · 14 KB
/
e2e.writer.js
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const {_it, createPoints, sendPoints, clokiWriteUrl, testID, start, end, storage, shard, clokiExtUrl, axiosPost,
extraHeaders
} = require('./common')
const protobufjs = require("protobufjs");
const path = require("path");
const {Point} = require("@influxdata/influxdb-client");
const {pushTimeseries} = require("prometheus-remote-write");
const fetch = require("node-fetch");
_it('push logs http', async () => {
console.log(testID)
let points = createPoints(testID, 0.5, start, end, {}, {})
points = createPoints(testID, 1, start, end, {}, points)
points = createPoints(testID, 2, start, end, {}, points)
points = createPoints(testID, 4, start, end, {}, points)
points = createPoints(testID + '_json', 1, start, end,
{ fmt: 'json', lbl_repl: 'val_repl', int_lbl: '1' }, points,
(i) => JSON.stringify({ lbl_repl: 'REPL', int_val: '1', new_lbl: 'new_val', str_id: i, arr: [1, 2, 3], obj: { o_1: 'v_1' } })
)
points = createPoints(testID + '_metrics', 1, start, end,
{ fmt: 'int', lbl_repl: 'val_repl', int_lbl: '1' }, points,
(i) => '',
(i) => i % 10
)
points = createPoints(testID + '_logfmt', 1, start, end,
{ fmt: 'logfmt', lbl_repl: 'val_repl', int_lbl: '1' }, points,
(i) => 'lbl_repl="REPL" int_val=1 new_lbl="new_val" str_id="' + i + '" '
)
await sendPoints(`http://${clokiWriteUrl}`, points)
await new Promise(resolve => setTimeout(resolve, 4000))
})
_it('push protobuff', async () => {
const PushRequest = protobufjs
.loadSync(path.join(__dirname, './loki.proto'))
.lookupType('PushRequest')
let points = createPoints(testID+'_PB', 0.5, start, end, {}, {})
points = {
streams: Object.values(points).map(stream => {
return {
labels: '{' + Object.entries(stream.stream).map(s => `${s[0]}=${JSON.stringify(s[1])}`).join(',') + '}',
entries: stream.values.map(v => ({
timestamp: { seconds: Math.floor(parseInt(v[0]) / 1e9).toString(), nanos: parseInt(v[0]) % 1e9 },
line: v[1]
}))
}
})
}
let body = PushRequest.encode(points).finish()
body = require('snappyjs').compress(body)
await axiosPost(`http://${clokiWriteUrl}/loki/api/v1/push`, body, {
headers: {
'Content-Type': 'application/x-protobuf',
'X-Scope-OrgID': '1',
'X-Shard': shard
}
})
await new Promise(f => setTimeout(f, 500))
})
_it('should send otlp', async () => {
const opentelemetry = require('@opentelemetry/api');
const { diag, DiagConsoleLogger, DiagLogLevel } = opentelemetry;
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
const { Resource } = require('@opentelemetry/resources');
const { ResourceAttributes: SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto');
const { ConnectInstrumentation } = require('@opentelemetry/instrumentation-connect');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const provider = new NodeTracerProvider({
resource: new Resource({
'service.name': 'testSvc',
}),
});
const connectInstrumentation = new ConnectInstrumentation();
registerInstrumentations({
tracerProvider: provider,
instrumentations: [
// Connect instrumentation expects HTTP layer to be instrumented
HttpInstrumentation,
connectInstrumentation,
],
});
const exporter = new OTLPTraceExporter({
headers: {
'X-Scope-OrgID': '1',
'X-Shard': shard,
...extraHeaders
},
url: 'http://' + clokiWriteUrl + '/v1/traces'
});
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register({});
const tracer = opentelemetry.trace.getTracer('connect-example');
const span = tracer.startSpan('test_span', {
attributes: {testId: '__TEST__'}
})
await new Promise(f => setTimeout(f, 100));
span.addEvent('test event', new Date());
span.status = {
code: 1
}
span.end();
storage.test_span = span
await new Promise(f => setTimeout(f, 2000))
})
_it('should send zipkin', async () => {
// Send Tempo data and expect status code 200
const obj = {
id: '1234ef45',
traceId: 'd6e9329d67b6146c',
timestamp: (Date.now() * 1000) + '',
duration: 1000 + '',
name: 'span from http',
tags: {
'http.method': 'GET',
'http.path': '/api'
},
localEndpoint: {
serviceName: 'node script'
}
}
const arr = [obj]
const data = JSON.stringify(arr)
console.log(data)
const url = `http://${clokiWriteUrl}/tempo/api/push`
console.log(url)
const test = await axiosPost(url, data, {
headers: {
"X-Scope-OrgID": '1',
'X-Shard': shard
}
})
expect(test.status).toEqual(200)
await new Promise(f => setTimeout(f, 500))
console.log('Tempo Insertion Successful')
})
_it('should post /tempo/spans', async () => {
// Send Tempo data and expect status code 200
const obj = {
id: '1234ef46',
traceId: 'd6e9329d67b6146d',
timestamp: Date.now() * 1000,
duration: 1000,
name: 'span from http',
tags: {
'http.method': 'GET',
'http.path': '/tempo/spans'
},
localEndpoint: {
serviceName: 'node script'
}
}
const arr = [obj]
const data = JSON.stringify(arr)
console.log(data)
const url = `http://${clokiWriteUrl}/tempo/spans`
console.log(url)
const test = await axiosPost(url, data, {
headers: {
"X-Scope-OrgID": '1',
'X-Shard': shard
}
})
expect(test.status).toEqual(200)
console.log('Tempo Insertion Successful')
})
_it('should send influx', async () => {
const {InfluxDB, Point} = require('@influxdata/influxdb-client')
const writeAPI = new InfluxDB({
url: `http://${clokiWriteUrl}/influx`,
headers: {
'X-Scope-OrgID': 1,
'X-Sender': 'influx',
'X-Shard': shard,
...extraHeaders
}
}).getWriteApi('', '', 'ns')
writeAPI.useDefaultTags({'test_id': testID + 'FLX'})
const points = []
for (let i = start; i < end; i += 60000) {
points.push(new Point('syslog')
.tag('tag1', 'val1')
.stringField('message', 'FLX_TEST')
.timestamp(new Date(i)))
}
writeAPI.writePoints(points)
await writeAPI.close()
await new Promise(f => setTimeout(f, 500))
})
_it('should send prometheus.remote.write', async () => {
const {pushTimeseries} = require('prometheus-remote-write')
const fetch = require('node-fetch')
const ts = []
for (const route of ['api/v1/prom/remote/write',
'prom/remote/write',
'api/prom/remote/write']) {
for (let i = start; i < end; i += 15000) {
ts.push({
labels: {
__name__: "test_metric",
test_id: testID + '_RWR',
route: route,
},
samples: [
{
value: 123,
timestamp: i,
},
],
})
}
const res = await pushTimeseries(ts, {
url: `http://${clokiWriteUrl}/${route}`,
fetch: (input, opts) => {
opts.headers['X-Scope-OrgID'] = '1'
opts.headers['X-Shard'] = shard
opts.headers = {
...opts.headers,
...extraHeaders
}
opts.headers['Content-Type']='application/x-protobuf'
return fetch(input, opts)
}
})
expect(res.status).toEqual(204)
await new Promise(f => setTimeout(f, 500))
}
})
_it('should /api/v2/spans', async () => {
// Send Tempo data and expect status code 200
const obj = {
id: '1234ef46',
traceId: 'd6e9329d67b6146e',
timestamp: Date.now() * 1000,
duration: 1000000,
name: 'span from http',
tags: {
'http.method': 'GET',
'http.path': '/tempo/spans'
},
localEndpoint: {
serviceName: 'node script'
}
}
const arr = [obj]
const data = JSON.stringify(arr)
console.log(data)
const url = `http://${clokiWriteUrl}/tempo/spans`
console.log(url)
const test = await axiosPost(url, data, {
headers: {
"X-Scope-OrgID": '1',
'X-Shard': shard
}
})
console.log('Tempo Insertion Successful')
})
_it('should send _ and % logs', async () => {
let points = createPoints(testID+"_like", 150, start, end, {}, {},
(i) => i % 2 ? "l_p%": "l1p2")
await sendPoints(`http://${clokiWriteUrl}`, points)
await new Promise(resolve => setTimeout(resolve, 1000))
})
_it('should write elastic', async () => {
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: `http://${clokiWriteUrl}`,
headers: {
'X-Scope-OrgID': '1',
...extraHeaders
}
})
const resp = await client.bulk({
refresh: true,
operations: [
{index: {_index: `test_${testID}`}},
{id: 1, text: 'If I fall, don\'t bring me back.', user: 'jon'},
{index: {_index: `test_${testID}`}},
{id: 2, text: 'Winter is coming', user: 'ned'},
{index: {_index: `test_${testID}`}},
{id: 3, text: 'A Lannister always pays his debts.', user: 'tyrion'},
{index: {_index: `test_${testID}`}},
{id: 4, text: 'I am the blood of the dragon.', user: 'daenerys'},
{index: {_index: `test_${testID}`}},
{id: 5, text: 'A girl is Arya Stark of Winterfell. And I\'m going home.', user: 'arya'}
]
})
expect(resp.errors).toBeFalsy()
await new Promise(resolve => setTimeout(resolve, 1000))
})
_it('should post /api/v1/labels', async () => {
const {pushTimeseries} = require('prometheus-remote-write')
let res = await pushTimeseries({
labels: {
[`${testID}_LBL`]: 'ok'
},
samples: [
{
value: 123,
timestamp: Date.now(),
},
],
}, {
url: `http://${clokiWriteUrl}/api/v1/prom/remote/write`,
fetch: (input, opts) => {
opts.headers['X-Scope-OrgID'] = '1'
opts.headers['X-Shard'] = shard
opts.headers = {
...opts.headers,
...extraHeaders
}
opts.headers['Content-Type']='application/x-protobuf'
return fetch(input, opts)
}
})
expect(res.status).toEqual(204)
await new Promise(f => setTimeout(f, 500))
res = await sendPoints(`http://${clokiWriteUrl}`, {0: {
stream: { [`${testID}_LBL_LOGS`]: 'ok' },
values: [[ `${BigInt(Date.now())*BigInt(1000000)}`, '123']]
}})
expect(Math.floor(res.status / 100)).toEqual(2)
/* TODO: POST not supported
const fd = new URLSearchParams()
await new Promise(resolve => setTimeout(resolve, 1000))
fd.append('start', `${Math.floor(Date.now() / 1000) - 10}`)
fd.append('end', `${Math.floor(Date.now() / 1000)}`)
const labels = await axiosPost(`http://${clokiExtUrl}/api/v1/labels`, fd, {
headers: {
'X-Scope-OrgID': '1',
'Content-Type': 'application/x-www-form-urlencoded',
'X-Shard': shard
}
})
expect(labels.data.data.find(d => d===`${testID}_LBL`)).toBeTruthy()
*/
})
_it('should send newrelic', async () => {
let body = {
timestamp: start,
test_id: testID + '_newrelic',
message: 'TEST NEWRELIC SINGLE'
}
const res = await axiosPost(`http://${clokiExtUrl}/log/v1`, body)
expect(res.status).toEqual(200)
})
_it('should send datadog logs', async () => {
const resp = await axiosPost(`http://${clokiWriteUrl}/api/v2/logs`, JSON.stringify([
{
"ddsource": `ddtest_${testID}`,
"ddtags": "env:staging,version:5.1",
"hostname": "i-012345678",
"message": "2019-11-19T14:37:58,995 INFO [process.name][20081] Hello World",
"service": "payment"
}
]), {
headers: {
'Content-Type': 'application/json',
'X-Scope-OrgID': '1'
}
});
expect(resp.status).toEqual(202)
await new Promise(f => setTimeout(f, 500))
})
_it('should send datadog metrics', async () => {
try {
const resp = await axiosPost(`http://${clokiWriteUrl}/api/v2/series`, JSON.stringify({
"series": [
{
"metric": `DDMetric`,
"type": 0,
"points": [
{
"timestamp": start / 1000,
"value": 0.7
}
],
"resources": [
{
"test_id": `${testID}_DDMetric`,
"name": "dummyhost",
"type": "host"
}
]
}
]
}), {
headers: {
'Content-Type': 'application/json',
'X-Scope-OrgID': '1'
}
});
expect(resp.status).toEqual(202)
await new Promise(f => setTimeout(f, 500))
} catch (e) {
console.log(JSON.stringify(e.response));
throw e;
}
})