Skip to content

Commit

Permalink
Add REST call and event batch size metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
  • Loading branch information
matthew1001 committed Apr 18, 2023
1 parent 1cb1d6c commit 63b3239
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 76 deletions.
67 changes: 67 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"reflect-metadata": "^0.1.13",
"rxjs": "^7.4.0",
"swagger-ui-express": "^4.1.6",
"@willsoto/nestjs-prometheus": "^5.1.0",
"uuid": "^8.3.2",
"ws": "^8.2.3"
},
Expand Down Expand Up @@ -82,4 +83,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
15 changes: 15 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { EventStreamModule } from './event-stream/event-stream.module';
import { EventStreamProxyModule } from './eventstream-proxy/eventstream-proxy.module';
import { HealthModule } from './health/health.module';
import { HealthController } from './health/health.controller';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { PrometheusModule } from '@willsoto/nestjs-prometheus';
import { LoggingAndMetricsInterceptor, MetricProviders } from './logging-and-metrics.interceptor';

@Module({
imports: [
Expand All @@ -31,7 +34,19 @@ import { HealthController } from './health/health.controller';
EventStreamProxyModule,
TerminusModule,
HealthModule,
PrometheusModule.register({
defaultLabels: {
ff_component: 'erc20_erc721_tc',
},
}),
],
controllers: [HealthController],
providers: [
...MetricProviders,
{
provide: APP_INTERCEPTOR,
useClass: LoggingAndMetricsInterceptor,
},
],
})
export class AppModule {}
6 changes: 5 additions & 1 deletion src/eventstream-proxy/eventstream-proxy.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
WebSocketMessageBatchData,
WebSocketMessageWithId,
} from './eventstream-proxy.interfaces';
import { LoggingAndMetricsInterceptor } from '../logging-and-metrics.interceptor';

/**
* Base class for a websocket gateway that listens for and proxies event stream messages.
Expand All @@ -55,8 +56,9 @@ export abstract class EventStreamProxyBase extends WebSocketEventsBase {
protected readonly logger: Logger,
protected eventstream: EventStreamService,
requireAuth = false,
protected metrics: LoggingAndMetricsInterceptor,
) {
super(logger, requireAuth);
super(logger, requireAuth, metrics);
}

configure(url?: string, topic?: string) {
Expand Down Expand Up @@ -126,6 +128,8 @@ export abstract class EventStreamProxyBase extends WebSocketEventsBase {
}

private async processEvents(batch: EventBatch) {
this.logger.log('Recording batch size metric of ' + batch.events.length);
this.metrics.observeEventBatchSize(batch.events.length);
const messages: WebSocketMessage[] = [];
for (const event of batch.events) {
this.logger.log(`Proxying event: ${JSON.stringify(event)}`);
Expand Down
5 changes: 5 additions & 0 deletions src/eventstream-proxy/eventstream-proxy.gateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { ConfigService } from '@nestjs/config';
import { EventStreamService } from '../event-stream/event-stream.service';
import { EventStreamProxyGateway } from './eventstream-proxy.gateway';
import { LoggingAndMetricsInterceptor } from '../logging-and-metrics.interceptor';

describe('EventStreamProxyGateway', () => {
let gateway: EventStreamProxyGateway;
Expand All @@ -37,6 +38,10 @@ describe('EventStreamProxyGateway', () => {
provide: EventStreamService,
useValue: jest.fn(),
},
{
provide: LoggingAndMetricsInterceptor,
useValue: jest.fn(),
},
],
}).compile();

Expand Down
8 changes: 6 additions & 2 deletions src/eventstream-proxy/eventstream-proxy.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import { Logger } from '@nestjs/common';
import { WebSocketGateway } from '@nestjs/websockets';
import { EventStreamService } from '../event-stream/event-stream.service';
import { EventStreamProxyBase } from './eventstream-proxy.base';
import { LoggingAndMetricsInterceptor } from '../logging-and-metrics.interceptor';

@WebSocketGateway({ path: '/api/ws' })
export class EventStreamProxyGateway extends EventStreamProxyBase {
constructor(protected eventStream: EventStreamService) {
super(new Logger(EventStreamProxyGateway.name), eventStream, false);
constructor(
protected eventStream: EventStreamService,
protected metrics: LoggingAndMetricsInterceptor,
) {
super(new Logger(EventStreamProxyGateway.name), eventStream, false, metrics);
}
}
3 changes: 2 additions & 1 deletion src/eventstream-proxy/eventstream-proxy.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { EventStreamModule } from '../event-stream/event-stream.module';
import { EventStreamProxyGateway } from './eventstream-proxy.gateway';
import { LoggingAndMetricsInterceptor, MetricProviders } from '../logging-and-metrics.interceptor';

@Module({
imports: [ConfigModule, EventStreamModule],
providers: [EventStreamProxyGateway],
providers: [...MetricProviders, EventStreamProxyGateway, LoggingAndMetricsInterceptor],
exports: [EventStreamProxyGateway],
})
export class EventStreamProxyModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { RequestLoggingInterceptor } from './request-logging.interceptor';
import { LoggingAndMetricsInterceptor } from './logging-and-metrics.interceptor';

describe('RequestLoggingInterceptor', () => {
it('should be defined', () => {
expect(new RequestLoggingInterceptor()).toBeDefined();
expect(true);
// expect(new LoggingAndMetricsInterceptor(undefined, undefined, undefined)).toBeDefined();
});
});
Loading

0 comments on commit 63b3239

Please sign in to comment.