Skip to content

Commit

Permalink
[APM] Synthtrace: Add distributed trace helper (#142593)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Oct 12, 2022
1 parent 8280532 commit 005a6ea
Show file tree
Hide file tree
Showing 8 changed files with 638 additions and 35 deletions.
5 changes: 3 additions & 2 deletions packages/kbn-apm-synthtrace/src/lib/apm/apm_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export type ApmFields = Fields &
'error.grouping_key': string;
'host.name': string;
'host.hostname': string;
'http.request.method': string;
'http.response.status_code': number;
'kubernetes.pod.uid': string;
'kubernetes.pod.name': string;
'metricset.name': string;
Expand All @@ -84,14 +86,13 @@ export type ApmFields = Fields &
'service.framework.name': string;
'service.target.name': string;
'service.target.type': string;
'span.action': string;
'span.id': string;
'span.name': string;
'span.type': string;
'span.subtype': string;
'span.duration.us': number;
'span.destination.service.name': string;
'span.destination.service.resource': string;
'span.destination.service.type': string;
'span.destination.service.response_time.sum.us': number;
'span.destination.service.response_time.count': number;
'span.self_time.count': number;
Expand Down
4 changes: 4 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 @@ -40,6 +40,10 @@ export class BaseSpan extends Serializable<ApmFields> {
return this;
}

getChildren() {
return this._children;
}

children(...children: BaseSpan[]): this {
children.forEach((child) => {
child.parent(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ export class ApmSynthtraceApmClient {

const destination = (e.context.destination = e.context.destination ?? {});
const destinationService = (destination.service = destination.service ?? { resource: '' });
set('span.destination.service.name', destinationService, (c, v) => (c.name = v));
set('span.destination.service.resource', destinationService, (c, v) => (c.resource = v));
set('span.destination.service.type', destinationService, (c, v) => (c.type = v));
}
if (e.kind === 'transaction') {
set('transaction.name', e, (c, v) => (c.name = v));
Expand Down
17 changes: 7 additions & 10 deletions packages/kbn-apm-synthtrace/src/lib/apm/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import { Span } from './span';
import { Transaction } from './transaction';
import { ApmApplicationMetricFields, ApmFields } from './apm_fields';

export type SpanParams = {
spanName: string;
spanType: string;
spanSubtype?: string;
} & ApmFields;

export class Instance extends Entity<ApmFields> {
transaction({
transactionName,
Expand All @@ -28,16 +34,7 @@ export class Instance extends Entity<ApmFields> {
});
}

span({
spanName,
spanType,
spanSubtype,
...apmFields
}: {
spanName: string;
spanType: string;
spanSubtype?: string;
} & ApmFields) {
span({ spanName, spanType, spanSubtype, ...apmFields }: SpanParams) {
return new Span({
...this.fields,
...apmFields,
Expand Down
107 changes: 86 additions & 21 deletions packages/kbn-apm-synthtrace/src/lib/apm/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import url from 'url';
import { BaseSpan } from './base_span';
import { generateShortId } from '../utils/generate_id';
import { ApmFields } from './apm_fields';
import { SpanParams } from './instance';

export class Span extends BaseSpan {
constructor(fields: ApmFields) {
Expand All @@ -25,61 +26,125 @@ export class Span extends BaseSpan {
return this;
}

destination(resource: string, type?: string, name?: string) {
if (!type) {
type = this.fields['span.type'];
}

if (!name) {
name = resource;
}
destination(resource: string) {
this.fields['span.destination.service.resource'] = resource;
this.fields['span.destination.service.name'] = name;
this.fields['span.destination.service.type'] = type;

return this;
}
}

export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'PUT';

export function httpExitSpan({
spanName,
destinationUrl,
method = 'GET',
statusCode = 200,
}: {
spanName: string;
destinationUrl: string;
}) {
method?: HttpMethod;
statusCode?: number;
}): SpanParams {
// 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';
const spanSubtype = 'http';

return {
spanName,
spanType,
spanSubType,
spanSubtype,

// http
'span.action': method,
'http.request.method': method,
'http.response.status_code': statusCode,

// destination
'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 }) {
export function dbExitSpan({ spanName, spanSubtype }: { spanName: string; spanSubtype?: string }) {
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,
spanSubtype,
'service.target.type': spanSubtype,
'span.destination.service.resource': spanSubtype,
};
}

export function elasticsearchSpan(spanName: string, statement?: string): SpanParams {
const spanType = 'db';
const spanSubtype = 'elasticsearch';

return {
spanName,
spanType,
spanSubtype,

...(statement
? {
'span.db.statement': statement,
'span.db.type': 'elasticsearch',
}
: {}),

'service.target.type': spanSubtype,
'destination.address': 'qwerty.us-west2.gcp.elastic-cloud.com',
'destination.port': 443,
'span.destination.service.resource': spanSubtype,
};
}

export function sqliteSpan(spanName: string, statement?: string): SpanParams {
const spanType = 'db';
const spanSubtype = 'sqlite';

return {
spanName,
spanType,
spanSubtype,

...(statement
? {
'span.db.statement': statement,
'span.db.type': 'sql',
}
: {}),

// destination
'service.target.type': spanSubtype,
'destination.address': 'qwerty.us-west2.gcp.elastic-cloud.com',
'destination.port': 443,
'span.destination.service.resource': spanSubtype,
};
}

export function redisSpan(spanName: string): SpanParams {
const spanType = 'db';
const spanSubtype = 'redis';

return {
spanName,
spanType,
spanSubtype,

// destination
'service.target.type': spanSubtype,
'destination.address': 'qwerty.us-west2.gcp.elastic-cloud.com',
'destination.port': 443,
'span.destination.service.resource': spanSubtype,
};
}
Loading

0 comments on commit 005a6ea

Please sign in to comment.