-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add metric summaries to spans #10432
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
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a351f91
feat(core): Add metric summaries to spans
timfish 9f4fdd7
Merge branch 'develop' into feat/metric-summaries
timfish 1d5fbd8
Fix lint
timfish 8832b70
Merge remote-tracking branch 'origin/feat/metric-summaries' into feat…
timfish 25dcfe6
Don't add to bundle size and fix all issues
timfish 9c4f3c1
Don't rename MetricSummary on export
timfish 286239b
Merge remote-tracking branch 'upstream/develop' into feat/metric-summ…
timfish 8b16e4d
minor
timfish d3f52df
correctly handle set metrics
timfish 1a485c2
Fix issues!
timfish 1e8fed7
Test all types
timfish 42aafac
Merge branch 'develop' into feat/metric-summaries
timfish 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
38 changes: 38 additions & 0 deletions
38
dev-packages/node-integration-tests/suites/tracing/metric-summaries/scenario.js
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,38 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
_experiments: { | ||
metricsAggregator: true, | ||
}, | ||
}); | ||
|
||
// Stop the process from exiting before the transaction is sent | ||
setInterval(() => {}, 1000); | ||
|
||
Sentry.startSpan( | ||
{ | ||
name: 'Test Transaction', | ||
op: 'transaction', | ||
}, | ||
() => { | ||
Sentry.metrics.increment('root-counter'); | ||
Sentry.metrics.increment('root-counter'); | ||
|
||
Sentry.startSpan( | ||
{ | ||
name: 'Some other span', | ||
op: 'transaction', | ||
}, | ||
() => { | ||
Sentry.metrics.increment('root-counter'); | ||
Sentry.metrics.increment('root-counter'); | ||
Sentry.metrics.increment('root-counter', 2); | ||
}, | ||
); | ||
}, | ||
); |
39 changes: 39 additions & 0 deletions
39
dev-packages/node-integration-tests/suites/tracing/metric-summaries/test.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,39 @@ | ||
import { createRunner } from '../../../utils/runner'; | ||
|
||
const EXPECTED_TRANSACTION = { | ||
transaction: 'Test Transaction', | ||
_metrics_summary: { | ||
'c:root-counter@none': { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs to be an array of metrics, based on what tags it has. |
||
min: 1, | ||
max: 1, | ||
count: 2, | ||
sum: 2, | ||
tags: { | ||
release: '1.0', | ||
transaction: 'Test Transaction', | ||
}, | ||
}, | ||
}, | ||
spans: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
description: 'Some other span', | ||
op: 'transaction', | ||
_metrics_summary: { | ||
'c:root-counter@none': { | ||
min: 1, | ||
max: 2, | ||
count: 3, | ||
sum: 4, | ||
tags: { | ||
release: '1.0', | ||
transaction: 'Test Transaction', | ||
}, | ||
}, | ||
}, | ||
}), | ||
]), | ||
}; | ||
|
||
test('Should add metric summaries to spans', done => { | ||
createRunner(__dirname, 'scenario.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done); | ||
}); |
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,88 @@ | ||
import type { MeasurementUnit, MetricSummaryAggregator as MetricSummaryAggregatorInterface } from '@sentry/types'; | ||
import type { MetricSummary } from '@sentry/types'; | ||
import type { Primitive } from '@sentry/types'; | ||
import { dropUndefinedKeys } from '@sentry/utils'; | ||
import { getActiveSpan } from '../tracing'; | ||
|
||
/** | ||
* Updates the metric summary on the currently active span | ||
*/ | ||
export function updateMetricSummaryOnActiveSpan( | ||
metricType: 'c' | 'g' | 's' | 'd', | ||
timfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sanitizedName: string, | ||
value: number, | ||
unit: MeasurementUnit, | ||
tags: Record<string, Primitive>, | ||
bucketKey: string, | ||
): void { | ||
const span = getActiveSpan(); | ||
if (span) { | ||
const summary = span.getMetricSummary() || new MetricSummaryAggregator(); | ||
summary.add(metricType, sanitizedName, value, unit, tags, bucketKey); | ||
span.setMetricSummary(summary); | ||
} | ||
} | ||
|
||
/** | ||
* Summaries metrics for spans | ||
*/ | ||
class MetricSummaryAggregator implements MetricSummaryAggregatorInterface { | ||
/** | ||
* key: bucketKey | ||
* value: [exportKey, MetricSpanSummary] | ||
*/ | ||
private readonly _measurements: Map<string, [string, MetricSummary]>; | ||
|
||
public constructor() { | ||
this._measurements = new Map<string, [string, MetricSummary]>(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public add( | ||
metricType: 'c' | 'g' | 's' | 'd', | ||
sanitizedName: string, | ||
value: number, | ||
unit: MeasurementUnit, | ||
tags: Record<string, Primitive>, | ||
bucketKey: string, | ||
): void { | ||
const exportKey = `${metricType}:${sanitizedName}@${unit}`; | ||
const bucketItem = this._measurements.get(bucketKey); | ||
|
||
if (bucketItem) { | ||
const [, summary] = bucketItem; | ||
this._measurements.set(bucketKey, [ | ||
exportKey, | ||
{ | ||
min: Math.min(summary.min, value), | ||
max: Math.max(summary.max, value), | ||
count: (summary.count += 1), | ||
sum: (summary.sum += value), | ||
tags: summary.tags, | ||
}, | ||
]); | ||
} else { | ||
this._measurements.set(bucketKey, [ | ||
exportKey, | ||
{ | ||
min: value, | ||
max: value, | ||
count: 1, | ||
sum: value, | ||
tags, | ||
}, | ||
]); | ||
} | ||
} | ||
|
||
/** @inheritdoc */ | ||
public getJson(): Record<string, MetricSummary> { | ||
const output: Record<string, MetricSummary> = {}; | ||
|
||
for (const [, [exportKey, summary]] of this._measurements) { | ||
output[exportKey] = dropUndefinedKeys(summary); | ||
} | ||
|
||
return output; | ||
} | ||
} |
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
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.