Loki: improve structured metadata in devenv (#78587)

This commit is contained in:
Sven Grossmann 2023-11-23 14:40:17 +01:00 committed by GitHub
parent 4f46fb412c
commit 762c7b6fe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,12 +47,12 @@ async function sleep(duration) {
});
}
async function lokiSendLogLine(timestampNs, line, tags, nonIndexed = {}) {
async function lokiSendLogLine(timestampNs, line, tags, structuredMetadata = {}) {
const data = {
streams: [
{
stream: tags,
values: [[timestampNs, line, nonIndexed]],
values: [[timestampNs, line, structuredMetadata]],
},
],
};
@ -68,6 +68,18 @@ function getSineValue(counter, loopLength) {
return Math.sin((Math.PI * 2 * counter) / loopLength);
}
function fakeTraceId() {
let traceId = '';
const chars = 'abcdef0123456789';
const idLength = 12;
let i = 0;
while (i < idLength) {
traceId += chars.charAt(Math.floor(Math.random() * chars.length));
i += 1;
}
return traceId;
}
function getRandomLogItem(counter) {
const randomText = `${Math.trunc(Math.random() * 1000 * 1000 * 1000)}`;
const maybeAnsiText = Math.random() < 0.5 ? 'with ANSI \u001b[31mpart of the text\u001b[0m' : '';
@ -176,8 +188,8 @@ async function sendOldLogs() {
const timestampNs = `${timestampMs}${getRandomNanosecPart()}`;
globalCounter += 1;
const item = getRandomLogItem(globalCounter)
await lokiSendLogLine(timestampNs, JSON.stringify(item), {age:'old', place:'moon', ...sharedLabels});
await lokiSendLogLine(timestampNs, logFmtLine(item), {age:'old', place:'luna', ...sharedLabels});
await lokiSendLogLine(timestampNs, JSON.stringify(item), {age:'old', place:'moon', ...sharedLabels}, {structuredMetadataKey: 'value', traceId: fakeTraceId()});
await lokiSendLogLine(timestampNs, logFmtLine(item), {age:'old', place:'luna', ...sharedLabels}, {structuredMetadataKey: 'value', traceId: fakeTraceId()});
};
}
@ -187,8 +199,8 @@ async function sendNewLogs() {
const nowMs = new Date().getTime();
const timestampNs = `${nowMs}${getRandomNanosecPart()}`;
const item = getRandomLogItem(globalCounter)
await lokiSendLogLine(timestampNs, JSON.stringify(item), {age:'new', place:'moon', ...sharedLabels}, {nonIndexed: 'value'});
await lokiSendLogLine(timestampNs, logFmtLine(item), {age:'new', place:'luna', ...sharedLabels}, {nonIndexed: 'value'});
await lokiSendLogLine(timestampNs, JSON.stringify(item), {age:'new', place:'moon', ...sharedLabels}, {structuredMetadataKey: 'value', traceId: fakeTraceId()});
await lokiSendLogLine(timestampNs, logFmtLine(item), {age:'new', place:'luna', ...sharedLabels}, {structuredMetadataKey: 'value', traceId: fakeTraceId()});
const sleepDuration = 200 + Math.random() * 800; // between 0.2 and 1 seconds
await sleep(sleepDuration);
}