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

[APM] Critical path for a single trace #143735

Merged
merged 14 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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
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]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very weird, why is it needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's basically method overloading, but in one signature. I think span(spanName) is often good enough and more concise than span({ spanName: ... }). I previously discussed this with @sqren when he changed it, and IIRC(?) we decided I would add back the original method when I would have time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it makes the signature simpler but I'd rather be without the extra complexity in synthtrace. But I don't feel strongly for it either way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'll take that back. I don't think the signature improved except for transaction() with a single arg.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the signature is more complicated but not less easy to use, which is my goal. Like you know, I disagree about span({...}) and transaction({...}) being the better interface. I do we think should add more defaults for span() though, IMHO span(spanName) is good enough too, we can default to custom/custom for span type and subtype.

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;
Comment on lines +23 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this overcomplicate this a bit? Is it really necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.


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