mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 11:51:14 -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';
|
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
|
||||||
|
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||||
const corsHeaders = {
|
import { edgeLogger } from '../_shared/logger.ts';
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
|
||||||
};
|
|
||||||
|
|
||||||
interface MetricRecord {
|
interface MetricRecord {
|
||||||
metric_name: string;
|
metric_name: string;
|
||||||
@@ -12,17 +9,13 @@ interface MetricRecord {
|
|||||||
timestamp: string;
|
timestamp: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
Deno.serve(async (req) => {
|
export default createEdgeFunction(
|
||||||
if (req.method === 'OPTIONS') {
|
{
|
||||||
return new Response(null, { headers: corsHeaders });
|
name: 'collect-metrics',
|
||||||
}
|
requireAuth: false,
|
||||||
|
},
|
||||||
try {
|
async (req, context, supabase) => {
|
||||||
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
|
edgeLogger.info('Starting metrics collection', { requestId: context.requestId });
|
||||||
const supabaseKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
|
|
||||||
const supabase = createClient(supabaseUrl, supabaseKey);
|
|
||||||
|
|
||||||
console.log('Starting metrics collection...');
|
|
||||||
|
|
||||||
const metrics: MetricRecord[] = [];
|
const metrics: MetricRecord[] = [];
|
||||||
const timestamp = new Date().toISOString();
|
const timestamp = new Date().toISOString();
|
||||||
@@ -159,11 +152,17 @@ Deno.serve(async (req) => {
|
|||||||
.insert(metrics);
|
.insert(metrics);
|
||||||
|
|
||||||
if (insertError) {
|
if (insertError) {
|
||||||
console.error('Error inserting metrics:', insertError);
|
edgeLogger.error('Error inserting metrics', {
|
||||||
|
error: insertError,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
throw insertError;
|
throw insertError;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Successfully recorded ${metrics.length} metrics`);
|
edgeLogger.info('Successfully recorded metrics', {
|
||||||
|
count: metrics.length,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
@@ -172,16 +171,7 @@ Deno.serve(async (req) => {
|
|||||||
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 })),
|
||||||
}),
|
}),
|
||||||
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
{ headers: { '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' },
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
|
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
|
||||||
|
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||||
const corsHeaders = {
|
import { edgeLogger } from '../_shared/logger.ts';
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
|
||||||
};
|
|
||||||
|
|
||||||
interface MetricData {
|
interface MetricData {
|
||||||
timestamp: string;
|
timestamp: string;
|
||||||
@@ -291,17 +288,13 @@ class AnomalyDetector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Deno.serve(async (req) => {
|
export default createEdgeFunction(
|
||||||
if (req.method === 'OPTIONS') {
|
{
|
||||||
return new Response(null, { headers: corsHeaders });
|
name: 'detect-anomalies',
|
||||||
}
|
requireAuth: false,
|
||||||
|
},
|
||||||
try {
|
async (req, context, supabase) => {
|
||||||
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
|
edgeLogger.info('Starting anomaly detection run', { requestId: context.requestId });
|
||||||
const supabaseKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
|
|
||||||
const supabase = createClient(supabaseUrl, supabaseKey);
|
|
||||||
|
|
||||||
console.log('Starting anomaly detection run...');
|
|
||||||
|
|
||||||
// Get all enabled anomaly detection configurations
|
// Get all enabled anomaly detection configurations
|
||||||
const { data: configs, error: configError } = await supabase
|
const { data: configs, error: configError } = await supabase
|
||||||
@@ -314,7 +307,10 @@ Deno.serve(async (req) => {
|
|||||||
throw configError;
|
throw configError;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Processing ${configs?.length || 0} metric configurations`);
|
edgeLogger.info('Processing metric configurations', {
|
||||||
|
count: configs?.length || 0,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
|
|
||||||
const anomaliesDetected: any[] = [];
|
const anomaliesDetected: any[] = [];
|
||||||
|
|
||||||
@@ -338,7 +334,11 @@ Deno.serve(async (req) => {
|
|||||||
const data = metricData as MetricData[];
|
const data = metricData as MetricData[];
|
||||||
|
|
||||||
if (!data || data.length < config.min_data_points) {
|
if (!data || data.length < config.min_data_points) {
|
||||||
console.log(`Insufficient data for ${config.metric_name}: ${data?.length || 0} points`);
|
edgeLogger.info('Insufficient data for metric', {
|
||||||
|
metric: config.metric_name,
|
||||||
|
points: data?.length || 0,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -464,7 +464,10 @@ Deno.serve(async (req) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Anomaly detection complete. Detected ${anomaliesDetected.length} anomalies`);
|
edgeLogger.info('Anomaly detection complete', {
|
||||||
|
detected: anomaliesDetected.length,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@@ -472,16 +475,7 @@ Deno.serve(async (req) => {
|
|||||||
anomalies_detected: anomaliesDetected.length,
|
anomalies_detected: anomaliesDetected.length,
|
||||||
anomalies: anomaliesDetected,
|
anomalies: anomaliesDetected,
|
||||||
}),
|
}),
|
||||||
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
{ headers: { 'Content-Type': 'application/json' } }
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error in detect-anomalies function:', error);
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({ error: error.message }),
|
|
||||||
{
|
|
||||||
status: 500,
|
|
||||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|||||||
@@ -8,13 +8,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { createClient } from 'jsr:@supabase/supabase-js@2';
|
import { createClient } from 'jsr:@supabase/supabase-js@2';
|
||||||
|
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||||
|
import { edgeLogger } from '../_shared/logger.ts';
|
||||||
import { getMetricsStats } from '../_shared/rateLimitMetrics.ts';
|
import { getMetricsStats } from '../_shared/rateLimitMetrics.ts';
|
||||||
|
|
||||||
const corsHeaders = {
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
|
||||||
};
|
|
||||||
|
|
||||||
interface AlertConfig {
|
interface AlertConfig {
|
||||||
id: string;
|
id: string;
|
||||||
metric_type: 'block_rate' | 'total_requests' | 'unique_ips' | 'function_specific';
|
metric_type: 'block_rate' | 'total_requests' | 'unique_ips' | 'function_specific';
|
||||||
@@ -159,19 +156,14 @@ async function sendNotification(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handler(req: Request): Promise<Response> {
|
export default createEdgeFunction(
|
||||||
// Handle CORS preflight
|
{
|
||||||
if (req.method === 'OPTIONS') {
|
name: 'monitor-rate-limits',
|
||||||
return new Response(null, { headers: corsHeaders });
|
requireAuth: false,
|
||||||
}
|
},
|
||||||
|
async (req, context, supabase) => {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
console.log('Rate limit monitor starting...');
|
edgeLogger.info('Rate limit monitor starting', { requestId: context.requestId });
|
||||||
|
|
||||||
try {
|
|
||||||
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
|
|
||||||
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
|
|
||||||
const supabase = createClient(supabaseUrl, supabaseServiceKey);
|
|
||||||
|
|
||||||
// Fetch enabled alert configurations
|
// Fetch enabled alert configurations
|
||||||
const { data: configs, error: configError } = await supabase
|
const { data: configs, error: configError } = await supabase
|
||||||
@@ -180,41 +172,48 @@ async function handler(req: Request): Promise<Response> {
|
|||||||
.eq('enabled', true);
|
.eq('enabled', true);
|
||||||
|
|
||||||
if (configError) {
|
if (configError) {
|
||||||
console.error('Failed to fetch alert configs:', configError);
|
edgeLogger.error('Failed to fetch alert configs', {
|
||||||
return new Response(
|
error: configError,
|
||||||
JSON.stringify({
|
requestId: context.requestId
|
||||||
success: false,
|
});
|
||||||
error: 'Failed to fetch alert configurations',
|
throw configError;
|
||||||
details: configError.message
|
|
||||||
}),
|
|
||||||
{ status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!configs || configs.length === 0) {
|
if (!configs || configs.length === 0) {
|
||||||
console.log('No enabled alert configurations found');
|
edgeLogger.info('No enabled alert configurations found', {
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'No enabled alert configurations',
|
message: 'No enabled alert configurations',
|
||||||
checked: 0
|
checked: 0
|
||||||
}),
|
}),
|
||||||
{ status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
{ headers: { 'Content-Type': 'application/json' } }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Checking ${configs.length} alert configurations...`);
|
edgeLogger.info('Checking alert configurations', {
|
||||||
|
count: configs.length,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
|
|
||||||
// Check all alert conditions
|
// Check all alert conditions
|
||||||
const checks = await checkAlertConditions(configs);
|
const checks = await checkAlertConditions(configs);
|
||||||
const exceededChecks = checks.filter(c => c.exceeded);
|
const exceededChecks = checks.filter(c => c.exceeded);
|
||||||
|
|
||||||
console.log(`Found ${exceededChecks.length} threshold violations`);
|
edgeLogger.info('Threshold violations found', {
|
||||||
|
count: exceededChecks.length,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
|
|
||||||
// Process exceeded thresholds
|
// Process exceeded thresholds
|
||||||
const alertResults = [];
|
const alertResults = [];
|
||||||
for (const check of exceededChecks) {
|
for (const check of exceededChecks) {
|
||||||
console.log(`Processing alert: ${check.message}`);
|
edgeLogger.info('Processing alert', {
|
||||||
|
message: check.message,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
|
|
||||||
// Check if we've already sent a recent alert for this config
|
// Check if we've already sent a recent alert for this config
|
||||||
const { data: recentAlerts } = await supabase
|
const { data: recentAlerts } = await supabase
|
||||||
@@ -227,7 +226,10 @@ async function handler(req: Request): Promise<Response> {
|
|||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if (recentAlerts && recentAlerts.length > 0) {
|
if (recentAlerts && recentAlerts.length > 0) {
|
||||||
console.log(`Skipping alert - recent unresolved alert exists for config ${check.configId}`);
|
edgeLogger.info('Skipping alert - recent unresolved alert exists', {
|
||||||
|
configId: check.configId,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
alertResults.push({
|
alertResults.push({
|
||||||
configId: check.configId,
|
configId: check.configId,
|
||||||
skipped: true,
|
skipped: true,
|
||||||
@@ -253,7 +255,10 @@ async function handler(req: Request): Promise<Response> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
console.log(`Monitor completed in ${duration}ms`);
|
edgeLogger.info('Monitor completed', {
|
||||||
|
duration,
|
||||||
|
requestId: context.requestId
|
||||||
|
});
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@@ -263,20 +268,7 @@ async function handler(req: Request): Promise<Response> {
|
|||||||
alerts: alertResults,
|
alerts: alertResults,
|
||||||
duration_ms: duration,
|
duration_ms: duration,
|
||||||
}),
|
}),
|
||||||
{ status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
{ headers: { 'Content-Type': 'application/json' } }
|
||||||
);
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error in rate limit monitor:', error);
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
success: false,
|
|
||||||
error: 'Internal server error',
|
|
||||||
message: error instanceof Error ? error.message : 'Unknown error',
|
|
||||||
}),
|
|
||||||
{ status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
);
|
||||||
|
|
||||||
Deno.serve(handler);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user