[Synthtrace] Support Unstructured Logs (#190864)

closes [3760](https://github.com/elastic/observability-dev/issues/3760)
This commit is contained in:
mohamedhamed-ahmed 2024-08-21 13:35:44 +03:00 committed by GitHub
parent 51764fa076
commit ad4a3ff60a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 75 additions and 0 deletions

View file

@ -7,13 +7,30 @@
*/
import { generateShortId } from '@kbn/apm-synthtrace-client';
import { faker } from '@faker-js/faker';
import { randomInt } from 'crypto';
import moment from 'moment';
const {
internet: { ipv4, userAgent, httpMethod, httpStatusCode },
word: { noun, verb },
} = faker;
// Utility function to get a random element from an array
const getAtIndexOrRandom = <T>(values: T[], index?: number) =>
values[index ?? randomInt(values.length)];
// Arrays for data
const LOG_LEVELS: string[] = ['FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'];
const JAVA_LOG_MESSAGES = [
'[main] com.example1.core.ApplicationCore - Critical failure: NullPointerException encountered during startup',
'[main] com.example.service.UserService - User registration completed for userId: 12345',
'[main] com.example3.util.JsonParser - Parsing JSON response from external API',
'[main] com.example4.security.AuthManager - Unauthorized access attempt detected for userId: 67890',
'[main] com.example5.dao.UserDao - Database query failed: java.sql.SQLException: Timeout expired',
];
const IP_ADDRESSES = [
'223.72.43.22',
'20.24.184.101',
@ -50,3 +67,14 @@ export const getGeoCoordinate = (index?: number) => getAtIndexOrRandom(GEO_COORD
export const getCloudProvider = (index?: number) => getAtIndexOrRandom(CLOUD_PROVIDERS, index);
export const getCloudRegion = (index?: number) => getAtIndexOrRandom(CLOUD_REGION, index);
export const getServiceName = (index?: number) => getAtIndexOrRandom(SERVICE_NAMES, index);
export const getJavaLog = () =>
`${moment().format('YYYY-MM-DD HH:mm:ss,SSS')} ${getAtIndexOrRandom(
LOG_LEVELS
)} ${getAtIndexOrRandom(JAVA_LOG_MESSAGES)}`;
export const getWebLog = () => {
const path = `/api/${noun()}/${verb()}`;
const bytes = randomInt(100, 4000);
return `${ipv4()} - - [${moment().toISOString()}] "${httpMethod()} ${path} HTTP/1.1" ${httpStatusCode()} ${bytes} "-" "${userAgent()}"`;
};

View file

@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { LogDocument, log } from '@kbn/apm-synthtrace-client';
import { Scenario } from '../cli/scenario';
import { IndexTemplateName } from '../lib/logs/custom_logsdb_index_templates';
import { withClient } from '../lib/utils/with_client';
import { parseLogsScenarioOpts } from './helpers/logs_scenario_opts_parser';
import { getJavaLog, getWebLog } from './helpers/logs_mock_data';
const scenario: Scenario<LogDocument> = async (runOptions) => {
const { isLogsDb } = parseLogsScenarioOpts(runOptions.scenarioOpts);
return {
bootstrap: async ({ logsEsClient }) => {
if (isLogsDb) await logsEsClient.createIndexTemplate(IndexTemplateName.LogsDb);
},
generate: ({ range, clients: { logsEsClient } }) => {
const { logger } = runOptions;
const datasetJavaLogs = (timestamp: number) =>
log.create({ isLogsDb }).dataset('java').message(getJavaLog()).timestamp(timestamp);
const datasetWebLogs = (timestamp: number) =>
log.create({ isLogsDb }).dataset('web').message(getWebLog()).timestamp(timestamp);
const logs = range
.interval('1m')
.rate(1)
.generator((timestamp) => {
return Array(200)
.fill(0)
.flatMap((_, index) => [datasetJavaLogs(timestamp), datasetWebLogs(timestamp)]);
});
return withClient(
logsEsClient,
logger.perf('generating_logs', () => logs)
);
},
};
};
export default scenario;