Skip to content

Commit

Permalink
Merge pull request #484 from JulienFauvel/fix/metric-annotation-wrong…
Browse files Browse the repository at this point in the history
…ly-typed

fix: fix/metric annotation wrongly typed
  • Loading branch information
pragmaticivan authored May 26, 2024
2 parents 746ca35 + 157e19f commit 3f8b4a4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 70 deletions.
19 changes: 0 additions & 19 deletions src/metrics/decorators/counter.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/metrics/decorators/histogram.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/metrics/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export * from './common';
export * from './counter';
export * from './histogram';
export * from './observable';
export * from './param';
34 changes: 0 additions & 34 deletions src/metrics/decorators/observable.ts

This file was deleted.

49 changes: 49 additions & 0 deletions src/metrics/decorators/param.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createParamDecorator } from '@nestjs/common';
import { OtelMetricOptions } from '../../interfaces/metric-options.interface';
import {
getOrCreateCounter,
getOrCreateHistogram,
getOrCreateObservableCounter,
getOrCreateObservableGauge,
getOrCreateObservableUpDownCounter,
} from '../metric-data';

export type MetricParamDecorator = (
name: string,
options?: OtelMetricOptions
) => ParameterDecorator;

function createMetricParamDecorator<T>(
type: string,
getOrCreateMetric: (name: string, options?: OtelMetricOptions) => T
): MetricParamDecorator {
return (name: string, options?: OtelMetricOptions): ParameterDecorator => {
return createParamDecorator(() => {
if (!name || name.length === 0) {
throw new Error(`${type} need a name argument`);
}
return getOrCreateMetric(name, options);
})();
};
}

export const OtelCounter = createMetricParamDecorator('OtelCounter', getOrCreateCounter);
export const OtelUpDownCounter = createMetricParamDecorator(
'OtelUpDownCounter',
getOrCreateCounter
);

export const OtelHistogram = createMetricParamDecorator('OtelHistogram', getOrCreateHistogram);

export const OtelObservableGauge = createMetricParamDecorator(
'OtelObservableGauge',
getOrCreateObservableGauge
);
export const OtelObservableCounter = createMetricParamDecorator(
'OtelObservableCounter',
getOrCreateObservableCounter
);
export const OtelObservableUpDownCounter = createMetricParamDecorator(
'OtelObservableUpDownCounter',
getOrCreateObservableUpDownCounter
);
10 changes: 7 additions & 3 deletions tests/e2e/metrics/decorators/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ describe('Common Decorators', () => {

beforeEach(done => {
exporter = new PrometheusExporter({}, () => {
meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
meterProvider = new MeterProvider({
readers: [exporter],
});
metrics.setGlobalMeterProvider(meterProvider);
done();
});
Expand All @@ -34,7 +35,7 @@ describe('Common Decorators', () => {
}
});

describe('Instance counter & Method counter', () => {
describe('Instance counter, method counter & param counter', () => {
it('creates an instance counter and increase counter when new instance is created', async () => {
const testingModule = await Test.createTestingModule({
imports: [
Expand All @@ -61,6 +62,9 @@ describe('Common Decorators', () => {

expect(/app_AppController_instances_total 1/.test(text)).toBeTruthy();
expect(/app_AppController_example_calls_total 1/.test(text)).toBeTruthy();

expect(/# HELP example_counter_total An example counter/.test(text)).toBeTruthy();
expect(/example_counter_total 1/.test(text)).toBeTruthy();
});
});
});
5 changes: 4 additions & 1 deletion tests/fixture-app/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Get, Controller } from '@nestjs/common';
import { Counter } from '@opentelemetry/api';
import { OtelInstanceCounter, OtelMethodCounter } from '../../src/metrics/decorators/common';
import { OtelCounter } from '../../src/metrics/decorators/param';

@OtelInstanceCounter()
@Controller('example')
Expand All @@ -11,7 +13,8 @@ export class AppController {

@Get(':id')
@OtelMethodCounter()
example() {
example(@OtelCounter('example_counter', { description: 'An example counter' }) counter: Counter) {
counter.add(1);
return 'example';
}
}

0 comments on commit 3f8b4a4

Please sign in to comment.