Skip to content

Commit

Permalink
adding basic uptime stats (#59)
Browse files Browse the repository at this point in the history
* adding basic uptime stats

Signed-off-by: Matt Raffel <[email protected]>

* adding basic uptime stats

Signed-off-by: Matt Raffel <[email protected]>
  • Loading branch information
matt-raffel-kiva authored Apr 18, 2022
1 parent df3dbc5 commit 3ee1038
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/app/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Get, Controller } from '@nestjs/common';
import { HttpConstants } from 'protocol-common/http-context/http.constants';
import { DisableAutoLogging } from 'protocol-common/disable.auto.logging.decorator';
import { AppService } from './app.service';
import { ServiceReportDto } from './dtos/service.report.dto';

/**
* Base route is just for various health check endpoints
Expand All @@ -9,6 +11,9 @@ import { DisableAutoLogging } from 'protocol-common/disable.auto.logging.decorat
@Controller()
export class AppController {

constructor(private readonly service: AppService) {
}

@Get()
base(): string {
return process.env.SERVICE_NAME;
Expand All @@ -23,4 +28,12 @@ export class AppController {
healthz(): string {
return HttpConstants.HEALTHZ_RESPONSE;
}

/**
* For the uptime statistics report (see Uptime feature brief)
*/
@Get('stats')
async generateStatsReport() : Promise<ServiceReportDto> {
return await this.service.generateStatsReport();
}
}
16 changes: 16 additions & 0 deletions src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { traceware } from 'protocol-common/tracer';
import rateLimit from 'express-rate-limit';
import swaggerUi from 'swagger-ui-express';
import swaggerDocument from '../docs/swagger.json';
import { ServiceReportDto } from './dtos/service.report.dto';

/**
* Sets up the gateway to handle external traffic, eg cors, rate-limiting, etc
*/
@Injectable()
export class AppService {

private static startedAt: Date;

/**
* Sets up app in a way that can be used by main.ts and e2e tests
*/
Expand All @@ -38,6 +41,8 @@ export class AppService {

app.use(traceware('gateway'));

AppService.startedAt = new Date();

// Default is 100 requests per minute
app.use(rateLimit({
windowMs: process.env.RATE_LIMIT_WINDOW_MS,
Expand All @@ -47,4 +52,15 @@ export class AppService {
// Load swagger docs and display
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
};

public async generateStatsReport(): Promise<ServiceReportDto> {
Logger.info('stats report generated');
const report: ServiceReportDto = new ServiceReportDto();
report.serviceName = process.env.SERVICE_NAME;
report.startedAt = AppService.startedAt.toDateString();
report.currentTime = new Date().toDateString();
report.versions = ['none'];

return Promise.resolve(report);
}
}
12 changes: 12 additions & 0 deletions src/app/dtos/service.report.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IsArray, IsString } from 'class-validator';

/**
* Please note: this structure is expected to be used across all services
* which report statistics
*/
export class ServiceReportDto {
@IsString() serviceName: string;
@IsString() startedAt: string;
@IsString() currentTime: string;
@IsArray() versions: string[];
}

0 comments on commit 3ee1038

Please sign in to comment.