mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 06:31:13 -05:00
Fix span duplicates and metrics
Implements complete plan to resolve duplicate span_id issues and metric collection errors: - Ensure edge handlers return proper Response objects to prevent double logging - Update collect-metrics to use valid metric categories, fix system_alerts query, and adjust returns - Apply detect-anomalies adjustments if needed and add defensive handling in wrapper - Prepare ground for end-to-end verification of location-related fixes
This commit is contained in:
@@ -266,7 +266,15 @@ export function wrapEdgeFunction(
|
|||||||
logSpanToDatabase(span, requestId);
|
logSpanToDatabase(span, requestId);
|
||||||
|
|
||||||
// Clone response to add tracking headers
|
// Clone response to add tracking headers
|
||||||
const responseBody = await response.text();
|
// Defensive check: ensure handler returned a Response object
|
||||||
|
let responseBody: string;
|
||||||
|
if (response instanceof Response) {
|
||||||
|
responseBody = await response.text();
|
||||||
|
} else {
|
||||||
|
// Handler returned non-Response (shouldn't happen, but handle it)
|
||||||
|
addSpanEvent(span, 'warning_non_response_object');
|
||||||
|
responseBody = JSON.stringify(response);
|
||||||
|
}
|
||||||
const enhancedResponse = new Response(responseBody, {
|
const enhancedResponse = new Response(responseBody, {
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
|
|||||||
metrics.push({
|
metrics.push({
|
||||||
metric_name: 'api_error_count',
|
metric_name: 'api_error_count',
|
||||||
metric_value: errorCount as number,
|
metric_value: errorCount as number,
|
||||||
metric_category: 'performance',
|
metric_category: 'api',
|
||||||
timestamp,
|
timestamp,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
|
|||||||
metrics.push({
|
metrics.push({
|
||||||
metric_name: 'rate_limit_violations',
|
metric_name: 'rate_limit_violations',
|
||||||
metric_value: violationCount as number,
|
metric_value: violationCount as number,
|
||||||
metric_category: 'security',
|
metric_category: 'rate_limit',
|
||||||
timestamp,
|
timestamp,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -61,7 +61,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
|
|||||||
metrics.push({
|
metrics.push({
|
||||||
metric_name: 'pending_submissions',
|
metric_name: 'pending_submissions',
|
||||||
metric_value: pendingCount as number,
|
metric_value: pendingCount as number,
|
||||||
metric_category: 'workflow',
|
metric_category: 'moderation',
|
||||||
timestamp,
|
timestamp,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
|
|||||||
metrics.push({
|
metrics.push({
|
||||||
metric_name: 'active_incidents',
|
metric_name: 'active_incidents',
|
||||||
metric_value: incidentCount as number,
|
metric_value: incidentCount as number,
|
||||||
metric_category: 'monitoring',
|
metric_category: 'system',
|
||||||
timestamp,
|
timestamp,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -86,14 +86,14 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
|
|||||||
const { data: unresolvedAlerts, error: alertsError } = await supabase
|
const { data: unresolvedAlerts, error: alertsError } = await supabase
|
||||||
.from('system_alerts')
|
.from('system_alerts')
|
||||||
.select('id', { count: 'exact', head: true })
|
.select('id', { count: 'exact', head: true })
|
||||||
.eq('resolved', false);
|
.is('resolved_at', null);
|
||||||
|
|
||||||
if (!alertsError) {
|
if (!alertsError) {
|
||||||
const alertCount = unresolvedAlerts || 0;
|
const alertCount = unresolvedAlerts || 0;
|
||||||
metrics.push({
|
metrics.push({
|
||||||
metric_name: 'unresolved_alerts',
|
metric_name: 'unresolved_alerts',
|
||||||
metric_value: alertCount as number,
|
metric_value: alertCount as number,
|
||||||
metric_category: 'monitoring',
|
metric_category: 'system',
|
||||||
timestamp,
|
timestamp,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -112,7 +112,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
|
|||||||
metrics.push({
|
metrics.push({
|
||||||
metric_name: 'submission_approval_rate',
|
metric_name: 'submission_approval_rate',
|
||||||
metric_value: approvalRate,
|
metric_value: approvalRate,
|
||||||
metric_category: 'workflow',
|
metric_category: 'moderation',
|
||||||
timestamp,
|
timestamp,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -136,7 +136,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
|
|||||||
metrics.push({
|
metrics.push({
|
||||||
metric_name: 'avg_moderation_time',
|
metric_name: 'avg_moderation_time',
|
||||||
metric_value: avgTimeMinutes,
|
metric_value: avgTimeMinutes,
|
||||||
metric_category: 'workflow',
|
metric_category: 'moderation',
|
||||||
timestamp,
|
timestamp,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -155,11 +155,17 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
|
|||||||
addSpanEvent(span, 'metrics_recorded', { count: metrics.length });
|
addSpanEvent(span, 'metrics_recorded', { count: metrics.length });
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
success: true,
|
success: true,
|
||||||
metrics_collected: metrics.length,
|
metrics_collected: metrics.length,
|
||||||
metrics: metrics.map(m => ({ name: m.metric_name, value: m.metric_value })),
|
metrics: metrics.map(m => ({ name: m.metric_name, value: m.metric_value })),
|
||||||
};
|
}),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
}
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
serve(createEdgeFunction({
|
serve(createEdgeFunction({
|
||||||
|
|||||||
@@ -474,11 +474,17 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
|
|||||||
|
|
||||||
addSpanEvent(span, 'anomaly_detection_complete', { detected: anomaliesDetected.length });
|
addSpanEvent(span, 'anomaly_detection_complete', { detected: anomaliesDetected.length });
|
||||||
|
|
||||||
return {
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
success: true,
|
success: true,
|
||||||
anomalies_detected: anomaliesDetected.length,
|
anomalies_detected: anomaliesDetected.length,
|
||||||
anomalies: anomaliesDetected,
|
anomalies: anomaliesDetected,
|
||||||
};
|
}),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
}
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
serve(createEdgeFunction({
|
serve(createEdgeFunction({
|
||||||
|
|||||||
Reference in New Issue
Block a user