Skip to content

Commit

Permalink
feat(prometheus): add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mKeRix committed Jan 10, 2021
1 parent 024e24c commit d6a90ea
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -50,6 +50,7 @@
"@nestjs/schedule": "^0.4.1",
"@nestjs/swagger": "^4.7.9",
"@nestjs/terminus": "^7.0.1",
"@willsoto/nestjs-prometheus": "^3.0.0",
"async-mqtt": "^2.6.1",
"chalk": "^4.1.0",
"class-transformer": "^0.3.1",
Expand All @@ -65,6 +66,7 @@
"mathjs": "^8.1.1",
"nest-emitter": "^1.1.1",
"nest-winston": "^1.4.0",
"prom-client": "^13.0.0",
"pureimage": "^0.2.5",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.6.3",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Expand Up @@ -10,6 +10,7 @@ import { NestEmitterModule } from 'nest-emitter';
import { EventEmitter } from 'events';
import { WINSTON_LOGGER } from './logger';
import { StatusModule } from './status/status.module';
import { PrometheusModule } from '@willsoto/nestjs-prometheus';

// eslint-disable-next-line @typescript-eslint/no-var-requires
export const VERSION = require('../package.json').version;
Expand All @@ -26,6 +27,7 @@ export const CONFIGURED_INTEGRATIONS = c
StatusModule,
ScheduleModule.forRoot(),
NestEmitterModule.forRoot(new EventEmitter()),
PrometheusModule.register(),
IntegrationsModule.register(CONFIGURED_INTEGRATIONS, WINSTON_LOGGER),
],
})
Expand Down
11 changes: 10 additions & 1 deletion src/integrations/bluetooth/bluetooth.module.ts
Expand Up @@ -4,10 +4,19 @@ import { BluetoothHealthIndicator } from './bluetooth.health';
import { ConfigModule } from '../../config/config.module';
import { StatusModule } from '../../status/status.module';
import { ScheduleModule } from '@nestjs/schedule';
import { makeCounterProvider } from '@willsoto/nestjs-prometheus';

@Module({
imports: [ConfigModule, StatusModule, ScheduleModule.forRoot()],
providers: [BluetoothService, BluetoothHealthIndicator],
providers: [
BluetoothService,
BluetoothHealthIndicator,
makeCounterProvider({
name: 'bluetooth_le_advertisements_received_count',
help:
'Number of Bluetooth Low Energy advertisements that were detected by this device',
}),
],
exports: [BluetoothService],
})
export class BluetoothModule {}
11 changes: 10 additions & 1 deletion src/integrations/bluetooth/bluetooth.service.spec.ts
@@ -1,3 +1,5 @@
import { makeCounterProvider } from '@willsoto/nestjs-prometheus';

const mockExec = jest.fn();
const mockNoble = {
state: 'poweredOn',
Expand Down Expand Up @@ -40,7 +42,14 @@ describe('BluetoothService', () => {

const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule],
providers: [BluetoothService, BluetoothHealthIndicator],
providers: [
BluetoothService,
BluetoothHealthIndicator,
makeCounterProvider({
name: 'bluetooth_le_advertisements_received_count',
help: '',
}),
],
})
.overrideProvider(BluetoothHealthIndicator)
.useValue(healthIndicator)
Expand Down
7 changes: 6 additions & 1 deletion src/integrations/bluetooth/bluetooth.service.ts
Expand Up @@ -9,6 +9,8 @@ import { Device } from '../bluetooth-classic/device';
import { promiseWithTimeout, sleep } from '../../util/promises';
import { Interval } from '@nestjs/schedule';
import _ from 'lodash';
import { Counter } from 'prom-client';
import { InjectMetric } from '@willsoto/nestjs-prometheus';

const RSSI_REGEX = new RegExp(/-?[0-9]+/);
const INQUIRY_LOCK_TIMEOUT = 30 * 1000;
Expand Down Expand Up @@ -44,7 +46,9 @@ export class BluetoothService {

constructor(
private readonly configService: ConfigService,
private readonly healthIndicator: BluetoothHealthIndicator
private readonly healthIndicator: BluetoothHealthIndicator,
@InjectMetric('bluetooth_le_advertisements_received_count')
private readonly advertisementReceivedCounter: Counter<string>
) {
this.classicConfig = this.configService.get('bluetoothClassic');
}
Expand Down Expand Up @@ -393,6 +397,7 @@ export class BluetoothService {
noble.on('stateChange', this.handleAdapterStateChange.bind(this));
noble.on('discover', () => {
this.lastLowEnergyDiscovery = new Date();
this.advertisementReceivedCounter.inc();
if (this.adapters.getState(this.lowEnergyAdapterId) === 'inactive') {
this.adapters.setState(this.lowEnergyAdapterId, 'scan');
}
Expand Down

0 comments on commit d6a90ea

Please sign in to comment.