Skip to content

Commit 1499c0f

Browse files
committed
[APM] Fix link to trace (#80993)
1 parent 580264f commit 1499c0f

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { getRedirectToTransactionDetailPageUrl } from './get_redirect_to_transaction_detail_page_url';
8+
import { parse } from 'url';
9+
10+
describe('getRedirectToTransactionDetailPageUrl', () => {
11+
const transaction = ({
12+
'@timestamp': '2020-01-01T00:01:00.000Z',
13+
service: { name: 'opbeans-node' },
14+
trace: { id: 'trace_id' },
15+
transaction: {
16+
id: 'transaction_id',
17+
name: 'transaction_name',
18+
type: 'request',
19+
duration: { us: 5000 },
20+
},
21+
} as unknown) as any;
22+
23+
const url = getRedirectToTransactionDetailPageUrl({ transaction });
24+
25+
it('rounds the start time down', () => {
26+
expect(parse(url, true).query.rangeFrom).toBe('2020-01-01T00:00:00.000Z');
27+
});
28+
29+
it('rounds the end time up', () => {
30+
expect(parse(url, true).query.rangeTo).toBe('2020-01-01T00:05:00.000Z');
31+
});
32+
33+
it('formats url correctly', () => {
34+
expect(url).toBe(
35+
'/services/opbeans-node/transactions/view?traceId=trace_id&transactionId=transaction_id&transactionName=transaction_name&transactionType=request&rangeFrom=2020-01-01T00%3A00%3A00.000Z&rangeTo=2020-01-01T00%3A05%3A00.000Z'
36+
);
37+
});
38+
});

x-pack/plugins/apm/public/components/app/TraceLink/get_redirect_to_transaction_detail_page_url.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,48 @@ export const getRedirectToTransactionDetailPageUrl = ({
1414
transaction: Transaction;
1515
rangeFrom?: string;
1616
rangeTo?: string;
17-
}) =>
18-
format({
17+
}) => {
18+
return format({
1919
pathname: `/services/${transaction.service.name}/transactions/view`,
2020
query: {
2121
traceId: transaction.trace.id,
2222
transactionId: transaction.transaction.id,
2323
transactionName: transaction.transaction.name,
2424
transactionType: transaction.transaction.type,
25-
rangeFrom,
26-
rangeTo,
25+
rangeFrom:
26+
rangeFrom ||
27+
roundToNearestMinute({
28+
timestamp: transaction['@timestamp'],
29+
direction: 'down',
30+
}),
31+
rangeTo:
32+
rangeTo ||
33+
roundToNearestMinute({
34+
timestamp: transaction['@timestamp'],
35+
diff: transaction.transaction.duration.us / 1000,
36+
direction: 'up',
37+
}),
2738
},
2839
});
40+
};
41+
42+
function roundToNearestMinute({
43+
timestamp,
44+
diff = 0,
45+
direction = 'up',
46+
}: {
47+
timestamp: string;
48+
diff?: number;
49+
direction?: 'up' | 'down';
50+
}) {
51+
const date = new Date(timestamp);
52+
const fiveMinutes = 1000 * 60 * 5; // round to 5 min
53+
54+
const ms = date.getTime() + diff;
55+
56+
return new Date(
57+
direction === 'down'
58+
? Math.floor(ms / fiveMinutes) * fiveMinutes
59+
: Math.ceil(ms / fiveMinutes) * fiveMinutes
60+
).toISOString();
61+
}

0 commit comments

Comments
 (0)