-
Notifications
You must be signed in to change notification settings - Fork 85
/
logging.js
64 lines (56 loc) · 1.87 KB
/
logging.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
const axios = require('axios');
module.exports = (serviceName, context) => {
return async (tracingObj) => {
// Tracing
const { api, tracer } = tracingObj;
const toLokiServer = async (details) => {
const { message, level, job, endpointLabel, endpoint, namespace } = details;
let error = false;
let stream = {
service_name: serviceName,
level,
job,
namespace,
};
if (endpoint) {
stream[endpointLabel] = endpoint;
}
try {
await axios.post(process.env.LOGS_TARGET, {
streams: [
{
stream,
'values': [
[ `${Date.now() * 1000000}`, message ]
]
}
]
});
} catch (err) {
console.log(`Logging error: ${err}`);
error = true;
}
return error;
};
// Logging system sends to Loki
const logEntry = async (details) => {
let logSpan;
let error = false;
if (context === 'requester') {
// Create a new span
logSpan = tracer.startSpan("log_to_loki");
}
if (process.env.LOGS_TARGET) {
error = await toLokiServer(details);
} else {
console.log(details.message);
}
if (context === 'requester') {
// Set the status code as OK and end the span
logSpan.setStatus({ code: (!error) ? api.SpanStatusCode.OK : api.SpanStatusCode.ERROR });
logSpan.end();
}
};
return logEntry;
};
}