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:
gpt-engineer-app[bot]
2025-11-12 04:57:54 +00:00
parent b6d1b99f2b
commit 5531376edf
3 changed files with 39 additions and 19 deletions

View File

@@ -28,7 +28,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
metrics.push({
metric_name: 'api_error_count',
metric_value: errorCount as number,
metric_category: 'performance',
metric_category: 'api',
timestamp,
});
}
@@ -45,7 +45,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
metrics.push({
metric_name: 'rate_limit_violations',
metric_value: violationCount as number,
metric_category: 'security',
metric_category: 'rate_limit',
timestamp,
});
}
@@ -61,7 +61,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
metrics.push({
metric_name: 'pending_submissions',
metric_value: pendingCount as number,
metric_category: 'workflow',
metric_category: 'moderation',
timestamp,
});
}
@@ -77,7 +77,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
metrics.push({
metric_name: 'active_incidents',
metric_value: incidentCount as number,
metric_category: 'monitoring',
metric_category: 'system',
timestamp,
});
}
@@ -86,14 +86,14 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
const { data: unresolvedAlerts, error: alertsError } = await supabase
.from('system_alerts')
.select('id', { count: 'exact', head: true })
.eq('resolved', false);
.is('resolved_at', null);
if (!alertsError) {
const alertCount = unresolvedAlerts || 0;
metrics.push({
metric_name: 'unresolved_alerts',
metric_value: alertCount as number,
metric_category: 'monitoring',
metric_category: 'system',
timestamp,
});
}
@@ -112,7 +112,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
metrics.push({
metric_name: 'submission_approval_rate',
metric_value: approvalRate,
metric_category: 'workflow',
metric_category: 'moderation',
timestamp,
});
}
@@ -136,7 +136,7 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
metrics.push({
metric_name: 'avg_moderation_time',
metric_value: avgTimeMinutes,
metric_category: 'workflow',
metric_category: 'moderation',
timestamp,
});
}
@@ -155,11 +155,17 @@ const handler = async (req: Request, { supabase, span, requestId }: EdgeFunction
addSpanEvent(span, 'metrics_recorded', { count: metrics.length });
}
return {
success: true,
metrics_collected: metrics.length,
metrics: metrics.map(m => ({ name: m.metric_name, value: m.metric_value })),
};
return new Response(
JSON.stringify({
success: true,
metrics_collected: metrics.length,
metrics: metrics.map(m => ({ name: m.metric_name, value: m.metric_value })),
}),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
}
);
};
serve(createEdgeFunction({