chore: add api new relic observability
This commit is contained in:
1 parent
d2fc126fed
commit
6dbc643c44
9 files changed
+1125
-21
No files matched your search
+8
-1
@@ -41,9 +41,16 @@ COPY --from=prod-deps --chown=nonroot:nonroot /app/package.json ./package.json
|
||||
COPY --from=prod-deps --chown=nonroot:nonroot /app/node_modules ./node_modules
|
||||
COPY --from=prod-deps --chown=nonroot:nonroot /app/apps/api/package.json ./apps/api/package.json
|
||||
COPY --from=prod-deps --chown=nonroot:nonroot /app/apps/api/node_modules ./apps/api/node_modules
|
||||
COPY --chown=nonroot:nonroot apps/api/newrelic.cjs ./apps/api/newrelic.cjs
|
||||
COPY --from=build --chown=nonroot:nonroot /app/apps/api/dist ./apps/api/dist
|
||||
COPY --chown=nonroot:nonroot apps/project.env ./project.env
|
||||
|
||||
EXPOSE 10712
|
||||
|
||||
CMD ["apps/api/dist/main.js"]
|
||||
ENV NEW_RELIC_HOME=/app/apps/api \
|
||||
NEW_RELIC_APP_NAME=lolympus \
|
||||
NEW_RELIC_AI_MONITORING_ENABLED=true \
|
||||
NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED=100000 \
|
||||
NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED=10000
|
||||
|
||||
CMD ["--import", "/app/apps/api/node_modules/newrelic/esm-loader.mjs", "-r", "/app/apps/api/node_modules/newrelic", "apps/api/dist/main.js"]
|
||||
@@ -128,6 +128,23 @@ pnpm start:web:dev
|
||||
|
||||
웹 앱은 기본적으로 `apps/web/.env.development` 값을 사용합니다. API 환경값은 `apps/project.env`와 실행 환경의 시크릿 설정을 함께 확인합니다.
|
||||
|
||||
### API New Relic APM
|
||||
|
||||
API 서버는 `newrelic` Node.js agent를 앱 코드보다 먼저 preload합니다. Docker 런타임과 `pnpm start:api:*` 스크립트 모두 `NEW_RELIC_HOME`을 API 패키지로 지정해 `apps/api/newrelic.cjs` 설정을 읽습니다.
|
||||
|
||||
Kubernetes에서는 Infisical이 `NEW_RELIC_LICENSE_KEY`를 프로세스 시작 전에 환경변수로 주입해야 합니다. 저장소에는 라이선스 키를 두지 않습니다.
|
||||
|
||||
비밀이 아닌 기본값은 Dockerfile과 `apps/api/newrelic.cjs`에 정의되어 있으며 필요하면 런타임 환경변수로 덮어쓸 수 있습니다.
|
||||
|
||||
```bash
|
||||
NEW_RELIC_APP_NAME=lolympus
|
||||
NEW_RELIC_AI_MONITORING_ENABLED=true
|
||||
NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED=100000
|
||||
NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED=10000
|
||||
```
|
||||
|
||||
New Relic Node agent의 정수 환경변수는 `100k` 같은 축약형 대신 `100000`처럼 숫자 문자열을 사용합니다.
|
||||
|
||||
카카오톡 기반 명령어 기능은 Dakao(커스텀 카카오톡 게이트웨이) 연동이 필요합니다. Dakao가 실행 중이 아니거나 NATS를 통해 API 서버와 연결되어 있지 않으면 일정 조회, 내전 참가, 참가 취소, 계정 등록 같은 카카오톡 명령어는 사용할 수 없습니다.
|
||||
|
||||
## 커맨드
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
'use strict';
|
||||
|
||||
function readBoolean(name, defaultValue) {
|
||||
const value = process.env[name];
|
||||
if (value == null || value === '') return defaultValue;
|
||||
|
||||
return !['0', 'false', 'f', 'no', 'n', 'disabled'].includes(
|
||||
value.toLowerCase(),
|
||||
);
|
||||
}
|
||||
|
||||
function readInteger(name, defaultValue) {
|
||||
const rawValue = process.env[name]?.trim();
|
||||
if (!rawValue) return defaultValue;
|
||||
|
||||
if (/^\d+$/.test(rawValue)) {
|
||||
return Number.parseInt(rawValue, 10);
|
||||
}
|
||||
|
||||
process.env[name] = String(defaultValue);
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
const hasLicenseKey = Boolean(process.env.NEW_RELIC_LICENSE_KEY);
|
||||
|
||||
exports.config = {
|
||||
app_name: [process.env.NEW_RELIC_APP_NAME || 'lolympus'],
|
||||
license_key: process.env.NEW_RELIC_LICENSE_KEY || '',
|
||||
agent_enabled: readBoolean('NEW_RELIC_ENABLED', hasLicenseKey),
|
||||
distributed_tracing: {
|
||||
enabled: true,
|
||||
},
|
||||
ai_monitoring: {
|
||||
enabled: readBoolean('NEW_RELIC_AI_MONITORING_ENABLED', true),
|
||||
},
|
||||
custom_insights_events: {
|
||||
max_samples_stored: readInteger(
|
||||
'NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED',
|
||||
100000,
|
||||
),
|
||||
},
|
||||
span_events: {
|
||||
max_samples_stored: readInteger(
|
||||
'NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED',
|
||||
10000,
|
||||
),
|
||||
},
|
||||
logging: {
|
||||
level: process.env.NEW_RELIC_LOG_LEVEL || 'info',
|
||||
},
|
||||
instrumentation: {
|
||||
timers: {
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
allow_all_headers: false,
|
||||
attributes: {
|
||||
exclude: [
|
||||
'request.headers.cookie',
|
||||
'request.headers.authorization',
|
||||
'request.headers.proxyAuthorization',
|
||||
'request.headers.setCookie*',
|
||||
'response.headers.cookie',
|
||||
'response.headers.authorization',
|
||||
'response.headers.proxyAuthorization',
|
||||
'response.headers.setCookie*',
|
||||
],
|
||||
},
|
||||
};
|
||||
@@ -10,9 +10,9 @@
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"seed:dev": "ts-node src/seed.ts",
|
||||
"seed:dakao-participant": "ts-node src/seed-dakao-participant.ts",
|
||||
"start:dev": "node -e \"process.env.NODE_ENV='dev'; require('./dist/main.js')\"",
|
||||
"start:staging": "node -e \"process.env.NODE_ENV='staging'; require('./dist/main.js')\"",
|
||||
"start:prod": "node -e \"process.env.NODE_ENV='prod'; require('./dist/main.js')\"",
|
||||
"start:dev": "NODE_ENV=dev NEW_RELIC_HOME=. node --import newrelic/esm-loader.mjs -r newrelic dist/main.js",
|
||||
"start:staging": "NODE_ENV=staging NEW_RELIC_HOME=. node --import newrelic/esm-loader.mjs -r newrelic dist/main.js",
|
||||
"start:prod": "NODE_ENV=prod NEW_RELIC_HOME=. node --import newrelic/esm-loader.mjs -r newrelic dist/main.js",
|
||||
"pm2:dev": "pm2 start npm --name lolympus-tournament-support --update-env -- run start:dev",
|
||||
"pm2:staging": "pm2 start npm --name lolympus-tournament-support --update-env -- run start:staging",
|
||||
"pm2:prod": "pm2 start npm --name lolympus-tournament-support --update-env -- run start:prod",
|
||||
@@ -38,6 +38,7 @@
|
||||
"jose": "^6.2.2",
|
||||
"mongoose": "^9.4.1",
|
||||
"nats": "^2.29.3",
|
||||
"newrelic": "^14.1.2",
|
||||
"redis": "^5.12.1",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1",
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
AUCTION_DURABILITY_RETRY_QUEUE_KEY,
|
||||
buildAuctionRedisKeys,
|
||||
} from './redis/auction-redis.keys';
|
||||
import { recordNewRelicCustomEvent } from '../global/observability/new-relic';
|
||||
const AUCTION_DURABILITY_TICK_INTERVAL_MS = 1000;
|
||||
const AUCTION_DURABILITY_BATCH_LIMIT = 20;
|
||||
const AUCTION_DURABILITY_RETRY_DELAY_MS = 5000;
|
||||
@@ -167,6 +168,21 @@ export class AuctionDurabilityScheduler
|
||||
}
|
||||
await this.applyDurabilityPayload(payload);
|
||||
applied += 1;
|
||||
// 영속 지연(lag): Redis 커밋 → Mongo 영속까지 걸린 시간 + 이번 배치 백로그.
|
||||
// 사용자가 본 "되돌리기/이력이 2~3 뒤처짐"을 수치화한다(지연이 클수록 undo
|
||||
// 패널이 stale 시퀀스를 보여 엉뚱한 스냅샷 복원 가능성↑).
|
||||
recordNewRelicCustomEvent('AuctionServerDurability', {
|
||||
matchId: typeof payload.matchId === 'string' ? payload.matchId : '',
|
||||
auctionStateTransitionId: payload.auctionStateTransitionId,
|
||||
auctionActionType: payload.auctionActionType,
|
||||
revision: payload.revision,
|
||||
auctionHistorySequence: payload.auctionHistorySequence,
|
||||
persistLatencyMS:
|
||||
typeof payload.committedAtRedisTimestampMS === 'number'
|
||||
? currentRedisTimestampMS - payload.committedAtRedisTimestampMS
|
||||
: -1,
|
||||
batchBacklog: payloadKeys.length,
|
||||
});
|
||||
await this.clearDurabilityPayload(payloadKey);
|
||||
} catch (error) {
|
||||
failed += 1;
|
||||
|
||||
@@ -40,6 +40,7 @@ import {
|
||||
import { AuctionRedisService } from './redis/auction-redis.service';
|
||||
import { AuctionRedisStateReaderService } from './redis/auction-redis-state-reader.service';
|
||||
import { shouldHydrateRedisAuctionState } from './redis/auction-redis.state';
|
||||
import { recordNewRelicCustomEvent } from '../global/observability/new-relic';
|
||||
|
||||
const AUCTION_MAX_TEAM_SIZE = 5;
|
||||
const AUCTION_MIN_BID_POINTS = 10;
|
||||
@@ -909,6 +910,33 @@ export class AuctionService {
|
||||
return this.resolveReplayedRedisActionRequest(matchId, actionRequest);
|
||||
}
|
||||
|
||||
// 중복 멤버 버그 스모킹건: 낙찰 직전, 이 선수가 이미 어느 팀에 들어있는지.
|
||||
// (markRedisSold는 검사 없이 append하므로 true면 그대로 중복이 만들어진다)
|
||||
const soldUserId = String(currentPlayer.userId);
|
||||
const existingTeamIndex = structureInput.auctionTeams.find((entry) =>
|
||||
(entry.members ?? []).some(
|
||||
(member) => String(member.userId) === soldUserId,
|
||||
),
|
||||
)?.teamIndex;
|
||||
recordNewRelicCustomEvent('AuctionServerSold', {
|
||||
matchId,
|
||||
auctionActionRequestId: params.auctionActionRequestId ?? null,
|
||||
auctionLifecycleId: this.stringFromState(state.auctionLifecycleId),
|
||||
revision: this.numberFromState(state.revision),
|
||||
currentAuctionPlayerOrderIndex:
|
||||
structureInput.currentAuctionPlayerOrderIndex,
|
||||
soldUserId,
|
||||
winningTeamIndex,
|
||||
currentBidPointAmount,
|
||||
playerAlreadyInTeam: existingTeamIndex !== undefined,
|
||||
existingTeamIndex:
|
||||
existingTeamIndex !== undefined ? Number(existingTeamIndex) : -1,
|
||||
totalAssignedMemberCount: structureInput.auctionTeams.reduce(
|
||||
(sum, entry) => sum + (entry.members?.length ?? 0),
|
||||
0,
|
||||
),
|
||||
});
|
||||
|
||||
const preferredPrimary = currentPlayer.preferredPositions?.[0]?.position;
|
||||
team.members = [
|
||||
...(team.members ?? []),
|
||||
@@ -1721,6 +1749,27 @@ export class AuctionService {
|
||||
actionRequest: params.actionRequest?.actionRequest,
|
||||
},
|
||||
);
|
||||
// 모든 redis 전환(sold/undo/next/open-bidding/complete)의 백본 타임라인.
|
||||
// auctionStateTransitionId = `${action}:${lifecycleId}:${auctionActionRequestId}`
|
||||
// 라서 클라의 auctionActionRequestId와 조인된다.
|
||||
recordNewRelicCustomEvent('AuctionServerTransition', {
|
||||
matchId,
|
||||
action: params.action,
|
||||
auctionStateTransitionId,
|
||||
auctionLifecycleId,
|
||||
expectedRevision,
|
||||
newRevision: 'rejected' in result ? -1 : expectedRevision + 1,
|
||||
auctionHistorySequence,
|
||||
currentAuctionPlayerOrderIndex:
|
||||
params.structureInput.currentAuctionPlayerOrderIndex,
|
||||
totalAssignedMemberCount: params.structureInput.auctionTeams.reduce(
|
||||
(sum, entry) => sum + (entry.members?.length ?? 0),
|
||||
0,
|
||||
),
|
||||
rejected: 'rejected' in result,
|
||||
rejectionCode: 'rejected' in result ? result.code : null,
|
||||
});
|
||||
|
||||
if ('rejected' in result) {
|
||||
// 게이트웨이가 설명적 거부코드로 변환할 수 있도록 내부 code(예: CONFLICT)를
|
||||
// 구조화해 던진다(message만 던지면 code 자리에 풀 문장이 들어가 매핑이 어렵다).
|
||||
@@ -1835,6 +1884,33 @@ export class AuctionService {
|
||||
0,
|
||||
);
|
||||
|
||||
// undo가 "어느 시점으로" 복원하는지 + 복원된 팀 구성을 기록한다. 라이브 revision과
|
||||
// 복원 스냅샷 revision의 간격(restoredRevisionLag)이 사용자가 본 "2~3 뒤처짐".
|
||||
// 이 이벤트 직후의 AuctionServerSold가 playerAlreadyInTeam:true면 "undo가 선수를
|
||||
// 안 지운 채 재낙찰 → 중복"이 한 타임라인으로 증명된다.
|
||||
const liveRevisionBeforeUndo = this.numberFromState(state.revision);
|
||||
const restoredRevision = this.numberFromState(params.snapshot.revision);
|
||||
recordNewRelicCustomEvent('AuctionServerUndo', {
|
||||
matchId,
|
||||
auctionActionRequestId:
|
||||
params.actionRequestParams?.auctionActionRequestId ?? null,
|
||||
auctionLifecycleId: this.stringFromState(state.auctionLifecycleId),
|
||||
targetHistorySequence: params.targetHistorySequence,
|
||||
liveRevisionBeforeUndo,
|
||||
restoredRevision,
|
||||
restoredRevisionLag: liveRevisionBeforeUndo - restoredRevision,
|
||||
restoredHistorySequence: this.numberFromState(
|
||||
params.snapshot.currentAuctionHistorySequence,
|
||||
),
|
||||
restoredPhase: phase,
|
||||
restoredOrderIndex: structureInput.currentAuctionPlayerOrderIndex,
|
||||
restoredAssignedMemberCount: structureInput.auctionTeams.reduce(
|
||||
(sum, entry) => sum + (entry.members?.length ?? 0),
|
||||
0,
|
||||
),
|
||||
undoingCompletedAuction: params.undoingCompletedAuction,
|
||||
});
|
||||
|
||||
return this.applyRedisStructureTransition({
|
||||
action: 'undo',
|
||||
actionRequest,
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import newrelicAgent from 'newrelic';
|
||||
|
||||
export type NewRelicAttributeValue = string | number | boolean | null;
|
||||
|
||||
// newrelic 기본 export는 타입이 느슨해 unsafe-call로 잡힌다. 우리가 쓰는 부분만
|
||||
// 좁게 타입을 고정한다(에이전트는 -r newrelic로 이미 preload됨).
|
||||
interface NewRelicCustomEventApi {
|
||||
recordCustomEvent(
|
||||
eventType: string,
|
||||
attributes: Record<string, NewRelicAttributeValue>,
|
||||
): void;
|
||||
}
|
||||
const agent = newrelicAgent as unknown as NewRelicCustomEventApi;
|
||||
|
||||
/**
|
||||
* 서버 커스텀 이벤트를 New Relic으로 보낸다(클라의 addPageAction 대응).
|
||||
*
|
||||
* - 에이전트는 `-r newrelic`로 이미 preload되어 있으므로 여기서 import만 하면 된다.
|
||||
* - `recordCustomEvent`는 활성 트랜잭션이 없어도 동작하므로 durability 스케줄러
|
||||
* tick이나 socket.io 핸들러처럼 HTTP 컨텍스트 밖에서도 그대로 호출할 수 있다.
|
||||
* - 평탄한 스칼라만 받는다(중첩 객체/배열 불가). undefined 속성은 제거한다.
|
||||
* - 에이전트 비활성/테스트 환경에서도 호출부가 깨지지 않도록 예외를 삼킨다.
|
||||
*/
|
||||
export function recordNewRelicCustomEvent(
|
||||
eventType: string,
|
||||
attributes: Record<string, NewRelicAttributeValue | undefined>,
|
||||
): void {
|
||||
try {
|
||||
const flat: Record<string, NewRelicAttributeValue> = {};
|
||||
for (const [key, value] of Object.entries(attributes)) {
|
||||
if (value !== undefined) flat[key] = value;
|
||||
}
|
||||
agent.recordCustomEvent(eventType, flat);
|
||||
} catch {
|
||||
// 관측성 실패가 비즈니스 로직을 막아선 안 된다.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import { createRequire } from 'node:module';
|
||||
|
||||
const CONFIG_PATH = '../newrelic.cjs';
|
||||
const requireConfig = createRequire(__filename);
|
||||
|
||||
interface NewRelicConfig {
|
||||
app_name: string[];
|
||||
license_key: string;
|
||||
agent_enabled: boolean;
|
||||
ai_monitoring: {
|
||||
enabled: boolean;
|
||||
};
|
||||
custom_insights_events: {
|
||||
max_samples_stored: number;
|
||||
};
|
||||
span_events: {
|
||||
max_samples_stored: number;
|
||||
};
|
||||
}
|
||||
|
||||
interface NewRelicConfigModule {
|
||||
config: NewRelicConfig;
|
||||
}
|
||||
|
||||
function loadConfig(): NewRelicConfig {
|
||||
jest.resetModules();
|
||||
const module = requireConfig(CONFIG_PATH) as unknown as NewRelicConfigModule;
|
||||
return module.config;
|
||||
}
|
||||
|
||||
describe('New Relic config', () => {
|
||||
let originalEnv: NodeJS.ProcessEnv;
|
||||
|
||||
beforeEach(() => {
|
||||
originalEnv = { ...process.env };
|
||||
delete process.env.NEW_RELIC_ENABLED;
|
||||
delete process.env.NEW_RELIC_LICENSE_KEY;
|
||||
delete process.env.NEW_RELIC_APP_NAME;
|
||||
delete process.env.NEW_RELIC_AI_MONITORING_ENABLED;
|
||||
delete process.env.NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED;
|
||||
delete process.env.NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED;
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.env = originalEnv;
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
it('uses safe defaults and stays disabled without a license key', () => {
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.app_name).toEqual(['lolympus']);
|
||||
expect(config.agent_enabled).toBe(false);
|
||||
expect(config.ai_monitoring.enabled).toBe(true);
|
||||
expect(config.custom_insights_events.max_samples_stored).toBe(100000);
|
||||
expect(config.span_events.max_samples_stored).toBe(10000);
|
||||
});
|
||||
|
||||
it('enables the agent when a license key exists and honors runtime overrides', () => {
|
||||
process.env.NEW_RELIC_LICENSE_KEY = 'test-license';
|
||||
process.env.NEW_RELIC_APP_NAME = 'lolympus-api-test';
|
||||
process.env.NEW_RELIC_AI_MONITORING_ENABLED = 'false';
|
||||
process.env.NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED = '5000';
|
||||
process.env.NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED = '4000';
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.app_name).toEqual(['lolympus-api-test']);
|
||||
expect(config.license_key).toBe('test-license');
|
||||
expect(config.agent_enabled).toBe(true);
|
||||
expect(config.ai_monitoring.enabled).toBe(false);
|
||||
expect(config.custom_insights_events.max_samples_stored).toBe(5000);
|
||||
expect(config.span_events.max_samples_stored).toBe(4000);
|
||||
});
|
||||
|
||||
it('normalizes shorthand sample sizes before New Relic applies env overrides', () => {
|
||||
process.env.NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED = '100k';
|
||||
process.env.NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED = '10k';
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
expect(config.custom_insights_events.max_samples_stored).toBe(100000);
|
||||
expect(config.span_events.max_samples_stored).toBe(10000);
|
||||
expect(
|
||||
process.env.NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED,
|
||||
).toBe('100000');
|
||||
expect(process.env.NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED).toBe('10000');
|
||||
});
|
||||
});
|
||||
Generated
+808
-17
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user