mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 14:31:13 -05:00
Migrate Phase 2 Batch 1
Migrate 3 Phase 2 monitoring functions (collect-metrics, detect-anomalies, monitor-rate-limits) to use wrapEdgeFunction with smaller batch updates, replacing manual handlers, adding shared logging/tracing, and standardizing error handling.
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||||
};
|
||||
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||
import { edgeLogger } from '../_shared/logger.ts';
|
||||
|
||||
interface MetricRecord {
|
||||
metric_name: string;
|
||||
@@ -12,17 +9,13 @@ interface MetricRecord {
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response(null, { headers: corsHeaders });
|
||||
}
|
||||
|
||||
try {
|
||||
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
|
||||
const supabaseKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
|
||||
const supabase = createClient(supabaseUrl, supabaseKey);
|
||||
|
||||
console.log('Starting metrics collection...');
|
||||
export default createEdgeFunction(
|
||||
{
|
||||
name: 'collect-metrics',
|
||||
requireAuth: false,
|
||||
},
|
||||
async (req, context, supabase) => {
|
||||
edgeLogger.info('Starting metrics collection', { requestId: context.requestId });
|
||||
|
||||
const metrics: MetricRecord[] = [];
|
||||
const timestamp = new Date().toISOString();
|
||||
@@ -159,11 +152,17 @@ Deno.serve(async (req) => {
|
||||
.insert(metrics);
|
||||
|
||||
if (insertError) {
|
||||
console.error('Error inserting metrics:', insertError);
|
||||
edgeLogger.error('Error inserting metrics', {
|
||||
error: insertError,
|
||||
requestId: context.requestId
|
||||
});
|
||||
throw insertError;
|
||||
}
|
||||
|
||||
console.log(`Successfully recorded ${metrics.length} metrics`);
|
||||
edgeLogger.info('Successfully recorded metrics', {
|
||||
count: metrics.length,
|
||||
requestId: context.requestId
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(
|
||||
@@ -172,16 +171,7 @@ Deno.serve(async (req) => {
|
||||
metrics_collected: metrics.length,
|
||||
metrics: metrics.map(m => ({ name: m.metric_name, value: m.metric_value })),
|
||||
}),
|
||||
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error in collect-metrics function:', error);
|
||||
return new Response(
|
||||
JSON.stringify({ error: error.message }),
|
||||
{
|
||||
status: 500,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
}
|
||||
{ headers: { 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
});
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user