Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] [Dataset quality] Failure store support in synthtrace (#195726) #195781

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const indexTemplates: {
template: {
settings: {
mode: 'logsdb',
default_pipeline: 'logs@default-pipeline',
},
},
priority: 500,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { Client } from '@elastic/elasticsearch';
import { Client, estypes } from '@elastic/elasticsearch';
import { pipeline, Readable } from 'stream';
import { LogDocument } from '@kbn/apm-synthtrace-client/src/lib/logs';
import { MappingTypeMapping } from '@elastic/elasticsearch/lib/api/types';
import { IngestProcessorContainer, MappingTypeMapping } from '@elastic/elasticsearch/lib/api/types';
import { ValuesType } from 'utility-types';
import { SynthtraceEsClient, SynthtraceEsClientOptions } from '../shared/base_client';
import { getSerializeTransform } from '../shared/get_serialize_transform';
import { Logger } from '../utils/create_logger';
import { indexTemplates, IndexTemplateName } from './custom_logsdb_index_templates';
import { getRoutingTransform } from '../shared/data_stream_get_routing_transform';

export const LogsIndex = 'logs';
export const LogsCustom = 'logs@custom';

export type LogsSynthtraceEsClientOptions = Omit<SynthtraceEsClientOptions, 'pipeline'>;

export class LogsSynthtraceEsClient extends SynthtraceEsClient<LogDocument> {
Expand Down Expand Up @@ -60,6 +64,47 @@ export class LogsSynthtraceEsClient extends SynthtraceEsClient<LogDocument> {
this.logger.error(`Index creation failed: ${index} - ${err.message}`);
}
}

async updateIndexTemplate(
indexName: string,
modify: (
template: ValuesType<
estypes.IndicesGetIndexTemplateResponse['index_templates']
>['index_template']
) => estypes.IndicesPutIndexTemplateRequest
) {
try {
const response = await this.client.indices.getIndexTemplate({
name: indexName,
});

await Promise.all(
response.index_templates.map((template) => {
return this.client.indices.putIndexTemplate({
...modify(template.index_template),
name: template.name,
});
})
);

this.logger.info(`Updated ${indexName} index template`);
} catch (err) {
this.logger.error(`Update index template failed: ${indexName} - ${err.message}`);
}
}

async createCustomPipeline(processors: IngestProcessorContainer[]) {
try {
this.client.ingest.putPipeline({
id: LogsCustom,
processors,
version: 1,
});
this.logger.info(`Custom pipeline created: ${LogsCustom}`);
} catch (err) {
this.logger.error(`Custom pipeline creation failed: ${LogsCustom} - ${err.message}`);
}
}
}

function logsPipeline() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ import {
getCluster,
getCloudRegion,
getCloudProvider,
MORE_THAN_1024_CHARS,
} from './helpers/logs_mock_data';
import { parseLogsScenarioOpts } from './helpers/logs_scenario_opts_parser';

const MORE_THAN_1024_CHARS =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?';

// Logs Data logic
const MESSAGE_LOG_LEVELS = [
{ message: 'A simple log', level: 'info' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ import {
} from '@kbn/apm-synthtrace-client';
import { Scenario } from '../cli/scenario';
import { withClient } from '../lib/utils/with_client';
import { getIpAddress } from './helpers/logs_mock_data';
import { MORE_THAN_1024_CHARS, getIpAddress } from './helpers/logs_mock_data';
import { getAtIndexOrRandom } from './helpers/get_at_index_or_random';

const MORE_THAN_1024_CHARS =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?';

const MONITOR_NAMES = Array(4)
.fill(null)
.map((_, idx) => `synth-monitor-${idx}`);
Expand Down
195 changes: 195 additions & 0 deletions packages/kbn-apm-synthtrace/src/scenarios/failed_logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { LogDocument, log, generateShortId, generateLongId } from '@kbn/apm-synthtrace-client';
import { merge } from 'lodash';
import { Scenario } from '../cli/scenario';
import { IndexTemplateName } from '../lib/logs/custom_logsdb_index_templates';
import { withClient } from '../lib/utils/with_client';
import {
getServiceName,
getCluster,
getCloudRegion,
getCloudProvider,
MORE_THAN_1024_CHARS,
} from './helpers/logs_mock_data';
import { parseLogsScenarioOpts } from './helpers/logs_scenario_opts_parser';
import { LogsIndex } from '../lib/logs/logs_synthtrace_es_client';

const processors = [
{
script: {
tag: 'normalize log level',
lang: 'painless',
source: `
String level = ctx['log.level'];
if ('0'.equals(level)) {
ctx['log.level'] = 'info';
} else if ('1'.equals(level)) {
ctx['log.level'] = 'debug';
} else if ('2'.equals(level)) {
ctx['log.level'] = 'warning';
} else if ('3'.equals(level)) {
ctx['log.level'] = 'error';
} else {
throw new Exception("Not a valid log level");
}
`,
},
},
];

// Logs Data logic
const MESSAGE_LOG_LEVELS = [
{ message: 'A simple log', level: '0' },
{
message: 'Another log message',
level: '1',
},
{
message: 'A log message generated from a warning',
level: '2',
},
{ message: 'Error with certificate: "ca_trusted_fingerprint"', level: '3' },
];

const scenario: Scenario<LogDocument> = async (runOptions) => {
const { isLogsDb } = parseLogsScenarioOpts(runOptions.scenarioOpts);
return {
bootstrap: async ({ logsEsClient }) => {
await logsEsClient.createCustomPipeline(processors);
if (isLogsDb) await logsEsClient.createIndexTemplate(IndexTemplateName.LogsDb);

await logsEsClient.updateIndexTemplate(
isLogsDb ? IndexTemplateName.LogsDb : LogsIndex,
(template) => {
const next = {
name: LogsIndex,
data_stream: {
failure_store: true,
},
};

return merge({}, template, next);
}
);
},
generate: ({ range, clients: { logsEsClient } }) => {
const { logger } = runOptions;

const constructLogsCommonData = () => {
const index = Math.floor(Math.random() * 3);
const serviceName = getServiceName(index);
const logMessage = MESSAGE_LOG_LEVELS[index];
const { clusterId, clusterName } = getCluster(index);
const cloudRegion = getCloudRegion(index);

const commonLongEntryFields: LogDocument = {
'trace.id': generateShortId(),
'agent.name': 'synth-agent',
'orchestrator.cluster.name': clusterName,
'orchestrator.cluster.id': clusterId,
'orchestrator.resource.id': generateShortId(),
'cloud.provider': getCloudProvider(),
'cloud.region': cloudRegion,
'cloud.availability_zone': `${cloudRegion}a`,
'cloud.project.id': generateShortId(),
'cloud.instance.id': generateShortId(),
'log.file.path': `/logs/${generateLongId()}/error.txt`,
};

return {
index,
serviceName,
logMessage,
cloudRegion,
commonLongEntryFields,
};
};

const datasetSynth1Logs = (timestamp: number) => {
const {
serviceName,
logMessage: { level, message },
commonLongEntryFields,
} = constructLogsCommonData();

return log
.create({ isLogsDb })
.dataset('synth.1')
.message(message)
.logLevel(level)
.service(serviceName)
.defaults(commonLongEntryFields)
.timestamp(timestamp);
};

const datasetSynth2Logs = (i: number, timestamp: number) => {
const {
serviceName,
logMessage: { level, message },
commonLongEntryFields,
} = constructLogsCommonData();
const isFailed = i % 60 === 0;
return log
.create({ isLogsDb })
.dataset('synth.2')
.message(message)
.logLevel(isFailed ? '4' : level) // "script_exception": Not a valid log level
.service(serviceName)
.defaults(commonLongEntryFields)
.timestamp(timestamp);
};

const datasetSynth3Logs = (i: number, timestamp: number) => {
const {
serviceName,
logMessage: { level, message },
cloudRegion,
commonLongEntryFields,
} = constructLogsCommonData();
const isMalformed = i % 10 === 0;
const isFailed = i % 80 === 0;
return log
.create({ isLogsDb })
.dataset('synth.3')
.message(message)
.logLevel(isFailed ? '5' : level) // "script_exception": Not a valid log level
.service(serviceName)
.defaults({
...commonLongEntryFields,
'cloud.availability_zone': isMalformed
? MORE_THAN_1024_CHARS // "ignore_above": 1024 in mapping
: `${cloudRegion}a`,
})
.timestamp(timestamp);
};

const logs = range
.interval('1m')
.rate(1)
.generator((timestamp) => {
return Array(200)
.fill(0)
.flatMap((_, index) => [
datasetSynth1Logs(timestamp),
datasetSynth2Logs(index, timestamp),
datasetSynth3Logs(index, timestamp),
]);
});

return withClient(
logsEsClient,
logger.perf('generating_logs', () => logs)
);
},
};
};

export default scenario;
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const SERVICE_NAMES = Array(3)
.fill(null)
.map((_, idx) => `synth-service-${idx}`);

export const MORE_THAN_1024_CHARS =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?';

// Functions to get random elements
export const getCluster = (index?: number) => getAtIndexOrRandom(CLUSTER, index);
export const getIpAddress = (index?: number) => getAtIndexOrRandom(IP_ADDRESSES, index);
Expand Down
18 changes: 8 additions & 10 deletions packages/kbn-apm-synthtrace/src/scenarios/logs_traces_hosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
*/

import {
log,
LogDocument,
ApmFields,
InfraDocument,
apm,
Instance,
infra,
ApmFields,
LogDocument,
apm,
generateShortId,
infra,
log,
} from '@kbn/apm-synthtrace-client';
import { Scenario } from '../cli/scenario';
import { IndexTemplateName } from '../lib/logs/custom_logsdb_index_templates';
import { Logger } from '../lib/utils/create_logger';
import { withClient } from '../lib/utils/with_client';
import { getSynthtraceEnvironment } from '../lib/utils/get_synthtrace_environment';
import { withClient } from '../lib/utils/with_client';
import { MORE_THAN_1024_CHARS } from './helpers/logs_mock_data';
import { parseLogsScenarioOpts, parseStringToBoolean } from './helpers/logs_scenario_opts_parser';
import { IndexTemplateName } from '../lib/logs/custom_logsdb_index_templates';

const ENVIRONMENT = getSynthtraceEnvironment(__filename);

Expand Down Expand Up @@ -475,6 +476,3 @@ const DATASETS = [
];

const LOG_LEVELS = ['info', 'error', 'warn', 'debug'];

const MORE_THAN_1024_CHARS =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?';
16 changes: 7 additions & 9 deletions packages/kbn-apm-synthtrace/src/scenarios/simple_logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { LogDocument, log, generateShortId, generateLongId } from '@kbn/apm-synthtrace-client';
import { LogDocument, generateLongId, generateShortId, log } from '@kbn/apm-synthtrace-client';
import moment from 'moment';
import { Scenario } from '../cli/scenario';
import { IndexTemplateName } from '../lib/logs/custom_logsdb_index_templates';
import { withClient } from '../lib/utils/with_client';
import {
getServiceName,
getGeoCoordinate,
getIpAddress,
getCluster,
MORE_THAN_1024_CHARS,
getAgentName,
getCloudProvider,
getCloudRegion,
getAgentName,
getCluster,
getGeoCoordinate,
getIpAddress,
getServiceName,
} from './helpers/logs_mock_data';
import { parseLogsScenarioOpts } from './helpers/logs_scenario_opts_parser';

Expand All @@ -30,9 +31,6 @@ const MESSAGE_LOG_LEVELS = [
{ message: 'Error with certificate: "ca_trusted_fingerprint"', level: 'error' },
];

const MORE_THAN_1024_CHARS =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?';

const scenario: Scenario<LogDocument> = async (runOptions) => {
const { isLogsDb } = parseLogsScenarioOpts(runOptions.scenarioOpts);

Expand Down
Loading