Skip to content

Commit

Permalink
[Synthtrace] Support Unstructured Logs (elastic#190864)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedhamed-ahmed authored Aug 21, 2024
1 parent 51764fa commit ad4a3ff
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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()}"`;
};
47 changes: 47 additions & 0 deletions packages/kbn-apm-synthtrace/src/scenarios/unstructured_logs.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit ad4a3ff

Please sign in to comment.