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

This commit is contained in:
Gábor Farkas 2023-03-10 16:09:59 +01:00 committed by GitHub
parent 9ca53d9596
commit f76d634ac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,10 +47,7 @@ async function sleep(duration) {
});
}
async function lokiSendLogLine(timestampMs, line, tags) {
// we keep nanosecond-timestamp in a string because
// as a number it would be too large
const timestampNs = `${timestampMs}000000`;
async function lokiSendLogLine(timestampNs, line, tags) {
const data = {
streams: [
{
@ -136,6 +133,24 @@ function calculateDelays(pointsCount) {
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() {
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
const delay = delays[i];
timestampMs += Math.trunc(delay * timeRange);
const timestampNs = `${timestampMs}${getRandomNanosecPart()}`;
const item = getRandomLogItem(i + 1)
await lokiSendLogLine(timestampMs, 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, JSON.stringify(item), {place:'moon', source: 'data', instance: 'server\\1', job: '"grafana/data"'});
await lokiSendLogLine(timestampNs, logFmtLine(item), {place:'luna', source: 'data', instance: 'server\\2', job: '"grafana/data"'});
};
}