-
Notifications
You must be signed in to change notification settings - Fork 1
MET-1593 DataDog compatible abstract metrics #906
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
133 changes: 133 additions & 0 deletions
133
packages/app/metrics-utils/src/AbstractDimensionalCounterMetric.spec.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,133 @@ | ||
| import type promClient from 'prom-client' | ||
| import { beforeEach, describe, expect, it, type Mock, vi } from 'vitest' | ||
| import { AbstractDimensionalCounterMetric } from './AbstractDimensionalCounterMetric.ts' | ||
|
|
||
| class ConcreteDimensionalCounterMetric extends AbstractDimensionalCounterMetric< | ||
| ['successful', 'failed'] | ||
| > { | ||
| constructor(client?: typeof promClient) { | ||
| super( | ||
| { | ||
| namePrefix: 'workflow_run:entitlements', | ||
| nameSuffix: 'counter', | ||
| helpDescription: 'Number of workflow runs per status', | ||
| dimensions: ['successful', 'failed'], | ||
| }, | ||
| client, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| describe('AbstractDimensionalCounterMetric', () => { | ||
| let incMock: Mock | ||
| let counterMock: Mock | ||
| let getSingleMetricMock: Mock | ||
| let client: typeof promClient | ||
|
|
||
| beforeEach(() => { | ||
| incMock = vi.fn() | ||
| // biome-ignore lint/complexity/useArrowFunction: required for vitest | ||
| counterMock = vi.fn().mockImplementation(function () { | ||
| return { inc: incMock } | ||
| }) | ||
| getSingleMetricMock = vi.fn() | ||
| client = { | ||
| Counter: counterMock, | ||
| register: { getSingleMetric: getSingleMetricMock }, | ||
| } as any as typeof promClient | ||
| }) | ||
|
|
||
| describe('registerMeasurement', () => { | ||
| it('gracefully aborts if metrics are not enabled', () => { | ||
| // Given | ||
| const metric = new ConcreteDimensionalCounterMetric(undefined) | ||
|
|
||
| // When | ||
| metric.registerMeasurement({ successful: 20, failed: 10 }) | ||
|
|
||
| // Then | ||
| expect(counterMock).not.toHaveBeenCalled() | ||
| expect(incMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('initializes correctly with 0 values for all dimensions', () => { | ||
| // Given | ||
| getSingleMetricMock.mockReturnValue(undefined) | ||
|
|
||
| // When | ||
| new ConcreteDimensionalCounterMetric(client) | ||
|
|
||
| // Then | ||
| expect(getSingleMetricMock).toHaveBeenCalledWith( | ||
| 'workflow_run:entitlements_successful:counter', | ||
| ) | ||
| expect(getSingleMetricMock).toHaveBeenCalledWith('workflow_run:entitlements_failed:counter') | ||
| expect(counterMock).toHaveBeenCalledWith({ | ||
| name: 'workflow_run:entitlements_successful:counter', | ||
| help: 'Number of workflow runs per status', | ||
| labelNames: [], | ||
| }) | ||
| expect(counterMock).toHaveBeenCalledWith({ | ||
| name: 'workflow_run:entitlements_failed:counter', | ||
| help: 'Number of workflow runs per status', | ||
| labelNames: [], | ||
| }) | ||
| expect(incMock).toHaveBeenCalledTimes(2) | ||
| expect(incMock).toHaveBeenCalledWith(0) | ||
| }) | ||
|
|
||
| it('reuses existing metric per dimension when already registered', () => { | ||
| // Given | ||
| const existingCounter = { inc: incMock } | ||
| getSingleMetricMock.mockReturnValue(existingCounter) | ||
|
|
||
| // When | ||
| new ConcreteDimensionalCounterMetric(client) | ||
|
|
||
| // Then | ||
| expect(counterMock).not.toHaveBeenCalled() | ||
| expect(incMock).toHaveBeenCalledTimes(2) | ||
| expect(incMock).toHaveBeenCalledWith(0) | ||
| }) | ||
|
|
||
| it('registers all measurements properly', () => { | ||
| // Given | ||
| getSingleMetricMock.mockReturnValue(undefined) | ||
| const metric = new ConcreteDimensionalCounterMetric(client) | ||
| incMock.mockClear() | ||
|
|
||
| // When | ||
| metric.registerMeasurement({ successful: 20, failed: 10 }) | ||
|
|
||
| // Then | ||
| expect(incMock).toHaveBeenCalledTimes(2) | ||
| expect(incMock).toHaveBeenCalledWith(20) | ||
| expect(incMock).toHaveBeenCalledWith(10) | ||
| }) | ||
|
|
||
| it('registers selected measurements only', () => { | ||
| // Given | ||
| getSingleMetricMock.mockReturnValue(undefined) | ||
| const metric = new ConcreteDimensionalCounterMetric(client) | ||
| incMock.mockClear() | ||
|
|
||
| // When | ||
| metric.registerMeasurement({ successful: 20 }) | ||
|
|
||
| // Then | ||
| expect(incMock).toHaveBeenCalledTimes(1) | ||
| expect(incMock).toHaveBeenCalledWith(20) | ||
| }) | ||
|
|
||
| it('should ignore measurements if client is not provided', () => { | ||
| // Given | ||
| const metric = new ConcreteDimensionalCounterMetric() | ||
|
|
||
| // When | ||
| metric.registerMeasurement({ successful: 20 }) | ||
|
|
||
| // Then | ||
| expect(incMock).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
| }) |
58 changes: 58 additions & 0 deletions
58
packages/app/metrics-utils/src/AbstractDimensionalCounterMetric.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,58 @@ | ||
| import type promClient from 'prom-client' | ||
| import type { Counter } from 'prom-client' | ||
|
|
||
| type DimensionalCounterMetricConfiguration<TDimensions extends string[]> = { | ||
| namePrefix: string | ||
| nameSuffix: string | ||
| helpDescription: string | ||
| dimensions: TDimensions | ||
| } | ||
|
|
||
| type DimensionalCounterMeasurement<TDimensions extends string[]> = Partial< | ||
| Record<TDimensions[number], number> | ||
| > | ||
|
|
||
| function buildDimensionalMetricName( | ||
| namePrefix: string, | ||
| dimension: string, | ||
| nameSuffix: string, | ||
| ): string { | ||
| return `${namePrefix}_${dimension}:${nameSuffix}` | ||
| } | ||
|
|
||
| export abstract class AbstractDimensionalCounterMetric<TDimensions extends string[]> { | ||
| private readonly counters: Map<TDimensions[number], Counter> | ||
|
|
||
| protected constructor( | ||
| metricConfig: DimensionalCounterMetricConfiguration<TDimensions>, | ||
| client?: typeof promClient, | ||
| ) { | ||
| this.counters = new Map() | ||
| if (!client) return | ||
|
|
||
| for (const dimension of metricConfig.dimensions) { | ||
| const name = buildDimensionalMetricName( | ||
| metricConfig.namePrefix, | ||
| dimension, | ||
| metricConfig.nameSuffix, | ||
| ) | ||
| const existing = client.register.getSingleMetric(name) | ||
| const counter = existing | ||
| ? (existing as Counter) | ||
| : new client.Counter({ name, help: metricConfig.helpDescription, labelNames: [] }) | ||
| counter.inc(0) | ||
| this.counters.set(dimension, counter) | ||
| } | ||
| } | ||
|
|
||
| public registerMeasurement(measurement: DimensionalCounterMeasurement<TDimensions>): void { | ||
| if (this.counters.size === 0) return | ||
|
|
||
| for (const [dimension, value] of Object.entries(measurement) as [ | ||
| TDimensions[number], | ||
| number, | ||
| ][]) { | ||
| this.counters.get(dimension)?.inc(value) | ||
| } | ||
| } | ||
| } |
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.