-
Notifications
You must be signed in to change notification settings - Fork 3.3k
chore(instrumentation): add timing statistics for getReleases
#42523
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
13108f1
feat: timing statistics for getReleases
justfalter 5d25a02
add GetReleasesSpanProcessor
justfalter f0f31a7
fixup instrumentation tests
justfalter 8424d88
pr feedback
justfalter 099e397
pr feedback: naming
justfalter 5036f0c
pr feedback: comments
justfalter 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import type { ReadableSpan } from '@opentelemetry/sdk-trace-base'; | ||
| import { ATTR_CODE_FUNCTION_NAME } from '@opentelemetry/semantic-conventions'; | ||
| import { partial } from '~test/util.ts'; | ||
| import { | ||
| ATTR_RENOVATE_DATASOURCE, | ||
| ATTR_RENOVATE_PACKAGE_NAME, | ||
| ATTR_RENOVATE_REGISTRY_URL, | ||
| } from '../../instrumentation/types.ts'; | ||
| import * as stats from '../../util/stats.ts'; | ||
| import { GetDatasourceReleasesSpanProcessor } from './span-processor.ts'; | ||
|
|
||
| vi.mock('../../util/stats.ts'); | ||
|
|
||
| describe('modules/datasource/span-processor', () => { | ||
| describe('GetDatasourceReleasesSpanProcessor', () => { | ||
| it('creates an instance', async () => { | ||
| const processor = new GetDatasourceReleasesSpanProcessor(); | ||
| expect(processor).toBeInstanceOf(GetDatasourceReleasesSpanProcessor); | ||
| await expect(processor.forceFlush()).resolves.toBeUndefined(); | ||
| expect(processor.onStart(partial(), partial())).toBeUndefined(); | ||
| await expect(processor.shutdown()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('writes span datapoints to GetDatasourceReleasesStats', () => { | ||
| const writeMock = vi.mocked(stats.GetDatasourceReleasesStats.write); | ||
|
|
||
| const processor = new GetDatasourceReleasesSpanProcessor(); | ||
| processor.onEnd( | ||
| partial<ReadableSpan>({ | ||
| ended: true, | ||
| attributes: { | ||
| [ATTR_CODE_FUNCTION_NAME]: 'getReleases', | ||
| [ATTR_RENOVATE_DATASOURCE]: 'npm', | ||
| [ATTR_RENOVATE_REGISTRY_URL]: 'https://registry.npmjs.org', | ||
| [ATTR_RENOVATE_PACKAGE_NAME]: 'lodash', | ||
| }, | ||
| duration: [1, 500_123_000], // 1.500123 seconds = 1500.123ms | ||
| }), | ||
| ); | ||
|
|
||
| expect(writeMock).toHaveBeenCalledOnce(); | ||
| expect(writeMock).toHaveBeenCalledWith( | ||
| 'npm', | ||
| 'https://registry.npmjs.org', | ||
| 'lodash', | ||
| 1500.123, | ||
| ); | ||
| }); | ||
|
|
||
| it('defaults registryUrl to an empty string if not provided', () => { | ||
| const writeMock = vi.mocked(stats.GetDatasourceReleasesStats.write); | ||
|
|
||
| const processor = new GetDatasourceReleasesSpanProcessor(); | ||
| processor.onEnd( | ||
| partial<ReadableSpan>({ | ||
| ended: true, | ||
| attributes: { | ||
| [ATTR_CODE_FUNCTION_NAME]: 'getReleases', | ||
| [ATTR_RENOVATE_DATASOURCE]: 'npm', | ||
| [ATTR_RENOVATE_PACKAGE_NAME]: 'lodash', | ||
| }, | ||
| duration: [1, 0], | ||
| }), | ||
| ); | ||
|
|
||
| expect(writeMock).toHaveBeenCalledOnce(); | ||
| expect(writeMock).toHaveBeenCalledWith('npm', '', 'lodash', 1000); | ||
| }); | ||
|
|
||
| interface NoWriteTestCase { | ||
| name: string; | ||
| span: Partial<ReadableSpan>; | ||
| } | ||
|
|
||
| const noWriteTestCases: NoWriteTestCase[] = [ | ||
| { | ||
| name: 'span is not ended', | ||
| span: { | ||
| ended: false, | ||
| attributes: { | ||
| [ATTR_CODE_FUNCTION_NAME]: 'getReleases', | ||
| [ATTR_RENOVATE_DATASOURCE]: 'npm', | ||
| [ATTR_RENOVATE_REGISTRY_URL]: 'https://registry.npmjs.org', | ||
| [ATTR_RENOVATE_PACKAGE_NAME]: 'lodash', | ||
| }, | ||
| duration: [1, 0], | ||
| }, | ||
| }, | ||
| { | ||
| name: 'function name is not getReleases', | ||
| span: { | ||
| ended: true, | ||
| attributes: { | ||
| [ATTR_CODE_FUNCTION_NAME]: 'somethingElse', | ||
| [ATTR_RENOVATE_DATASOURCE]: 'npm', | ||
| [ATTR_RENOVATE_REGISTRY_URL]: 'https://registry.npmjs.org', | ||
| [ATTR_RENOVATE_PACKAGE_NAME]: 'lodash', | ||
| }, | ||
| duration: [1, 0], | ||
| }, | ||
| }, | ||
| { | ||
| name: 'datasource is not provided', | ||
| span: { | ||
| ended: true, | ||
| attributes: { | ||
| [ATTR_CODE_FUNCTION_NAME]: 'getReleases', | ||
| [ATTR_RENOVATE_REGISTRY_URL]: 'https://registry.npmjs.org', | ||
| [ATTR_RENOVATE_PACKAGE_NAME]: 'lodash', | ||
| }, | ||
| duration: [1, 0], | ||
| }, | ||
| }, | ||
| { | ||
| name: 'package name is not provided', | ||
| span: { | ||
| ended: true, | ||
| attributes: { | ||
| [ATTR_CODE_FUNCTION_NAME]: 'getReleases', | ||
| [ATTR_RENOVATE_DATASOURCE]: 'npm', | ||
| [ATTR_RENOVATE_REGISTRY_URL]: 'https://registry.npmjs.org', | ||
| }, | ||
| duration: [1, 0], | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| test.each(noWriteTestCases)( | ||
| 'does not write span datapoints to GetDatasourceReleasesStats if $name', | ||
| ({ span }) => { | ||
| const writeMock = vi.mocked(stats.GetDatasourceReleasesStats.write); | ||
| const processor = new GetDatasourceReleasesSpanProcessor(); | ||
| processor.onEnd(partial<ReadableSpan>(span)); | ||
| expect(writeMock).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
| }); | ||
| }); |
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,56 @@ | ||
| import type { Context } from '@opentelemetry/api'; | ||
| import type { | ||
| ReadableSpan, | ||
| Span, | ||
| SpanProcessor, | ||
| } from '@opentelemetry/sdk-trace-base'; | ||
| import { ATTR_CODE_FUNCTION_NAME } from '@opentelemetry/semantic-conventions'; | ||
| import { | ||
| ATTR_RENOVATE_DATASOURCE, | ||
| ATTR_RENOVATE_PACKAGE_NAME, | ||
| ATTR_RENOVATE_REGISTRY_URL, | ||
| } from '../../instrumentation/types.ts'; | ||
| import { GetDatasourceReleasesStats } from '../../util/stats.ts'; | ||
|
|
||
| export class GetDatasourceReleasesSpanProcessor implements SpanProcessor { | ||
| forceFlush(): Promise<void> { | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| onStart(_span: Span, _parentContext: Context): void { | ||
| // no implementation | ||
| } | ||
|
|
||
| onEnd(span: ReadableSpan): void { | ||
| if (!span.ended) { | ||
| return; | ||
| } | ||
|
|
||
| if (span.attributes[ATTR_CODE_FUNCTION_NAME] !== 'getReleases') { | ||
| return; | ||
| } | ||
|
|
||
| const datasource = span.attributes[ATTR_RENOVATE_DATASOURCE] as string; | ||
| const registryUrl = (span.attributes[ATTR_RENOVATE_REGISTRY_URL] ?? | ||
| '') as string; | ||
| const packageName = span.attributes[ATTR_RENOVATE_PACKAGE_NAME] as string; | ||
|
|
||
| if (!datasource || !packageName) { | ||
| return; | ||
| } | ||
|
|
||
| // duration[0] is seconds, duration[1] is nanoseconds. | ||
| const durationMs = span.duration[0] * 1000 + span.duration[1] / 1_000_000; | ||
|
|
||
| GetDatasourceReleasesStats.write( | ||
| datasource, | ||
| registryUrl, | ||
| packageName, | ||
| durationMs, | ||
| ); | ||
| } | ||
|
|
||
| shutdown(): Promise<void> { | ||
| return Promise.resolve(); | ||
| } | ||
| } |
Oops, something went wrong.
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.