fix: address Code Reviewer findings for Datadog/Sentry integration FRE-4806
P1: Load dd-trace before other modules via datadog-init.ts entry point P1: Batch all CloudWatch metrics into single PutMetricDataCommand per request P2: Deduplicate warning logs with else-if for high latency vs error P3: Add response.ok check to Datadog log forwarding fetch P3: Update getSentryHub() to use getCurrentScope() for Sentry SDK 8.x Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -62,6 +62,35 @@ export async function emitMetric(
|
||||
}
|
||||
}
|
||||
|
||||
export async function emitBatchMetrics(metrics: {
|
||||
serviceName: string;
|
||||
data: { metricName: string; value: number; unit: StandardUnit; dimensions?: Record<string, string> }[];
|
||||
}) {
|
||||
const cw = getClient();
|
||||
if (!cw) return;
|
||||
|
||||
const metricData = metrics.data.map((m) => ({
|
||||
MetricName: m.metricName,
|
||||
Dimensions: [
|
||||
{ Name: 'service', Value: metrics.serviceName },
|
||||
...(m.dimensions ? Object.entries(m.dimensions).map(([n, v]) => ({ Name: n, Value: v })) : []),
|
||||
],
|
||||
Value: m.value,
|
||||
Unit: m.unit,
|
||||
}));
|
||||
|
||||
const command = new PutMetricDataCommand({
|
||||
Namespace: NAMESPACE,
|
||||
MetricData: metricData,
|
||||
});
|
||||
|
||||
try {
|
||||
await cw.send(command);
|
||||
} catch (err) {
|
||||
console.warn('[CloudWatch] Batch metric emit failed:', (err as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
export async function emitLatency(
|
||||
serviceName: string,
|
||||
latencyMs: number,
|
||||
|
||||
8
packages/monitoring/src/datadog-init.ts
Normal file
8
packages/monitoring/src/datadog-init.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { getMonitoringConfig } from './config';
|
||||
import { initDatadog } from './datadog';
|
||||
import { initSentry } from './sentry';
|
||||
import { initDatadogLogs } from './datadog-logs';
|
||||
|
||||
initDatadog();
|
||||
initSentry();
|
||||
initDatadogLogs();
|
||||
@@ -24,7 +24,7 @@ export function initDatadogLogs() {
|
||||
service,
|
||||
});
|
||||
|
||||
await fetch(`${logIntakeUrl}/api/v2/logs`, {
|
||||
const response = await fetch(`${logIntakeUrl}/api/v2/logs`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'DD-API-KEY': process.env.DD_API_KEY!,
|
||||
@@ -32,6 +32,12 @@ export function initDatadogLogs() {
|
||||
},
|
||||
body: payload,
|
||||
});
|
||||
if (!response.ok) {
|
||||
console.warn(
|
||||
`[Datadog Logs] HTTP ${response.status} response from intake API`,
|
||||
await response.text()
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[Datadog Logs] Forward failed:', (err as Error).message);
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ export function setSentryContext(name: string, data: Record<string, unknown>) {
|
||||
export function getSentryHub() {
|
||||
try {
|
||||
const Sentry = require('@sentry/node');
|
||||
return Sentry.getCurrentHub?.() || Sentry.hub;
|
||||
return Sentry.getCurrentScope?.() || Sentry.getCurrentHub?.() || Sentry.hub;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user