devenv: loki: generate timestamps with various precision (#64509)

pull/64611/head^2
Gábor Farkas 2 years ago committed by GitHub
parent 9ca53d9596
commit f76d634ac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      devenv/docker/blocks/loki/data/data.js

@ -47,10 +47,7 @@ async function sleep(duration) {
}); });
} }
async function lokiSendLogLine(timestampMs, line, tags) { async function lokiSendLogLine(timestampNs, line, tags) {
// we keep nanosecond-timestamp in a string because
// as a number it would be too large
const timestampNs = `${timestampMs}000000`;
const data = { const data = {
streams: [ streams: [
{ {
@ -136,6 +133,24 @@ function calculateDelays(pointsCount) {
return delays; return delays;
} }
function getRandomNanosecPart() {
// we want to have cases with milliseconds-only, with microsec and nanosec.
const mode = Math.random();
if (mode < 0.333) {
// only milisec precision
return '000000';
}
if (mode < 0.666) {
// microsec precision
return Math.trunc(Math.random()*1000).toString().padStart(3, '0') + '000'
}
// nanosec precision
return Math.trunc(Math.random()*1000000).toString().padStart(6, '0')
}
async function main() { async function main() {
const delays = calculateDelays(DAYS * POINTS_PER_DAY); const delays = calculateDelays(DAYS * POINTS_PER_DAY);
@ -144,9 +159,10 @@ async function main() {
for(let i =0; i < delays.length; i++ ) { // i cannot do a forEach because of the `await` inside for(let i =0; i < delays.length; i++ ) { // i cannot do a forEach because of the `await` inside
const delay = delays[i]; const delay = delays[i];
timestampMs += Math.trunc(delay * timeRange); timestampMs += Math.trunc(delay * timeRange);
const timestampNs = `${timestampMs}${getRandomNanosecPart()}`;
const item = getRandomLogItem(i + 1) const item = getRandomLogItem(i + 1)
await lokiSendLogLine(timestampMs, JSON.stringify(item), {place:'moon', source: 'data', instance: 'server\\1', job: '"grafana/data"'}); await lokiSendLogLine(timestampNs, JSON.stringify(item), {place:'moon', source: 'data', instance: 'server\\1', job: '"grafana/data"'});
await lokiSendLogLine(timestampMs, logFmtLine(item), {place:'luna', source: 'data', instance: 'server\\2', job: '"grafana/data"'}); await lokiSendLogLine(timestampNs, logFmtLine(item), {place:'luna', source: 'data', instance: 'server\\2', job: '"grafana/data"'});
}; };
} }

Loading…
Cancel
Save