Migrate remaining edge functions to wrapper

Refactor process-expired-bans, detect-location, detect-anomalies, rate-limit-metrics, and collect-metrics to use createEdgeFunction wrapper with standardized error handling, tracing, and reduced boilerplate. Update signatures to receive { supabase, span, requestId } (and user where applicable), replace manual logging with span events, remove per-function boilerplate, and ensure consistent wrapper configuration (cors, auth, rate limits, and tracing).
This commit is contained in:
gpt-engineer-app[bot]
2025-11-11 20:30:24 +00:00
parent 4040fd783e
commit de921a5fcf
5 changed files with 197 additions and 376 deletions

View File

@@ -1,6 +1,7 @@
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
import { edgeLogger } from '../_shared/logger.ts';
import { serve } from 'https://deno.land/std@0.190.0/http/server.ts';
import { createEdgeFunction, type EdgeFunctionContext } from '../_shared/edgeFunctionWrapper.ts';
import { addSpanEvent } from '../_shared/logger.ts';
import { corsHeaders } from '../_shared/cors.ts';
interface MetricRecord {
metric_name: string;
@@ -9,13 +10,8 @@ interface MetricRecord {
timestamp: string;
}
export default createEdgeFunction(
{
name: 'collect-metrics',
requireAuth: false,
},
async (req, context, supabase) => {
edgeLogger.info('Starting metrics collection', { requestId: context.requestId });
const handler = async (req: Request, { supabase, span, requestId }: EdgeFunctionContext) => {
addSpanEvent(span, 'starting_metrics_collection', { requestId });
const metrics: MetricRecord[] = [];
const timestamp = new Date().toISOString();
@@ -151,27 +147,24 @@ export default createEdgeFunction(
.from('metric_time_series')
.insert(metrics);
if (insertError) {
edgeLogger.error('Error inserting metrics', {
error: insertError,
requestId: context.requestId
});
throw insertError;
}
edgeLogger.info('Successfully recorded metrics', {
count: metrics.length,
requestId: context.requestId
});
if (insertError) {
addSpanEvent(span, 'error_inserting_metrics', { error: insertError.message });
throw insertError;
}
return new Response(
JSON.stringify({
success: true,
metrics_collected: metrics.length,
metrics: metrics.map(m => ({ name: m.metric_name, value: m.metric_value })),
}),
{ headers: { 'Content-Type': 'application/json' } }
);
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 })),
};
};
serve(createEdgeFunction({
name: 'collect-metrics',
requireAuth: false,
corsHeaders,
enableTracing: true,
}, handler));