Skip to content

Commit

Permalink
[APM] Synthtrace: add dbExitSpan and httpExitSpan helpers (elasti…
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Sep 7, 2022
1 parent d5e4ece commit ed2c3e5
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/kbn-apm-synthtrace/src/lib/apm/apm_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ApmException {
message: string;
}
export interface Observer {
type: string;
version: string;
version_major: number;
}
Expand All @@ -42,6 +43,8 @@ export type ApmFields = Fields &
'agent.name': string;
'agent.version': string;
'container.id': string;
'destination.address': string;
'destination.port': number;
'ecs.version': string;
'event.outcome': string;
'event.ingested': number;
Expand Down Expand Up @@ -75,6 +78,8 @@ export type ApmFields = Fields &
'service.runtime.name': string;
'service.runtime.version': string;
'service.framework.name': string;
'service.target.name': string;
'service.target.type': string;
'span.id': string;
'span.name': string;
'span.type': string;
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-apm-synthtrace/src/lib/apm/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export class Instance extends Entity<ApmFields> {
});
}

span(spanName: string, spanType: string, spanSubtype?: string) {
span(spanName: string, spanType: string, spanSubtype?: string, apmFields?: ApmFields) {
return new Span({
...this.fields,
...apmFields,
'span.name': spanName,
'span.type': spanType,
'span.subtype': spanSubtype,
Expand Down
54 changes: 54 additions & 0 deletions packages/kbn-apm-synthtrace/src/lib/apm/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import url from 'url';
import { BaseSpan } from './base_span';
import { generateShortId } from '../utils/generate_id';
import { ApmFields } from './apm_fields';
Expand Down Expand Up @@ -39,3 +40,56 @@ export class Span extends BaseSpan {
return this;
}
}

export function httpExitSpan({
spanName,
destinationUrl,
}: {
spanName: string;
destinationUrl: string;
}): [string, string, string, ApmFields] {
// origin: 'http://opbeans-go:3000',
// host: 'opbeans-go:3000',
// hostname: 'opbeans-go',
// port: '3000',
const destination = new url.URL(destinationUrl);

const spanType = 'external';
const spanSubType = 'http';

return [
spanName,
spanType,
spanSubType,
{
'destination.address': destination.hostname,
'destination.port': parseInt(destination.port, 10),
'service.target.name': destination.host,
'span.destination.service.name': destination.origin,
'span.destination.service.resource': destination.host,
'span.destination.service.type': 'external',
},
];
}

export function dbExitSpan({
spanName,
spanSubType,
}: {
spanName: string;
spanSubType?: string;
}): [string, string, string | undefined, ApmFields] {
const spanType = 'db';

return [
spanName,
spanType,
spanSubType,
{
'service.target.type': spanSubType,
'span.destination.service.name': spanSubType,
'span.destination.service.resource': spanSubType,
'span.destination.service.type': spanType,
},
];
}
1 change: 1 addition & 0 deletions packages/kbn-apm-synthtrace/src/lib/stream_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export class StreamProcessor<TFields extends Fields = ApmFields> {
private static enrich(document: ApmFields, version: string, versionMajor: number): ApmFields {
// see https://github.com/elastic/apm-server/issues/7088 can not be provided as flat key/values
document.observer = {
type: 'synthtrace',
version: version ?? '8.2.0',
version_major: versionMajor,
};
Expand Down
93 changes: 93 additions & 0 deletions packages/kbn-apm-synthtrace/src/scenarios/distributed_trace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 { apm, timerange } from '../..';
import { ApmFields } from '../lib/apm/apm_fields';
import { Scenario } from '../cli/scenario';

import { RunOptions } from '../cli/utils/parse_run_cli_flags';
import { getSynthtraceEnvironment } from '../lib/utils/get_synthtrace_environment';
import { httpExitSpan } from '../lib/apm/span';

const ENVIRONMENT = getSynthtraceEnvironment(__filename);

const scenario: Scenario<ApmFields> = async (runOptions: RunOptions) => {
return {
generate: ({ from, to }) => {
const range = timerange(from, to);
const transactionName = '240rpm/75% 1000ms';
const successfulTimestamps = range.interval('1s').rate(3);

const opbeansRum = apm.service('opbeans-rum', ENVIRONMENT, 'rum-js').instance('my-instance');
const opbeansNode = apm
.service('opbeans-node', ENVIRONMENT, 'nodejs')
.instance('my-instance');
const opbeansGo = apm.service('opbeans-go', ENVIRONMENT, 'go').instance('my-instance');

const traces = successfulTimestamps.generator((timestamp) => {
// opbeans-rum
return opbeansRum
.transaction(transactionName)
.duration(400)
.timestamp(timestamp)
.children(
// opbeans-rum -> opbeans-node
opbeansRum
.span(
...httpExitSpan({
spanName: 'GET /api/products/top',
destinationUrl: 'http://opbeans-node:3000',
})
)
.duration(300)
.timestamp(timestamp)

.children(
// opbeans-node
opbeansNode
.transaction('Initial transaction in opbeans-node')
.duration(300)
.timestamp(timestamp)
.children(
opbeansNode
// opbeans-node -> opbeans-go
.span(
...httpExitSpan({
spanName: 'GET opbeans-go:3000',
destinationUrl: 'http://opbeans-go:3000',
})
)
.timestamp(timestamp)
.duration(400)

.children(
// opbeans-go
opbeansGo

.transaction('Initial transaction in opbeans-go')
.timestamp(timestamp)
.duration(200)
.children(
opbeansGo
.span('custom_operation', 'custom')
.timestamp(timestamp)
.duration(100)
.success()
)
)
)
)
);
});

return traces;
},
};
};

export default scenario;
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('output apm events to elasticsearch', () => {
"version": "1.4",
},
"observer": Object {
"type": "synthtrace",
"version": "8.0.0",
"version_major": 8,
},
Expand Down

0 comments on commit ed2c3e5

Please sign in to comment.