-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[APM] Generate breakdown metrics #114390
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
Merged
dgieselaar
merged 6 commits into
elastic:master
from
dgieselaar:generate-breakdown-metrics
Oct 13, 2021
Merged
[APM] Generate breakdown metrics #114390
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f7ca882
[APM] Generate breakdown metrics
dgieselaar 042ad7b
Merge branch 'master' of github.com:elastic/kibana into generate-brea…
dgieselaar 47f0683
Merge branch 'master' of github.com:elastic/kibana into generate-brea…
dgieselaar f1ec09c
Add API test for instance statistics
dgieselaar a3b6c7d
Merge branch 'master' of github.com:elastic/kibana into generate-brea…
dgieselaar a530b5b
Merge branch 'master' into generate-breakdown-metrics
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * 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 moment from 'moment'; | ||
| import { pickBy } from 'lodash'; | ||
| import objectHash from 'object-hash'; | ||
| import { Fields } from '../entity'; | ||
| import { createPicker } from './create_picker'; | ||
|
|
||
| export function aggregate(events: Fields[], fields: string[]) { | ||
| const picker = createPicker(fields); | ||
|
|
||
| const metricsets = new Map<string, { key: Fields; events: Fields[] }>(); | ||
|
|
||
| function getMetricsetKey(span: Fields) { | ||
| const timestamp = moment(span['@timestamp']).valueOf(); | ||
| return { | ||
| '@timestamp': timestamp - (timestamp % (60 * 1000)), | ||
| ...pickBy(span, picker), | ||
| }; | ||
| } | ||
|
|
||
| for (const event of events) { | ||
| const key = getMetricsetKey(event); | ||
| const id = objectHash(key); | ||
|
|
||
| let metricset = metricsets.get(id); | ||
|
|
||
| if (!metricset) { | ||
| metricset = { | ||
| key: { ...key, 'processor.event': 'metric', 'processor.name': 'metric' }, | ||
| events: [], | ||
| }; | ||
| metricsets.set(id, metricset); | ||
| } | ||
|
|
||
| metricset.events.push(event); | ||
| } | ||
|
|
||
| return Array.from(metricsets.values()); | ||
| } |
16 changes: 16 additions & 0 deletions
16
packages/elastic-apm-generator/src/lib/utils/create_picker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| export function createPicker(fields: string[]) { | ||
| const wildcards = fields | ||
| .filter((field) => field.endsWith('.*')) | ||
| .map((field) => field.replace('*', '')); | ||
|
|
||
| return (value: unknown, key: string) => { | ||
| return fields.includes(key) || wildcards.some((field) => key.startsWith(field)); | ||
| }; | ||
|
sorenlouv marked this conversation as resolved.
|
||
| } | ||
145 changes: 145 additions & 0 deletions
145
packages/elastic-apm-generator/src/lib/utils/get_breakdown_metrics.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * 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 objectHash from 'object-hash'; | ||
| import { groupBy, pickBy } from 'lodash'; | ||
| import { Fields } from '../entity'; | ||
| import { createPicker } from './create_picker'; | ||
|
|
||
| const instanceFields = [ | ||
| 'container.*', | ||
| 'kubernetes.*', | ||
| 'agent.*', | ||
| 'process.*', | ||
| 'cloud.*', | ||
| 'service.*', | ||
| 'host.*', | ||
| ]; | ||
|
|
||
| const instancePicker = createPicker(instanceFields); | ||
|
|
||
| const metricsetPicker = createPicker([ | ||
| 'transaction.type', | ||
| 'transaction.name', | ||
| 'span.type', | ||
| 'span.subtype', | ||
| ]); | ||
|
|
||
| export function getBreakdownMetrics(events: Fields[]) { | ||
| const txWithSpans = groupBy( | ||
| events.filter( | ||
| (event) => event['processor.event'] === 'span' || event['processor.event'] === 'transaction' | ||
| ), | ||
| (event) => event['transaction.id'] | ||
| ); | ||
|
|
||
| const metricsets: Map<string, Fields> = new Map(); | ||
|
|
||
| Object.keys(txWithSpans).forEach((transactionId) => { | ||
| const txEvents = txWithSpans[transactionId]; | ||
| const transaction = txEvents.find((event) => event['processor.event'] === 'transaction')!; | ||
|
|
||
| const eventsById: Record<string, Fields> = {}; | ||
| const activityByParentId: Record<string, Array<{ from: number; to: number }>> = {}; | ||
| for (const event of txEvents) { | ||
| const id = | ||
| event['processor.event'] === 'transaction' ? event['transaction.id'] : event['span.id']; | ||
| eventsById[id!] = event; | ||
|
|
||
| const parentId = event['parent.id']; | ||
|
|
||
| if (!parentId) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!activityByParentId[parentId]) { | ||
| activityByParentId[parentId] = []; | ||
| } | ||
|
|
||
| const from = event['@timestamp']! * 1000; | ||
| const to = | ||
| from + | ||
| (event['processor.event'] === 'transaction' | ||
| ? event['transaction.duration.us']! | ||
| : event['span.duration.us']!); | ||
|
|
||
| activityByParentId[parentId].push({ from, to }); | ||
| } | ||
|
|
||
| // eslint-disable-next-line guard-for-in | ||
| for (const id in eventsById) { | ||
| const event = eventsById[id]; | ||
| const activities = activityByParentId[id] || []; | ||
|
|
||
| const timeStart = event['@timestamp']! * 1000; | ||
|
|
||
| let selfTime = 0; | ||
| let lastMeasurement = timeStart; | ||
| const changeTimestamps = [ | ||
| ...new Set([ | ||
| timeStart, | ||
| ...activities.flatMap((activity) => [activity.from, activity.to]), | ||
| timeStart + | ||
| (event['processor.event'] === 'transaction' | ||
| ? event['transaction.duration.us']! | ||
| : event['span.duration.us']!), | ||
| ]), | ||
| ]; | ||
|
|
||
| for (const timestamp of changeTimestamps) { | ||
| const hasActiveChildren = activities.some( | ||
| (activity) => activity.from < timestamp && activity.to >= timestamp | ||
| ); | ||
|
|
||
| if (!hasActiveChildren) { | ||
| selfTime += timestamp - lastMeasurement; | ||
| } | ||
|
|
||
| lastMeasurement = timestamp; | ||
| } | ||
|
|
||
| const key = { | ||
| '@timestamp': event['@timestamp']! - (event['@timestamp']! % (30 * 1000)), | ||
| 'transaction.type': transaction['transaction.type'], | ||
| 'transaction.name': transaction['transaction.name'], | ||
| ...pickBy(event, metricsetPicker), | ||
| }; | ||
|
|
||
| const instance = pickBy(event, instancePicker); | ||
|
|
||
| const metricsetId = objectHash(key); | ||
|
|
||
| let metricset = metricsets.get(metricsetId); | ||
|
|
||
| if (!metricset) { | ||
| metricset = { | ||
| ...key, | ||
| ...instance, | ||
| 'processor.event': 'metric', | ||
| 'processor.name': 'metric', | ||
| 'metricset.name': `span_breakdown`, | ||
| 'span.self_time.count': 0, | ||
| 'span.self_time.sum.us': 0, | ||
| }; | ||
|
|
||
| if (event['processor.event'] === 'transaction') { | ||
| metricset['span.type'] = 'app'; | ||
| } else { | ||
| metricset['span.type'] = event['span.type']; | ||
| metricset['span.subtype'] = event['span.subtype']; | ||
| } | ||
|
|
||
| metricsets.set(metricsetId, metricset); | ||
| } | ||
|
|
||
| metricset['span.self_time.count']!++; | ||
| metricset['span.self_time.sum.us']! += selfTime; | ||
| } | ||
| }); | ||
|
|
||
| return Array.from(metricsets.values()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.