Skip to content

Commit

Permalink
[APM] Critical path for a single trace (#143735)
Browse files Browse the repository at this point in the history
* [APM] Critical path for a single trace

* Add tech preview badge

* Update synthtrace tests

* Add new setting to mapping

* Make sure timestamp.us is set for error events as well
  • Loading branch information
dgieselaar authored Oct 27, 2022
1 parent 7a7b031 commit 4bd8693
Show file tree
Hide file tree
Showing 38 changed files with 779 additions and 39 deletions.
1 change: 1 addition & 0 deletions packages/kbn-apm-synthtrace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

export { timerange } from './src/lib/timerange';
export { apm } from './src/lib/apm';
export { dedot } from './src/lib/utils/dedot';
export { stackMonitoring } from './src/lib/stack_monitoring';
export { observer } from './src/lib/agent_config';
export { cleanWriteTargets } from './src/lib/utils/clean_write_targets';
Expand Down
6 changes: 6 additions & 0 deletions packages/kbn-apm-synthtrace/src/lib/apm/apm_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ export class ApmError extends Serializable<ApmFields> {
);
return [data];
}

timestamp(value: number) {
const ret = super.timestamp(value);
this.fields['timestamp.us'] = value * 1000;
return ret;
}
}
6 changes: 6 additions & 0 deletions packages/kbn-apm-synthtrace/src/lib/apm/base_span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@ export class BaseSpan extends Serializable<ApmFields> {
});
return this;
}

override timestamp(timestamp: number) {
const ret = super.timestamp(timestamp);
this.fields['timestamp.us'] = timestamp * 1000;
return ret;
}
}
45 changes: 35 additions & 10 deletions packages/kbn-apm-synthtrace/src/lib/apm/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,49 @@ export type SpanParams = {
} & ApmFields;

export class Instance extends Entity<ApmFields> {
transaction({
transactionName,
transactionType = 'request',
}: {
transactionName: string;
transactionType?: string;
}) {
transaction(
...options:
| [{ transactionName: string; transactionType?: string }]
| [string]
| [string, string]
) {
let transactionName: string;
let transactionType: string | undefined;
if (options.length === 2) {
transactionName = options[0];
transactionType = options[1];
} else if (typeof options[0] === 'string') {
transactionName = options[0];
} else {
transactionName = options[0].transactionName;
transactionType = options[0].transactionType;
}

return new Transaction({
...this.fields,
'transaction.name': transactionName,
'transaction.type': transactionType,
'transaction.type': transactionType || 'request',
});
}

span({ spanName, spanType, spanSubtype, ...apmFields }: SpanParams) {
span(...options: [string, string] | [string, string, string] | [SpanParams]) {
let spanName: string;
let spanType: string;
let spanSubtype: string;
let fields: ApmFields;

if (options.length === 3 || options.length === 2) {
spanName = options[0];
spanType = options[1];
spanSubtype = options[2] || 'unknown';
fields = {};
} else {
({ spanName, spanType, spanSubtype = 'unknown', ...fields } = options[0]);
}

return new Span({
...this.fields,
...apmFields,
...fields,
'span.name': spanName,
'span.type': spanType,
'span.subtype': spanSubtype,
Expand Down
21 changes: 11 additions & 10 deletions packages/kbn-apm-synthtrace/src/lib/apm/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ export class Service extends Entity<ApmFields> {
}
}

export function service({
name,
environment,
agentName,
}: {
name: string;
environment: string;
agentName: string;
}) {
export function service(name: string, environment: string, agentName: string): Service;

export function service(options: { name: string; environment: string; agentName: string }): Service;

export function service(
...args: [{ name: string; environment: string; agentName: string }] | [string, string, string]
) {
const [serviceName, environment, agentName] =
args.length === 1 ? [args[0].name, args[0].environment, args[0].agentName] : args;

return new Service({
'service.name': name,
'service.name': serviceName,
'service.environment': environment,
'agent.name': agentName,
});
Expand Down
5 changes: 1 addition & 4 deletions packages/kbn-apm-synthtrace/src/lib/stream_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ export class StreamProcessor<TFields extends Fields = ApmFields> {
document['service.node.name'] =
document['service.node.name'] || document['container.id'] || document['host.name'];
document['ecs.version'] = '1.4';
// TODO this non standard field should not be enriched here
if (document['processor.event'] !== 'metric') {
document['timestamp.us'] = document['@timestamp']! * 1000;
}

return document;
}

Expand Down
1 change: 1 addition & 0 deletions packages/kbn-apm-synthtrace/src/lib/utils/dedot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export function dedot(source: Record<string, any>, target: Record<string, any>)
const val = source[key as keyof typeof source];
set(target, key, val);
}
return target;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ describe('output apm events to elasticsearch', () => {
"name": "instance-a",
},
},
"timestamp": Object {
"us": 1609455600000000,
},
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('simple trace', () => {
'service.environment': 'production',
'service.name': 'opbeans-java',
'service.node.name': 'instance-1',
'timestamp.us': 1609459200000000,
'trace.id': '00000000000000000000000000000241',
'transaction.duration.us': 1000000,
'transaction.id': '0000000000000240',
Expand Down Expand Up @@ -113,6 +114,7 @@ describe('simple trace', () => {
'span.name': 'GET apm-*/_search',
'span.subtype': 'elasticsearch',
'span.type': 'db',
'timestamp.us': 1609459200050000,
'trace.id': '00000000000000000000000000000301',
'transaction.id': '0000000000000300',
});
Expand Down
Loading

0 comments on commit 4bd8693

Please sign in to comment.