From 52c775368b127400e79f57c8639c4309cbd0db73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Farkas?= Date: Thu, 2 Jun 2022 10:23:26 +0200 Subject: [PATCH] devenv: loki: better fake logs (#50024) * devenv: loki: more log-levels * devenv: loki: logfmt mode --- devenv/docker/blocks/loki/data/data.js | 52 +++++++++++++++++++++----- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/devenv/docker/blocks/loki/data/data.js b/devenv/docker/blocks/loki/data/data.js index 38f0d9c2525..72714a93798 100644 --- a/devenv/docker/blocks/loki/data/data.js +++ b/devenv/docker/blocks/loki/data/data.js @@ -66,16 +66,51 @@ async function lokiSendLogLine(timestampMs, line, tags) { await jsonRequest(data, 'POST', url, 204); } -function getRandomLogLine(counter) { +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' : ''; - return JSON.stringify({ + return { _entry: `log text ${maybeAnsiText} [${randomText}]`, counter: counter.toString(), - float: Math.random() > 0.2 ? (100 * Math.random()).toString() : 'NaN', + float: Math.random() > 0.2 ? (Math.trunc(100000 * Math.random())/1000).toString() : 'NaN', label: chooseRandomElement(['val1', 'val2', 'val3']), - level: chooseRandomElement(['info', 'info', 'error']), + level: chooseRandomElement(['debug','info', 'info', 'info', 'info', 'warning', 'error', 'error']), + }; +} + +function getRandomJSONLogLine(counter) { + const item = getRandomLogItem(counter) + return JSON.stringify(item) +} + +const logFmtProblemRe = /[="\n]/; + +// we are not really escaping things, we just check +// that we don't need to escape :-) +function escapeLogFmtKey(key) { + if (logFmtProblemRe.test(key)) { + throw new Error(`invalid logfmt-key: ${key}`) + } + return key; +} + +function escapeLogFmtValue(value) { + if (logFmtProblemRe.test(value)) { + throw new Error(`invalid logfmt-value: ${key}`) + } + + // we must handle the space-character because we have values with spaces :-( + return value.indexOf(' ') === -1 ? value : `"${value}"` +} + +function logFmtLine(item) { + const parts = Object.entries(item).map(([k,v]) => { + const key = escapeLogFmtKey(k.toString()); + const value = escapeLogFmtValue(v.toString()); + return `${key}=${value}`; }); + + return parts.join(' '); } const SLEEP_ANGLE_STEP = Math.PI / 200; @@ -86,15 +121,12 @@ function getNextSineWaveSleepDuration() { } async function main() { - const tags = { - place: 'moon', - }; - for (let step = 0; step < 300; step++) { await sleep(getNextSineWaveSleepDuration()); const timestampMs = new Date().getTime(); - const line = getRandomLogLine(step + 1); - lokiSendLogLine(timestampMs, line, tags); + const item = getRandomLogItem(step + 1) + lokiSendLogLine(timestampMs, JSON.stringify(item), {place:'moon'}); + lokiSendLogLine(timestampMs, logFmtLine(item), {place:'luna'}); } }