|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import { ApmFields, apm, Instance } from '@kbn/apm-synthtrace-client'; |
| 10 | +import { flatten, random, times } from 'lodash'; |
| 11 | +import { Scenario } from '../cli/scenario'; |
| 12 | +import { getSynthtraceEnvironment } from '../lib/utils/get_synthtrace_environment'; |
| 13 | +import { withClient } from '../lib/utils/with_client'; |
| 14 | +import { getRandomNameForIndex } from './helpers/random_names'; |
| 15 | + |
| 16 | +const ENVIRONMENT = getSynthtraceEnvironment(__filename); |
| 17 | + |
| 18 | +const scenario: Scenario<ApmFields> = async ({ logger, scenarioOpts = { services: 2000 } }) => { |
| 19 | + const numServices = scenarioOpts.services; |
| 20 | + const transactionName = 'GET /order/{id}'; |
| 21 | + const languages = [ |
| 22 | + 'go', |
| 23 | + 'dotnet', |
| 24 | + 'java', |
| 25 | + 'python', |
| 26 | + 'nodejs', |
| 27 | + 'php', |
| 28 | + 'webjs', |
| 29 | + 'swift', |
| 30 | + 'android', |
| 31 | + ]; |
| 32 | + const agentVersions: Record<string, string[]> = { |
| 33 | + go: ['2.1.0', '2.0.0', '1.15.0', '1.14.0', '1.13.1'], |
| 34 | + dotnet: ['1.18.0', '1.17.0', '1.16.1', '1.16.0', '1.15.0'], |
| 35 | + java: ['1.34.1', '1.34.0', '1.33.0', '1.32.0', '1.32.0'], |
| 36 | + python: ['6.12.0', '6.11.0', '6.10.2', '6.10.1', '6.10.0'], |
| 37 | + nodejs: ['1.34.1', '1.34.0', '1.33.0', '1.32.0', '1.32.0'], |
| 38 | + php: ['1.34.1', '1.34.0', '1.33.0', '1.32.0', '1.32.0'], |
| 39 | + webjs: ['6.12.0', '6.11.0', '6.10.2', '6.10.1', '6.10.0'], |
| 40 | + swift: ['1.18.0', '1.17.0', '1.16.1', '1.16.0', '1.15.0'], |
| 41 | + android: ['6.12.0', '6.11.0', '6.10.2', '6.10.1', '6.10.0'], |
| 42 | + }; |
| 43 | + |
| 44 | + return { |
| 45 | + generate: ({ range, clients: { apmEsClient } }) => { |
| 46 | + const instances = flatten( |
| 47 | + times(numServices).map((index) => { |
| 48 | + const language = languages[index % languages.length]; |
| 49 | + const agentLanguageVersions = agentVersions[language]; |
| 50 | + const agentVersion = agentLanguageVersions[index % agentLanguageVersions.length]; |
| 51 | + |
| 52 | + const numOfInstances = (index % 3) + 1; |
| 53 | + return times(numOfInstances).map((instanceIndex) => |
| 54 | + apm |
| 55 | + .service({ |
| 56 | + name: `${getRandomNameForIndex(index)}-${language}-${index}`, |
| 57 | + environment: ENVIRONMENT, |
| 58 | + agentName: |
| 59 | + index % 2 ? `opentelemetry/${language}/elastic` : `otlp/${language}/elastic`, |
| 60 | + }) |
| 61 | + .instance(`instance-${index}-${instanceIndex}`) |
| 62 | + .defaults({ 'agent.version': agentVersion, 'service.language.name': language }) |
| 63 | + ); |
| 64 | + }) |
| 65 | + ); |
| 66 | + |
| 67 | + const instanceSpans = (instance: Instance) => { |
| 68 | + const hasHighDuration = Math.random() > 0.5; |
| 69 | + const throughput = random(1, 10); |
| 70 | + |
| 71 | + return range.ratePerMinute(throughput).generator((timestamp) => { |
| 72 | + const parentDuration = hasHighDuration ? random(1000, 5000) : random(100, 1000); |
| 73 | + const generateError = random(1, 4) % 3 === 0; |
| 74 | + const span = instance |
| 75 | + .transaction({ transactionName }) |
| 76 | + .timestamp(timestamp) |
| 77 | + .duration(parentDuration); |
| 78 | + |
| 79 | + return !generateError |
| 80 | + ? span.success() |
| 81 | + : span.failure().errors( |
| 82 | + instance |
| 83 | + .error({ |
| 84 | + message: `No handler for ${transactionName}`, |
| 85 | + type: 'No handler', |
| 86 | + culprit: 'request', |
| 87 | + }) |
| 88 | + .timestamp(timestamp + 50) |
| 89 | + ); |
| 90 | + }); |
| 91 | + }; |
| 92 | + |
| 93 | + return withClient( |
| 94 | + apmEsClient, |
| 95 | + logger.perf('generating_apm_events', () => instances.map(instanceSpans)) |
| 96 | + ); |
| 97 | + }, |
| 98 | + }; |
| 99 | +}; |
| 100 | + |
| 101 | +export default scenario; |
0 commit comments