perf(cmpp): instrument inbound flow and unbatch submit worker

This commit is contained in:
hectorzhao
2026-08-20 11:27:35 +08:00
parent c4f36fc50d
commit b9a71fe0b9
23 changed files with 760 additions and 138 deletions
@@ -1,4 +1,4 @@
import { BadRequestException, Body, Controller, Post } from '@nestjs/common';
import { BadRequestException, Body, Controller, Optional, Post } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
GatewayInboundAuthDto,
@@ -19,6 +19,7 @@ import { GatewayDownstreamConnectionEventDto } from '../sms-config/sms-config.co
import { SmsConfigService } from '../sms-config/sms-config.service';
import { ProtocolLogsService, type ProtocolLogInput } from '../protocol-logs/protocol-logs.service';
import { SecurityDetectionService } from '../security-detection/security-detection.service';
import { MetricsService } from '../metrics/metrics.service';
@ApiTags('gateway-events')
@Controller('gateway/events')
@@ -28,6 +29,7 @@ export class GatewayEventsController {
private readonly smsConfig: SmsConfigService,
private readonly protocolLogs: ProtocolLogsService,
private readonly security: SecurityDetectionService,
@Optional() private readonly metrics?: MetricsService,
) {}
@Post('submit-result')
@@ -139,6 +141,9 @@ export class GatewayEventsController {
direction: ProtocolLogInput['direction'] = 'channel_to_platform',
) {
const startedAt = Date.now();
const inboundMetricStartedAt = eventType === 'submit' && direction === 'client_to_platform'
? this.metrics?.beginCmppInboundStage()
: undefined;
const value = body as Record<string, unknown>;
const common: Omit<ProtocolLogInput, 'status'> = {
protocol: 'cmpp',
@@ -173,6 +178,9 @@ export class GatewayEventsController {
status: 'success',
durationMs: Date.now() - startedAt,
});
if (inboundMetricStartedAt != null) {
this.metrics?.finishCmppInboundStage(inboundMetricStartedAt, 'total', 'success');
}
return result;
} catch (error) {
this.protocolLogs.record({
@@ -181,6 +189,9 @@ export class GatewayEventsController {
durationMs: Date.now() - startedAt,
detail: { error: error instanceof Error ? error.message : 'unknown error' },
});
if (inboundMetricStartedAt != null) {
this.metrics?.finishCmppInboundStage(inboundMetricStartedAt, 'total', 'error');
}
throw error;
}
}