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
@@ -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 {
|
||||
// 관측성 실패가 비즈니스 로직을 막아선 안 된다.
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user