diff --git a/cabal.project b/cabal.project
index 361bb2cf5..9eb59cf2c 100644
--- a/cabal.project
+++ b/cabal.project
@@ -39,8 +39,8 @@ source-repository-package
source-repository-package
type: git
- location: https://github.com/tonyalaribe/proto-lens
- tag: da3a3c7d8f43b7b22a3325a6706eb2aad98f41be
+ location: https://github.com/dawkaka/proto-lens
+ tag: cf4e060ea3376b68b65a9e207115f55b38cf1a12
subdir: proto-lens-setup
subdir: proto-lens-protoc
subdir: proto-lens-runtime
diff --git a/monoscope.cabal b/monoscope.cabal
index 6cdaca782..cefed77aa 100644
--- a/monoscope.cabal
+++ b/monoscope.cabal
@@ -80,6 +80,8 @@ library
Models.Apis.Monitors
Models.Apis.Reports
Models.Apis.RequestDumps
+ Models.Apis.Errors
+ Models.Apis.LogPatterns
Models.Apis.Shapes
Models.Apis.Slack
Models.Projects.Dashboards
diff --git a/package.yaml b/package.yaml
index cb44df54d..61d686ab5 100644
--- a/package.yaml
+++ b/package.yaml
@@ -13,7 +13,6 @@ custom-setup:
dependencies:
- base
- Cabal
- - proto-lens-setup
build-tools:
- proto-lens-protoc:proto-lens-protoc
diff --git a/src/BackgroundJobs.hs b/src/BackgroundJobs.hs
index 92984951f..940a80a2b 100644
--- a/src/BackgroundJobs.hs
+++ b/src/BackgroundJobs.hs
@@ -18,11 +18,12 @@ import Data.Text.Display (display)
import Data.Time (DayOfWeek (Monday), UTCTime (utctDay), ZonedTime, addUTCTime, dayOfWeek, formatTime, getZonedTime)
import Data.Time.Clock (diffUTCTime)
import Data.Time.Format (defaultTimeLocale)
-import Data.Time.LocalTime (LocalTime (localDay), ZonedTime (zonedTimeToLocalTime), getCurrentTimeZone, utcToZonedTime)
+import Data.Time.LocalTime (LocalTime (localDay), ZonedTime (zonedTimeToLocalTime), getCurrentTimeZone, utcToZonedTime, zonedTimeToUTC)
import Data.UUID qualified as UUID
import Data.UUID.V4 qualified as UUIDV4
import Data.Vector qualified as V
import Database.PostgreSQL.Simple (SomePostgreSqlException)
+import Database.PostgreSQL.Simple.Newtypes (Aeson (..))
import Database.PostgreSQL.Simple.SqlQQ (sql)
import Database.PostgreSQL.Simple.Types
import Effectful (Eff, IOE, (:>))
@@ -38,10 +39,12 @@ import Log (LogLevel (..), Logger, runLogT)
import Log qualified as LogLegacy
import Models.Apis.Anomalies qualified as Anomalies
import Models.Apis.Endpoints qualified as Endpoints
+import Models.Apis.Errors qualified as Errors
import Models.Apis.Fields.Facets qualified as Facets
import Models.Apis.Fields.Types qualified as Fields
import Models.Apis.Issues qualified as Issues
import Models.Apis.Issues.Enhancement qualified as Enhancement
+import Models.Apis.LogPatterns qualified as LogPatterns
import Models.Apis.Monitors qualified as Monitors
import Models.Apis.Reports qualified as Reports
import Models.Apis.RequestDumps (ATError (..))
@@ -64,7 +67,7 @@ import OpenTelemetry.Attributes qualified as OA
import OpenTelemetry.Trace (TracerProvider)
import Pages.Charts.Charts qualified as Charts
import Pages.Reports qualified as RP
-import Pkg.DeriveUtils (UUIDId (..))
+import Pkg.DeriveUtils (BaselineState (..), UUIDId (..))
import Pkg.Drain qualified as Drain
import Pkg.GitHub qualified as GitHub
import Pkg.Mail (NotificationAlerts (..), sendDiscordAlert, sendPostmarkEmail, sendSlackAlert, sendSlackMessage, sendWhatsAppAlert)
@@ -80,21 +83,13 @@ import System.Logging qualified as Log
import System.Tracing (SpanStatus (..), Tracing, addEvent, setStatus, withSpan)
import System.Types (ATBackgroundCtx, DB, runBackground)
import UnliftIO.Exception (bracket, catch, try)
-import Utils (DBField)
+import Utils (DBField, toXXHash)
data BgJobs
= InviteUserToProject Users.UserId Projects.ProjectId Text Text
| CreatedProjectSuccessfully Users.UserId Projects.ProjectId Text Text
| SendDiscordData Users.UserId Projects.ProjectId Text [Text] Text
- | -- NewAnomaly Projects.ProjectId Anomalies.AnomalyTypes Anomalies.AnomalyActions TargetHash
- NewAnomaly
- { projectId :: Projects.ProjectId
- , createdAt :: ZonedTime
- , anomalyType :: Text
- , anomalyAction :: Text
- , targetHashes :: [Text]
- }
| DailyReports Projects.ProjectId
| WeeklyReports Projects.ProjectId
| DailyJob
@@ -114,6 +109,23 @@ data BgJobs
| GitSyncFromRepo Projects.ProjectId
| GitSyncPushDashboard Projects.ProjectId UUID.UUID -- projectId, dashboardId
| GitSyncPushAllDashboards Projects.ProjectId -- Push all existing dashboards to repo
+ | -- Error processing jobs
+ ErrorBaselineCalculation Projects.ProjectId -- Calculate baselines for all errors in a project
+ | ErrorSpikeDetection Projects.ProjectId -- Detect error spikes and create issues
+ | NewErrorDetected Projects.ProjectId Text -- projectId, error hash - creates issue for new error
+ | -- Log pattern processing jobs
+ LogPatternBaselineCalculation Projects.ProjectId -- Calculate baselines for log patterns
+ | LogPatternSpikeDetection Projects.ProjectId -- Detect log pattern volume spikes
+ | NewLogPatternDetected Projects.ProjectId Text -- projectId, pattern hash - notify about new pattern
+ | -- Endpoint anomaly detection jobs
+ EndpointBaselineCalculation Projects.ProjectId -- Calculate baselines for endpoints (latency, error rate, volume)
+ | EndpointLatencyDegradationDetection Projects.ProjectId -- Detect endpoint latency degradation
+ | EndpointErrorRateSpikeDetection Projects.ProjectId -- Detect endpoint error rate spikes
+ | EndpointVolumeRateChangeDetection Projects.ProjectId -- Detect endpoint volume changes (spike/drop)
+ | -- API change detection jobs (from SQL triggers)
+ NewEndpoint Projects.ProjectId Text -- projectId, endpoint hash - creates issue for new endpoint
+ | NewShape Projects.ProjectId Text -- projectId, shape hash - creates issue for new shape
+ | NewFieldChange Projects.ProjectId Text -- projectId, field hash - creates issue for field change
deriving stock (Generic, Show)
deriving anyclass (AE.FromJSON, AE.ToJSON)
@@ -167,7 +179,6 @@ processBackgroundJob authCtx bgJob =
case bgJob of
GenerateOtelFacetsBatch pids timestamp -> generateOtelFacetsBatch pids timestamp
QueryMonitorsTriggered queryMonitorIds -> queryMonitorsTriggered queryMonitorIds authCtx
- NewAnomaly{projectId, createdAt, anomalyType, anomalyAction, targetHashes} -> newAnomalyJob projectId createdAt anomalyType anomalyAction (V.fromList targetHashes)
InviteUserToProject userId projectId reciever projectTitle' -> do
userM <- Users.userById userId
whenJust userM \user -> do
@@ -352,7 +363,24 @@ processBackgroundJob authCtx bgJob =
GitSyncFromRepo pid -> gitSyncFromRepo pid
GitSyncPushDashboard pid dashboardId -> gitSyncPushDashboard pid (UUIDId dashboardId)
GitSyncPushAllDashboards pid -> gitSyncPushAllDashboards pid
- QueryMonitorsCheck -> checkTriggeredQueryMonitors
+ QueryMonitorsCheck -> pass -- checkTriggeredQueryMonitors
+ -- Error processing jobs
+ ErrorBaselineCalculation pid -> calculateErrorBaselines pid
+ ErrorSpikeDetection pid -> detectErrorSpikes pid authCtx
+ NewErrorDetected pid errorHash -> processNewError pid errorHash authCtx
+ -- Log pattern processing jobs
+ LogPatternBaselineCalculation pid -> calculateLogPatternBaselines pid
+ LogPatternSpikeDetection pid -> detectLogPatternSpikes pid authCtx
+ NewLogPatternDetected pid patternHash -> processNewLogPattern pid patternHash authCtx
+ -- Endpoint anomaly detection jobs
+ EndpointBaselineCalculation pid -> calculateEndpointBaselines pid
+ EndpointLatencyDegradationDetection pid -> detectEndpointLatencyDegradation pid authCtx
+ EndpointErrorRateSpikeDetection pid -> detectEndpointErrorRateSpike pid authCtx
+ EndpointVolumeRateChangeDetection pid -> detectEndpointVolumeRateChange pid authCtx
+ -- API change detection jobs
+ NewEndpoint pid hash -> processNewEndpoint pid hash authCtx
+ NewShape pid hash -> processNewShape pid hash authCtx
+ NewFieldChange pid hash -> processNewFieldChange pid hash authCtx
-- | Run hourly scheduled tasks for all projects
@@ -382,6 +410,21 @@ runHourlyJob scheduledTime hour = do
let batchJob = BackgroundJobs.GenerateOtelFacetsBatch (V.fromList batch) scheduledTime
createJob conn "background_jobs" batchJob
+ -- Schedule baseline calculation and spike detection for active projects
+ liftIO $ withResource ctx.jobsPool \conn ->
+ forM_ activeProjects \pid -> do
+ -- Error baseline and spike detection
+ _ <- createJob conn "background_jobs" $ ErrorBaselineCalculation pid
+ _ <- createJob conn "background_jobs" $ ErrorSpikeDetection pid
+ -- Log pattern baseline and spike detection
+ _ <- createJob conn "background_jobs" $ LogPatternBaselineCalculation pid
+ _ <- createJob conn "background_jobs" $ LogPatternSpikeDetection pid
+ -- Endpoint baseline and anomaly detection
+ _ <- createJob conn "background_jobs" $ EndpointBaselineCalculation pid
+ _ <- createJob conn "background_jobs" $ EndpointLatencyDegradationDetection pid
+ _ <- createJob conn "background_jobs" $ EndpointErrorRateSpikeDetection pid
+ createJob conn "background_jobs" $ EndpointVolumeRateChangeDetection pid
+
-- Cleanup expired query cache entries
deletedCount <- QueryCache.cleanupExpiredCache
Relude.when (deletedCount > 0) $ Log.logInfo "Cleaned up expired query cache entries" ("deleted_count", AE.toJSON deletedCount)
@@ -481,52 +524,61 @@ logsPatternExtraction scheduledTime pid = do
limitVal = 250
paginate :: Int -> UTCTime -> ATBackgroundCtx ()
paginate offset startTime = do
- otelEvents <- PG.query [sql| SELECT kind, id::text, coalesce(body::text,''), coalesce(summary::text,'') FROM otel_logs_and_spans WHERE project_id = ? AND timestamp >= ? AND timestamp < ? AND (summary_pattern IS NULL OR log_pattern IS NULL) OFFSET ? LIMIT ?|] (pid, startTime, scheduledTime, offset, limitVal)
+ otelEvents :: [(Text, Text, Text, Text, Maybe Text, Maybe Text, Maybe Text)] <- PG.query [sql| SELECT kind, id::text, coalesce(body::text,''), coalesce(summary::text,''), context___trace_id, resource___service___name, level FROM otel_logs_and_spans WHERE project_id = ? AND timestamp >= ? AND timestamp < ? AND (summary_pattern IS NULL OR log_pattern IS NULL) OFFSET ? LIMIT ?|] (pid, startTime, scheduledTime, offset, limitVal)
unless (null otelEvents) do
Log.logInfo "Fetching events for pattern extraction" ("offset", AE.toJSON offset, "count", AE.toJSON (length otelEvents))
- let (logs, summaries) = L.partition (\(k, _, _, _) -> k == "log") otelEvents
- processPatterns "log" "log_pattern" (V.fromList [(i, body) | (_, i, body, _) <- logs]) pid scheduledTime startTime
- processPatterns "summary" "summary_pattern" (V.fromList [(i, s) | (_, i, _, s) <- summaries]) pid scheduledTime startTime
+ let (logs, summaries) = L.partition (\(k, _, _, _, _, _, _) -> k == "log") otelEvents
+ processPatterns "log" "log_pattern" (V.fromList [(i, body, trId, serviceName, level) | (_, i, body, _, trId, serviceName, level) <- logs]) pid scheduledTime startTime
+ processPatterns "summary" "summary_pattern" (V.fromList [(i, s, trId, serviceName, level) | (_, i, _, s, trId, serviceName, level) <- summaries]) pid scheduledTime startTime
Log.logInfo "Completed events pattern extraction for page" ("offset", AE.toJSON offset)
Relude.when (length otelEvents == limitVal) $ paginate (offset + limitVal) startTime
-- | Generic pattern extraction for logs or summaries
-processPatterns :: Text -> Text -> V.Vector (Text, Text) -> Projects.ProjectId -> UTCTime -> UTCTime -> ATBackgroundCtx ()
+-- events: (id, content, traceId, serviceName, level)
+processPatterns :: Text -> Text -> V.Vector (Text, Text, Maybe Text, Maybe Text, Maybe Text) -> Projects.ProjectId -> UTCTime -> UTCTime -> ATBackgroundCtx ()
processPatterns kind fieldName events pid scheduledTime since = do
Relude.when (not $ V.null events) $ do
- let qq = [text| select $fieldName from otel_logs_and_spans where project_id= ? AND timestamp >= now() - interval '1 hour' and $fieldName is not null GROUP BY $fieldName ORDER BY count(*) desc limit 20|]
- existingPatterns <- coerce @[Only Text] @[Text] <$> PG.query (Query $ encodeUtf8 qq) pid
- let known = V.fromList $ map ("",) existingPatterns
- combined = known <> events
+ existingPatterns <- LogPatterns.getLogPatternTexts pid
+ let known = V.fromList $ map (\pat -> ("", False, pat, Nothing, Nothing, Nothing)) existingPatterns
+ -- Include level in content for pattern matching so different levels create different patterns
+ combined = known <> ((\(logId, content, trId, serviceName, level) -> (logId, True, content, trId, serviceName, level)) <$> events)
drainTree = processBatch (kind == "summary") combined scheduledTime Drain.emptyDrainTree
newPatterns = Drain.getAllLogGroups drainTree
-- Only log if patterns were extracted
Relude.when (V.length newPatterns > 0)
$ Log.logInfo ("Extracted " <> kind <> " patterns") ("count", AE.toJSON $ V.length newPatterns)
- forM_ newPatterns \(patternTxt, ids) -> do
+ forM_ newPatterns \(sampleMsg, patternTxt, ids) -> do
let q = [text|UPDATE otel_logs_and_spans SET $fieldName = ? WHERE project_id = ? AND timestamp > ? AND id::text = ANY(?)|]
- unless (V.null ids)
- $ void
- $ PG.execute (Query $ encodeUtf8 q) (patternTxt, pid, since, V.filter (/= "") ids)
+ unless (V.null ids) $ do
+ -- Update otel_logs_and_spans with pattern
+ void $ PG.execute (Query $ encodeUtf8 q) (patternTxt, pid, since, V.filter (/= "") ids)
+ Relude.when (kind == "log" && not (T.null patternTxt)) $ do
+ let (serviceName, logLevel, logTraceId) = case V.head ids of
+ logId | logId /= "" -> case V.find (\(i, _, _, sName, lvl) -> i == logId) events of
+ Just (_, _, trId, sName, lvl) -> (sName, lvl, trId)
+ Nothing -> (Nothing, Nothing, Nothing)
+ _ -> (Nothing, Nothing, Nothing)
+ let patternHash = toXXHash patternTxt
+ void $ LogPatterns.upsertLogPattern pid patternTxt patternHash serviceName logLevel logTraceId (Just sampleMsg)
--- | Process a batch of (id, content) pairs through Drain
-processBatch :: Bool -> V.Vector (Text, Text) -> UTCTime -> Drain.DrainTree -> Drain.DrainTree
+-- | Process a batch of (id, isSampleLog, content, serviceName, level) tuples through Drain
+processBatch :: Bool -> V.Vector (Text, Bool, Text, Maybe Text, Maybe Text, Maybe Text) -> UTCTime -> Drain.DrainTree -> Drain.DrainTree
processBatch isSummary batch now inTree =
- V.foldl' (\tree (logId, content) -> processNewLog isSummary logId content now tree) inTree batch
+ V.foldl' (\tree (logId, isSampleLog, content, _, _, _) -> processNewLog isSummary logId isSampleLog content now tree) inTree batch
-processNewLog :: Bool -> Text -> Text -> UTCTime -> Drain.DrainTree -> Drain.DrainTree
-processNewLog isSummary logId content now tree =
+processNewLog :: Bool -> Text -> Bool -> Text -> UTCTime -> Drain.DrainTree -> Drain.DrainTree
+processNewLog _isSummary logId isSampleLog content now tree =
let tokens = Drain.generateDrainTokens content
in if V.null tokens
then tree
else
let tokenCount = V.length tokens
firstToken = V.head tokens
- in Drain.updateTreeWithLog tree tokenCount firstToken tokens logId content now
+ in Drain.updateTreeWithLog tree tokenCount firstToken tokens logId isSampleLog content now
-- | Process errors from OpenTelemetry spans to detect runtime exceptions
@@ -623,13 +675,13 @@ processOneMinuteErrors scheduledTime pid = do
-- Log.logInfo "Completed 1-minute error processing" ()
-- | Process and insert errors for a specific project
-processProjectErrors :: Projects.ProjectId -> V.Vector RequestDumps.ATError -> ATBackgroundCtx ()
+processProjectErrors :: Projects.ProjectId -> V.Vector Errors.ATError -> ATBackgroundCtx ()
processProjectErrors pid errors = do
-- Process each error, extracting HTTP fields if available
let processedErrors = V.map processError errors
-- Extract queries and params
- let (_, queries, paramsList) = V.unzip3 processedErrors
+ let (queries, paramsList) = V.unzip processedErrors
-- Bulk insert errors
result <- try $ V.zipWithM_ PG.execute queries paramsList
@@ -638,14 +690,13 @@ processProjectErrors pid errors = do
Left (e :: SomePostgreSqlException) ->
Log.logAttention "Failed to insert errors" ("error", AE.toJSON $ show e)
Right _ ->
- -- Only log if errors were actually inserted (reduces noise in tests)
Relude.when (V.length errors > 0)
$ Log.logInfo "Successfully inserted errors for project"
$ AE.object [("project_id", AE.toJSON pid.toText), ("error_count", AE.toJSON $ V.length errors)]
where
-- Process a single error - the error already has requestMethod and requestPath
-- set by getAllATErrors if it was extracted from span context
- processError :: RequestDumps.ATError -> (RequestDumps.ATError, Query, [DBField])
+ processError :: Errors.ATError -> (Query, [DBField])
processError = RequestMessages.processErrors pid Nothing Nothing Nothing
@@ -1039,7 +1090,7 @@ sendReportForProject pid rType = do
slowQueriesCount = V.length slowDbQueries
slowQueriesList = if slowQueriesCount == 0 then [AE.object ["message" AE..= "No slow queries detected."]] else (\(x, y, z) -> AE.object ["statement" AE..= x, "latency" AE..= z, "total" AE..= y]) <$> slowDbQueries
totalAnomalies = length anomalies'
- (errTotal, apiTotal, qTotal) = L.foldl (\(e, a, m) (_, _, _, _, t) -> (e + if t == Issues.RuntimeException then 1 else 0, a + if t == Issues.APIChange then 1 else 0, m + if t == Issues.QueryAlert then 1 else 0)) (0, 0, 0) anomalies'
+ (errTotal, apiTotal, qTotal) = L.foldl (\(e, a, m) (_, _, _, _, t) -> (e + if t == Issues.RuntimeException then 1 else 0, a + if t == Issues.NewEndpoint then 1 else 0, m + if t == Issues.QueryAlert then 1 else 0)) (0, 0, 0) anomalies'
runtimeErrorsBarPercentage = if totalAnomalies == 0 then 0 else (fromIntegral errTotal / fromIntegral totalAnomalies) * 99
apiChangesBarPercentage = if totalAnomalies == 0 then 0 else (fromIntegral apiTotal / fromIntegral totalAnomalies) * 99
alertIssuesBarPercentage = if totalAnomalies == 0 then 0 else (fromIntegral qTotal / fromIntegral totalAnomalies) * 99
@@ -1073,178 +1124,6 @@ emailQueryMonitorAlert :: Monitors.QueryMonitorEvaled -> CI.CI Text -> Maybe Use
emailQueryMonitorAlert monitorE@Monitors.QueryMonitorEvaled{alertConfig} email userM = whenJust userM (const pass)
--- FIXME: implement query alert email using postmark
--- sendEmail
--- (CI.original email)
--- [fmt| 🤖 APITOOLKIT: log monitor triggered `{alertConfig.title}` |]
--- [fmtTrim|
--- Hi {user.firstName},
---
--- The monitor: `{alertConfig.title}` was triggered and got above it's defined threshold.
---
---
--- Regards,
--- Apitoolkit team
--- |]
-
--- | Process new anomalies detected by database triggers
--- This job is created by the apis.new_anomaly_proc() stored procedure
--- when new entities (endpoints, shapes, fields, formats, errors) are inserted.
---
--- Anomaly Processing Strategy:
--- 1. API Changes (endpoint/shape/format) -> Group by endpoint into single issue
--- 2. Runtime Exceptions -> Create individual issues for each error
--- 3. All issues are queued for LLM enhancement if configured
--- 4. Notifications are sent based on project settings
-newAnomalyJob :: Projects.ProjectId -> ZonedTime -> Text -> Text -> V.Vector Text -> ATBackgroundCtx ()
-newAnomalyJob pid createdAt anomalyTypesT anomalyActionsT targetHashes = do
- authCtx <- ask @Config.AuthContext
- let anomalyType = fromMaybe (error "parseAnomalyTypes returned Nothing") $ Anomalies.parseAnomalyTypes anomalyTypesT
- Log.logInfo "Processing new anomalies" ()
- case anomalyType of
- -- API Change anomalies (endpoint, shape, format) - group into single issue per endpoint
- -- This prevents notification spam when multiple related changes occur
- Anomalies.ATEndpoint -> processAPIChangeAnomalies pid targetHashes
- Anomalies.ATShape -> processAPIChangeAnomalies pid targetHashes
- Anomalies.ATFormat -> processAPIChangeAnomalies pid targetHashes
- -- Runtime exceptions get individual issues
- -- Each unique error pattern gets its own issue for tracking
- Anomalies.ATRuntimeException -> do
- errors <- Anomalies.errorsByHashes pid targetHashes
-
- -- Create one issue per error
- forM_ errors \err -> do
- issue <- liftIO $ Issues.createRuntimeExceptionIssue pid err.errorData
- Issues.insertIssue issue
- -- Queue enhancement job
- _ <- liftIO $ withResource authCtx.jobsPool \conn ->
- createJob conn "background_jobs" $ BackgroundJobs.EnhanceIssuesWithLLM pid (V.singleton issue.id)
- -- Send notifications only if project exists and has alerts enabled
- projectM <- Projects.projectById pid
- whenJust projectM \project -> Relude.when project.errorAlerts do
- users <- Projects.usersByProjectId pid
- let issueId = UUID.toText issue.id.unUUIDId
- forM_ project.notificationsChannel \case
- Projects.NSlack ->
- forM_ errors \err' -> sendSlackAlert (RuntimeErrorAlert{issueId = issueId, errorData = err'.errorData}) pid project.title Nothing
- Projects.NDiscord ->
- forM_ errors \err' -> sendDiscordAlert (RuntimeErrorAlert{issueId = issueId, errorData = err'.errorData}) pid project.title Nothing
- Projects.NPhone ->
- forM_ errors \err' ->
- sendWhatsAppAlert (RuntimeErrorAlert{issueId = issueId, errorData = err'.errorData}) pid project.title project.whatsappNumbers
- Projects.NEmail ->
- forM_ users \u -> do
- let errosJ =
- ( \ee ->
- let e = ee.errorData
- in AE.object
- [ "root_error_message" AE..= e.rootErrorMessage
- , "error_type" AE..= e.errorType
- , "error_message" AE..= e.message
- , "stack_trace" AE..= e.stackTrace
- , "when" AE..= formatTime defaultTimeLocale "%b %-e, %Y, %-l:%M:%S %p" e.when
- , "hash" AE..= e.hash
- , "tech" AE..= e.technology
- , "request_info" AE..= (fromMaybe "" e.requestMethod <> " " <> fromMaybe "" e.requestPath)
- , "root_error_type" AE..= e.rootErrorType
- ]
- )
- <$> errors
- title = project.title
- errors_url = authCtx.env.hostUrl <> "p/" <> pid.toText <> "/issues/"
- templateVars =
- [aesonQQ|{
- "project_name": #{title},
- "errors_url": #{errors_url},
- "errors": #{errosJ}
- }|]
- sendPostmarkEmail (CI.original u.email) (Just ("runtime-errors", templateVars)) Nothing
- -- Ignore other anomaly types
- _ -> pass
-
-
--- | Process API change anomalies (endpoint, shape, format) into unified APIChange issues
--- This function groups related anomalies by endpoint to prevent notification spam.
--- For example, if a new endpoint is added with 5 fields and 2 formats, instead of
--- creating 8 separate issues, we create 1 issue that encompasses all changes.
---
--- Grouping Strategy:
--- 1. All anomalies are grouped by their endpoint hash
--- 2. If an open issue exists for that endpoint, update it with new anomalies
--- 3. Otherwise, create a new issue containing all anomalies for that endpoint
-processAPIChangeAnomalies :: Projects.ProjectId -> V.Vector Text -> ATBackgroundCtx ()
-processAPIChangeAnomalies pid targetHashes = do
- authCtx <- ask @Config.AuthContext
-
- -- Get all anomalies
- anomaliesList <- Anomalies.getAnomaliesVM pid targetHashes
- let anomaliesVM = V.fromList anomaliesList
-
- -- Group by endpoint hash to consolidate related changes
- let anomaliesByEndpoint = groupAnomaliesByEndpointHash anomaliesVM
-
- -- Process each endpoint group
- forM_ anomaliesByEndpoint \(endpointHash, anomalies) -> do
- -- Check for existing open issue to avoid duplicates
- existingIssueM <- Issues.findOpenIssueForEndpoint pid endpointHash
-
- case existingIssueM of
- Just existingIssue -> do
- -- Update existing issue with new anomaly data
- let apiChangeData =
- Issues.APIChangeData
- { endpointMethod = fromMaybe "UNKNOWN" $ viaNonEmpty head $ V.toList $ V.mapMaybe (.endpointMethod) anomalies
- , endpointPath = fromMaybe "/" $ viaNonEmpty head $ V.toList $ V.mapMaybe (.endpointUrlPath) anomalies
- , endpointHost = "Unknown"
- , anomalyHashes = V.map (.targetHash) anomalies
- , shapeChanges = V.empty -- Simplified for now
- , formatChanges = V.empty -- Simplified for now
- , newFields = V.concatMap (.shapeNewUniqueFields) anomalies
- , deletedFields = V.concatMap (.shapeDeletedFields) anomalies
- , modifiedFields = V.concatMap (.shapeUpdatedFieldFormats) anomalies
- }
- Issues.updateIssueWithNewAnomaly existingIssue.id apiChangeData
- Nothing -> do
- -- Create new issue
- issue <- liftIO $ Issues.createAPIChangeIssue pid endpointHash anomalies
- Issues.insertIssue issue
-
- -- Queue enhancement job
- _ <- liftIO $ withResource authCtx.jobsPool \conn ->
- createJob conn "background_jobs" $ BackgroundJobs.EnhanceIssuesWithLLM pid (V.singleton issue.id)
- pass
-
- -- Send notifications
- projectM <- Projects.projectById pid
- whenJust projectM \project -> do
- users <- Projects.usersByProjectId pid
- let endpointInfo =
- map
- ( \(_, anoms) ->
- let firstAnom = V.head anoms
- in fromMaybe "UNKNOWN" firstAnom.endpointMethod <> " " <> fromMaybe "/" firstAnom.endpointUrlPath
- )
- anomaliesByEndpoint
- -- Only send notifications if we have valid endpoint info
- Relude.when (project.endpointAlerts && not (null endpointInfo)) do
- let alert = EndpointAlert{project = project.title, endpoints = V.fromList endpointInfo, endpointHash = fromMaybe "" $ viaNonEmpty head $ V.toList targetHashes}
-
- forM_ project.notificationsChannel \case
- Projects.NSlack -> sendSlackAlert alert pid project.title Nothing
- Projects.NDiscord -> sendDiscordAlert alert pid project.title Nothing
- Projects.NPhone -> sendWhatsAppAlert alert pid project.title project.whatsappNumbers
- Projects.NEmail -> do
- forM_ users \u -> do
- let templateVars =
- AE.object
- [ "user_name" AE..= u.firstName
- , "project_name" AE..= project.title
- , "anomaly_url" AE..= (authCtx.env.hostUrl <> "p/" <> pid.toText <> "/issues")
- , "endpoint_name" AE..= endpointInfo
- ]
- sendPostmarkEmail (CI.original u.email) (Just ("anomaly-endpoint-2", templateVars)) Nothing
-
-
-- | Group anomalies by endpoint hash
groupAnomaliesByEndpointHash :: V.Vector Anomalies.AnomalyVM -> [(Text, V.Vector Anomalies.AnomalyVM)]
groupAnomaliesByEndpointHash anomalies =
@@ -1637,3 +1516,445 @@ monitorStatus triggerLessThan warnThreshold alertThreshold alertRecovery warnRec
where
breached t = if triggerLessThan then t >= value else t <= value
recovered r t = if triggerLessThan then value > fromMaybe t r else value < fromMaybe t r
+
+
+calculateErrorBaselines :: Projects.ProjectId -> ATBackgroundCtx ()
+calculateErrorBaselines pid = do
+ Log.logInfo "Calculating error baselines" pid
+ errors <- Errors.getActiveErrors pid
+
+ forM_ errors \err -> do
+ -- Get hourly stats from error_events over last 7 days (168 hours)
+ statsM <- Errors.getErrorEventStats err.id 168
+ case statsM of
+ Nothing -> pass
+ Just stats -> do
+ let newSamples = stats.totalHours
+ newMean = stats.hourlyMean
+ newStddev = stats.hourlyStddev
+ -- Establish baseline after 24 hours of data
+ newState = if newSamples >= 24 then BSEstablished else BSLearning
+ _ <- Errors.updateBaseline err.id newState newMean newStddev newSamples
+ pass
+
+ Log.logInfo "Finished calculating error baselines" (pid, length errors)
+
+
+-- | Detect error spikes and create issues
+-- Uses error_events table for current rate calculation
+detectErrorSpikes :: Projects.ProjectId -> Config.AuthContext -> ATBackgroundCtx ()
+detectErrorSpikes pid authCtx = do
+ Log.logInfo "Detecting error spikes" pid
+
+ -- Get all errors with their current hour counts in one query
+ errorsWithRates <- Errors.getErrorsWithCurrentRates pid
+
+ forM_ errorsWithRates \errRate -> do
+ -- Only check errors with established baselines
+ case (errRate.baselineState, errRate.baselineMean, errRate.baselineStddev) of
+ (BSEstablished, Just mean, Just stddev) | stddev > 0 -> do
+ let currentRate = fromIntegral errRate.currentHourCount :: Double
+ zScore = (currentRate - mean) / stddev
+ isSpike = abs zScore > 3.0 && currentRate > mean + 5
+
+ Relude.when isSpike $ do
+ Log.logInfo "Error spike detected" (errRate.errorId, errRate.errorType, currentRate, mean, zScore)
+
+ -- Get full error record for issue creation
+ errorM <- Errors.getErrorById errRate.errorId
+ whenJust errorM \err -> do
+ _ <- Errors.updateErrorState err.id Errors.ESEscalating
+ issue <- liftIO $ Issues.createErrorSpikeIssue pid err currentRate mean stddev
+ Issues.insertIssue issue
+ liftIO $ withResource authCtx.jobsPool \conn ->
+ void $ createJob conn "background_jobs" $ EnhanceIssuesWithLLM pid (V.singleton issue.id)
+
+ Log.logInfo "Created issue for error spike" (pid, err.id, issue.id)
+ _ -> pass -- Skip errors without established baseline
+ Log.logInfo "Finished error spike detection" pid
+
+
+-- | Process a new error and create an issue
+processNewError :: Projects.ProjectId -> Text -> Config.AuthContext -> ATBackgroundCtx ()
+processNewError pid errorHash authCtx = do
+ Log.logInfo "Processing new error" (pid, errorHash)
+ errorM <- Errors.getErrorByHash pid errorHash
+ case errorM of
+ Nothing -> Log.logAttention "Error not found for new error processing" (pid, errorHash)
+ Just err -> do
+ -- Only create issue for truly new errors (state = 'new')
+ Relude.when (err.state == Errors.ESNew) $ do
+ -- Create a runtime exception issue
+ issue <- liftIO $ Issues.createNewErrorIssue pid err
+ Issues.insertIssue issue
+ -- Queue LLM enhancement
+ liftIO $ withResource authCtx.jobsPool \conn ->
+ void $ createJob conn "background_jobs" $ EnhanceIssuesWithLLM pid (V.singleton issue.id)
+ -- Send notifications only if project exists and has alerts enabled
+ projectM <- Projects.projectById pid
+ whenJust projectM \project -> Relude.when project.errorAlerts do
+ users <- Projects.usersByProjectId pid
+ let issueId = UUID.toText issue.id.unUUIDId
+ forM_ project.notificationsChannel \case
+ Projects.NSlack -> sendSlackAlert (RuntimeErrorAlert{issueId = issueId, errorData = err.errorData}) pid project.title Nothing
+ Projects.NDiscord -> sendDiscordAlert (RuntimeErrorAlert{issueId = issueId, errorData = err.errorData}) pid project.title Nothing
+ Projects.NPhone -> sendWhatsAppAlert (RuntimeErrorAlert{issueId = issueId, errorData = err.errorData}) pid project.title project.whatsappNumbers
+ Projects.NEmail ->
+ forM_ users \u -> do
+ let e = err.errorData
+ errorsJ =
+ V.singleton
+ $ AE.object
+ [ "root_error_message" AE..= e.rootErrorMessage
+ , "error_type" AE..= e.errorType
+ , "error_message" AE..= e.message
+ , "stack_trace" AE..= e.stackTrace
+ , "when" AE..= formatTime defaultTimeLocale "%b %-e, %Y, %-l:%M:%S %p" e.when
+ , "hash" AE..= e.hash
+ , "tech" AE..= e.technology
+ , "request_info" AE..= (fromMaybe "" e.requestMethod <> " " <> fromMaybe "" e.requestPath)
+ , "root_error_type" AE..= e.rootErrorType
+ ]
+ title = project.title
+ errors_url = authCtx.env.hostUrl <> "p/" <> pid.toText <> "/issues/"
+ templateVars =
+ [aesonQQ|{
+ "project_name": #{title},
+ "errors_url": #{errors_url},
+ "errors": #{errorsJ}
+ }|]
+ sendPostmarkEmail (CI.original u.email) (Just ("runtime-errors", templateVars)) Nothing
+
+ Log.logInfo "Created issue for new error" (pid, err.id, issue.id)
+
+
+-- ============================================================================
+-- Log Pattern Processing Jobs
+-- ============================================================================
+
+-- | Calculate baselines for log patterns
+-- Uses hourly counts from otel_logs_and_spans over the last 7 days
+calculateLogPatternBaselines :: Projects.ProjectId -> ATBackgroundCtx ()
+calculateLogPatternBaselines pid = do
+ Log.logInfo "Calculating log pattern baselines" pid
+ now <- Time.currentTime
+ -- Get all non-ignored patterns
+ patterns <- LogPatterns.getLogPatterns pid Nothing 1000 0
+ forM_ patterns \lp -> do
+ -- Get hourly stats from otel_logs_and_spans over last 7 days (168 hours)
+ statsM <- LogPatterns.getPatternStats pid lp.logPattern 168
+ case statsM of
+ Nothing -> pass
+ Just stats -> do
+ let newSamples = stats.totalHours
+ newMean = stats.hourlyMean
+ newStddev = stats.hourlyStddev
+ patternAgeDays = diffUTCTime now (zonedTimeToUTC lp.createdAt) / (24 * 60 * 60)
+ newState = if newMean > 100 || patternAgeDays >= 14 then BSEstablished else BSLearning
+ _ <- LogPatterns.updateBaseline pid lp.patternHash newState newMean newStddev newSamples
+ pass
+
+ Log.logInfo "Finished calculating log pattern baselines" (pid, length patterns)
+
+
+-- | Detect log pattern volume spikes and create issues
+-- Uses otel_logs_and_spans table for current rate calculation
+detectLogPatternSpikes :: Projects.ProjectId -> Config.AuthContext -> ATBackgroundCtx ()
+detectLogPatternSpikes pid authCtx = do
+ Log.logInfo "Detecting log pattern spikes" pid
+
+ -- Get all patterns with their current hour counts in one query
+ patternsWithRates <- LogPatterns.getPatternsWithCurrentRates pid
+
+ forM_ patternsWithRates \lpRate -> do
+ -- Only check patterns with established baselines
+ case (lpRate.baselineState, lpRate.baselineMean, lpRate.baselineStddev) of
+ (BSEstablished, Just mean, Just stddev) | stddev > 0 -> do
+ let currentRate = fromIntegral lpRate.currentHourCount :: Double
+ zScore = (currentRate - mean) / stddev
+ -- Spike detection: >3 std devs AND at least 10 more events than baseline
+ isSpike = abs zScore > 3.0 && currentRate > mean + 10
+
+ Relude.when isSpike $ do
+ Log.logInfo "Log pattern spike detected" (lpRate.patternId, lpRate.logPattern, currentRate, mean, zScore)
+
+ -- Get full pattern record for issue creation
+ patternM <- LogPatterns.getLogPatternById lpRate.patternId
+ whenJust patternM \lp -> do
+ issue <- liftIO $ Issues.createLogPatternRateChangeIssue pid lp currentRate mean stddev "spike"
+ Issues.insertIssue issue
+ liftIO $ withResource authCtx.jobsPool \conn ->
+ void $ createJob conn "background_jobs" $ EnhanceIssuesWithLLM pid (V.singleton issue.id)
+
+ Log.logInfo "Created issue for log pattern spike" (pid, lp.id, issue.id)
+ _ -> pass -- Skip patterns without established baseline
+ Log.logInfo "Finished log pattern spike detection" pid
+
+
+-- | Process a new log pattern and create an issue
+processNewLogPattern :: Projects.ProjectId -> Text -> Config.AuthContext -> ATBackgroundCtx ()
+processNewLogPattern pid patternHash authCtx = do
+ Log.logInfo "Processing new log pattern" (pid, patternHash)
+ totalEvents <- do
+ res <- PG.query [sql| SELECT count(5000) from otel_logs_and_spans WHERE project_id = ? AND timestamp >= now() - interval '7 days' |] (Only pid)
+ case res of
+ [Only cnt] -> return cnt
+ _ -> return 0
+ if totalEvents < 5000
+ then Log.logInfo "Skipping new log pattern issue creation due to low event volume" (pid, patternHash, totalEvents)
+ else do
+ patternM <- LogPatterns.getLogPatternByHash pid patternHash
+ whenJust patternM \lp -> do
+ Relude.when (lp.state == LogPatterns.LPSNew) $ do
+ issue <- liftIO $ Issues.createLogPatternIssue pid lp
+ Issues.insertIssue issue
+ liftIO $ withResource authCtx.jobsPool \conn ->
+ void $ createJob conn "background_jobs" $ EnhanceIssuesWithLLM pid (V.singleton issue.id)
+ Log.logInfo "Created issue for new log pattern" (pid, lp.id, issue.id)
+
+
+calculateEndpointBaselines :: Projects.ProjectId -> ATBackgroundCtx ()
+calculateEndpointBaselines pid = do
+ Log.logInfo "Calculating endpoint baselines" pid
+ endpoints <- Endpoints.getActiveEndpoints pid
+ now <- Time.currentTime
+
+ forM_ endpoints \ep -> do
+ -- Get hourly stats over last 7 days (168 hours)
+ statsM <- Endpoints.getEndpointStats pid ep.hash 168
+ case statsM of
+ Nothing -> pass
+ Just stats -> do
+ let newSamples = stats.totalHours
+ endpointAgeDays = diffUTCTime now ep.createdAt / (24 * 60 * 60)
+ newState = if stats.hourlyMeanRequests > 100 || (endpointAgeDays >= 14 && stats.hourlyMeanRequests > 0) then BSEstablished else BSLearning
+ Endpoints.updateEndpointBaseline
+ ep.id
+ newState
+ stats.hourlyMeanErrors
+ stats.hourlyStddevErrors
+ stats.meanLatency
+ stats.stddevLatency
+ stats.p95Latency
+ stats.p99Latency
+ stats.hourlyMeanRequests
+ stats.hourlyStddevRequests
+ newSamples
+
+ Log.logInfo "Finished calculating endpoint baselines" (pid, length endpoints)
+
+
+detectEndpointLatencyDegradation :: Projects.ProjectId -> Config.AuthContext -> ATBackgroundCtx ()
+detectEndpointLatencyDegradation pid authCtx = do
+ Log.logInfo "Detecting endpoint latency degradation" pid
+ endpointsWithRates <- Endpoints.getEndpointsWithCurrentRates pid
+ forM_ endpointsWithRates \epRate -> do
+ -- Check P95 latency degradation
+ case (epRate.baselineLatencyP95, epRate.baselineLatencyStddev, epRate.currentHourLatencyP95) of
+ (Just baselineP95, Just stddev, Just currentP95) | stddev > 0 -> do
+ let zScore = (currentP95 - baselineP95) / stddev
+ -- Degradation: >3 std devs (positive or negative) AND at least 50% increase
+ isDegraded = abs zScore > 3.0 && currentP95 > baselineP95 * 1.1
+ Relude.when isDegraded $ do
+ Log.logInfo "Endpoint latency degradation detected" (epRate.endpointHash, currentP95, baselineP95, zScore)
+ issue <- liftIO $ Issues.createEndpointLatencyDegradationIssue pid epRate.endpointHash epRate.method epRate.urlPath (Just epRate.host) currentP95 baselineP95 stddev "p95" V.empty
+ Issues.insertIssue issue
+ liftIO $ withResource authCtx.jobsPool \conn ->
+ void $ createJob conn "background_jobs" $ EnhanceIssuesWithLLM pid (V.singleton issue.id)
+ Log.logInfo "Created issue for endpoint latency degradation" (pid, epRate.endpointHash, issue.id)
+ _ -> pass
+
+ Log.logInfo "Finished endpoint latency degradation detection" pid
+
+
+detectEndpointErrorRateSpike :: Projects.ProjectId -> Config.AuthContext -> ATBackgroundCtx ()
+detectEndpointErrorRateSpike pid authCtx = do
+ Log.logInfo "Detecting endpoint error rate spikes" pid
+
+ endpointsWithRates <- Endpoints.getEndpointsWithCurrentRates pid
+
+ forM_ endpointsWithRates \epRate -> do
+ case (epRate.baselineErrorRateMean, epRate.baselineErrorRateStddev) of
+ (Just baselineMean, Just stddev) | stddev > 0 && epRate.currentHourRequests > 0 -> do
+ let currentErrorRate = fromIntegral epRate.currentHourErrors / fromIntegral epRate.currentHourRequests
+ zScore = (currentErrorRate - baselineMean) / stddev
+ -- Spike: >3 std devs AND error rate > 5% AND at least 5 errors
+ isSpike = abs zScore > 3.0 && currentErrorRate > 0.05 && epRate.currentHourErrors >= 5
+
+ Relude.when isSpike $ do
+ Log.logInfo "Endpoint error rate spike detected" (epRate.endpointHash, currentErrorRate, baselineMean, zScore)
+ issue <-
+ liftIO
+ $ Issues.createEndpointErrorRateSpikeIssue
+ pid
+ epRate.endpointHash
+ epRate.method
+ epRate.urlPath
+ (Just epRate.host)
+ currentErrorRate
+ baselineMean
+ stddev
+ epRate.currentHourErrors
+ epRate.currentHourRequests
+ V.empty
+ Issues.insertIssue issue
+ liftIO $ withResource authCtx.jobsPool \conn ->
+ void $ createJob conn "background_jobs" $ EnhanceIssuesWithLLM pid (V.singleton issue.id)
+ Log.logInfo "Created issue for endpoint error rate spike" (pid, epRate.endpointHash, issue.id)
+ _ -> pass
+
+ Log.logInfo "Finished endpoint error rate spike detection" pid
+
+
+detectEndpointVolumeRateChange :: Projects.ProjectId -> Config.AuthContext -> ATBackgroundCtx ()
+detectEndpointVolumeRateChange pid authCtx = do
+ Log.logInfo "Detecting endpoint volume rate changes" pid
+ endpointsWithRates <- Endpoints.getEndpointsWithCurrentRates pid
+ forM_ endpointsWithRates \epRate -> do
+ case (epRate.baselineVolumeHourlyMean, epRate.baselineVolumeHourlyStddev) of
+ (Just baselineMean, Just stddev) | stddev > 0 && baselineMean > 10 -> do
+ let currentRate = fromIntegral epRate.currentHourRequests
+ zScore = (currentRate - baselineMean) / stddev
+ absZScore = abs zScore
+ -- Significant change: >3 std devs in either direction
+ isSignificantChange = absZScore > 3.0
+ direction = if currentRate > baselineMean then "spike" else "drop"
+ Relude.when isSignificantChange $ do
+ Log.logInfo "Endpoint volume rate change detected" (epRate.endpointHash, currentRate, baselineMean, zScore, direction)
+ issue <-
+ liftIO
+ $ Issues.createEndpointVolumeRateChangeIssue
+ pid
+ epRate.endpointHash
+ epRate.method
+ epRate.urlPath
+ (Just epRate.host)
+ currentRate
+ baselineMean
+ stddev
+ direction
+
+ Issues.insertIssue issue
+ liftIO $ withResource authCtx.jobsPool \conn ->
+ void $ createJob conn "background_jobs" $ EnhanceIssuesWithLLM pid (V.singleton issue.id)
+
+ Log.logInfo "Created issue for endpoint volume rate change" (pid, epRate.endpointHash, issue.id, direction)
+ _ -> pass
+
+ Log.logInfo "Finished endpoint volume rate change detection" pid
+
+
+processNewEndpoint :: Projects.ProjectId -> Text -> Config.AuthContext -> ATBackgroundCtx ()
+processNewEndpoint pid hash authCtx = do
+ Log.logInfo "Processing new endpoint" (pid, hash)
+ totalEvents <- do
+ res <- PG.query [sql| SELECT count(5000) from otel_logs_and_spans WHERE project_id = ? AND timestamp >= now() - interval '7 days' |] (Only pid)
+ case res of
+ [Only cnt] -> return cnt
+ _ -> return 0
+ if totalEvents < 5000
+ then Log.logInfo "Skipping new endpoint issue creation due to low event volume" (pid, hash, totalEvents)
+ else do
+ projectM <- Projects.projectById pid
+ whenJust projectM \project -> do
+ endpointM <- Endpoints.getEndpointByHash pid hash
+ case endpointM of
+ Nothing -> Log.logAttention "Endpoint not found for new endpoint processing" (pid, hash)
+ Just ep -> do
+ issue <- liftIO $ Issues.createNewEndpointIssue pid ep.hash ep.method ep.urlPath ep.host
+ Issues.insertIssue issue
+ Log.logInfo "Created issue for new endpoint" (pid, hash, issue.id)
+ -- Send notifications only if project exists and has alerts enabled
+ Relude.when project.endpointAlerts $ do
+ users <- Projects.usersByProjectId pid
+ let issueId = UUID.toText issue.id.unUUIDId
+ title = project.title
+ forM_ project.notificationsChannel \case
+ Projects.NSlack -> sendSlackAlert (EndpointAlert title ep.method ep.urlPath ep.hash) pid project.title Nothing
+ Projects.NDiscord -> sendDiscordAlert (EndpointAlert title ep.method ep.urlPath ep.hash) pid project.title Nothing
+ Projects.NPhone -> sendWhatsAppAlert (EndpointAlert title ep.method ep.urlPath ep.hash) pid project.title project.whatsappNumbers
+ Projects.NEmail ->
+ forM_ users \u -> do
+ let endpoint_url = authCtx.env.hostUrl <> "p/" <> pid.toText <> "/anomalies/" <> issueId
+ endpoint_name = ep.method <> " " <> ep.urlPath
+ firstName = u.firstName
+ templateVars =
+ [aesonQQ|{
+ "user_name": #{firstName},
+ "project_name": #{title},
+ "anomaly_url": #{endpoint_url},
+ "endpoint_name": #{endpoint_name}
+ }|]
+ sendPostmarkEmail (CI.original u.email) (Just ("anomaly-endpoint-2", templateVars)) Nothing
+
+
+processNewShape :: Projects.ProjectId -> Text -> Config.AuthContext -> ATBackgroundCtx ()
+processNewShape pid hash authCtx = do
+ Log.logInfo "Processing new shape" (pid, hash)
+ projectM <- Projects.projectById pid
+ whenJust projectM \project -> do
+ shapeM <- Shapes.getShapeForIssue pid hash
+ whenJust shapeM \shape -> do
+ endpointM <- Endpoints.getEndpointByHash pid shape.endpointHash
+ case endpointM of
+ Just endpoint | endpoint.baselineState == BSEstablished -> do
+ existingIssueM <- Issues.findOpenIssueForEndpoint pid hash
+ case existingIssueM of
+ Just issue -> do
+ Log.logInfo "Skipping new shape issue creation due to existing open issue for endpoint" (pid, hash, issue.id)
+ let Aeson rawIssueData = issue.issueData
+ shapeData = AE.decode (AE.encode rawIssueData) :: Maybe Issues.NewShapeData
+ whenJust shapeData \sh -> do
+ let newData = sh{Issues.newShapesAfterIssue = V.snoc sh.newShapesAfterIssue hash}
+ Issues.updateIssueData issue.id (AE.toJSON newData)
+ Nothing -> do
+ issue <-
+ liftIO
+ $ Issues.createNewShapeIssue
+ pid
+ shape.shapeHash
+ shape.endpointHash
+ shape.method
+ shape.path
+ shape.statusCode
+ shape.exampleRequestPayload
+ shape.exampleResponsePayload
+ shape.newFields
+ shape.deletedFields
+ shape.modifiedFields
+ shape.fieldHashes
+ Issues.insertIssue issue
+ -- llm enhancement job is not needed
+ Log.logInfo "Created issue for new shape" (pid, hash, issue.id)
+ _ -> pass
+
+
+processNewFieldChange :: Projects.ProjectId -> Text -> Config.AuthContext -> ATBackgroundCtx ()
+processNewFieldChange pid hash authCtx = do
+ Log.logInfo "Processing new field change" (pid, hash)
+ pass -- Temporarily disabled field change issue creation
+
+-- fieldM <- Fields.getFieldForIssue hash
+
+-- case fieldM of
+-- Nothing -> Log.logAttention "Field not found for new field change processing" (pid, hash)
+-- Just fld -> do
+-- issue <-
+-- liftIO
+-- $ Issues.createFieldChangeIssue
+-- pid
+-- fld.fieldHash
+-- fld.endpointHash
+-- fld.method
+-- fld.path
+-- fld.keyPath
+-- fld.fieldCategory
+-- Nothing
+-- fld.fieldType
+-- "added"
+-- Issues.insertIssue issue
+
+-- liftIO $ withResource authCtx.jobsPool \conn ->
+-- void $ createJob conn "background_jobs" $ EnhanceIssuesWithLLM pid (V.singleton issue.id)
+
+-- Log.logInfo "Created issue for new field change" (pid, hash, issue.id)
diff --git a/src/Models/Apis/Endpoints.hs b/src/Models/Apis/Endpoints.hs
index 368c563dc..a2d8a373b 100644
--- a/src/Models/Apis/Endpoints.hs
+++ b/src/Models/Apis/Endpoints.hs
@@ -8,10 +8,17 @@ module Models.Apis.Endpoints (
EndpointRequestStats (..),
Host (..),
HostEvents (..),
+ EndpointWithCurrentRates (..),
+ EndpointStats (..),
bulkInsertEndpoints,
dependenciesAndEventsCount,
endpointRequestStatsByProject,
countEndpointInbox,
+ getEndpointsWithCurrentRates,
+ getEndpointStats,
+ updateEndpointBaseline,
+ getEndpointByHash,
+ getActiveEndpoints,
)
where
@@ -33,7 +40,7 @@ import Effectful.PostgreSQL (withConnection)
import Effectful.PostgreSQL qualified as PG
import Models.Projects.Projects qualified as Projects
import NeatInterpolation (text)
-import Pkg.DeriveUtils (UUIDId (..))
+import Pkg.DeriveUtils (BaselineState (..), UUIDId (..))
import Relude
import System.Types (DB)
@@ -59,6 +66,20 @@ data Endpoint = Endpoint
, hash :: Text
, outgoing :: Bool
, description :: Text
+ , firstTraceId :: Maybe Text
+ , recentTraceId :: Maybe Text
+ , service :: Maybe Text
+ , baselineState :: BaselineState
+ , baselineSamples :: Int
+ , baselineUpdatedAt :: Maybe UTCTime
+ , baselineErrorRateMean :: Maybe Double
+ , baselineErrorRateStddev :: Maybe Double
+ , baselineLatencyMean :: Maybe Double
+ , baselineLatencyStddev :: Maybe Double
+ , baselineLatencyP95 :: Maybe Double
+ , baselineLatencyP99 :: Maybe Double
+ , baselineVolumeHourlyMean :: Maybe Double
+ , baselineVolumeHourlyStddev :: Maybe Double
}
deriving stock (Eq, Generic, Show)
deriving anyclass (Default, FromRow, NFData, ToRow)
@@ -70,8 +91,8 @@ bulkInsertEndpoints :: DB es => V.Vector Endpoint -> Eff es ()
bulkInsertEndpoints endpoints = void $ PG.executeMany q $ V.toList rowsToInsert
where
q =
- [sql| INSERT INTO apis.endpoints (project_id, url_path, url_params, method, host, hash, outgoing)
- VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT (hash) DO NOTHING;
+ [sql| INSERT INTO apis.endpoints (project_id, url_path, url_params, method, host, hash, outgoing, first_trace_id, recent_trace_id, service)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (hash) DO NOTHING;
|]
rowsToInsert =
endpoints <&> \endpoint ->
@@ -82,6 +103,9 @@ bulkInsertEndpoints endpoints = void $ PG.executeMany q $ V.toList rowsToInsert
, endpoint.host
, endpoint.hash
, endpoint.outgoing
+ , endpoint.firstTraceId
+ , endpoint.recentTraceId
+ , endpoint.service
)
@@ -233,3 +257,189 @@ countEndpointInbox pid host requestType = do
AND ann.acknowledged_at IS NULL
AND host = ?
|]
+
+
+-- | Endpoint with current hourly rates for anomaly detection
+data EndpointWithCurrentRates = EndpointWithCurrentRates
+ { endpointId :: EndpointId
+ , endpointHash :: Text
+ , method :: Text
+ , urlPath :: Text
+ , host :: Text
+ , baselineState :: BaselineState
+ , baselineErrorRateMean :: Maybe Double
+ , baselineErrorRateStddev :: Maybe Double
+ , baselineLatencyMean :: Maybe Double
+ , baselineLatencyStddev :: Maybe Double
+ , baselineLatencyP95 :: Maybe Double
+ , baselineLatencyP99 :: Maybe Double
+ , baselineVolumeHourlyMean :: Maybe Double
+ , baselineVolumeHourlyStddev :: Maybe Double
+ , currentHourRequests :: Int
+ , currentHourErrors :: Int
+ , currentHourLatencyP50 :: Maybe Double
+ , currentHourLatencyP95 :: Maybe Double
+ , currentHourLatencyP99 :: Maybe Double
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (Default, FromRow, NFData)
+
+
+-- | Endpoint stats for baseline calculation
+data EndpointStats = EndpointStats
+ { totalHours :: Int
+ , hourlyMeanRequests :: Double
+ , hourlyStddevRequests :: Double
+ , hourlyMeanErrors :: Double
+ , hourlyStddevErrors :: Double
+ , meanLatency :: Double
+ , stddevLatency :: Double
+ , p95Latency :: Double
+ , p99Latency :: Double
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (Default, FromRow, NFData)
+
+
+-- | Get endpoints with their current hourly rates for anomaly detection
+getEndpointsWithCurrentRates :: DB es => Projects.ProjectId -> Eff es [EndpointWithCurrentRates]
+getEndpointsWithCurrentRates pid = PG.query q (pid, pid)
+ where
+ q =
+ [sql|
+ WITH current_hour_stats AS (
+ SELECT
+ attributes->'http'->>'route' AS url_path,
+ attributes___http___request___method AS method,
+ COUNT(*) AS request_count,
+ COUNT(*) FILTER (WHERE attributes___http___response___status_code::int >= 500 OR attributes___http___response___status_code::int = 0) AS error_count,
+ PERCENTILE_CONT(0.50) WITHIN GROUP (ORDER BY duration) AS p50_latency,
+ PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY duration) AS p95_latency,
+ PERCENTILE_CONT(0.99) WITHIN GROUP (ORDER BY duration) AS p99_latency
+ FROM otel_logs_and_spans
+ WHERE project_id = ?
+ AND name = 'monoscope.http'
+ AND timestamp >= NOW() - INTERVAL '1 hour'
+ GROUP BY url_path, method
+ )
+ SELECT
+ e.id AS endpoint_id,
+ e.hash AS endpoint_hash,
+ e.method,
+ e.url_path,
+ e.host,
+ e.baseline_state,
+ e.baseline_error_rate_mean,
+ e.baseline_error_rate_stddev,
+ e.baseline_latency_mean,
+ e.baseline_latency_stddev,
+ e.baseline_latency_p95,
+ e.baseline_latency_p99,
+ e.baseline_volume_hourly_mean,
+ e.baseline_volume_hourly_stddev,
+ COALESCE(chs.request_count, 0)::int AS current_hour_requests,
+ COALESCE(chs.error_count, 0)::int AS current_hour_errors,
+ chs.p50_latency AS current_hour_latency_p50,
+ chs.p95_latency AS current_hour_latency_p95,
+ chs.p99_latency AS current_hour_latency_p99
+ FROM apis.endpoints e
+ LEFT JOIN current_hour_stats chs
+ ON e.url_path = chs.url_path AND e.method = chs.method
+ WHERE e.project_id = ?
+ AND e.baseline_state = 'established'
+ |]
+
+
+-- | Get endpoint stats for baseline calculation over N hours
+getEndpointStats :: DB es => Projects.ProjectId -> Text -> Int -> Eff es (Maybe EndpointStats)
+getEndpointStats pid endpointHash hours = listToMaybe <$> PG.query q (pid, endpointHash, hours)
+ where
+ q =
+ [sql|
+ WITH hourly_stats AS (
+ SELECT
+ DATE_TRUNC('hour', timestamp) AS hour,
+ COUNT(*) AS request_count,
+ COUNT(*) FILTER (WHERE attributes___http___response___status_code::int >= 500 OR attributes___http___response___status_code::int = 0) AS error_count,
+ AVG(duration) AS avg_latency,
+ PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY duration) AS p95_latency,
+ PERCENTILE_CONT(0.99) WITHIN GROUP (ORDER BY duration) AS p99_latency
+ FROM otel_logs_and_spans ols
+ JOIN apis.endpoints e ON e.url_path = (ols.attributes->'http'->>'route')
+ AND e.method = ols.attributes___http___request___method
+ WHERE ols.project_id = ?
+ AND e.hash = ?
+ AND ols.name = 'monoscope.http'
+ AND ols.timestamp >= NOW() - MAKE_INTERVAL(hours => ?)
+ GROUP BY DATE_TRUNC('hour', timestamp)
+ )
+ SELECT
+ COUNT(*)::int AS total_hours,
+ COALESCE(AVG(request_count), 0)::float8 AS hourly_mean_requests,
+ COALESCE(STDDEV(request_count), 0)::float8 AS hourly_stddev_requests,
+ COALESCE(AVG(error_count::float / NULLIF(request_count, 0)), 0)::float8 AS hourly_mean_errors,
+ COALESCE(STDDEV(error_count::float / NULLIF(request_count, 0)), 0)::float8 AS hourly_stddev_errors,
+ COALESCE(AVG(avg_latency), 0)::float8 AS mean_latency,
+ COALESCE(STDDEV(avg_latency), 0)::float8 AS stddev_latency,
+ COALESCE(AVG(p95_latency), 0)::float8 AS p95_latency,
+ COALESCE(AVG(p99_latency), 0)::float8 AS p99_latency
+ FROM hourly_stats
+ HAVING COUNT(*) > 0
+ |]
+
+
+-- | Update endpoint baseline values
+updateEndpointBaseline :: DB es => EndpointId -> BaselineState -> Double -> Double -> Double -> Double -> Double -> Double -> Double -> Double -> Int -> Eff es ()
+updateEndpointBaseline eid bState errMean errStddev latMean latStddev latP95 latP99 volMean volStddev samples =
+ void $ PG.execute q (bState, errMean, errStddev, latMean, latStddev, latP95, latP99, volMean, volStddev, samples, eid)
+ where
+ q =
+ [sql|
+ UPDATE apis.endpoints
+ SET baseline_state = ?,
+ baseline_error_rate_mean = ?,
+ baseline_error_rate_stddev = ?,
+ baseline_latency_mean = ?,
+ baseline_latency_stddev = ?,
+ baseline_latency_p95 = ?,
+ baseline_latency_p99 = ?,
+ baseline_volume_hourly_mean = ?,
+ baseline_volume_hourly_stddev = ?,
+ baseline_samples = ?,
+ baseline_updated_at = NOW()
+ WHERE id = ?
+ |]
+
+
+-- | Get endpoint by hash
+getEndpointByHash :: DB es => Projects.ProjectId -> Text -> Eff es (Maybe Endpoint)
+getEndpointByHash pid hash = listToMaybe <$> PG.query q (pid, hash)
+ where
+ q =
+ [sql|
+ SELECT id, created_at, updated_at, project_id, url_path, url_params, method, host, hash, outgoing, description,
+ first_trace_id, recent_trace_id, service,
+ baseline_state, baseline_samples, baseline_updated_at,
+ baseline_error_rate_mean, baseline_error_rate_stddev,
+ baseline_latency_mean, baseline_latency_stddev, baseline_latency_p95, baseline_latency_p99,
+ baseline_volume_hourly_mean, baseline_volume_hourly_stddev
+ FROM apis.endpoints
+ WHERE project_id = ? AND hash = ?
+ |]
+
+
+-- | Get all active endpoints for a project (for baseline calculation)
+getActiveEndpoints :: DB es => Projects.ProjectId -> Eff es [Endpoint]
+getActiveEndpoints pid = PG.query q (Only pid)
+ where
+ q =
+ [sql|
+ SELECT id, created_at, updated_at, project_id, url_path, url_params, method, host, hash, outgoing, description,
+ first_trace_id, recent_trace_id, service,
+ baseline_state, baseline_samples, baseline_updated_at,
+ baseline_error_rate_mean, baseline_error_rate_stddev,
+ baseline_latency_mean, baseline_latency_stddev, baseline_latency_p95, baseline_latency_p99,
+ baseline_volume_hourly_mean, baseline_volume_hourly_stddev
+ FROM apis.endpoints
+ WHERE project_id = ?
+ |]
diff --git a/src/Models/Apis/Errors.hs b/src/Models/Apis/Errors.hs
new file mode 100644
index 000000000..be49545aa
--- /dev/null
+++ b/src/Models/Apis/Errors.hs
@@ -0,0 +1,1233 @@
+module Models.Apis.Errors (
+ Error (..),
+ ErrorId,
+ ErrorState (..),
+ ErrorEvent (..),
+ ErrorEventId,
+ ATError (..),
+ ErrorL (..),
+ -- Queries
+ getErrors,
+ getErrorById,
+ getErrorByHash,
+ getActiveErrors,
+ updateOccurrenceCounts,
+ updateErrorState,
+ updateErrorStateByProjectAndHash,
+ getErrorLByHash,
+ updateBaseline,
+ resolveError,
+ upsertErrorQueryAndParam,
+ assignError,
+ -- Error Events (for baseline/spike detection)
+ HourlyBucket (..),
+ ErrorEventStats (..),
+ ErrorWithCurrentRate (..),
+ getHourlyErrorCounts,
+ getCurrentHourErrorCount,
+ getErrorEventStats,
+ checkErrorSpike,
+ getErrorsWithCurrentRates,
+ -- Error Fingerprinting (Sentry-style)
+ StackFrame (..),
+ parseStackTrace,
+ normalizeStackTrace,
+ normalizeMessage,
+ computeErrorFingerprint,
+)
+where
+
+import Data.Aeson qualified as AE
+import Data.Char (isSpace)
+import Data.Default
+import Data.List qualified as L
+import Data.Text qualified as T
+import Data.Time
+import Data.UUID qualified as UUID
+import Data.Vector qualified as V
+import Database.PostgreSQL.Entity.Types (CamelToSnake, Entity, FieldModifiers, GenericEntity, PrimaryKey, Schema, TableName)
+import Database.PostgreSQL.Simple (FromRow, Only (..), ToRow)
+import Database.PostgreSQL.Simple.FromField (FromField, ResultError (ConversionFailed, UnexpectedNull), fromField, returnError)
+import Database.PostgreSQL.Simple.Newtypes (Aeson (..))
+import Database.PostgreSQL.Simple.SqlQQ (sql)
+import Database.PostgreSQL.Simple.ToField (Action (Escape), ToField, toField)
+import Database.PostgreSQL.Simple.Types (Query (Query))
+import Deriving.Aeson qualified as DAE
+import Effectful (Eff)
+import Effectful.PostgreSQL qualified as PG
+import Models.Apis.RequestDumps qualified as RequestDump
+import Models.Apis.RequestDumps qualified as RequestDumps
+import Models.Projects.Projects qualified as Projects
+import Models.Users.Users qualified as Users
+import Pkg.DeriveUtils (BaselineState (..))
+import Relude hiding (id)
+import System.Types (DB)
+import Text.RE.TDFA (RE, SearchReplace, ed, (*=~/))
+import Utils (DBField (MkDBField), replaceAllFormats, toXXHash)
+
+
+newtype ErrorId = ErrorId {unErrorId :: UUID.UUID}
+ deriving stock (Generic, Show)
+ deriving newtype (AE.FromJSON, AE.ToJSON, Eq, FromField, NFData, Ord, ToField)
+
+
+newtype ErrorEventId = ErrorEventId {unErrorEventId :: UUID.UUID}
+ deriving stock (Generic, Show)
+ deriving newtype (AE.FromJSON, AE.ToJSON, Eq, FromField, NFData, Ord, ToField)
+
+
+data ErrorState
+ = ESNew
+ | ESEscalating
+ | ESOngoing
+ | ESResolved
+ | ESRegressed
+ deriving stock (Eq, Generic, Show)
+ deriving anyclass (NFData)
+ deriving
+ (AE.FromJSON, AE.ToJSON)
+ via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] ErrorState
+
+
+errorStateToText :: ErrorState -> Text
+errorStateToText ESNew = "new"
+errorStateToText ESEscalating = "escalating"
+errorStateToText ESOngoing = "ongoing"
+errorStateToText ESResolved = "resolved"
+errorStateToText ESRegressed = "regressed"
+
+
+parseErrorState :: (Eq s, IsString s) => s -> Maybe ErrorState
+parseErrorState "new" = Just ESNew
+parseErrorState "escalating" = Just ESEscalating
+parseErrorState "ongoing" = Just ESOngoing
+parseErrorState "resolved" = Just ESResolved
+parseErrorState "regressed" = Just ESRegressed
+parseErrorState _ = Nothing
+
+
+instance ToField ErrorState where
+ toField = Escape . encodeUtf8 <$> errorStateToText
+
+
+instance FromField ErrorState where
+ fromField f mdata =
+ case mdata of
+ Nothing -> returnError UnexpectedNull f ""
+ Just bs ->
+ case parseErrorState bs of
+ Just s -> pure s
+ Nothing -> pure ESNew
+
+
+data Error = Error
+ { id :: ErrorId
+ , projectId :: Projects.ProjectId
+ , createdAt :: ZonedTime
+ , updatedAt :: ZonedTime
+ , errorType :: Text
+ , message :: Text
+ , stacktrace :: Text
+ , hash :: Text
+ , environment :: Maybe Text
+ , service :: Maybe Text
+ , runtime :: Maybe Text
+ , errorData :: ATError
+ , firstTraceId :: Maybe Text
+ , recentTraceId :: Maybe Text
+ , firstEventId :: Maybe ErrorEventId
+ , lastEventId :: Maybe ErrorEventId
+ , state :: ErrorState
+ , assigneeId :: Maybe Users.UserId
+ , assignedAt :: Maybe ZonedTime
+ , resolvedAt :: Maybe ZonedTime
+ , regressedAt :: Maybe ZonedTime
+ , occurrences1m :: Int
+ , occurrences5m :: Int
+ , occurrences1h :: Int
+ , occurrences24h :: Int
+ , quietMinutes :: Int
+ , resolutionThresholdMinutes :: Int
+ , baselineState :: BaselineState
+ , baselineSamples :: Int
+ , baselineErrorRateMean :: Maybe Double
+ , baselineErrorRateStddev :: Maybe Double
+ , baselineUpdatedAt :: Maybe ZonedTime
+ , isIgnored :: Bool
+ , ignoredUntil :: Maybe ZonedTime
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow, NFData, ToRow)
+ deriving
+ (Entity)
+ via (GenericEntity '[Schema "apis", TableName "errors", PrimaryKey "id", FieldModifiers '[CamelToSnake]] Error)
+ deriving
+ (AE.FromJSON, AE.ToJSON)
+ via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] Error
+
+
+-- error aggreted with number of occurrences and affected users
+data ErrorL = ErrorL
+ { id :: ErrorId
+ , projectId :: Projects.ProjectId
+ , createdAt :: ZonedTime
+ , updatedAt :: ZonedTime
+ , errorType :: Text
+ , message :: Text
+ , stacktrace :: Text
+ , hash :: Text
+ , environment :: Maybe Text
+ , service :: Maybe Text
+ , runtime :: Maybe Text
+ , errorData :: ATError
+ , firstTraceId :: Maybe Text
+ , recentTraceId :: Maybe Text
+ , firstEventId :: Maybe ErrorEventId
+ , lastEventId :: Maybe ErrorEventId
+ , state :: ErrorState
+ , assigneeId :: Maybe Users.UserId
+ , assignedAt :: Maybe ZonedTime
+ , resolvedAt :: Maybe ZonedTime
+ , regressedAt :: Maybe ZonedTime
+ , occurrences1m :: Int
+ , occurrences5m :: Int
+ , occurrences1h :: Int
+ , occurrences24h :: Int
+ , quietMinutes :: Int
+ , resolutionThresholdMinutes :: Int
+ , baselineState :: BaselineState
+ , baselineSamples :: Int
+ , baselineErrorRateMean :: Maybe Double
+ , baselineErrorRateStddev :: Maybe Double
+ , baselineUpdatedAt :: Maybe ZonedTime
+ , isIgnored :: Bool
+ , ignoredUntil :: Maybe ZonedTime
+ , occurrences :: Int
+ , affectedUsers :: Int
+ , lastOccurredAt :: Maybe ZonedTime
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow, NFData, ToRow)
+ deriving
+ (Entity)
+ via (GenericEntity '[Schema "apis", TableName "errors", PrimaryKey "id", FieldModifiers '[CamelToSnake]] Error)
+ deriving
+ (AE.FromJSON, AE.ToJSON)
+ via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] ErrorL
+
+
+data ATError = ATError
+ { projectId :: Maybe Projects.ProjectId
+ , when :: UTCTime
+ , errorType :: Text
+ , rootErrorType :: Text
+ , message :: Text
+ , rootErrorMessage :: Text
+ , stackTrace :: Text
+ , hash :: Text
+ , technology :: Maybe RequestDumps.SDKTypes
+ , requestMethod :: Maybe Text
+ , requestPath :: Maybe Text
+ , serviceName :: Maybe Text
+ , environment :: Maybe Text
+ , runtime :: Maybe Text
+ , traceId :: Maybe Text
+ , spanId :: Maybe Text
+ , parentSpanId :: Maybe Text
+ , endpointHash :: Maybe Text
+ , userId :: Maybe Text
+ , userEmail :: Maybe Text
+ , sessionId :: Maybe Text
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (Default, NFData)
+ deriving (FromField, ToField) via Aeson ATError
+ deriving
+ (AE.FromJSON, AE.ToJSON)
+ via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] ATError
+
+
+data ErrorEvent = ErrorEvent
+ { id :: ErrorEventId
+ , projectId :: Projects.ProjectId
+ , errorId :: ErrorId
+ , occurredAt :: ZonedTime
+ , targetHash :: Text
+ , errorType :: Text
+ , message :: Text
+ , stackTrace :: Text
+ , serviceName :: Text
+ , release :: Maybe Text
+ , platformVersion :: Maybe Text
+ , requestMethod :: Maybe Text
+ , requestPath :: Maybe Text
+ , endpointHash :: Maybe Text
+ , traceId :: Maybe Text
+ , spanId :: Maybe Text
+ , parentSpanId :: Maybe Text
+ , userId :: Maybe Text
+ , userEmail :: Maybe Text
+ , sessionId :: Maybe Text
+ , sampleRate :: Double
+ , ingestionId :: Maybe UUID.UUID
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow, NFData, ToRow)
+ deriving
+ (Entity)
+ via (GenericEntity '[Schema "apis", TableName "error_events", PrimaryKey "id", FieldModifiers '[CamelToSnake]] ErrorEvent)
+ deriving
+ (AE.FromJSON, AE.ToJSON)
+ via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] ErrorEvent
+
+
+-- | Get errors for a project with optional state filter
+getErrors :: DB es => Projects.ProjectId -> Maybe ErrorState -> Int -> Int -> Eff es [Error]
+getErrors pid mstate limit offset = PG.query q (pid, maybe "%" errorStateToText mstate, limit, offset)
+ where
+ q =
+ [sql|
+ SELECT id, project_id, created_at, updated_at,
+ error_type, message, stacktrace, hash,
+ environment, service, runtime, error_data,
+ first_trace_id, recent_trace_id, first_event_id, last_event_id,
+ state, assignee_id, assigned_at, resolved_at, regressed_at,
+ occurrences_1m, occurrences_5m, occurrences_1h, occurrences_24h,
+ quiet_minutes, resolution_threshold_minutes,
+ baseline_state, baseline_samples,
+ baseline_error_rate_mean, baseline_error_rate_stddev, baseline_updated_at,
+ is_ignored, ignored_until
+ FROM apis.errors
+ WHERE project_id = ? AND state LIKE ?
+ ORDER BY updated_at DESC
+ LIMIT ? OFFSET ?
+ |]
+
+
+-- | Get error by ID
+getErrorById :: DB es => ErrorId -> Eff es (Maybe Error)
+getErrorById eid = do
+ results <- PG.query q (Only eid)
+ return $ listToMaybe results
+ where
+ q =
+ [sql|
+ SELECT id, project_id, created_at, updated_at,
+ error_type, message, stacktrace, hash,
+ environment, service, runtime, error_data,
+ first_trace_id, recent_trace_id, first_event_id, last_event_id,
+ state, assignee_id, assigned_at, resolved_at, regressed_at,
+ occurrences_1m, occurrences_5m, occurrences_1h, occurrences_24h,
+ quiet_minutes, resolution_threshold_minutes,
+ baseline_state, baseline_samples,
+ baseline_error_rate_mean, baseline_error_rate_stddev, baseline_updated_at,
+ is_ignored, ignored_until
+ FROM apis.errors
+ WHERE id = ?
+ |]
+
+
+-- | Get error by hash
+getErrorByHash :: DB es => Projects.ProjectId -> Text -> Eff es (Maybe Error)
+getErrorByHash pid hash = do
+ results <- PG.query q (pid, hash)
+ return $ listToMaybe results
+ where
+ q =
+ [sql|
+ SELECT id, project_id, created_at, updated_at,
+ error_type, message, stacktrace, hash,
+ environment, service, runtime, error_data,
+ first_trace_id, recent_trace_id, first_event_id, last_event_id,
+ state, assignee_id, assigned_at, resolved_at, regressed_at,
+ occurrences_1m, occurrences_5m, occurrences_1h, occurrences_24h,
+ quiet_minutes, resolution_threshold_minutes,
+ baseline_state, baseline_samples,
+ baseline_error_rate_mean, baseline_error_rate_stddev, baseline_updated_at,
+ is_ignored, ignored_until
+ FROM apis.errors
+ WHERE project_id = ? AND hash = ?
+ |]
+
+
+getErrorLByHash :: DB es => Projects.ProjectId -> Text -> Eff es (Maybe ErrorL)
+getErrorLByHash pid hash = do
+ results <- PG.query q (pid, hash)
+ return $ listToMaybe results
+ where
+ q =
+ [sql|
+ SELECT id, project_id, created_at, updated_at,
+ error_type, message, stacktrace, hash,
+ environment, service, runtime, error_data,
+ first_trace_id, recent_trace_id, first_event_id, last_event_id,
+ state, assignee_id, assigned_at, resolved_at, regressed_at,
+ occurrences_1m, occurrences_5m, occurrences_1h, occurrences_24h,
+ quiet_minutes, resolution_threshold_minutes,
+ baseline_state, baseline_samples,
+ baseline_error_rate_mean, baseline_error_rate_stddev, baseline_updated_at,
+ is_ignored, ignored_until,
+ (SELECT COUNT(*) FROM apis.error_events WHERE target_hash = e.hash) AS occurrences,
+ (SELECT COUNT(DISTINCT user_id) FROM apis.error_events WHERE target_hash = e.hash) AS affected_users,
+ (SELECT MAX(occurred_at) FROM apis.error_events WHERE target_hash = e.hash) AS last_occurred_at
+ FROM apis.errors e
+ WHERE project_id = ? AND hash = ?
+ |]
+
+
+-- | Get active (non-resolved) errors
+getActiveErrors :: DB es => Projects.ProjectId -> Eff es [Error]
+getActiveErrors pid = PG.query q (Only pid)
+ where
+ q =
+ [sql|
+ SELECT id, project_id, created_at, updated_at,
+ error_type, message, stacktrace, hash,
+ environment, service, runtime, error_data,
+ first_trace_id, recent_trace_id, first_event_id, last_event_id,
+ state, assignee_id, assigned_at, resolved_at, regressed_at,
+ occurrences_1m, occurrences_5m, occurrences_1h, occurrences_24h,
+ quiet_minutes, resolution_threshold_minutes,
+ baseline_state, baseline_samples,
+ baseline_error_rate_mean, baseline_error_rate_stddev, baseline_updated_at,
+ is_ignored, ignored_until
+ FROM apis.errors
+ WHERE project_id = ? AND state != 'resolved'
+ ORDER BY updated_at DESC
+ |]
+
+
+-- | Update occurrence counts (called periodically to decay counts)
+updateOccurrenceCounts :: DB es => Eff es Int64
+updateOccurrenceCounts =
+ PG.execute_ q
+ where
+ q =
+ [sql|
+ UPDATE apis.errors SET
+ occurrences_1m = 0,
+ occurrences_5m = CASE WHEN occurrences_5m > 0 THEN occurrences_5m - occurrences_1m ELSE 0 END,
+ occurrences_1h = CASE WHEN occurrences_1h > 0 THEN occurrences_1h - occurrences_5m ELSE 0 END,
+ occurrences_24h = CASE WHEN occurrences_24h > 0 THEN occurrences_24h - occurrences_1h ELSE 0 END,
+ quiet_minutes = quiet_minutes + 1,
+ state = CASE
+ WHEN state IN ('new', 'escalating', 'ongoing') AND quiet_minutes >= resolution_threshold_minutes THEN 'resolved'
+ ELSE state
+ END,
+ resolved_at = CASE
+ WHEN state IN ('new', 'escalating', 'ongoing') AND quiet_minutes >= resolution_threshold_minutes THEN NOW()
+ ELSE resolved_at
+ END
+ WHERE state != 'resolved' OR occurrences_24h > 0
+ |]
+
+
+updateErrorState :: DB es => ErrorId -> ErrorState -> Eff es Int64
+updateErrorState eid newState = PG.execute q (errorStateToText newState, eid)
+ where
+ q =
+ [sql| UPDATE apis.errors SET state = ?, updated_at = NOW() WHERE id = ? |]
+
+
+updateErrorStateByProjectAndHash :: DB es => Projects.ProjectId -> Text -> ErrorState -> Eff es Int64
+updateErrorStateByProjectAndHash pid hash newState = PG.execute q (errorStateToText newState, pid, hash)
+ where
+ q =
+ [sql| UPDATE apis.errors SET state = ?, updated_at = NOW() WHERE project_id = ? AND hash = ? |]
+
+
+resolveError :: DB es => ErrorId -> Eff es Int64
+resolveError eid = PG.execute q (Only eid)
+ where
+ q = [sql| UPDATE apis.errors SET state = 'resolved', resolved_at = NOW(), updated_at = NOW() WHERE id = ? |]
+
+
+assignError :: DB es => ErrorId -> Users.UserId -> Eff es Int64
+assignError eid uid = PG.execute q (uid, eid)
+ where
+ q = [sql| UPDATE apis.errors SET assignee_id = ?, assigned_at = NOW(), updated_at = NOW() WHERE id = ? |]
+
+
+-- | Update baseline data for an error
+updateBaseline :: DB es => ErrorId -> BaselineState -> Double -> Double -> Int -> Eff es Int64
+updateBaseline eid bState rateMean rateStddev samples =
+ PG.execute q (bState, rateMean, rateStddev, samples, eid)
+ where
+ q =
+ [sql|
+ UPDATE apis.errors
+ SET baseline_state = ?,
+ baseline_error_rate_mean = ?,
+ baseline_error_rate_stddev = ?,
+ baseline_samples = ?,
+ baseline_updated_at = NOW()
+ WHERE id = ?
+ |]
+
+
+-- | Hourly bucket for error event aggregation
+data HourlyBucket = HourlyBucket
+ { hourStart :: UTCTime
+ , eventCount :: Int
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow)
+
+
+-- | Get hourly error counts for a specific error over a time range
+-- Returns counts bucketed by hour for baseline calculation
+getHourlyErrorCounts :: DB es => ErrorId -> Int -> Eff es [HourlyBucket]
+getHourlyErrorCounts eid hoursBack =
+ PG.query q (eid, hoursBack)
+ where
+ q =
+ [sql|
+ SELECT
+ date_trunc('hour', occurred_at) AS hour_start,
+ COUNT(*)::INT AS event_count
+ FROM apis.error_events
+ WHERE error_id = ?
+ AND occurred_at >= NOW() - INTERVAL '1 hour' * ?
+ GROUP BY date_trunc('hour', occurred_at)
+ ORDER BY hour_start
+ |]
+
+
+-- | Get current hour error count for a specific error
+getCurrentHourErrorCount :: DB es => ErrorId -> Eff es Int
+getCurrentHourErrorCount eid = do
+ results <- PG.query q (Only eid)
+ case results of
+ [Only count] -> return count
+ _ -> return 0
+ where
+ q =
+ [sql|
+ SELECT COUNT(*)::INT
+ FROM apis.error_events
+ WHERE error_id = ?
+ AND occurred_at >= date_trunc('hour', NOW())
+ |]
+
+
+-- | Get error event stats for baseline calculation
+-- Returns mean and stddev of hourly counts over the lookback period
+data ErrorEventStats = ErrorEventStats
+ { hourlyMean :: Double
+ , hourlyStddev :: Double
+ , totalHours :: Int
+ , totalEvents :: Int
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow)
+
+
+getErrorEventStats :: DB es => ErrorId -> Int -> Eff es (Maybe ErrorEventStats)
+getErrorEventStats eid hoursBack = do
+ results <- PG.query q (eid, hoursBack)
+ return $ listToMaybe results
+ where
+ q =
+ [sql|
+ WITH hourly_counts AS (
+ SELECT
+ date_trunc('hour', occurred_at) AS hour_start,
+ COUNT(*) AS event_count
+ FROM apis.error_events
+ WHERE error_id = ?
+ AND occurred_at >= NOW() - INTERVAL '1 hour' * ?
+ GROUP BY date_trunc('hour', occurred_at)
+ )
+ SELECT
+ COALESCE(AVG(event_count), 0)::FLOAT AS hourly_mean,
+ COALESCE(STDDEV(event_count), 0)::FLOAT AS hourly_stddev,
+ COUNT(*)::INT AS total_hours,
+ COALESCE(SUM(event_count), 0)::INT AS total_events
+ FROM hourly_counts
+ |]
+
+
+-- | Check if an error is spiking compared to its baseline
+-- Returns (isSpike, currentRate, zScore) if baseline is established
+checkErrorSpike :: DB es => Error -> Eff es (Maybe (Bool, Double, Double))
+checkErrorSpike err = do
+ case (err.baselineState, err.baselineErrorRateMean, err.baselineErrorRateStddev) of
+ (BSEstablished, Just mean, Just stddev) | stddev > 0 -> do
+ currentCount <- getCurrentHourErrorCount err.id
+ let currentRate = fromIntegral currentCount :: Double
+ zScore = (currentRate - mean) / stddev
+ -- Spike: > 3 std devs AND at least 5 more than mean (avoid noise on low-volume errors)
+ isSpike = zScore > 3.0 && currentRate > mean + 5
+ return $ Just (isSpike, currentRate, zScore)
+ _ -> return Nothing
+
+
+-- | Get all errors with their current hour counts (for batch spike detection)
+data ErrorWithCurrentRate = ErrorWithCurrentRate
+ { errorId :: ErrorId
+ , projectId :: Projects.ProjectId
+ , errorType :: Text
+ , message :: Text
+ , service :: Maybe Text
+ , baselineState :: BaselineState
+ , baselineMean :: Maybe Double
+ , baselineStddev :: Maybe Double
+ , currentHourCount :: Int
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow)
+
+
+getErrorsWithCurrentRates :: DB es => Projects.ProjectId -> Eff es [ErrorWithCurrentRate]
+getErrorsWithCurrentRates pid =
+ PG.query q (pid)
+ where
+ q =
+ [sql|
+ SELECT
+ e.id,
+ e.project_id,
+ e.error_type,
+ e.message,
+ e.service,
+ e.baseline_state,
+ e.baseline_error_rate_mean,
+ e.baseline_error_rate_stddev,
+ COALESCE(counts.current_count, 0)::INT AS current_hour_count
+ FROM apis.errors e
+ LEFT JOIN (
+ SELECT error_id, COUNT(*) AS current_count
+ FROM apis.error_events
+ WHERE occurred_at >= date_trunc('hour', NOW())
+ GROUP BY error_id
+ ) counts ON counts.error_id = e.id
+ WHERE e.project_id = ?
+ AND e.state != 'resolved'
+ AND e.is_ignored = false
+ |]
+
+
+-- | Upsert an error (insert or update on conflict)
+upsertErrorQueryAndParam :: Projects.ProjectId -> ATError -> (Query, [DBField])
+upsertErrorQueryAndParam pid err = (q, params)
+ where
+ q =
+ [sql|
+ INSERT INTO apis.errors (
+ project_id, error_type, message, stacktrace, hash,
+ environment, service, runtime, error_data,
+ first_trace_id, recent_trace_id,
+ occurrences_1m, occurrences_5m, occurrences_1h, occurrences_24h
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, 1, 1, 1)
+ ON CONFLICT (project_id, hash) DO UPDATE SET
+ updated_at = NOW(),
+ message = EXCLUDED.message,
+ recent_trace_id = EXCLUDED.recent_trace_id,
+ occurrences_1m = apis.errors.occurrences_1m + 1,
+ occurrences_5m = apis.errors.occurrences_5m + 1,
+ occurrences_1h = apis.errors.occurrences_1h + 1,
+ occurrences_24h = apis.errors.occurrences_24h + 1,
+ state = CASE
+ WHEN apis.errors.state = 'resolved' THEN 'regressed'
+ WHEN apis.errors.state = 'new' AND apis.errors.occurrences_1h > 10 THEN 'escalating'
+ ELSE apis.errors.state
+ END,
+ regressed_at = CASE
+ WHEN apis.errors.state = 'resolved' THEN NOW()
+ ELSE apis.errors.regressed_at
+ END
+ |]
+ params =
+ [ MkDBField err.projectId
+ , MkDBField err.errorType
+ , MkDBField err.message
+ , MkDBField err.stackTrace
+ , MkDBField err.hash
+ , MkDBField err.environment
+ , MkDBField err.serviceName
+ , MkDBField err.runtime
+ , MkDBField err
+ , MkDBField err.traceId
+ , MkDBField err.traceId
+ ]
+
+
+-- =============================================================================
+-- Error Fingerprinting (Sentry-style)
+-- =============================================================================
+-- Fingerprinting priority:
+-- 1. Stack trace (if available and has in-app frames)
+-- 2. Exception type + normalized message
+-- 3. Normalized message only (fallback)
+--
+-- Stack trace normalization extracts:
+-- - Module/package name
+-- - Function name (cleaned per platform)
+-- - Context line (whitespace normalized, max 120 chars)
+--
+-- Message normalization:
+-- - Limits to first 2 non-empty lines
+-- - Replaces UUIDs, IPs, emails, timestamps, numbers with placeholders
+
+-- | Represents a parsed stack frame
+data StackFrame = StackFrame
+ { sfFilePath :: Text
+ -- ^ Full file path or module path
+ , sfModule :: Maybe Text
+ -- ^ Module/package name (extracted from path)
+ , sfFunction :: Text
+ -- ^ Function/method name
+ , sfLineNumber :: Maybe Int
+ -- ^ Line number
+ , sfColumnNumber :: Maybe Int
+ -- ^ Column number
+ , sfContextLine :: Maybe Text
+ -- ^ Source code at this frame (if available)
+ , sfIsInApp :: Bool
+ -- ^ Whether this is application code vs library/system
+ }
+ deriving stock (Eq, Generic, Show)
+ deriving anyclass (NFData)
+
+
+-- | Parse a stack trace into a list of stack frames based on runtime
+--
+-- >>> length $ parseStackTrace "nodejs" "at processTicksAndRejections (node:internal/process/task_queues:95:5)\nat handleRequest (/app/src/server.js:42:15)"
+-- 2
+--
+-- >>> map sfFunction $ parseStackTrace "python" "File \"/app/main.py\", line 10, in main\nFile \"/app/utils.py\", line 5, in helper"
+-- ["main","helper"]
+--
+-- >>> map sfIsInApp $ parseStackTrace "nodejs" "at handleRequest (/app/src/server.js:42:15)\nat processTicksAndRejections (node:internal/process/task_queues:95:5)"
+-- [True,False]
+--
+-- >>> map sfFunction $ parseStackTrace "java" "at com.example.MyClass.doWork(MyClass.java:25)\nat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)"
+-- ["doWork","doDispatch"]
+parseStackTrace :: Text -> Text -> [StackFrame]
+parseStackTrace mSdk stackText =
+ let lns = filter (not . T.null . T.strip) $ T.lines stackText
+ in mapMaybe (parseStackFrame mSdk) lns
+
+
+-- | Parse a single stack frame line based on SDK type
+parseStackFrame :: Text -> Text -> Maybe StackFrame
+parseStackFrame runtime line =
+ let trimmed = T.strip line
+ in case runtime of
+ "go" -> parseGoFrame trimmed
+ "nodejs" -> parseJsFrame trimmed
+ "webjs" -> parseJsFrame trimmed
+ "python" -> parsePythonFrame trimmed
+ "java" -> parseJavaFrame trimmed
+ "php" -> parsePhpFrame trimmed
+ "dotnet" -> parseDotNetFrame trimmed
+ _ -> parseGenericFrame trimmed
+
+
+-- | Parse Go stack frame: "goroutine 1 [running]:" or "main.foo(0x1234)"
+-- Format: package.function(args) or /path/to/file.go:123 +0x1f
+parseGoFrame :: Text -> Maybe StackFrame
+parseGoFrame line
+ | "goroutine" `T.isPrefixOf` line = Nothing -- Skip goroutine headers
+ | ".go:" `T.isInfixOf` line =
+ -- File path line: /path/to/file.go:123 +0x1f
+ let (pathPart, _) = T.breakOn " +" line
+ (filePath, lineCol) = T.breakOnEnd ":" pathPart
+ lineNum = readMaybe $ toString $ T.takeWhile (/= ':') lineCol
+ in Just
+ StackFrame
+ { sfFilePath = T.dropEnd 1 filePath -- Remove trailing ':'
+ , sfModule = extractGoModule filePath
+ , sfFunction = ""
+ , sfLineNumber = lineNum
+ , sfColumnNumber = Nothing
+ , sfContextLine = Nothing
+ , sfIsInApp = isGoInApp filePath
+ }
+ | "(" `T.isInfixOf` line =
+ -- Function call line: main.foo(0x1234, 0x5678)
+ let (funcPart, _) = T.breakOn "(" line
+ in Just
+ StackFrame
+ { sfFilePath = ""
+ , sfModule = extractGoModuleFromFunc funcPart
+ , sfFunction = extractGoFuncName funcPart
+ , sfLineNumber = Nothing
+ , sfColumnNumber = Nothing
+ , sfContextLine = Nothing
+ , sfIsInApp = isGoFuncInApp funcPart
+ }
+ | otherwise = Nothing
+ where
+ extractGoModule path =
+ let parts = T.splitOn "/" path
+ in if length parts > 2
+ then Just $ T.intercalate "/" $ take 3 parts
+ else Nothing
+
+ extractGoModuleFromFunc func =
+ let parts = T.splitOn "." func
+ in if length parts > 1
+ then T.intercalate "." <$> viaNonEmpty init parts
+ else Nothing
+
+ extractGoFuncName func =
+ let parts = T.splitOn "." func
+ in fromMaybe func $ viaNonEmpty last parts
+
+ isGoInApp path =
+ not
+ $ any
+ (`T.isInfixOf` path)
+ ["go/src/", "pkg/mod/", "vendor/", "/runtime/", "/net/", "/syscall/"]
+
+ isGoFuncInApp func =
+ not
+ $ any
+ (`T.isPrefixOf` func)
+ ["runtime.", "syscall.", "net.", "net/http.", "reflect."]
+
+
+-- | Parse JavaScript stack frame
+-- Formats:
+-- at functionName (filePath:line:col)
+-- at filePath:line:col
+-- at async functionName (filePath:line:col)
+parseJsFrame :: Text -> Maybe StackFrame
+parseJsFrame line
+ | "at " `T.isPrefixOf` T.strip line =
+ let content = T.strip $ T.drop 3 $ T.strip line
+ -- Handle "at async ..."
+ content' =
+ if "async " `T.isPrefixOf` content
+ then T.drop 6 content
+ else content
+ in if "(" `T.isInfixOf` content'
+ then parseJsWithParens content'
+ else parseJsWithoutParens content'
+ | otherwise = Nothing
+ where
+ parseJsWithParens txt =
+ let (funcPart, rest) = T.breakOn " (" txt
+ locationPart = T.dropAround (\c -> c == '(' || c == ')') rest
+ (filePath, lineCol) = parseJsLocation locationPart
+ (lineNum, colNum) = parseLineCol lineCol
+ in Just
+ StackFrame
+ { sfFilePath = filePath
+ , sfModule = extractJsModule filePath
+ , sfFunction = cleanJsFunction funcPart
+ , sfLineNumber = lineNum
+ , sfColumnNumber = colNum
+ , sfContextLine = Nothing
+ , sfIsInApp = isJsInApp filePath
+ }
+
+ parseJsWithoutParens txt =
+ let (filePath, lineCol) = parseJsLocation txt
+ (lineNum, colNum) = parseLineCol lineCol
+ in Just
+ StackFrame
+ { sfFilePath = filePath
+ , sfModule = extractJsModule filePath
+ , sfFunction = ""
+ , sfLineNumber = lineNum
+ , sfColumnNumber = colNum
+ , sfContextLine = Nothing
+ , sfIsInApp = isJsInApp filePath
+ }
+
+ parseJsLocation loc =
+ -- Split from the right to handle paths with colons (Windows)
+ let parts = T.splitOn ":" loc
+ n = length parts
+ in if n >= 3
+ then (T.intercalate ":" $ take (n - 2) parts, T.intercalate ":" $ drop (n - 2) parts)
+ else (loc, "")
+
+ parseLineCol lc =
+ let parts = T.splitOn ":" lc
+ in case parts of
+ [l, c] -> (readMaybe $ toString l, readMaybe $ toString c)
+ [l] -> (readMaybe $ toString l, Nothing)
+ _ -> (Nothing, Nothing)
+
+ extractJsModule path =
+ let baseName = fromMaybe path $ viaNonEmpty last $ T.splitOn "/" path
+ in Just
+ $ T.toLower
+ $ fromMaybe baseName
+ $ T.stripSuffix ".js" baseName
+ <|> T.stripSuffix ".ts" baseName
+ <|> T.stripSuffix ".mjs" baseName
+ <|> T.stripSuffix ".cjs" baseName
+
+ cleanJsFunction func =
+ -- Remove namespacing: Object.foo.bar -> bar
+ let parts = T.splitOn "." func
+ in fromMaybe func $ viaNonEmpty last parts
+
+ isJsInApp path =
+ not
+ $ any
+ (`T.isInfixOf` path)
+ ["node_modules/", "", "internal/", "node:"]
+
+
+-- | Parse Python stack frame
+-- Format: File "path/to/file.py", line 123, in function_name
+parsePythonFrame :: Text -> Maybe StackFrame
+parsePythonFrame line
+ | "File \"" `T.isPrefixOf` T.strip line =
+ let content = T.drop 6 $ T.strip line -- Remove 'File "'
+ (filePath, rest) = T.breakOn "\"" content
+ -- Parse ", line 123, in func_name"
+ parts = T.splitOn ", " $ T.drop 2 rest -- Skip '",'
+ lineNum = case find ("line " `T.isPrefixOf`) parts of
+ Just p -> readMaybe $ toString $ T.drop 5 p
+ Nothing -> Nothing
+ funcName = case find ("in " `T.isPrefixOf`) parts of
+ Just p -> T.drop 3 p
+ Nothing -> ""
+ in Just
+ StackFrame
+ { sfFilePath = filePath
+ , sfModule = extractPythonModule filePath
+ , sfFunction = cleanPythonFunction funcName
+ , sfLineNumber = lineNum
+ , sfColumnNumber = Nothing
+ , sfContextLine = Nothing
+ , sfIsInApp = isPythonInApp filePath
+ }
+ | otherwise = Nothing
+ where
+ extractPythonModule path =
+ let baseName = fromMaybe path $ viaNonEmpty last $ T.splitOn "/" path
+ moduleName = fromMaybe baseName $ T.stripSuffix ".py" baseName
+ in Just moduleName
+
+ cleanPythonFunction func =
+ -- Remove lambda indicators
+ T.replace "" "lambda"
+ $ T.replace "" "listcomp"
+ $ T.replace "" "dictcomp" func
+
+ isPythonInApp path =
+ not
+ $ any
+ (`T.isInfixOf` path)
+ ["site-packages/", "dist-packages/", "/lib/python", " Maybe StackFrame
+parseJavaFrame line
+ | "at " `T.isPrefixOf` T.strip line =
+ let content = T.drop 3 $ T.strip line
+ (qualifiedMethod, rest) = T.breakOn "(" content
+ locationPart = T.dropAround (\c -> c == '(' || c == ')') rest
+ (fileName, lineNum) = parseJavaLocation locationPart
+ (moduleName, funcName) = splitJavaQualified qualifiedMethod
+ in Just
+ StackFrame
+ { sfFilePath = fileName
+ , sfModule = Just moduleName
+ , sfFunction = cleanJavaFunction funcName
+ , sfLineNumber = lineNum
+ , sfColumnNumber = Nothing
+ , sfContextLine = Nothing
+ , sfIsInApp = isJavaInApp qualifiedMethod
+ }
+ | otherwise = Nothing
+ where
+ parseJavaLocation loc =
+ let (file, lineStr) = T.breakOn ":" loc
+ in (file, readMaybe $ toString $ T.drop 1 lineStr)
+
+ splitJavaQualified qualified =
+ let parts = T.splitOn "." qualified
+ in if length parts > 1
+ then case (viaNonEmpty init parts, viaNonEmpty last parts) of
+ (Just ps, Just l) -> (T.intercalate "." ps, l)
+ _ -> ("", qualified)
+ else ("", qualified)
+
+ cleanJavaFunction func =
+ -- Remove generics: method -> method
+ T.takeWhile (/= '<') func
+
+ isJavaInApp qualified =
+ not
+ $ any
+ (`T.isPrefixOf` qualified)
+ ["java.", "javax.", "sun.", "com.sun.", "jdk.", "org.springframework."]
+
+
+-- | Parse PHP stack frame
+-- Format: #0 /path/to/file.php(123): ClassName->methodName()
+parsePhpFrame :: Text -> Maybe StackFrame
+parsePhpFrame line
+ | "#" `T.isPrefixOf` T.strip line =
+ let content = T.drop 1 $ T.dropWhile (/= ' ') $ T.strip line -- Skip "#N "
+ (pathPart, funcPart) = T.breakOn ": " content
+ (filePath, lineNum) = parsePhpPath pathPart
+ funcName = T.takeWhile (/= '(') $ T.drop 2 funcPart
+ in Just
+ StackFrame
+ { sfFilePath = filePath
+ , sfModule = extractPhpModule filePath
+ , sfFunction = cleanPhpFunction funcName
+ , sfLineNumber = lineNum
+ , sfColumnNumber = Nothing
+ , sfContextLine = Nothing
+ , sfIsInApp = isPhpInApp filePath
+ }
+ | otherwise = Nothing
+ where
+ parsePhpPath path =
+ let (file, lineStr) = T.breakOn "(" path
+ lineNum = readMaybe $ toString $ T.takeWhile (/= ')') $ T.drop 1 lineStr
+ in (file, lineNum)
+
+ extractPhpModule path =
+ let baseName = fromMaybe path $ viaNonEmpty last $ T.splitOn "/" path
+ in Just $ fromMaybe baseName $ T.stripSuffix ".php" baseName
+
+ cleanPhpFunction func =
+ -- Remove {closure} markers
+ T.replace "{closure}" "closure"
+ $
+ -- Simplify class::method or class->method
+ let parts = T.splitOn "->" func
+ in if length parts > 1
+ then fromMaybe func $ viaNonEmpty last parts
+ else
+ let parts' = T.splitOn "::" func
+ in if length parts' > 1 then fromMaybe func $ viaNonEmpty last parts' else func
+
+ isPhpInApp path =
+ not
+ $ any
+ (`T.isInfixOf` path)
+ ["/vendor/", "/phar://"]
+
+
+-- | Parse .NET stack frame
+-- Format: at Namespace.Class.Method(params) in /path/to/file.cs:line 123
+parseDotNetFrame :: Text -> Maybe StackFrame
+parseDotNetFrame line
+ | "at " `T.isPrefixOf` T.strip line =
+ let content = T.drop 3 $ T.strip line
+ (methodPart, locationPart) = T.breakOn " in " content
+ qualifiedMethod = T.takeWhile (/= '(') methodPart
+ (moduleName, funcName) = splitDotNetQualified qualifiedMethod
+ (filePath, lineNum) = parseDotNetLocation $ T.drop 4 locationPart
+ in Just
+ StackFrame
+ { sfFilePath = filePath
+ , sfModule = Just moduleName
+ , sfFunction = cleanDotNetFunction funcName
+ , sfLineNumber = lineNum
+ , sfColumnNumber = Nothing
+ , sfContextLine = Nothing
+ , sfIsInApp = isDotNetInApp qualifiedMethod
+ }
+ | otherwise = Nothing
+ where
+ splitDotNetQualified qualified =
+ let parts = T.splitOn "." qualified
+ in if length parts > 1
+ then case (viaNonEmpty init parts, viaNonEmpty last parts) of
+ (Just ps, Just l) -> (T.intercalate "." ps, l)
+ _ -> ("", qualified)
+ else ("", qualified)
+
+ parseDotNetLocation loc =
+ let (path, lineStr) = T.breakOn ":line " loc
+ in (path, readMaybe $ toString $ T.drop 6 lineStr)
+
+ cleanDotNetFunction func =
+ -- Remove generic arity: Method`1 -> Method
+ T.takeWhile (/= '`') func
+
+ isDotNetInApp qualified =
+ not
+ $ any
+ (`T.isPrefixOf` qualified)
+ ["System.", "Microsoft.", "Newtonsoft."]
+
+
+-- | Generic stack frame parser for unknown formats
+parseGenericFrame :: Text -> Maybe StackFrame
+parseGenericFrame line =
+ let trimmed = T.strip line
+ in if T.null trimmed || "..." `T.isPrefixOf` trimmed
+ then Nothing
+ else
+ Just
+ StackFrame
+ { sfFilePath = trimmed
+ , sfModule = Nothing
+ , sfFunction = extractGenericFunction trimmed
+ , sfLineNumber = extractGenericLineNumber trimmed
+ , sfColumnNumber = Nothing
+ , sfContextLine = Nothing
+ , sfIsInApp = True -- Assume in-app by default
+ }
+ where
+ extractGenericFunction txt =
+ -- Try to find function-like patterns
+ let parts = T.words txt
+ in case find (\p -> "(" `T.isInfixOf` p || "." `T.isInfixOf` p) parts of
+ Just p -> T.takeWhile (/= '(') p
+ Nothing -> fromMaybe "" $ listToMaybe parts
+
+ extractGenericLineNumber txt =
+ -- Look for :NUMBER or line NUMBER patterns
+ let parts = T.splitOn ":" txt
+ in case parts of
+ _ : rest -> listToMaybe $ mapMaybe (readMaybe . toString . T.takeWhile (/= ':')) rest
+ _ -> Nothing
+
+
+-- | Normalize a stack trace for fingerprinting
+-- Returns a list of normalized frame strings suitable for hashing
+--
+-- >>> normalizeStackTrace "nodejs" "at handleRequest (/app/src/server.js:42:15)\nat processTicksAndRejections (node:internal/process/task_queues:95:5)"
+-- "server|handleRequest"
+--
+-- >>> normalizeStackTrace "python" "File \"/app/main.py\", line 10, in main\nFile \"/app/utils.py\", line 5, in helper"
+-- "main|main\nutils|helper"
+--
+-- >>> normalizeStackTrace "java" "at com.example.MyClass.doWork(MyClass.java:25)\nat com.example.Service.process(Service.java:50)"
+-- "com.example.MyClass|doWork\ncom.example.Service|process"
+--
+-- >>> normalizeStackTrace "unknown" "some random frame info"
+-- "some"
+normalizeStackTrace :: Text -> Text -> Text
+normalizeStackTrace runtime stackText =
+ let frames = parseStackTrace runtime stackText
+ inAppFrames = filter sfIsInApp frames
+ framesToUse = if null inAppFrames then frames else inAppFrames
+ normalizedFrames = map normalizeFrame framesToUse
+ in T.intercalate "\n" normalizedFrames
+ where
+ normalizeFrame :: StackFrame -> Text
+ normalizeFrame frame =
+ let modulePart = fromMaybe "" frame.sfModule
+ funcPart = normalizeFunction frame.sfFunction
+ -- Context line: normalize whitespace, truncate if > 120 chars
+ contextPart = maybe "" normalizeContextLine frame.sfContextLine
+ in T.intercalate "|" $ filter (not . T.null) [modulePart, funcPart, contextPart]
+
+ normalizeFunction :: Text -> Text
+ normalizeFunction func =
+ -- Common normalizations across platforms:
+ -- 1. Remove memory addresses (0x...)
+ -- 2. Remove generic type parameters
+ -- 3. Remove parameter types
+ -- 4. Normalize anonymous/lambda markers
+ let noAddr = T.unwords $ filter (not . ("0x" `T.isPrefixOf`)) $ T.words func
+ noGenerics = T.takeWhile (/= '<') noAddr
+ noParams = T.takeWhile (/= '(') noGenerics
+ in T.strip noParams
+
+ normalizeContextLine :: Text -> Text
+ normalizeContextLine ctx =
+ let normalized = T.unwords $ T.words ctx
+ in if T.length normalized > 120
+ then "" -- Skip overly long context lines (like Sentry does)
+ else normalized
+
+
+-- | Normalize an error message for fingerprinting
+-- Limits to first 2 non-empty lines and replaces variable content
+--
+-- >>> normalizeMessage "Connection refused to 192.168.1.100:5432"
+-- "Connection refused to {ipv4}{port}"
+--
+-- >>> normalizeMessage "User c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd not found"
+-- "User {uuid} not found"
+--
+-- >>> normalizeMessage "Error 404: Resource 12345 not available\n\nDetails here\nMore info"
+-- "Error {integer}: Resource {integer} not available Details here"
+--
+-- >>> normalizeMessage " Trimmed message "
+-- "Trimmed message"
+normalizeMessage :: Text -> Text
+normalizeMessage msg =
+ let lns = take 2 $ filter (not . T.null . T.strip) $ T.lines msg
+ combined = T.intercalate " " $ map T.strip lns
+ -- Replace variable patterns with placeholders
+ normalized = replaceAllFormats combined
+ in T.strip normalized
+
+
+-- | Compute the error fingerprint hash using Sentry-style prioritization
+-- Priority:
+-- 1. Stack trace (if has meaningful in-app frames)
+-- 2. Exception type + message
+-- 3. Message only
+--
+-- With stack trace - uses projectId, exceptionType, and normalized stack:
+-- >>> computeErrorFingerprint "proj1" (Just "svc") (Just "span") "nodejs" "TypeError" "msg" "at handler (/app/index.js:10:5)"
+-- "269748a1"
+--
+-- Without stack trace but with exception type - uses projectId, service, span, type, and message:
+-- >>> computeErrorFingerprint "proj1" (Just "user-service") (Just "/api/users") "nodejs" "ValidationError" "Invalid email format" ""
+-- "af0aa163"
+--
+-- Without stack trace or exception type - uses projectId, service, span, and message only:
+-- >>> computeErrorFingerprint "proj1" (Just "api") (Just "/health") "nodejs" "" "Connection refused to 192.168.1.1:5432" ""
+-- "44e50418"
+--
+-- Same error from different IPs produces same fingerprint (IP normalized):
+-- >>> computeErrorFingerprint "proj1" (Just "db") Nothing "python" "" "Connection refused to 10.0.0.1:5432" "" == computeErrorFingerprint "proj1" (Just "db") Nothing "python" "" "Connection refused to 172.16.0.50:5432" ""
+-- True
+--
+-- Same stack trace produces same fingerprint regardless of message:
+-- >>> computeErrorFingerprint "proj1" Nothing Nothing "nodejs" "Error" "message 1" "at handler (/app/index.js:10:5)" == computeErrorFingerprint "proj1" Nothing Nothing "nodejs" "Error" "different message" "at handler (/app/index.js:10:5)"
+-- True
+computeErrorFingerprint :: Text -> Maybe Text -> Maybe Text -> Text -> Text -> Text -> Text -> Text
+computeErrorFingerprint projectIdText mService spanName runtime exceptionType message stackTrace =
+ let
+ -- Normalize components
+ normalizedStack = normalizeStackTrace runtime stackTrace
+ normalizedMsg = normalizeMessage message
+ normalizedType = T.strip exceptionType
+
+ -- Build fingerprint components based on priority
+ fingerprintComponents =
+ if hasUsableStackTrace normalizedStack
+ then
+ [ projectIdText
+ , normalizedType
+ , normalizedStack
+ ]
+ else
+ if not (T.null normalizedType)
+ then
+ [ projectIdText
+ , fromMaybe "" mService
+ , fromMaybe "" spanName
+ , normalizedType
+ , normalizedMsg
+ ]
+ else
+ [ projectIdText
+ , fromMaybe "" mService
+ , fromMaybe "" spanName
+ , normalizedMsg
+ ]
+
+ -- Combine and hash
+ combined = T.intercalate "|" $ filter (not . T.null) fingerprintComponents
+ in
+ toXXHash combined
+ where
+ hasUsableStackTrace :: Text -> Bool
+ hasUsableStackTrace normalized =
+ let lns = T.lines normalized
+ nonEmptyLines = filter (not . T.null . T.strip) lns
+ in length nonEmptyLines >= 1
diff --git a/src/Models/Apis/Issues.hs b/src/Models/Apis/Issues.hs
index 9b534e4a2..e092bd94a 100644
--- a/src/Models/Apis/Issues.hs
+++ b/src/Models/Apis/Issues.hs
@@ -23,6 +23,16 @@ module Models.Apis.Issues (
APIChangeData (..),
RuntimeExceptionData (..),
QueryAlertData (..),
+ LogPatternData (..),
+ ErrorEscalatingData (..),
+ ErrorRegressedData (..),
+ LogPatternRateChangeData (..),
+ EndpointLatencyDegradationData (..),
+ EndpointErrorRateSpikeData (..),
+ EndpointVolumeRateChangeData (..),
+ NewEndpointData (..),
+ NewShapeData (..),
+ FieldChangeData (..),
-- * Database Operations
insertIssue,
@@ -34,11 +44,23 @@ module Models.Apis.Issues (
updateIssueCriticality,
acknowledgeIssue,
selectIssueByHash,
+ updateIssueData,
-- * Conversion Functions
- createAPIChangeIssue,
- createRuntimeExceptionIssue,
createQueryAlertIssue,
+ createNewErrorIssue,
+ createErrorSpikeIssue,
+ createLogPatternIssue,
+ createErrorEscalatingIssue,
+ createErrorRegressedIssue,
+ createLogPatternRateChangeIssue,
+ createEndpointLatencyDegradationIssue,
+ createEndpointErrorRateSpikeIssue,
+ createEndpointVolumeRateChangeIssue,
+ createNewEndpointIssue,
+ createNewShapeIssue,
+ createFieldChangeIssue,
+ findOpenShapeIssueForStatusCode,
-- * Utilities
issueIdText,
@@ -81,6 +103,8 @@ import Effectful (Eff)
import Effectful.PostgreSQL qualified as PG
import Models.Apis.Anomalies (PayloadChange)
import Models.Apis.Anomalies qualified as Anomalies
+import Models.Apis.Errors qualified as Errors
+import Models.Apis.LogPatterns qualified as LogPatterns
import Models.Apis.RequestDumps qualified as RequestDumps
import Models.Projects.Projects qualified as Projects
import Models.Users.Users qualified as Users
@@ -101,29 +125,54 @@ issueIdText = idToText
-- | Issue types
data IssueType
- = APIChange
+ = NewEndpoint
+ | NewShape
+ | FieldChange
| RuntimeException
| QueryAlert
+ | LogPattern
+ | ErrorEscalating
+ | ErrorRegressed
+ | LogPatternRateChange
+ | EndpointLatencyDegradation
+ | EndpointErrorRateSpike
+ | EndpointVolumeRateChange
deriving stock (Eq, Generic, Show)
deriving anyclass (NFData)
deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.ConstructorTagModifier '[DAE.CamelToSnake]] IssueType
-instance Default IssueType where
- def = APIChange
-
-
issueTypeToText :: IssueType -> Text
-issueTypeToText APIChange = "api_change" -- Maps to anomaly_type 'shape' in DB
+issueTypeToText NewEndpoint = "new_endpoint"
+issueTypeToText NewShape = "new_shape"
+issueTypeToText FieldChange = "field_change"
issueTypeToText RuntimeException = "runtime_exception"
issueTypeToText QueryAlert = "query_alert"
+issueTypeToText LogPattern = "log_pattern"
+issueTypeToText ErrorEscalating = "error_escalating"
+issueTypeToText ErrorRegressed = "error_regressed"
+issueTypeToText LogPatternRateChange = "log_pattern_rate_change"
+issueTypeToText EndpointLatencyDegradation = "endpoint_latency_degradation"
+issueTypeToText EndpointErrorRateSpike = "endpoint_error_rate_spike"
+issueTypeToText EndpointVolumeRateChange = "endpoint_volume_rate_change"
parseIssueType :: Text -> Maybe IssueType
-parseIssueType "api_change" = Just APIChange
-parseIssueType "shape" = Just APIChange -- Handle DB anomaly_type
+parseIssueType "new_endpoint" = Just NewEndpoint
+parseIssueType "new_shape" = Just NewShape
+parseIssueType "field_change" = Just FieldChange
+parseIssueType "shape" = Just NewShape -- Handle DB anomaly_type
+parseIssueType "endpoint" = Just NewEndpoint -- Handle DB anomaly_type
+parseIssueType "field" = Just FieldChange -- Handle DB anomaly_type
parseIssueType "runtime_exception" = Just RuntimeException
parseIssueType "query_alert" = Just QueryAlert
+parseIssueType "log_pattern" = Just LogPattern
+parseIssueType "error_escalating" = Just ErrorEscalating
+parseIssueType "error_regressed" = Just ErrorRegressed
+parseIssueType "log_pattern_rate_change" = Just LogPatternRateChange
+parseIssueType "endpoint_latency_degradation" = Just EndpointLatencyDegradation
+parseIssueType "endpoint_error_rate_spike" = Just EndpointErrorRateSpike
+parseIssueType "endpoint_volume_rate_change" = Just EndpointVolumeRateChange
parseIssueType _ = Nothing
@@ -190,6 +239,200 @@ data QueryAlertData = QueryAlertData
deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] QueryAlertData
+-- | Log Pattern issue data (new pattern detected)
+data LogPatternData = LogPatternData
+ { patternHash :: Text
+ , logPattern :: Text
+ , sampleMessage :: Maybe Text
+ , logLevel :: Maybe Text
+ , serviceName :: Maybe Text
+ , firstSeenAt :: UTCTime
+ , occurrenceCount :: Int
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson LogPatternData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] LogPatternData
+
+
+-- | Error Escalating issue data (error rate increasing over time)
+data ErrorEscalatingData = ErrorEscalatingData
+ { errorHash :: Text
+ , errorType :: Text
+ , errorMessage :: Text
+ , serviceName :: Maybe Text
+ , currentState :: Text -- "escalating"
+ , previousState :: Text -- "new" or "ongoing"
+ , occurrences1h :: Int
+ , occurrences24h :: Int
+ , escalationRate :: Double -- rate of increase (e.g., 2.5x)
+ , escalationWindow :: Text -- "1h", "6h", "24h"
+ , firstSeenAt :: UTCTime
+ , lastSeenAt :: UTCTime
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson ErrorEscalatingData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] ErrorEscalatingData
+
+
+-- | Error Regressed issue data (previously resolved error returned)
+data ErrorRegressedData = ErrorRegressedData
+ { errorHash :: Text
+ , errorType :: Text
+ , errorMessage :: Text
+ , serviceName :: Maybe Text
+ , resolvedAt :: UTCTime -- when it was previously resolved
+ , regressedAt :: UTCTime -- when it came back
+ , quietPeriodMinutes :: Int -- how long it was quiet
+ , previousOccurrences :: Int -- count before resolution
+ , newOccurrences :: Int -- count since regression
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson ErrorRegressedData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] ErrorRegressedData
+
+
+-- | Log Pattern Rate Change issue data (volume spike/drop)
+data LogPatternRateChangeData = LogPatternRateChangeData
+ { patternHash :: Text
+ , logPattern :: Text
+ , sampleMessage :: Maybe Text
+ , logLevel :: Maybe Text
+ , serviceName :: Maybe Text
+ , currentRatePerHour :: Double
+ , baselineMean :: Double
+ , baselineStddev :: Double
+ , zScore :: Double -- standard deviations from baseline
+ , changePercent :: Double -- percentage change from baseline
+ , changeDirection :: Text -- "spike" or "drop"
+ , detectedAt :: UTCTime
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson LogPatternRateChangeData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] LogPatternRateChangeData
+
+
+-- | Endpoint Latency Degradation issue data
+data EndpointLatencyDegradationData = EndpointLatencyDegradationData
+ { endpointHash :: Text
+ , endpointMethod :: Text
+ , endpointPath :: Text
+ , serviceName :: Maybe Text
+ , currentLatencyMs :: Double -- current p50/p95/p99
+ , baselineLatencyMs :: Double -- baseline p50/p95/p99
+ , baselineStddev :: Double
+ , zScore :: Double
+ , degradationPercent :: Double -- percentage increase
+ , percentile :: Text -- "p50", "p95", "p99"
+ , sampleTraceIds :: V.Vector Text -- example traces for investigation
+ , detectedAt :: UTCTime
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson EndpointLatencyDegradationData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] EndpointLatencyDegradationData
+
+
+-- | Endpoint Error Rate Spike issue data
+data EndpointErrorRateSpikeData = EndpointErrorRateSpikeData
+ { endpointHash :: Text
+ , endpointMethod :: Text
+ , endpointPath :: Text
+ , serviceName :: Maybe Text
+ , currentErrorRate :: Double -- current error rate (0.0 - 1.0)
+ , baselineErrorRate :: Double -- baseline error rate
+ , baselineStddev :: Double
+ , zScore :: Double
+ , spikePercent :: Double -- percentage increase
+ , errorCount :: Int -- number of errors in window
+ , totalRequests :: Int -- total requests in window
+ , topErrorTypes :: V.Vector Text -- most common error types
+ , detectedAt :: UTCTime
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson EndpointErrorRateSpikeData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] EndpointErrorRateSpikeData
+
+
+-- | Endpoint Volume Rate Change issue data (traffic spike or drop)
+data EndpointVolumeRateChangeData = EndpointVolumeRateChangeData
+ { endpointHash :: Text
+ , endpointMethod :: Text
+ , endpointPath :: Text
+ , serviceName :: Maybe Text
+ , currentRatePerHour :: Double -- current requests/hour
+ , baselineRatePerHour :: Double -- baseline requests/hour
+ , baselineStddev :: Double
+ , zScore :: Double
+ , changePercent :: Double -- percentage change
+ , changeDirection :: Text -- "spike" or "drop"
+ , detectedAt :: UTCTime
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson EndpointVolumeRateChangeData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] EndpointVolumeRateChangeData
+
+
+-- | New Endpoint issue data
+data NewEndpointData = NewEndpointData
+ { endpointHash :: Text
+ , endpointMethod :: Text
+ , endpointPath :: Text
+ , endpointHost :: Text
+ , firstSeenAt :: UTCTime
+ , initialShapes :: V.Vector Text
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson NewEndpointData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] NewEndpointData
+
+
+-- | New Shape issue data
+data NewShapeData = NewShapeData
+ { shapeHash :: Text
+ , endpointHash :: Text
+ , endpointMethod :: Text
+ , endpointPath :: Text
+ , statusCode :: Int
+ , exampleRequestPayload :: AE.Value
+ , exampleResponsePayload :: AE.Value
+ , newFields :: V.Vector Text
+ , deletedFields :: V.Vector Text
+ , modifiedFields :: V.Vector Text
+ , fieldHashes :: V.Vector Text
+ , firstSeenAt :: UTCTime
+ , newShapesAfterIssue :: V.Vector Text
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson NewShapeData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] NewShapeData
+
+
+-- | Field Change issue data (for individual field changes)
+data FieldChangeData = FieldChangeData
+ { fieldHash :: Text
+ , endpointHash :: Text
+ , endpointMethod :: Text
+ , endpointPath :: Text
+ , keyPath :: Text
+ , fieldCategory :: Text
+ , previousType :: Maybe Text
+ , newType :: Text
+ , changeType :: Text
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (NFData)
+ deriving (FromField, ToField) via Aeson FieldChangeData
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] FieldChangeData
+
+
-- | Main Issue type
data Issue = Issue
{ id :: IssueId
@@ -197,30 +440,23 @@ data Issue = Issue
, updatedAt :: ZonedTime
, projectId :: Projects.ProjectId
, issueType :: IssueType
- , endpointHash :: Text -- For API changes, empty for others
- -- Status fields
+ , sourceType :: Text
+ , targetHash :: Text
+ , endpointHash :: Text
, acknowledgedAt :: Maybe ZonedTime
, acknowledgedBy :: Maybe Users.UserId
, archivedAt :: Maybe ZonedTime
- , -- Issue details
- title :: Text
- , service :: Text
+ , title :: Text
+ , service :: Maybe Text
+ , environment :: Maybe Text
, critical :: Bool
, severity :: Text -- "critical", "warning", "info"
- -- Impact metrics
- , affectedRequests :: Int
- , affectedClients :: Int
- , errorRate :: Maybe Double
- , -- Actions
- recommendedAction :: Text
+ , recommendedAction :: Text
, migrationComplexity :: Text -- "low", "medium", "high", "n/a"
- -- Data payload (polymorphic based on issueType)
, issueData :: Aeson AE.Value
- , -- Payload changes tracking (for API changes)
- requestPayloads :: Aeson [PayloadChange]
+ , requestPayloads :: Aeson [PayloadChange]
, responsePayloads :: Aeson [PayloadChange]
- , -- LLM enhancement tracking
- llmEnhancedAt :: Maybe UTCTime
+ , llmEnhancedAt :: Maybe UTCTime
, llmEnhancementVersion :: Maybe Int
}
deriving stock (Generic, Show)
@@ -235,18 +471,18 @@ instance Default Issue where
, createdAt = error "createdAt must be set"
, updatedAt = error "updatedAt must be set"
, projectId = def
- , issueType = def
+ , issueType = error "issueType must be set"
+ , sourceType = ""
+ , targetHash = ""
, endpointHash = ""
, acknowledgedAt = Nothing
, acknowledgedBy = Nothing
, archivedAt = Nothing
, title = ""
- , service = ""
+ , service = Nothing
+ , environment = Nothing
, critical = False
, severity = "info"
- , affectedRequests = 0
- , affectedClients = 0
- , errorRate = Nothing
, recommendedAction = ""
, migrationComplexity = "low"
, issueData = Aeson AE.Null
@@ -269,7 +505,7 @@ data IssueL = IssueL
, acknowledgedBy :: Maybe Users.UserId
, archivedAt :: Maybe ZonedTime
, title :: Text
- , service :: Text
+ , service :: Maybe Text
, critical :: Bool
, severity :: Text -- Computed in query
, affectedRequests :: Int -- Will be converted from affected_payloads in query
@@ -300,24 +536,18 @@ insertIssue issue = void $ PG.execute q issue
q =
[sql|
INSERT INTO apis.issues (
- id, created_at, updated_at, project_id, issue_type, endpoint_hash,
+ id, created_at, updated_at, project_id, issue_type, source_type, target_hash, endpoint_hash,
acknowledged_at, acknowledged_by, archived_at,
- title, service, critical, severity,
- affected_requests, affected_clients, error_rate,
+ title, service, environment, critical, severity,
recommended_action, migration_complexity,
issue_data, request_payloads, response_payloads,
llm_enhanced_at, llm_enhancement_version
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
-ON CONFLICT (project_id, endpoint_hash)
- WHERE issue_type = 'api_change'
- AND acknowledged_at IS NULL
- AND archived_at IS NULL
- AND endpoint_hash != ''
+ON CONFLICT (project_id, target_hash, issue_type)
+ WHERE acknowledged_at IS NULL AND archived_at IS NULL
DO UPDATE SET
updated_at = EXCLUDED.updated_at,
- affected_requests = issues.affected_requests + EXCLUDED.affected_requests,
- affected_clients = GREATEST(issues.affected_clients, EXCLUDED.affected_clients),
- issue_data = issues.issue_data || EXCLUDED.issue_data
+ issue_data = EXCLUDED.issue_data
|]
@@ -351,7 +581,7 @@ selectIssues pid _typeM isAcknowledged isArchived limit offset timeRangeM sortM
q =
[text|
SELECT id, created_at, updated_at, project_id, issue_type::text, endpoint_hash, acknowledged_at, acknowledged_by, archived_at, title, service, critical,
- CASE WHEN critical THEN 'critical' ELSE 'info' END, affected_requests, affected_clients, NULL::double precision,
+ CASE WHEN critical THEN 'critical' ELSE 'info' END, 0::int, 0::int, NULL::double precision,
recommended_action, migration_complexity, issue_data, request_payloads, response_payloads, NULL::timestamp with time zone, NULL::int, 0::bigint, updated_at
FROM apis.issues WHERE project_id = ? $timefilter $ackF $archF $orderBy LIMIT ? OFFSET ?
|]
@@ -360,7 +590,7 @@ selectIssues pid _typeM isAcknowledged isArchived limit offset timeRangeM sortM
-- | Find open issue for endpoint
findOpenIssueForEndpoint :: DB es => Projects.ProjectId -> Text -> Eff es (Maybe Issue)
-findOpenIssueForEndpoint pid endpointHash = listToMaybe <$> PG.query q (pid, "api_change" :: Text, endpointHash)
+findOpenIssueForEndpoint pid endpointHash = listToMaybe <$> PG.query q (pid, "new-endpoint" :: Text, endpointHash)
where
q =
[sql|
@@ -374,6 +604,22 @@ findOpenIssueForEndpoint pid endpointHash = listToMaybe <$> PG.query q (pid, "ap
|]
+findOpenShapeIssueForStatusCode :: DB es => Projects.ProjectId -> Text -> Int -> Eff es (Maybe Issue)
+findOpenShapeIssueForStatusCode pid endpointHash statusCode = listToMaybe <$> PG.query q (pid, "new-shape" :: Text, endpointHash, statusCode)
+ where
+ q =
+ [sql|
+ SELECT * FROM apis.issues
+ WHERE project_id = ?
+ AND issue_type = ?
+ AND endpoint_hash = ?
+ AND (issue_data->>'status_code')::int = ?
+ AND acknowledged_at IS NULL
+ AND archived_at IS NULL
+ LIMIT 1
+ |]
+
+
-- | Update issue with new anomaly data
updateIssueWithNewAnomaly :: DB es => IssueId -> APIChangeData -> Eff es ()
updateIssueWithNewAnomaly issueId newData = void $ PG.execute q (Aeson newData, issueId)
@@ -381,9 +627,8 @@ updateIssueWithNewAnomaly issueId newData = void $ PG.execute q (Aeson newData,
q =
[sql|
UPDATE apis.issues
- SET
+ SET
issue_data = issue_data || ?::jsonb,
- affected_requests = affected_requests + 1,
updated_at = NOW()
WHERE id = ?
|]
@@ -433,96 +678,49 @@ acknowledgeIssue issueId userId = void $ PG.execute q (userId, issueId)
|]
--- | Create API Change issue from anomalies
-createAPIChangeIssue :: Projects.ProjectId -> Text -> V.Vector Anomalies.AnomalyVM -> IO Issue
-createAPIChangeIssue projectId endpointHash anomalies = do
+-- | Derive source type from issue type
+issueTypeToSourceType :: IssueType -> Text
+issueTypeToSourceType NewEndpoint = "endpoint"
+issueTypeToSourceType NewShape = "shape"
+issueTypeToSourceType FieldChange = "shape"
+issueTypeToSourceType RuntimeException = "error"
+issueTypeToSourceType QueryAlert = "query"
+issueTypeToSourceType LogPattern = "log_pattern"
+issueTypeToSourceType ErrorEscalating = "error"
+issueTypeToSourceType ErrorRegressed = "error"
+issueTypeToSourceType LogPatternRateChange = "log_pattern"
+issueTypeToSourceType EndpointLatencyDegradation = "endpoint"
+issueTypeToSourceType EndpointErrorRateSpike = "endpoint"
+issueTypeToSourceType EndpointVolumeRateChange = "endpoint"
+
+
+-- | Helper to create an issue with common defaults
+mkIssue :: AE.ToJSON a => Projects.ProjectId -> IssueType -> Text -> Text -> Maybe Text -> Bool -> Text -> Text -> Text -> Text -> a -> IO Issue
+mkIssue projectId issueType targetHash endpointHash service critical severity title recommendedAction migrationComplexity issueData = do
issueId <- UUIDId <$> UUID4.nextRandom
now <- getCurrentTime
-
- let firstAnomaly = V.head anomalies
- apiChangeData =
- APIChangeData
- { endpointMethod = fromMaybe "UNKNOWN" firstAnomaly.endpointMethod
- , endpointPath = fromMaybe "/" firstAnomaly.endpointUrlPath
- , endpointHost = "Unknown"
- , anomalyHashes = V.map (.targetHash) anomalies
- , shapeChanges = V.empty -- Simplified for now
- , formatChanges = V.empty -- Simplified for now
- , newFields = V.concatMap (.shapeNewUniqueFields) anomalies
- , deletedFields = V.concatMap (.shapeDeletedFields) anomalies
- , modifiedFields = V.concatMap (.shapeUpdatedFieldFormats) anomalies
- }
-
- breakingChanges = V.length apiChangeData.deletedFields + V.length apiChangeData.modifiedFields
- isCritical = breakingChanges > 0
-
+ zonedNow <- utcToLocalZonedTime now
pure
Issue
{ id = issueId
- , createdAt = firstAnomaly.createdAt
- , updatedAt = firstAnomaly.updatedAt
+ , createdAt = zonedNow
+ , updatedAt = zonedNow
, projectId = projectId
- , issueType = APIChange
+ , issueType = issueType
+ , sourceType = issueTypeToSourceType issueType
+ , targetHash = targetHash
, endpointHash = endpointHash
, acknowledgedAt = Nothing
, acknowledgedBy = Nothing
, archivedAt = Nothing
- , title = "API structure has changed"
- , service = Anomalies.detectService Nothing firstAnomaly.endpointUrlPath
- , critical = isCritical
- , severity = if isCritical then "critical" else "warning"
- , affectedRequests = 0
- , affectedClients = 0
- , errorRate = Nothing
- , recommendedAction = "Review the API changes and update your integration accordingly."
- , migrationComplexity = if breakingChanges > 5 then "high" else if breakingChanges > 0 then "medium" else "low"
- , issueData = Aeson $ AE.toJSON apiChangeData
- , requestPayloads = Aeson [] -- Will be populated during enhancement
- , responsePayloads = Aeson [] -- Will be populated during enhancement
- , llmEnhancedAt = Nothing
- , llmEnhancementVersion = Nothing
- }
-
-
--- | Create Runtime Exception issue
-createRuntimeExceptionIssue :: Projects.ProjectId -> RequestDumps.ATError -> IO Issue
-createRuntimeExceptionIssue projectId atError = do
- issueId <- UUIDId <$> UUID4.nextRandom
- errorZonedTime <- utcToLocalZonedTime atError.when
-
- let exceptionData =
- RuntimeExceptionData
- { errorType = atError.errorType
- , errorMessage = atError.message
- , stackTrace = atError.stackTrace
- , requestPath = atError.requestPath
- , requestMethod = atError.requestMethod
- , occurrenceCount = 1
- , firstSeen = atError.when
- , lastSeen = atError.when
- }
-
- pure
- Issue
- { id = issueId
- , createdAt = errorZonedTime
- , updatedAt = errorZonedTime
- , projectId = projectId
- , issueType = RuntimeException
- , endpointHash = fromMaybe "" atError.hash
- , acknowledgedAt = Nothing
- , acknowledgedBy = Nothing
- , archivedAt = Nothing
- , title = atError.rootErrorType <> ": " <> T.take 100 atError.message
- , service = fromMaybe "unknown-service" atError.serviceName
- , critical = True
- , severity = "critical"
- , affectedRequests = 1
- , affectedClients = 0
- , errorRate = Nothing
- , recommendedAction = "Investigate the error and implement a fix."
- , migrationComplexity = "n/a"
- , issueData = Aeson $ AE.toJSON exceptionData
+ , title = title
+ , service = service
+ , environment = Nothing
+ , critical = critical
+ , severity = severity
+ , recommendedAction = recommendedAction
+ , migrationComplexity = migrationComplexity
+ , issueData = Aeson $ AE.toJSON issueData
, requestPayloads = Aeson []
, responsePayloads = Aeson []
, llmEnhancedAt = Nothing
@@ -533,10 +731,7 @@ createRuntimeExceptionIssue projectId atError = do
-- | Create Query Alert issue
createQueryAlertIssue :: Projects.ProjectId -> Text -> Text -> Text -> Double -> Double -> Text -> IO Issue
createQueryAlertIssue projectId queryId queryName queryExpr threshold actual thresholdType = do
- issueId <- UUIDId <$> UUID4.nextRandom
now <- getCurrentTime
- zonedNow <- utcToLocalZonedTime now
-
let alertData =
QueryAlertData
{ queryId = queryId
@@ -547,33 +742,441 @@ createQueryAlertIssue projectId queryId queryName queryExpr threshold actual thr
, thresholdType = thresholdType
, triggeredAt = now
}
+ mkIssue
+ projectId
+ QueryAlert
+ ""
+ ""
+ (Just "Monitoring")
+ True
+ "warning"
+ (queryName <> " threshold " <> thresholdType <> " " <> show threshold)
+ "Review the query results and take appropriate action."
+ "n/a"
+ alertData
- pure
- Issue
- { id = issueId
- , createdAt = zonedNow
- , updatedAt = zonedNow
- , projectId = projectId
- , issueType = QueryAlert
- , endpointHash = ""
- , acknowledgedAt = Nothing
- , acknowledgedBy = Nothing
- , archivedAt = Nothing
- , title = queryName <> " threshold " <> thresholdType <> " " <> show threshold
- , service = "Monitoring"
- , critical = True
- , severity = "warning"
- , affectedRequests = 0
- , affectedClients = 0
- , errorRate = Nothing
- , recommendedAction = "Review the query results and take appropriate action."
- , migrationComplexity = "n/a"
- , issueData = Aeson $ AE.toJSON alertData
- , requestPayloads = Aeson []
- , responsePayloads = Aeson []
- , llmEnhancedAt = Nothing
- , llmEnhancementVersion = Nothing
- }
+
+-- | Create issue for a new error
+createNewErrorIssue :: Projects.ProjectId -> Errors.Error -> IO Issue
+createNewErrorIssue projectId err = do
+ now <- getCurrentTime
+ let exceptionData =
+ RuntimeExceptionData
+ { errorType = err.errorType
+ , errorMessage = err.message
+ , stackTrace = err.stacktrace
+ , requestPath = Nothing
+ , requestMethod = Nothing
+ , occurrenceCount = 1
+ , firstSeen = now
+ , lastSeen = now
+ }
+ mkIssue
+ projectId
+ RuntimeException
+ err.hash
+ err.hash
+ err.service
+ True
+ "critical"
+ ("New Error: " <> err.errorType <> " - " <> T.take 80 err.message)
+ "Investigate the new error and implement a fix."
+ "n/a"
+ exceptionData
+
+
+-- | Create issue for an error spike
+createErrorSpikeIssue :: Projects.ProjectId -> Errors.Error -> Double -> Double -> Double -> IO Issue
+createErrorSpikeIssue projectId err currentRate baselineMean baselineStddev = do
+ now <- getCurrentTime
+ let zScore = if baselineStddev > 0 then (currentRate - baselineMean) / baselineStddev else 0
+ increasePercent = if baselineMean > 0 then ((currentRate / baselineMean) - 1) * 100 else 0
+ exceptionData =
+ RuntimeExceptionData
+ { errorType = err.errorType
+ , errorMessage = err.message
+ , stackTrace = err.stacktrace
+ , requestPath = Nothing
+ , requestMethod = Nothing
+ , occurrenceCount = round currentRate
+ , firstSeen = now
+ , lastSeen = now
+ }
+ mkIssue
+ projectId
+ RuntimeException
+ err.hash
+ err.hash
+ err.service
+ True
+ "critical"
+ ("Error Spike: " <> err.errorType <> " (" <> T.pack (show (round increasePercent :: Int)) <> "% increase)")
+ ("Error rate has spiked " <> T.pack (show (round zScore :: Int)) <> " standard deviations above baseline. Current: " <> T.pack (show (round currentRate :: Int)) <> "/hr, Baseline: " <> T.pack (show (round baselineMean :: Int)) <> "/hr. Investigate recent deployments or changes.")
+ "n/a"
+ exceptionData
+
+
+-- | Create an issue for an escalating error
+createErrorEscalatingIssue :: Projects.ProjectId -> Errors.Error -> Text -> Double -> Text -> IO Issue
+createErrorEscalatingIssue projectId err prevState escalationRate escalationWindow = do
+ now <- getCurrentTime
+ let escalatingData =
+ ErrorEscalatingData
+ { errorHash = err.hash
+ , errorType = err.errorType
+ , errorMessage = err.message
+ , serviceName = err.service
+ , currentState = "escalating"
+ , previousState = prevState
+ , occurrences1h = err.occurrences1h
+ , occurrences24h = err.occurrences24h
+ , escalationRate = escalationRate
+ , escalationWindow = escalationWindow
+ , firstSeenAt = now
+ , lastSeenAt = now
+ }
+ mkIssue
+ projectId
+ ErrorEscalating
+ err.hash
+ err.hash
+ err.service
+ True
+ "critical"
+ ("Error Escalating: " <> err.errorType <> " (" <> T.pack (show (round (escalationRate * 100) :: Int)) <> "% increase)")
+ ("Error rate is escalating (" <> T.pack (show escalationRate) <> "x over " <> escalationWindow <> "). Investigate immediately.")
+ "n/a"
+ escalatingData
+
+
+-- | Create an issue for a regressed error
+createErrorRegressedIssue :: Projects.ProjectId -> Errors.Error -> UTCTime -> Int -> Int -> IO Issue
+createErrorRegressedIssue projectId err resolvedAtTime quietMins prevOccurrences = do
+ now <- getCurrentTime
+ let regressedData =
+ ErrorRegressedData
+ { errorHash = err.hash
+ , errorType = err.errorType
+ , errorMessage = err.message
+ , serviceName = err.service
+ , resolvedAt = resolvedAtTime
+ , regressedAt = now
+ , quietPeriodMinutes = quietMins
+ , previousOccurrences = prevOccurrences
+ , newOccurrences = 1
+ }
+ mkIssue
+ projectId
+ ErrorRegressed
+ err.hash
+ err.hash
+ err.service
+ True
+ "critical"
+ ("Error Regressed: " <> err.errorType <> " (after " <> T.pack (show quietMins) <> " min quiet)")
+ ("Previously resolved error has returned after " <> T.pack (show quietMins) <> " minutes. The original fix may be incomplete.")
+ "n/a"
+ regressedData
+
+
+-- | Create an issue for a new log pattern
+createLogPatternIssue :: Projects.ProjectId -> LogPatterns.LogPattern -> IO Issue
+createLogPatternIssue projectId lp = do
+ now <- getCurrentTime
+ let logPatternData =
+ LogPatternData
+ { patternHash = lp.patternHash
+ , logPattern = lp.logPattern
+ , sampleMessage = lp.sampleMessage
+ , logLevel = lp.logLevel
+ , serviceName = lp.serviceName
+ , firstSeenAt = now
+ , occurrenceCount = fromIntegral lp.occurrenceCount
+ }
+ severity = case lp.logLevel of
+ Just "error" -> "critical"
+ Just "warning" -> "warning"
+ _ -> "info"
+ mkIssue
+ projectId
+ LogPattern
+ lp.patternHash
+ lp.patternHash
+ lp.serviceName
+ (lp.logLevel == Just "error")
+ severity
+ ("New Log Pattern: " <> T.take 100 lp.logPattern)
+ "A new log pattern has been detected. Review to ensure it's expected behavior."
+ "n/a"
+ logPatternData
+
+
+-- | Create an issue for a log pattern rate change
+createLogPatternRateChangeIssue :: Projects.ProjectId -> LogPatterns.LogPattern -> Double -> Double -> Double -> Text -> IO Issue
+createLogPatternRateChangeIssue projectId lp currentRate baselineMean baselineStddev direction = do
+ now <- getCurrentTime
+ let zScoreVal = if baselineStddev > 0 then abs (currentRate - baselineMean) / baselineStddev else 0
+ changePercentVal = if baselineMean > 0 then abs ((currentRate / baselineMean) - 1) * 100 else 0
+ rateChangeData =
+ LogPatternRateChangeData
+ { patternHash = lp.patternHash
+ , logPattern = lp.logPattern
+ , sampleMessage = lp.sampleMessage
+ , logLevel = lp.logLevel
+ , serviceName = lp.serviceName
+ , currentRatePerHour = currentRate
+ , baselineMean = baselineMean
+ , baselineStddev = baselineStddev
+ , zScore = zScoreVal
+ , changePercent = changePercentVal
+ , changeDirection = direction
+ , detectedAt = now
+ }
+ severity = case (direction, lp.logLevel) of
+ ("spike", Just "error") -> "critical"
+ ("spike", _) -> "warning"
+ ("drop", _) -> "info"
+ _ -> "info"
+ mkIssue
+ projectId
+ LogPatternRateChange
+ lp.patternHash
+ lp.patternHash
+ lp.serviceName
+ (direction == "spike" && lp.logLevel == Just "error")
+ severity
+ ("Log Pattern " <> T.toTitle direction <> ": " <> T.take 60 lp.logPattern <> " (" <> T.pack (show (round changePercentVal :: Int)) <> "%)")
+ ("Log pattern volume " <> direction <> " detected. Current: " <> T.pack (show (round currentRate :: Int)) <> "/hr, Baseline: " <> T.pack (show (round baselineMean :: Int)) <> "/hr (" <> T.pack (show (round zScoreVal :: Int)) <> " std devs).")
+ "n/a"
+ rateChangeData
+
+
+-- | Create an issue for endpoint latency degradation
+createEndpointLatencyDegradationIssue :: Projects.ProjectId -> Text -> Text -> Text -> Maybe Text -> Double -> Double -> Double -> Text -> V.Vector Text -> IO Issue
+createEndpointLatencyDegradationIssue projectId epHash method path serviceName currentLatency baselineLatency baselineStddev percentile traceIds = do
+ now <- getCurrentTime
+ let degradationPct = if baselineLatency > 0 then ((currentLatency / baselineLatency) - 1) * 100 else 0
+ zScoreVal = if baselineStddev > 0 then (currentLatency - baselineLatency) / baselineStddev else 0
+ latencyData =
+ EndpointLatencyDegradationData
+ { endpointHash = epHash
+ , endpointMethod = method
+ , endpointPath = path
+ , serviceName = serviceName
+ , currentLatencyMs = currentLatency
+ , baselineLatencyMs = baselineLatency
+ , baselineStddev = baselineStddev
+ , zScore = zScoreVal
+ , degradationPercent = degradationPct
+ , percentile = percentile
+ , sampleTraceIds = traceIds
+ , detectedAt = now
+ }
+ severity = if degradationPct > 100 then "critical" else if degradationPct > 50 then "warning" else "info"
+ mkIssue
+ projectId
+ EndpointLatencyDegradation
+ epHash
+ epHash
+ serviceName
+ (degradationPct > 100)
+ severity
+ ("Latency Degradation: " <> method <> " " <> path <> " (" <> percentile <> " +" <> T.pack (show (round degradationPct :: Int)) <> "%)")
+ ("Endpoint " <> percentile <> " latency increased from " <> T.pack (show (round baselineLatency :: Int)) <> "ms to " <> T.pack (show (round currentLatency :: Int)) <> "ms. Check recent deployments and dependencies.")
+ "n/a"
+ latencyData
+
+
+-- | Create an issue for endpoint error rate spike
+createEndpointErrorRateSpikeIssue :: Projects.ProjectId -> Text -> Text -> Text -> Maybe Text -> Double -> Double -> Double -> Int -> Int -> V.Vector Text -> IO Issue
+createEndpointErrorRateSpikeIssue projectId epHash method path serviceName currentRate baselineRate baselineStddev errorCount totalReqs topErrors = do
+ now <- getCurrentTime
+ let zScoreVal = if baselineStddev > 0 then (currentRate - baselineRate) / baselineStddev else 0
+ spikePct = if baselineRate > 0 then ((currentRate / baselineRate) - 1) * 100 else currentRate * 100
+ errorRateData =
+ EndpointErrorRateSpikeData
+ { endpointHash = epHash
+ , endpointMethod = method
+ , endpointPath = path
+ , serviceName = serviceName
+ , currentErrorRate = currentRate
+ , baselineErrorRate = baselineRate
+ , baselineStddev = baselineStddev
+ , zScore = zScoreVal
+ , spikePercent = spikePct
+ , errorCount = errorCount
+ , totalRequests = totalReqs
+ , topErrorTypes = topErrors
+ , detectedAt = now
+ }
+ severity = if currentRate > 0.1 then "critical" else if currentRate > 0.05 then "warning" else "info"
+ mkIssue
+ projectId
+ EndpointErrorRateSpike
+ epHash
+ epHash
+ serviceName
+ (currentRate > 0.1)
+ severity
+ ("Error Rate Spike: " <> method <> " " <> path <> " (" <> T.pack (show (round (currentRate * 100) :: Int)) <> "% errors)")
+ ("Error rate spiked from " <> T.pack (show (round (baselineRate * 100) :: Int)) <> "% to " <> T.pack (show (round (currentRate * 100) :: Int)) <> "% (" <> T.pack (show errorCount) <> "/" <> T.pack (show totalReqs) <> " requests failed).")
+ "n/a"
+ errorRateData
+
+
+-- | Create an issue for endpoint volume rate change
+createEndpointVolumeRateChangeIssue :: Projects.ProjectId -> Text -> Text -> Text -> Maybe Text -> Double -> Double -> Double -> Text -> IO Issue
+createEndpointVolumeRateChangeIssue projectId epHash method path serviceName currentRate baselineRate baselineStddev direction = do
+ now <- getCurrentTime
+ let zScoreVal = if baselineStddev > 0 then abs (currentRate - baselineRate) / baselineStddev else 0
+ changePct = if baselineRate > 0 then abs ((currentRate / baselineRate) - 1) * 100 else 0
+ volumeData =
+ EndpointVolumeRateChangeData
+ { endpointHash = epHash
+ , endpointMethod = method
+ , endpointPath = path
+ , serviceName = serviceName
+ , currentRatePerHour = currentRate
+ , baselineRatePerHour = baselineRate
+ , baselineStddev = baselineStddev
+ , zScore = zScoreVal
+ , changePercent = changePct
+ , changeDirection = direction
+ , detectedAt = now
+ }
+ severity = case (direction, changePct) of
+ ("drop", pct) | pct > 80 -> "critical"
+ ("drop", pct) | pct > 50 -> "warning"
+ ("spike", pct) | pct > 200 -> "warning"
+ _ -> "info"
+ mkIssue
+ projectId
+ EndpointVolumeRateChange
+ epHash
+ epHash
+ serviceName
+ (direction == "drop" && changePct > 80)
+ severity
+ ("Traffic " <> T.toTitle direction <> ": " <> method <> " " <> path <> " (" <> T.pack (show (round changePct :: Int)) <> "%)")
+ ("Endpoint traffic " <> direction <> " detected. Current: " <> T.pack (show (round currentRate :: Int)) <> " req/hr, Baseline: " <> T.pack (show (round baselineRate :: Int)) <> " req/hr.")
+ "n/a"
+ volumeData
+
+
+-- | Create an issue for a new endpoint
+createNewEndpointIssue :: Projects.ProjectId -> Text -> Text -> Text -> Text -> IO Issue
+createNewEndpointIssue projectId epHash method path host = do
+ now <- getCurrentTime
+ let endpointData =
+ NewEndpointData
+ { endpointHash = epHash
+ , endpointMethod = method
+ , endpointPath = path
+ , endpointHost = host
+ , firstSeenAt = now
+ , initialShapes = V.empty
+ }
+ mkIssue
+ projectId
+ NewEndpoint
+ epHash
+ epHash
+ (Just host)
+ False
+ "info"
+ "New Endpoint detected"
+ "A new API endpoint has been detected. Review to ensure it matches your API specification."
+ "n/a"
+ endpointData
+
+
+-- | Create an issue for a new shape
+createNewShapeIssue :: Projects.ProjectId -> Text -> Text -> Text -> Text -> Int -> AE.Value -> AE.Value -> V.Vector Text -> V.Vector Text -> V.Vector Text -> V.Vector Text -> IO Issue
+createNewShapeIssue projectId shHash epHash method path statusCode reqPayload respPayload newFlds deletedFlds modifiedFlds fldHashes = do
+ now <- getCurrentTime
+ let shapeData =
+ NewShapeData
+ { shapeHash = shHash
+ , endpointHash = epHash
+ , endpointMethod = method
+ , endpointPath = path
+ , statusCode = statusCode
+ , exampleRequestPayload = reqPayload
+ , exampleResponsePayload = respPayload
+ , newFields = newFlds
+ , deletedFields = deletedFlds
+ , modifiedFields = modifiedFlds
+ , fieldHashes = fldHashes
+ , firstSeenAt = now
+ , newShapesAfterIssue = V.empty
+ }
+ hasBreakingChanges = not (V.null deletedFlds) || not (V.null modifiedFlds)
+ changeCount = V.length newFlds + V.length deletedFlds + V.length modifiedFlds
+ severity = if hasBreakingChanges then "critical" else "warning"
+ complexity = if V.length deletedFlds > 5 then "high" else if hasBreakingChanges then "medium" else "low"
+ action =
+ if hasBreakingChanges
+ then "Breaking API changes detected: " <> T.pack (show (V.length deletedFlds)) <> " deleted, " <> T.pack (show (V.length modifiedFlds)) <> " modified fields. Update clients immediately."
+ else "New API shape detected with " <> T.pack (show (V.length newFlds)) <> " new fields. Review for compatibility."
+ mkIssue
+ projectId
+ NewShape
+ shHash
+ epHash
+ Nothing
+ hasBreakingChanges
+ severity
+ ("New Shape: " <> method <> " " <> path <> " (" <> T.pack (show statusCode) <> ") - " <> T.pack (show changeCount) <> " field changes")
+ action
+ complexity
+ shapeData
+
+
+-- | Create an issue for a field change
+createFieldChangeIssue :: Projects.ProjectId -> Text -> Text -> Text -> Text -> Text -> Text -> Maybe Text -> Text -> Text -> IO Issue
+createFieldChangeIssue projectId fldHash epHash method path keyPath category prevType newType changeType = do
+ let fieldData =
+ FieldChangeData
+ { fieldHash = fldHash
+ , endpointHash = epHash
+ , endpointMethod = method
+ , endpointPath = path
+ , keyPath = keyPath
+ , fieldCategory = category
+ , previousType = prevType
+ , newType = newType
+ , changeType = changeType
+ }
+ isBreaking = changeType `elem` ["removed", "type_changed"]
+ titlePrefix = case changeType of
+ "added" -> "New Field"
+ "removed" -> "Removed Field"
+ "type_changed" -> "Field Type Changed"
+ "format_changed" -> "Field Format Changed"
+ _ -> "Field Changed"
+ mkIssue
+ projectId
+ FieldChange
+ fldHash
+ epHash
+ Nothing
+ isBreaking
+ (if isBreaking then "critical" else "info")
+ (titlePrefix <> ": " <> keyPath <> " in " <> method <> " " <> path)
+ ""
+ (if isBreaking then "medium" else "low")
+ fieldData
+
+
+updateIssueData :: DB es => IssueId -> AE.Value -> Eff es ()
+updateIssueData issueId newData = void $ PG.execute q (Aeson newData, issueId)
+ where
+ q =
+ [sql|
+ UPDATE apis.issues
+ SET issue_data = ?
+ WHERE id = ?
+ |]
-- | Conversation type for AI chats
diff --git a/src/Models/Apis/LogPatterns.hs b/src/Models/Apis/LogPatterns.hs
new file mode 100644
index 000000000..cee0d8ff6
--- /dev/null
+++ b/src/Models/Apis/LogPatterns.hs
@@ -0,0 +1,339 @@
+module Models.Apis.LogPatterns (
+ LogPattern (..),
+ LogPatternId,
+ LogPatternState (..),
+ getLogPatterns,
+ getLogPatternTexts,
+ getLogPatternByHash,
+ acknowledgeLogPatterns,
+ upsertLogPattern,
+ updateLogPatternStats,
+ updateBaseline,
+ -- Pattern stats from otel_logs_and_spans
+ PatternStats (..),
+ getPatternStats,
+ getCurrentHourPatternCount,
+ -- Pattern with current rate for spike detection
+ LogPatternWithRate (..),
+ getPatternsWithCurrentRates,
+ getLogPatternById,
+)
+where
+
+import Data.Aeson qualified as AE
+import Data.Text qualified as T
+import Data.Time
+import Data.UUID qualified as UUID
+import Data.Vector qualified as V
+import Database.PostgreSQL.Entity.Types (CamelToSnake, Entity, FieldModifiers, GenericEntity, PrimaryKey, Schema, TableName)
+import Database.PostgreSQL.Simple (FromRow, Only (Only), ToRow)
+import Database.PostgreSQL.Simple.FromField (FromField, ResultError (ConversionFailed, UnexpectedNull), fromField, returnError)
+import Database.PostgreSQL.Simple.SqlQQ (sql)
+import Database.PostgreSQL.Simple.ToField (Action (Escape), ToField, toField)
+import Deriving.Aeson qualified as DAE
+import Effectful (Eff)
+import Effectful.PostgreSQL qualified as PG
+import Models.Projects.Projects qualified as Projects
+import Models.Users.Users qualified as Users
+import Pkg.DeriveUtils (BaselineState (..))
+import Relude hiding (id)
+import System.Types (DB)
+
+
+newtype LogPatternId = LogPatternId {unLogPatternId :: Int64}
+ deriving stock (Generic, Show)
+ deriving newtype (AE.FromJSON, AE.ToJSON, Eq, FromField, NFData, Ord, ToField)
+
+
+data LogPatternState
+ = LPSNew
+ | LPSAcknowledged
+ | LPSIgnored
+ deriving stock (Eq, Generic, Show)
+ deriving anyclass (NFData)
+ deriving
+ (AE.FromJSON, AE.ToJSON)
+ via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] LogPatternState
+
+
+logPatternStateToText :: LogPatternState -> Text
+logPatternStateToText LPSNew = "new"
+logPatternStateToText LPSAcknowledged = "acknowledged"
+logPatternStateToText LPSIgnored = "ignored"
+
+
+parseLogPatternState :: (Eq s, IsString s) => s -> Maybe LogPatternState
+parseLogPatternState "new" = Just LPSNew
+parseLogPatternState "acknowledged" = Just LPSAcknowledged
+parseLogPatternState "ignored" = Just LPSIgnored
+parseLogPatternState _ = Nothing
+
+
+instance ToField LogPatternState where
+ toField = Escape . encodeUtf8 <$> logPatternStateToText
+
+
+instance FromField LogPatternState where
+ fromField f mdata =
+ case mdata of
+ Nothing -> returnError UnexpectedNull f ""
+ Just bs ->
+ case parseLogPatternState bs of
+ Just s -> pure s
+ Nothing -> returnError ConversionFailed f $ "Conversion error: Expected log pattern state, got " <> decodeUtf8 bs <> " instead."
+
+
+data LogPattern = LogPattern
+ { id :: LogPatternId
+ , projectId :: Projects.ProjectId
+ , createdAt :: ZonedTime
+ , updatedAt :: ZonedTime
+ , logPattern :: Text
+ , patternHash :: Text
+ , serviceName :: Maybe Text
+ , logLevel :: Maybe Text
+ , sampleMessage :: Maybe Text
+ , firstSeenAt :: ZonedTime
+ , lastSeenAt :: ZonedTime
+ , occurrenceCount :: Int64
+ , state :: LogPatternState
+ , acknowledgedBy :: Maybe Users.UserId
+ , acknowledgedAt :: Maybe ZonedTime
+ , baselineState :: BaselineState
+ , baselineVolumeHourlyMean :: Maybe Double
+ , baselineVolumeHourlyStddev :: Maybe Double
+ , baselineSamples :: Int
+ , baselineUpdatedAt :: Maybe ZonedTime
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow, NFData, ToRow)
+ deriving
+ (Entity)
+ via (GenericEntity '[Schema "apis", TableName "log_patterns", PrimaryKey "id", FieldModifiers '[CamelToSnake]] LogPattern)
+ deriving
+ (AE.FromJSON, AE.ToJSON)
+ via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] LogPattern
+
+
+-- | Get all log patterns for a project
+getLogPatterns :: DB es => Projects.ProjectId -> Maybe LogPatternState -> Int -> Int -> Eff es [LogPattern]
+getLogPatterns pid mstate limit offset = PG.query q (pid, maybe "%" logPatternStateToText mstate, limit, offset)
+ where
+ q =
+ [sql|
+ SELECT id, project_id, created_at, updated_at, log_pattern, pattern_hash,
+ service_name, log_level, sample_message, first_seen_at, last_seen_at,
+ occurrence_count, state, acknowledged_by, acknowledged_at,
+ baseline_state, baseline_volume_hourly_mean, baseline_volume_hourly_stddev,
+ baseline_samples, baseline_updated_at
+ FROM apis.log_patterns
+ WHERE project_id = ? AND state LIKE ?
+ ORDER BY last_seen_at DESC
+ LIMIT ? OFFSET ?
+ |]
+
+
+getLogPatternTexts :: DB es => Projects.ProjectId -> Eff es [Text]
+getLogPatternTexts pid = do
+ result <- PG.query q (Only pid)
+ return $ fmap (\(Only pat) -> pat) result
+ where
+ q = [sql| SELECT log_pattern FROM apis.log_patterns WHERE project_id = ?|]
+
+
+-- | Get log pattern by hash
+getLogPatternByHash :: DB es => Projects.ProjectId -> Text -> Eff es (Maybe LogPattern)
+getLogPatternByHash pid hash = do
+ results <- PG.query q (pid, hash)
+ return $ listToMaybe results
+ where
+ q =
+ [sql|
+ SELECT id, project_id, created_at, updated_at, log_pattern, pattern_hash,
+ service_name, log_level, sample_message, first_seen_at, last_seen_at,
+ occurrence_count, state, acknowledged_by, acknowledged_at,
+ baseline_state, baseline_volume_hourly_mean, baseline_volume_hourly_stddev,
+ baseline_samples, baseline_updated_at
+ FROM apis.log_patterns
+ WHERE project_id = ? AND pattern_hash = ?
+ |]
+
+
+-- | Acknowledge log patterns
+acknowledgeLogPatterns :: DB es => Users.UserId -> V.Vector Text -> Eff es Int64
+acknowledgeLogPatterns uid patternHashes
+ | V.null patternHashes = pure 0
+ | otherwise = PG.execute q (uid, patternHashes)
+ where
+ q =
+ [sql|
+ UPDATE apis.log_patterns
+ SET state = 'acknowledged', acknowledged_by = ?, acknowledged_at = NOW()
+ WHERE pattern_hash = ANY(?)
+ |]
+
+
+upsertLogPattern :: DB es => Projects.ProjectId -> Text -> Text -> Maybe Text -> Maybe Text -> Maybe Text -> Maybe Text -> Eff es Int64
+upsertLogPattern pid pat patHash serviceName logLevel trId sampleMsg =
+ PG.execute q (pid, pat, patHash, serviceName, logLevel, trId, sampleMsg)
+ where
+ q =
+ [sql|
+ INSERT INTO apis.log_patterns (project_id, log_pattern, pattern_hash, service_name, log_level, trace_id, sample_message)
+ VALUES (?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT (project_id, pattern_hash) DO UPDATE SET
+ last_seen_at = NOW(),
+ occurrence_count = apis.log_patterns.occurrence_count + 1,
+ sample_message = COALESCE(EXCLUDED.sample_message, apis.log_patterns.sample_message),
+ service_name = COALESCE(EXCLUDED.service_name, apis.log_patterns.service_name),
+ trace_id = COALESCE(EXCLUDED.trace_id, apis.log_patterns.trace_id)
+ |]
+
+
+-- | Update log pattern statistics (occurrence count, last seen)
+updateLogPatternStats :: DB es => Projects.ProjectId -> Text -> Int64 -> Eff es Int64
+updateLogPatternStats pid patHash additionalCount =
+ PG.execute q (additionalCount, pid, patHash)
+ where
+ q =
+ [sql|
+ UPDATE apis.log_patterns
+ SET occurrence_count = occurrence_count + ?,
+ last_seen_at = NOW()
+ WHERE project_id = ? AND pattern_hash = ?
+ |]
+
+
+updateBaseline :: DB es => Projects.ProjectId -> Text -> BaselineState -> Double -> Double -> Int -> Eff es Int64
+updateBaseline pid patHash bState hourlyMean hourlyStddev samples =
+ PG.execute q (bState, hourlyMean, hourlyStddev, samples, pid, patHash)
+ where
+ q =
+ [sql|
+ UPDATE apis.log_patterns
+ SET baseline_state = ?,
+ baseline_volume_hourly_mean = ?,
+ baseline_volume_hourly_stddev = ?,
+ baseline_samples = ?,
+ baseline_updated_at = NOW()
+ WHERE project_id = ? AND pattern_hash = ?
+ |]
+
+
+-- | Stats for a log pattern from otel_logs_and_spans
+data PatternStats = PatternStats
+ { hourlyMean :: Double
+ , hourlyStddev :: Double
+ , totalHours :: Int
+ , totalEvents :: Int
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow)
+
+
+-- | Get pattern stats from otel_logs_and_spans
+getPatternStats :: DB es => Projects.ProjectId -> Text -> Int -> Eff es (Maybe PatternStats)
+getPatternStats pid pattern' hoursBack = do
+ results <- PG.query q (pid, pattern', hoursBack)
+ return $ listToMaybe results
+ where
+ q =
+ [sql|
+ WITH hourly_counts AS (
+ SELECT
+ date_trunc('hour', timestamp) AS hour_start,
+ COUNT(*) AS event_count
+ FROM otel_logs_and_spans
+ WHERE project_id = ?::text
+ AND log_pattern = ?
+ AND timestamp >= NOW() - INTERVAL '1 hour' * ?
+ GROUP BY date_trunc('hour', timestamp)
+ )
+ SELECT
+ COALESCE(AVG(event_count), 0)::FLOAT AS hourly_mean,
+ COALESCE(STDDEV(event_count), 0)::FLOAT AS hourly_stddev,
+ COUNT(*)::INT AS total_hours,
+ COALESCE(SUM(event_count), 0)::INT AS total_events
+ FROM hourly_counts
+ |]
+
+
+-- | Get current hour count for a pattern
+getCurrentHourPatternCount :: DB es => Projects.ProjectId -> Text -> Eff es Int
+getCurrentHourPatternCount pid pattern' = do
+ results <- PG.query q (pid, pattern')
+ case results of
+ [Only count] -> return count
+ _ -> return 0
+ where
+ q =
+ [sql|
+ SELECT COUNT(*)::INT
+ FROM otel_logs_and_spans
+ WHERE project_id = ?::text
+ AND log_pattern = ?
+ AND timestamp >= date_trunc('hour', NOW())
+ |]
+
+
+-- | Log pattern with current rate (for batch spike detection)
+data LogPatternWithRate = LogPatternWithRate
+ { patternId :: LogPatternId
+ , projectId :: Projects.ProjectId
+ , logPattern :: Text
+ , patternHash :: Text
+ , baselineState :: BaselineState
+ , baselineMean :: Maybe Double
+ , baselineStddev :: Maybe Double
+ , currentHourCount :: Int
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow)
+
+
+-- | Get all patterns with their current hour counts
+getPatternsWithCurrentRates :: DB es => Projects.ProjectId -> Eff es [LogPatternWithRate]
+getPatternsWithCurrentRates pid =
+ PG.query q (pid, pid)
+ where
+ q =
+ [sql|
+ SELECT
+ lp.id,
+ lp.project_id,
+ lp.log_pattern,
+ lp.pattern_hash,
+ lp.baseline_state,
+ lp.baseline_volume_hourly_mean,
+ lp.baseline_volume_hourly_stddev,
+ COALESCE(counts.current_count, 0)::INT AS current_hour_count
+ FROM apis.log_patterns lp
+ LEFT JOIN (
+ SELECT log_pattern, COUNT(*) AS current_count
+ FROM otel_logs_and_spans
+ WHERE project_id = ?::text
+ AND timestamp >= date_trunc('hour', NOW())
+ AND log_pattern IS NOT NULL
+ GROUP BY log_pattern
+ ) counts ON counts.log_pattern = lp.log_pattern
+ WHERE lp.project_id = ?
+ AND lp.state != 'ignored'
+ |]
+
+
+-- | Get a pattern by ID
+getLogPatternById :: DB es => LogPatternId -> Eff es (Maybe LogPattern)
+getLogPatternById lpid = do
+ results <- PG.query q (Only lpid)
+ return $ listToMaybe results
+ where
+ q =
+ [sql|
+ SELECT id, project_id, created_at, updated_at, log_pattern, pattern_hash,
+ service_name, log_level, sample_message, first_seen_at, last_seen_at,
+ occurrence_count, state, acknowledged_by, acknowledged_at,
+ baseline_state, baseline_volume_hourly_mean, baseline_volume_hourly_stddev,
+ baseline_samples, baseline_updated_at
+ FROM apis.log_patterns
+ WHERE id = ?
+ |]
diff --git a/src/Models/Apis/Shapes.hs b/src/Models/Apis/Shapes.hs
index 2f553afb6..8a3c3b4f7 100644
--- a/src/Models/Apis/Shapes.hs
+++ b/src/Models/Apis/Shapes.hs
@@ -3,7 +3,10 @@ module Models.Apis.Shapes (
ShapeWithFields (..),
SwShape (..),
ShapeId,
+ ShapeForIssue (..),
bulkInsertShapes,
+ getShapeForIssue,
+ getShapeByHash,
)
where
@@ -12,7 +15,7 @@ import Data.Default (Default)
import Data.Time (UTCTime)
import Data.Vector qualified as V
import Database.PostgreSQL.Entity.Types (CamelToSnake, Entity, FieldModifiers, GenericEntity, PrimaryKey, Schema, TableName)
-import Database.PostgreSQL.Simple (FromRow, ToRow)
+import Database.PostgreSQL.Simple (FromRow, Only (..), ToRow)
import Database.PostgreSQL.Simple.FromField (FromField)
import Database.PostgreSQL.Simple.Newtypes (Aeson (..))
import Database.PostgreSQL.Simple.SqlQQ (sql)
@@ -60,6 +63,11 @@ data Shape = Shape
, statusCode :: Int
, responseDescription :: Text
, requestDescription :: Text
+ , exampleRequestPayload :: AE.Value
+ , exampleResponsePayload :: AE.Value
+ , firstTraceId :: Maybe Text
+ , recentTraceId :: Maybe Text
+ , service :: Maybe Text
}
deriving stock (Generic, Show)
deriving anyclass (Default, FromRow, NFData, ToRow)
@@ -72,10 +80,10 @@ bulkInsertShapes :: DB es => V.Vector Shape -> Eff es ()
bulkInsertShapes shapes = void $ PG.executeMany q $ V.toList rowsToInsert
where
q =
- [sql|
+ [sql|
INSERT INTO apis.shapes
- (project_id, endpoint_hash, query_params_keypaths, request_body_keypaths, response_body_keypaths, request_headers_keypaths, response_headers_keypaths, field_hashes, hash, status_code, request_description, response_description)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING|]
+ (project_id, endpoint_hash, query_params_keypaths, request_body_keypaths, response_body_keypaths, request_headers_keypaths, response_headers_keypaths, field_hashes, hash, status_code, request_description, response_description, example_request_payload, example_response_payload, first_trace_id, recent_trace_id, service)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING|]
rowsToInsert =
V.map
( \shape ->
@@ -89,8 +97,13 @@ bulkInsertShapes shapes = void $ PG.executeMany q $ V.toList rowsToInsert
, shape.fieldHashes
, shape.hash
, fromIntegral shape.statusCode
- , ""
- , ""
+ , shape.requestDescription
+ , shape.responseDescription
+ , shape.exampleRequestPayload
+ , shape.exampleResponsePayload
+ , shape.firstTraceId
+ , shape.recentTraceId
+ , shape.service
)
)
shapes
@@ -114,3 +127,59 @@ data SwShape = SwShape
deriving anyclass (AE.ToJSON)
deriving (FromField) via Aeson SwShape
deriving (AE.FromJSON) via DAE.CustomJSON '[DAE.OmitNothingFields, DAE.FieldLabelModifier '[DAE.CamelToSnake]] SwShape
+
+
+-- | Shape data needed for creating issues
+data ShapeForIssue = ShapeForIssue
+ { shapeHash :: Text
+ , endpointHash :: Text
+ , method :: Text
+ , path :: Text
+ , statusCode :: Int
+ , exampleRequestPayload :: AE.Value
+ , exampleResponsePayload :: AE.Value
+ , newFields :: V.Vector Text
+ , deletedFields :: V.Vector Text
+ , modifiedFields :: V.Vector Text
+ , fieldHashes :: V.Vector Text
+ }
+ deriving stock (Generic, Show)
+ deriving anyclass (FromRow)
+
+
+getShapeForIssue :: DB es => Projects.ProjectId -> Text -> Eff es (Maybe ShapeForIssue)
+getShapeForIssue pid hash = listToMaybe <$> PG.query q (pid, hash)
+ where
+ q =
+ [sql|
+ SELECT
+ s.hash,
+ s.endpoint_hash,
+ COALESCE(e.method, 'UNKNOWN'),
+ COALESCE(e.url_path, '/'),
+ s.status_code,
+ COALESCE(s.example_request_payload, '{}'::jsonb),
+ COALESCE(s.example_response_payload, '{}'::jsonb),
+ COALESCE(s.new_unique_fields, '{}'::TEXT[]),
+ COALESCE(s.deleted_fields, '{}'::TEXT[]),
+ COALESCE(s.updated_field_formats, '{}'::TEXT[]),
+ COALESCE(s.field_hashes, '{}'::TEXT[])
+ FROM apis.shapes s
+ LEFT JOIN apis.endpoints e ON e.hash = s.endpoint_hash
+ WHERE s.project_id = ? AND s.hash = ?
+ |]
+
+
+getShapeByHash :: DB es => Projects.ProjectId -> Text -> Eff es (Maybe Shape)
+getShapeByHash pid hash = listToMaybe <$> PG.query q (pid, hash)
+ where
+ q =
+ [sql|
+ SELECT id, created_at, updated_at, approved_on, project_id, endpoint_hash,
+ query_params_keypaths, request_body_keypaths, response_body_keypaths,
+ request_headers_keypaths, response_headers_keypaths, field_hashes,
+ hash, status_code, response_description, request_description,
+ example_request_payload, example_response_payload, first_trace_id, recent_trace_id, service
+ FROM apis.shapes
+ WHERE project_id = ? AND hash = ?
+ |]
diff --git a/src/Models/Telemetry/Telemetry.hs b/src/Models/Telemetry/Telemetry.hs
index 6073df0dd..da7556aaf 100644
--- a/src/Models/Telemetry/Telemetry.hs
+++ b/src/Models/Telemetry/Telemetry.hs
@@ -84,6 +84,7 @@ import Effectful.Log (Log)
import Effectful.PostgreSQL (WithConnection)
import Effectful.PostgreSQL qualified as PG
import Effectful.Reader.Static qualified as Eff
+import Models.Apis.Errors qualified as Errors
import Models.Apis.RequestDumps qualified as RequestDumps
import Models.Projects.Projects qualified as Projects
import NeatInterpolation (text)
@@ -97,7 +98,7 @@ import System.Logging qualified as Log
import System.Types (DB)
import Text.Regex.TDFA.Text ()
import UnliftIO (throwIO, tryAny)
-import Utils (lookupValueText, toXXHash)
+import Utils (lookupValueText)
-- Helper function to get nested value from a map using dot notation
@@ -1012,7 +1013,7 @@ getErrorEvents _ = []
-- | Extract all runtime errors from a collection of spans
-- This is the main entry point for error anomaly detection
-getAllATErrors :: V.Vector OtelLogsAndSpans -> V.Vector RequestDumps.ATError
+getAllATErrors :: V.Vector OtelLogsAndSpans -> V.Vector Errors.ATError
getAllATErrors = V.concatMap extractErrorsFromSpan
where
extractErrorsFromSpan spanObj =
@@ -1026,7 +1027,7 @@ getAllATErrors = V.concatMap extractErrorsFromSpan
-- - exception.message: The exception message
-- - exception.stacktrace: The stacktrace
-- Also extracts HTTP context (method, path) for better error tracking
-extractATError :: OtelLogsAndSpans -> AE.Value -> Maybe RequestDumps.ATError
+extractATError :: OtelLogsAndSpans -> AE.Value -> Maybe Errors.ATError
extractATError spanObj (AE.Object o) = do
AE.Object attrs' <- KEM.lookup "event_attributes" o
AE.Object attrs <- KEM.lookup "exception" attrs'
@@ -1042,17 +1043,24 @@ extractATError spanObj (AE.Object o) = do
Just (AE.String s) -> Just s
_ -> Nothing
getTextOrEmpty k = fromMaybe "" (lookupText k)
+ getUserAttrM k v = case unAesonTextMaybe spanObj.resource >>= Map.lookup v of
+ Just (AE.Object userAttrs) -> KEM.lookup k userAttrs >>= asText
+ _ -> Nothing
typ = getTextOrEmpty "type"
msg = getTextOrEmpty "message"
stack = getTextOrEmpty "stacktrace"
+ userId = getUserAttrM "id" "user"
+ userEmail = getUserAttrM "email" "user"
+ sessionId = getUserAttrM "id" "session"
+
-- TODO: parse telemetry.sdk.name to SDKTypes
tech = case unAesonTextMaybe spanObj.resource >>= Map.lookup "telemetry" of
Just (AE.Object tel) ->
KEM.lookup "sdk" tel
>>= ( \case
- AE.Object sdkObj -> KEM.lookup "name" sdkObj >>= asText
+ AE.Object sdkObj -> KEM.lookup "language" sdkObj >>= asText
_ -> Nothing
)
_ -> Nothing
@@ -1066,28 +1074,37 @@ extractATError spanObj (AE.Object o) = do
asText (AE.String t) = Just t
asText _ = Nothing
+ pid = UUID.fromText spanObj.project_id >>= (Just . UUIDId)
-- Build ATError structure for anomaly detection
-- The hash is critical for grouping similar errors together
-- Hash components: projectId + service + span name + error type + sanitized message/stack
-- This ensures similar errors are grouped while allowing variations in the actual message
+ -- projectId mService mEndpoint runtime exceptionType message stackTrace
+
return
- $ RequestDumps.ATError
- { projectId = UUID.fromText spanObj.project_id >>= (Just . UUIDId)
+ $ Errors.ATError
+ { projectId = pid
, when = spanObj.timestamp
, errorType = typ
, rootErrorType = typ
, message = msg
, rootErrorMessage = msg
, stackTrace = stack
- , hash = Just (toXXHash (spanObj.project_id <> fromMaybe "" serviceName <> fromMaybe "" spanObj.name <> typ <> replaceAllFormats (msg <> stack)))
+ , hash = Errors.computeErrorFingerprint spanObj.project_id serviceName spanObj.name (fromMaybe "unknown" tech) typ msg stack
, technology = Nothing
, serviceName = serviceName
, requestMethod = method
, requestPath = urlPath
, spanId = spanId
, traceId = trId
- , stack = tech
+ , runtime = tech
+ , parentSpanId = spanObj.parent_id
+ , endpointHash = Nothing
+ , environment = Nothing
+ , userId = userId
+ , userEmail = userEmail
+ , sessionId = sessionId
}
extractATError _ _ = Nothing
diff --git a/src/Pages/Anomalies.hs b/src/Pages/Anomalies.hs
index b6e1eb10e..a2720a1a2 100644
--- a/src/Pages/Anomalies.hs
+++ b/src/Pages/Anomalies.hs
@@ -22,6 +22,10 @@ module Pages.Anomalies (
AIChatForm (..),
aiChatPostH,
aiChatHistoryGetH,
+ -- Error actions
+ ErrorAssignForm (..),
+ errorActionPostH,
+ errorAssignPostH,
)
where
@@ -37,6 +41,7 @@ import Data.Vector qualified as V
import Database.PostgreSQL.Simple (Only (Only))
import Database.PostgreSQL.Simple.Newtypes (Aeson (..), getAeson)
import Database.PostgreSQL.Simple.SqlQQ (sql)
+import Deriving.Aeson qualified as DAE
import Effectful.PostgreSQL qualified as PG
import Effectful.Reader.Static (ask)
import Effectful.Time qualified as Time
@@ -48,14 +53,16 @@ import Lucid.Hyperscript (__)
import Models.Apis.Anomalies (FieldChange (..), PayloadChange (..))
import Models.Apis.Anomalies qualified as Anomalies
import Models.Apis.Endpoints qualified as Endpoints
+import Models.Apis.Errors qualified as Errors
import Models.Apis.Fields.Facets qualified as Facets
import Models.Apis.Issues qualified as Issues
import Models.Apis.RequestDumps qualified as RequestDump
+import Models.Apis.Shapes qualified as Shapes
import Models.Projects.Projects qualified as Projects
import Models.Telemetry.Schema qualified as Schema
import Models.Telemetry.Telemetry qualified as Telemetry
import Models.Users.Sessions qualified as Sessions
-import Models.Users.Users (User (id))
+import Models.Users.Users (User (id), UserId)
import NeatInterpolation (text)
import Pages.BodyWrapper (BWConfig (..), PageCtx (..))
import Pages.Components (emptyState_, resizer_, statBox_)
@@ -71,7 +78,7 @@ import System.Config (AuthContext (..), EnvConfig (..))
import System.Types (ATAuthCtx, RespHeaders, addErrorToast, addRespHeaders, addSuccessToast)
import Text.MMark qualified as MMark
import Text.Time.Pretty (prettyTimeAuto)
-import Utils (changeTypeFillColor, checkFreeTierExceeded, escapedQueryPartial, faSprite_, formatUTC, lookupValueText, methodFillColor, statusFillColor, toUriStr)
+import Utils (changeTypeFillColor, checkFreeTierExceeded, escapedQueryPartial, faSprite_, formatUTC, getDurationNSMS, lookupValueText, methodFillColor, statusFillColor, toUriStr)
import Web.FormUrlEncoded (FromForm)
@@ -82,6 +89,71 @@ newtype AnomalyBulkForm = AnomalyBulk
deriving anyclass (FromForm)
+-- | Helper type to extract common endpoint fields from any issue data type
+data IssueEndpointInfo = IssueEndpointInfo
+ { endpointMethod :: Text
+ , endpointPath :: Text
+ }
+ deriving stock (Generic)
+ deriving (AE.FromJSON) via DAE.CustomJSON '[DAE.FieldLabelModifier '[DAE.CamelToSnake]] IssueEndpointInfo
+
+
+-- | Card style variants for anomaly metric cards
+data CardStyle = CardNeutral | CardHighlight | CardWarning | CardError | CardInfo
+ deriving (Eq)
+
+
+-- | Metric card data for anomaly displays
+data MetricCard = MetricCard
+ { label :: Text
+ , icon :: Text
+ , value :: Html ()
+ , style :: CardStyle
+ , arrowIcon :: Maybe Text
+ }
+
+
+-- | Get CSS classes for a card style
+cardStyleClasses :: CardStyle -> (Text, Text, Text)
+cardStyleClasses = \case
+ CardNeutral -> ("border-strokeWeak", "", "text-textWeak")
+ CardHighlight -> ("border-strokeInformation-weak", "bg-fillInformation-weak", "text-fillInformation-strong")
+ CardWarning -> ("border-strokeWarning-weak", "bg-fillWarning-weak", "text-fillWarning-strong")
+ CardError -> ("border-strokeError-weak", "bg-fillError-weak", "text-fillError-strong")
+ CardInfo -> ("border-strokeInformation-weak", "bg-fillInformation-weak", "text-fillInformation-strong")
+
+
+-- | Render a single metric card
+renderMetricCard :: MetricCard -> Html ()
+renderMetricCard card = do
+ let (borderClass, bgClass, iconColorClass) = cardStyleClasses card.style
+ valueColorClass = case card.style of
+ CardNeutral -> "text-textStrong"
+ _ -> iconColorClass
+ div_ [class_ $ "rounded-lg border p-4 " <> borderClass <> " " <> bgClass] do
+ div_ [class_ "flex items-center justify-between mb-2"] do
+ span_ [class_ "text-xs text-textWeak uppercase tracking-wide"] $ toHtml card.label
+ faSprite_ card.icon "regular" $ "w-4 h-4 " <> iconColorClass
+ div_ [class_ $ "flex items-center gap-1 " <> valueColorClass] do
+ span_ [class_ "text-xl font-bold"] card.value
+ whenJust card.arrowIcon $ \arrow ->
+ faSprite_ arrow "regular" "w-4 h-4"
+
+
+-- | Render the full anomaly metrics section with cards and alert box
+anomalyMetricsSection :: [MetricCard] -> CardStyle -> Text -> Text -> Html ()
+anomalyMetricsSection cards alertStyle alertTitle alertMessage = do
+ let (_, alertBgClass, alertIconClass) = cardStyleClasses alertStyle
+ div_ [class_ "mb-4"] do
+ div_ [class_ "grid grid-cols-4 gap-3"] do
+ forM_ cards renderMetricCard
+ div_ [class_ $ "mt-3 p-4 rounded-lg flex items-start gap-3 " <> alertBgClass] do
+ faSprite_ "circle-exclamation" "regular" $ "w-5 h-5 flex-shrink-0 mt-0.5 " <> alertIconClass
+ div_ [] do
+ div_ [class_ "font-semibold text-textStrong text-sm"] $ toHtml alertTitle
+ div_ [class_ "text-sm text-textWeak mt-1"] $ toHtml alertMessage
+
+
acknowledgeAnomalyGetH :: Projects.ProjectId -> Anomalies.AnomalyId -> Maybe Text -> ATAuthCtx (RespHeaders AnomalyAction)
acknowledgeAnomalyGetH pid aid hostM = do
(sess, project) <- Sessions.sessionAndProject pid
@@ -188,31 +260,73 @@ anomalyDetailCore pid firstM fetchIssue = do
$ PageCtx baseBwconf
$ toHtml ("Issue not found" :: Text)
Just issue -> do
- let bwconf =
+ errorM <-
+ issue.issueType & \case
+ Issues.RuntimeException -> Errors.getErrorLByHash pid issue.targetHash
+ _ -> pure Nothing
+ let isErrorRelated = issue.issueType `elem` [Issues.RuntimeException, Issues.ErrorEscalating, Issues.ErrorRegressed]
+ isResolved = maybe False (\e -> e.state == Errors.ESResolved) errorM
+ bwconf =
baseBwconf
{ pageActions = Just $ div_ [class_ "flex gap-2"] do
anomalyAcknowledgeButton pid (UUIDId issue.id.unUUIDId) (isJust issue.acknowledgedAt) ""
anomalyArchiveButton pid (UUIDId issue.id.unUUIDId) (isJust issue.archivedAt)
+ -- Add error-specific buttons for runtime error issues
+ when isErrorRelated $ do
+ errorResolveButton_ pid issue.targetHash isResolved
}
- errorM <-
- issue.issueType & \case
- Issues.RuntimeException -> Anomalies.errorByHash pid issue.endpointHash
- _ -> pure Nothing
+ -- Helper to fetch trace and spans given a trace ID
+ let fetchTraceData traceIdM timeHint = case traceIdM of
+ Just trId -> do
+ trM <- Telemetry.getTraceDetails pid trId timeHint now
+ case trM of
+ Just traceItem -> do
+ spanRecords' <- Telemetry.getSpanRecordsByTraceId pid traceItem.traceId (Just traceItem.traceStartTime) now
+ pure (Just traceItem, V.fromList spanRecords')
+ Nothing -> pure (Nothing, V.empty)
+ Nothing -> pure (Nothing, V.empty)
+
(trItem, spanRecs) <- case errorM of
Just err -> do
let targetTIdM = maybe err.recentTraceId (const err.firstTraceId) firstM
targetTme = maybe (zonedTimeToUTC err.updatedAt) (const $ zonedTimeToUTC err.createdAt) firstM
- case targetTIdM of
- Just x -> do
- trM <- Telemetry.getTraceDetails pid x (Just targetTme) now
- case trM of
- Just traceItem -> do
- spanRecords' <-
- Telemetry.getSpanRecordsByTraceId pid traceItem.traceId (Just traceItem.traceStartTime) now
- pure (Just traceItem, V.fromList spanRecords')
- Nothing -> pure (Nothing, V.empty)
- Nothing -> pure (Nothing, V.empty)
- Nothing -> pure (Nothing, V.empty)
+ fetchTraceData targetTIdM (Just targetTme)
+ Nothing -> case issue.issueType of
+ Issues.NewShape -> do
+ shapeM <- Shapes.getShapeByHash pid issue.targetHash
+ case shapeM of
+ Just shape -> do
+ let targetTIdM = maybe shape.recentTraceId (const shape.firstTraceId) firstM
+ fetchTraceData targetTIdM (Just $ shape.createdAt)
+ Nothing -> pure (Nothing, V.empty)
+ Issues.NewEndpoint -> do
+ endpointM <- Endpoints.getEndpointByHash pid issue.targetHash
+ case endpointM of
+ Just endpoint -> do
+ let targetTIdM = maybe endpoint.recentTraceId (const endpoint.firstTraceId) firstM
+ fetchTraceData targetTIdM (Just endpoint.createdAt)
+ Nothing -> pure (Nothing, V.empty)
+ Issues.EndpointLatencyDegradation -> do
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.EndpointLatencyDegradationData) -> do
+ let targetTIdM = d.sampleTraceIds V.!? 0
+ fetchTraceData targetTIdM (Just d.detectedAt)
+ _ -> pure (Nothing, V.empty)
+ Issues.EndpointErrorRateSpike -> do
+ endpointM <- Endpoints.getEndpointByHash pid issue.targetHash
+ case endpointM of
+ Just endpoint -> do
+ let targetTIdM = maybe endpoint.recentTraceId (const endpoint.firstTraceId) firstM
+ fetchTraceData targetTIdM (Just endpoint.createdAt)
+ Nothing -> pure (Nothing, V.empty)
+ Issues.EndpointVolumeRateChange -> do
+ endpointM <- Endpoints.getEndpointByHash pid issue.targetHash
+ case endpointM of
+ Just endpoint -> do
+ let targetTIdM = maybe endpoint.recentTraceId (const endpoint.firstTraceId) firstM
+ fetchTraceData targetTIdM (Just endpoint.createdAt)
+ Nothing -> pure (Nothing, V.empty)
+ _ -> pure (Nothing, V.empty)
addRespHeaders $ PageCtx bwconf $ anomalyDetailPage pid issue trItem spanRecs errorM now (isJust firstM)
@@ -253,11 +367,11 @@ timeStatBox_ title timeStr
| otherwise = pass
-anomalyDetailPage :: Projects.ProjectId -> Issues.Issue -> Maybe Telemetry.Trace -> V.Vector Telemetry.OtelLogsAndSpans -> Maybe Anomalies.ATError -> UTCTime -> Bool -> Html ()
+anomalyDetailPage :: Projects.ProjectId -> Issues.Issue -> Maybe Telemetry.Trace -> V.Vector Telemetry.OtelLogsAndSpans -> Maybe Errors.ErrorL -> UTCTime -> Bool -> Html ()
anomalyDetailPage pid issue tr otellogs errM now isFirst = do
let spanRecs = V.catMaybes $ Telemetry.convertOtelLogsAndSpansToSpanRecord <$> otellogs
issueId = UUID.toText issue.id.unUUIDId
- div_ [class_ "pt-8 mx-auto px-4 w-full flex flex-col gap-4 h-full overflow-auto pb-32"] do
+ div_ [class_ "pt-8 mx-auto px-4 w-full flex flex-col gap-4 overflow-auto pb-32"] do
-- Header
div_ [class_ "flex flex-col gap-3"] do
div_ [class_ "flex gap-2 flex-wrap items-center"] do
@@ -268,37 +382,37 @@ anomalyDetailPage pid issue tr otellogs errM now isFirst = do
_ -> pass
h3_ [class_ "text-textStrong text-2xl font-semibold"] $ toHtml issue.title
p_ [class_ "text-sm text-textWeak max-w-3xl"] $ toHtml issue.recommendedAction
-
- -- Metrics & Timeline Row (8-column grid: 4 stats + chart)
- div_ [class_ "grid grid-cols-4 lg:grid-cols-8 gap-4"] do
- -- Stats (1 column each)
- statBox_ (Just pid) Nothing "Affected Requests" "" (show issue.affectedRequests) Nothing Nothing
- statBox_ (Just pid) Nothing "Affected Clients" "" (show issue.affectedClients) Nothing Nothing
- whenJust errM $ \err -> do
- timeStatBox_ "First Seen" $ prettyTimeAuto now $ zonedTimeToUTC err.createdAt
- timeStatBox_ "Last Seen" $ prettyTimeAuto now $ zonedTimeToUTC err.updatedAt
- -- Timeline (4 columns)
- div_ [class_ "col-span-4"]
- $ Widget.widget_
- $ (def :: Widget.Widget)
- { Widget.standalone = Just True
- , Widget.id = Just $ issueId <> "-timeline"
- , Widget.wType = Widget.WTTimeseries
- , Widget.title = Just "Error trends"
- , Widget.showTooltip = Just True
- , Widget.xAxis = Just (def{Widget.showAxisLabel = Just True})
- , Widget.yAxis = Just (def{Widget.showOnlyMaxLabel = Just True})
- , Widget.query = Just "status_code == \"ERROR\" | summarize count(*) by bin_auto(timestamp), status_code"
- , Widget._projectId = Just issue.projectId
- , Widget.hideLegend = Just True
- }
+ let widget title q =
+ div_ [class_ "col-span-4"]
+ $ Widget.widget_
+ $ (def :: Widget.Widget)
+ { Widget.standalone = Just True
+ , Widget.id = Just $ issueId <> "-timeline"
+ , Widget.naked = Just True
+ , Widget.wType = Widget.WTTimeseries
+ , Widget.title = Just title
+ , Widget.showTooltip = Just True
+ , Widget.xAxis = Just (def{Widget.showAxisLabel = Just True})
+ , Widget.yAxis = Just (def{Widget.showOnlyMaxLabel = Just True})
+ , Widget.query = Just q
+ , Widget._projectId = Just issue.projectId
+ , Widget.hideLegend = Just True
+ }
-- Two Column Layout
- div_ [class_ "flex flex-col gap-4"] do
- div_ [class_ "grid grid-cols-2 gap-4 w-full"] do
- case issue.issueType of
- Issues.RuntimeException -> do
- case AE.fromJSON (getAeson issue.issueData) of
- AE.Success (exceptionData :: Issues.RuntimeExceptionData) -> do
+ case issue.issueType of
+ Issues.RuntimeException -> do
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (exceptionData :: Issues.RuntimeExceptionData) -> do
+ div_ [class_ "grid grid-cols-4 lg:grid-cols-8 gap-4"] do
+ -- Stats (1 column each)
+ whenJust errM $ \err -> do
+ statBox_ (Just pid) Nothing "Affected Requests" "" (show err.occurrences) Nothing Nothing
+ statBox_ (Just pid) Nothing "Affected Clients" "" (show err.affectedUsers) Nothing Nothing
+ timeStatBox_ "First Seen" $ prettyTimeAuto now $ zonedTimeToUTC err.createdAt
+ timeStatBox_ "Last Seen" $ prettyTimeAuto now $ zonedTimeToUTC (fromMaybe err.updatedAt err.lastOccurredAt)
+ widget "Error trend" "status_code == \"ERROR\" | summarize count(*) by bin_auto(timestamp), status_code"
+ div_ [class_ "flex flex-col gap-4"] do
+ div_ [class_ "grid grid-cols-2 gap-4 w-full"] do
div_ [class_ "surface-raised rounded-2xl overflow-hidden"] do
div_ [class_ "px-4 py-3 border-b border-strokeWeak flex items-center justify-between"] do
div_ [class_ "flex items-center gap-2"] do
@@ -325,7 +439,6 @@ anomalyDetailPage pid issue tr otellogs errM now isFirst = do
div_ [] do
span_ [class_ "text-xs text-textWeak"] "First seen:"
span_ [class_ "ml-2 text-xs"] $ toHtml $ compactTimeAgo $ toText $ prettyTimeAuto now (zonedTimeToUTC err.createdAt)
-
div_ [class_ "flex items-center gap-2"] do
faSprite_ "calendar" "regular" "w-3 h-3"
div_ [] do
@@ -336,69 +449,251 @@ anomalyDetailPage pid issue tr otellogs errM now isFirst = do
faSprite_ "code" "regular" "w-4 h-4"
div_ [] do
span_ [class_ "text-sm text-textWeak"] "Stack:"
- span_ [class_ "ml-2 text-sm"] $ toHtml $ fromMaybe "Unknown stack" err.errorData.stack
+ span_ [class_ "ml-2 text-sm"] $ toHtml $ fromMaybe "Unknown stack" err.errorData.runtime
div_ [class_ "flex items-center gap-2"] do
faSprite_ "server" "regular" "w-3 h-3"
div_ [] do
span_ [class_ "text-sm text-textWeak"] "Service:"
span_ [class_ "ml-2 text-sm"] $ toHtml $ fromMaybe "Unknown service" err.errorData.serviceName
- _ -> pass
- Issues.QueryAlert -> do
- case AE.fromJSON (getAeson issue.issueData) of
- AE.Success (alertData :: Issues.QueryAlertData) -> do
- div_ [class_ "mb-4"] do
- span_ [class_ "text-sm text-textWeak mb-2 block font-medium"] "Query:"
- div_ [class_ "bg-fillInformation-weak border border-strokeInformation-weak rounded-lg p-3 text-sm font-mono text-fillInformation-strong max-w-2xl overflow-x-auto"]
- $ toHtml alertData.queryExpression
- _ -> pass
+ _ -> pass
+ Issues.QueryAlert -> do
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (alertData :: Issues.QueryAlertData) -> do
+ div_ [class_ "mb-4"] do
+ span_ [class_ "text-sm text-textWeak mb-2 block font-medium"] "Query:"
+ div_ [class_ "bg-fillInformation-weak border border-strokeInformation-weak rounded-lg p-3 text-sm font-mono text-fillInformation-strong max-w-2xl overflow-x-auto"]
+ $ toHtml alertData.queryExpression
+ _ -> pass
+ Issues.NewEndpoint ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.NewEndpointData) -> do
+ div_ [class_ "flex items-center gap-3 mb-4 p-3 rounded-lg"] do
+ span_ [class_ $ "badge " <> methodFillColor d.endpointMethod] $ toHtml d.endpointMethod
+ span_ [class_ "monospace bg-fillWeaker px-2 py-1 rounded text-sm text-textStrong"] $ toHtml d.endpointPath
+ div_ [class_ "w-px h-4 bg-strokeWeak"] ""
+ span_ [class_ "flex items-center gap-1.5 text-sm text-textWeak"] do
+ faSprite_ "server" "regular" "h-3 w-3"
+ toHtml d.endpointHost
+ -- Stats and chart
+ div_ [class_ "grid grid-cols-4 lg:grid-cols-8 gap-4 mb-4"] do
+ timeStatBox_ "First Seen" $ prettyTimeAuto now d.firstSeenAt
+ widget "Request trend" $ "attributes.http.request.method==\"" <> d.endpointMethod <> "\" AND attributes.http.route==\"" <> d.endpointPath <> "\" | summarize count(*) by bin_auto(timestamp)"
+ _ -> pass
+ Issues.NewShape ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.NewShapeData) -> do
+ div_ [class_ "flex items-center gap-3 mb-4 p-3 rounded-lg"] do
+ span_ [class_ $ "badge " <> methodFillColor d.endpointMethod] $ toHtml d.endpointMethod
+ span_ [class_ "monospace bg-fillWeaker px-2 py-1 rounded text-sm text-textStrong"] $ toHtml d.endpointPath
+ div_ [class_ "w-px h-4 bg-strokeWeak"] ""
+
+ div_ [class_ "grid grid-cols-2 lg:grid-cols-4 gap-4 mb-4"] do
+ statBox_ (Just pid) Nothing "Status Code" "" (show d.statusCode) Nothing Nothing
+ statBox_ (Just pid) Nothing "New Fields" "" (show $ V.length d.newFields) Nothing Nothing
+ statBox_ (Just pid) Nothing "Deleted Fields" "" (show $ V.length d.deletedFields) Nothing Nothing
+ unless (V.null d.newFields) do
+ div_ [class_ "surface-raised rounded-2xl overflow-hidden mb-4"] do
+ div_ [class_ "px-4 py-3 border-b border-strokeWeak"] do
+ span_ [class_ "text-sm font-medium text-textStrong"] "New Fields"
+ div_ [class_ "p-4"] do
+ ul_ [class_ "list-disc list-inside text-sm text-textWeak"] do
+ forM_ d.newFields $ \f -> li_ [] $ toHtml f
+ unless (V.null d.deletedFields) do
+ div_ [class_ "surface-raised rounded-2xl overflow-hidden mb-4"] do
+ div_ [class_ "px-4 py-3 border-b border-strokeWeak"] do
+ span_ [class_ "text-sm font-medium text-textError"] "Deleted Fields"
+ div_ [class_ "p-4"] do
+ ul_ [class_ "list-disc list-inside text-sm text-textWeak"] do
+ forM_ d.deletedFields $ \f -> li_ [] $ toHtml f
+ _ -> pass
+ Issues.FieldChange ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.FieldChangeData) -> do
+ div_ [class_ "grid grid-cols-2 lg:grid-cols-4 gap-4 mb-4"] do
+ statBox_ (Just pid) Nothing "Endpoint" "" (d.endpointMethod <> " " <> d.endpointPath) Nothing Nothing
+ statBox_ (Just pid) Nothing "Field Path" "" d.keyPath Nothing Nothing
+ statBox_ (Just pid) Nothing "Change Type" "" d.changeType Nothing Nothing
+ statBox_ (Just pid) Nothing "New Type" "" d.newType Nothing Nothing
+ whenJust d.previousType $ \prev -> do
+ div_ [class_ "mb-4 p-4 bg-fillWeaker rounded-lg border border-strokeWeak"] do
+ span_ [class_ "text-sm text-textWeak"] "Previous type: "
+ span_ [class_ "text-sm font-mono text-textStrong"] $ toHtml prev
+ _ -> pass
+ Issues.LogPattern ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.LogPatternData) -> do
+ div_ [class_ "grid grid-cols-2 lg:grid-cols-4 gap-4 mb-4"] do
+ statBox_ (Just pid) Nothing "Log Level" "" (fromMaybe "Unknown" d.logLevel) Nothing Nothing
+ statBox_ (Just pid) Nothing "Service" "" (fromMaybe "Unknown" d.serviceName) Nothing Nothing
+ statBox_ (Just pid) Nothing "Occurrences" "" (show d.occurrenceCount) Nothing Nothing
+ timeStatBox_ "First Seen" $ prettyTimeAuto now d.firstSeenAt
+ div_ [class_ "surface-raised rounded-2xl overflow-hidden mb-4"] do
+ div_ [class_ "px-4 py-3 border-b border-strokeWeak"] do
+ span_ [class_ "text-sm font-medium text-textStrong"] "Log Pattern"
+ div_ [class_ "p-4"] do
+ pre_ [class_ "text-sm text-textWeak font-mono whitespace-pre-wrap"] $ toHtml d.logPattern
+ whenJust d.sampleMessage $ \msg -> do
+ div_ [class_ "surface-raised rounded-2xl overflow-hidden mb-4"] do
+ div_ [class_ "px-4 py-3 border-b border-strokeWeak"] do
+ span_ [class_ "text-sm font-medium text-textStrong"] "Sample Message"
+ div_ [class_ "p-4"] do
+ pre_ [class_ "text-sm text-textWeak font-mono whitespace-pre-wrap"] $ toHtml msg
+ _ -> pass
+ Issues.ErrorEscalating ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.ErrorEscalatingData) -> do
+ div_ [class_ "grid grid-cols-2 lg:grid-cols-4 gap-4 mb-4"] do
+ statBox_ (Just pid) Nothing "Escalation Rate" "" (show d.escalationRate <> "x") Nothing Nothing
+ statBox_ (Just pid) Nothing "Window" "" d.escalationWindow Nothing Nothing
+ statBox_ (Just pid) Nothing "Last Hour" "" (show d.occurrences1h) Nothing Nothing
+ statBox_ (Just pid) Nothing "Last 24h" "" (show d.occurrences24h) Nothing Nothing
+ div_ [class_ "surface-raised rounded-2xl overflow-hidden mb-4"] do
+ div_ [class_ "px-4 py-3 border-b border-strokeWeak"] do
+ span_ [class_ "text-sm font-medium text-textStrong"] "Error Details"
+ div_ [class_ "p-4 flex flex-col gap-2"] do
+ div_ [] do
+ span_ [class_ "text-sm text-textWeak"] "Exception: "
+ span_ [class_ "text-sm font-mono text-textStrong"] $ toHtml d.errorType
+ div_ [] do
+ span_ [class_ "text-sm text-textWeak"] "Message: "
+ span_ [class_ "text-sm text-textStrong"] $ toHtml d.errorMessage
+ whenJust d.serviceName $ \svc -> do
+ div_ [] do
+ span_ [class_ "text-sm text-textWeak"] "Service: "
+ span_ [class_ "text-sm text-textStrong"] $ toHtml svc
+ _ -> pass
+ Issues.ErrorRegressed ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.ErrorRegressedData) -> do
+ let quietDays = d.quietPeriodMinutes `div` 1440
+ quietHours = (d.quietPeriodMinutes `mod` 1440) `div` 60
+ quietStr = if quietDays > 0 then show quietDays <> " days" else show quietHours <> " hours"
+ div_ [class_ "grid grid-cols-2 lg:grid-cols-4 gap-4 mb-4"] do
+ statBox_ (Just pid) Nothing "Quiet Period" "" quietStr Nothing Nothing
+ statBox_ (Just pid) Nothing "Previous Occurrences" "" (show d.previousOccurrences) Nothing Nothing
+ statBox_ (Just pid) Nothing "New Occurrences" "" (show d.newOccurrences) Nothing Nothing
+ timeStatBox_ "Regressed At" $ prettyTimeAuto now d.regressedAt
+ div_ [class_ "surface-raised rounded-2xl overflow-hidden mb-4"] do
+ div_ [class_ "px-4 py-3 border-b border-strokeWeak"] do
+ span_ [class_ "text-sm font-medium text-textStrong"] "Error Details"
+ div_ [class_ "p-4 flex flex-col gap-2"] do
+ div_ [] do
+ span_ [class_ "text-sm text-textWeak"] "Exception: "
+ span_ [class_ "text-sm font-mono text-textStrong"] $ toHtml d.errorType
+ div_ [] do
+ span_ [class_ "text-sm text-textWeak"] "Message: "
+ span_ [class_ "text-sm text-textStrong"] $ toHtml d.errorMessage
+ _ -> pass
+ Issues.LogPatternRateChange ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.LogPatternRateChangeData) -> do
+ div_ [class_ "grid grid-cols-2 lg:grid-cols-4 gap-4 mb-4"] do
+ statBox_ (Just pid) Nothing "Direction" "" d.changeDirection Nothing Nothing
+ statBox_ (Just pid) Nothing "Change" "" (show (round d.changePercent :: Int) <> "%") Nothing Nothing
+ statBox_ (Just pid) Nothing "Current Rate" "" (show (round d.currentRatePerHour :: Int) <> "/hr") Nothing Nothing
+ statBox_ (Just pid) Nothing "Baseline" "" (show (round d.baselineMean :: Int) <> "/hr") Nothing Nothing
+ div_ [class_ "surface-raised rounded-2xl overflow-hidden mb-4"] do
+ div_ [class_ "px-4 py-3 border-b border-strokeWeak"] do
+ span_ [class_ "text-sm font-medium text-textStrong"] "Log Pattern"
+ div_ [class_ "p-4"] do
+ pre_ [class_ "text-sm text-textWeak font-mono whitespace-pre-wrap"] $ toHtml d.logPattern
+ _ -> pass
+ Issues.EndpointLatencyDegradation ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.EndpointLatencyDegradationData) -> do
+ div_ [class_ "flex items-center gap-3 mb-4 p-3 rounded-lg"] do
+ span_ [class_ $ "badge " <> methodFillColor d.endpointMethod] $ toHtml d.endpointMethod
+ span_ [class_ "monospace bg-fillWeaker px-2 py-1 rounded text-sm text-textStrong"] $ toHtml d.endpointPath
+ div_ [class_ "w-px h-4 bg-strokeWeak"] ""
+ div_ [class_ "grid grid-cols-2 lg:grid-cols-4 gap-4 mb-4"] do
+ statBox_ (Just pid) Nothing "Percentile" "" d.percentile Nothing Nothing
+ statBox_ (Just pid) Nothing "Current Latency" "" (toText $ getDurationNSMS (round d.currentLatencyMs)) Nothing Nothing
+ statBox_ (Just pid) Nothing "Baseline" "" (toText $ getDurationNSMS (round d.baselineLatencyMs)) Nothing Nothing
+ div_ [class_ "p-4 bg-fillWarning-weak rounded-lg border border-strokeWarning-weak mb-4"] do
+ span_ [class_ "text-sm text-fillWarning-strong font-medium"] $ toHtml $ "Latency increased by " <> show (round d.degradationPercent :: Int) <> "% (z-score: " <> show (round d.zScore :: Int) <> ")"
+ _ -> pass
+ Issues.EndpointErrorRateSpike ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.EndpointErrorRateSpikeData) -> do
+ div_ [class_ "flex items-center gap-3 mb-4 p-3 rounded-lg"] do
+ span_ [class_ $ "badge " <> methodFillColor d.endpointMethod] $ toHtml d.endpointMethod
+ span_ [class_ "monospace bg-fillWeaker px-2 py-1 rounded text-sm text-textStrong"] $ toHtml d.endpointPath
+ div_ [class_ "w-px h-4 bg-strokeWeak"] ""
+
+ div_ [class_ "grid grid-cols-2 lg:grid-cols-4 gap-4 mb-4"] do
+ statBox_ (Just pid) Nothing "Error Rate" "" (show (round (d.currentErrorRate * 100) :: Int) <> "%") Nothing Nothing
+ statBox_ (Just pid) Nothing "Errors" "" (show d.errorCount <> "/" <> show d.totalRequests) Nothing Nothing
+ statBox_ (Just pid) Nothing "Baseline" "" (show (round (d.baselineErrorRate * 100) :: Int) <> "%") Nothing Nothing
+ unless (V.null d.topErrorTypes) do
+ div_ [class_ "surface-raised rounded-2xl overflow-hidden mb-4"] do
+ div_ [class_ "px-4 py-3 border-b border-strokeWeak"] do
+ span_ [class_ "text-sm font-medium text-textStrong"] "Top Error Types"
+ div_ [class_ "p-4"] do
+ ul_ [class_ "list-disc list-inside text-sm text-textWeak"] do
+ forM_ d.topErrorTypes $ \e -> li_ [class_ "font-mono"] $ toHtml e
+ _ -> pass
+ Issues.EndpointVolumeRateChange ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.EndpointVolumeRateChangeData) -> do
+ div_ [class_ "flex items-center gap-3 mb-4 p-3 rounded-lg"] do
+ span_ [class_ $ "badge " <> methodFillColor d.endpointMethod] $ toHtml d.endpointMethod
+ span_ [class_ "monospace bg-fillWeaker px-2 py-1 rounded text-sm text-textStrong"] $ toHtml d.endpointPath
+ div_ [class_ "w-px h-4 bg-strokeWeak"] ""
+ div_ [class_ "grid grid-cols-2 lg:grid-cols-4 gap-4 mb-4"] do
+ statBox_ (Just pid) Nothing "Direction" "" d.changeDirection Nothing Nothing
+ statBox_ (Just pid) Nothing "Current Rate" "" (show (round d.currentRatePerHour :: Int) <> "/hr") Nothing Nothing
+ statBox_ (Just pid) Nothing "Baseline" "" (show (round d.baselineRatePerHour :: Int) <> "/hr") Nothing Nothing
+ let alertClass = if d.changeDirection == "drop" then "bg-fillWarning-weak border-strokeWarning-weak text-fillWarning-strong" else "bg-fillInformation-weak border-strokeInformation-weak text-fillInformation-strong"
+ div_ [class_ $ "p-4 rounded-lg border mb-4 " <> alertClass] do
+ span_ [class_ "text-sm font-medium"] $ toHtml $ "Traffic " <> d.changeDirection <> " by " <> show (round (abs d.changePercent) :: Int) <> "%"
_ -> pass
- div_ [class_ "surface-raised rounded-2xl overflow-hidden", id_ "error-details-container"] do
- div_ [class_ "px-4 border-b border-b-strokeWeak flex items-center justify-between"] do
- div_ [class_ "flex items-center gap-2"] do
- faSprite_ "magnifying-glass-chart" "regular" "w-4 h-4 text-iconNeutral"
- h4_ [class_ "text-textStrong text-lg font-medium"] "Investigation"
- div_ [class_ "flex items-center"] do
- let aUrl = "/p/" <> pid.toText <> "/anomalies/" <> issueId <> ""
- a_ [href_ $ aUrl <> "?first_occurrence=true", class_ $ (if isFirst then "text-textBrand font-medium" else "text-textWeak hover:text-textStrong") <> " text-xs py-3 px-3 cursor-pointer transition-colors", term "data-tippy-content" "Show first trace the error occured"] "First"
- a_ [href_ aUrl, class_ $ (if isFirst then "text-textWeak hover:text-textStrong" else "text-textBrand font-medium") <> " text-xs py-3 px-3 cursor-pointer transition-colors", term "data-tippy-content" "Show recent trace the error occured"] "Recent"
- span_ [class_ "mx-4 w-px h-4 bg-strokeWeak"] pass
- button_ [class_ "text-xs py-3 px-3 cursor-pointer err-tab t-tab-active font-medium", onclick_ "navigatable(this, '#span-content', '#error-details-container', 't-tab-active', 'err')"] "Trace"
- button_ [class_ "text-xs py-3 px-3 cursor-pointer err-tab font-medium", onclick_ "navigatable(this, '#log-content', '#error-details-container', 't-tab-active', 'err')"] "Logs"
- button_ [class_ "text-xs py-3 px-3 cursor-pointer err-tab font-medium", onclick_ "navigatable(this, '#replay-content', '#error-details-container', 't-tab-active', 'err')"] "Replay"
- div_ [class_ "p-2 w-full overflow-x-hidden"] do
- div_ [class_ "flex w-full err-tab-content", id_ "span-content"] do
- div_ [id_ "trace_container", class_ "grow-1 max-w-[80%] w-1/2 min-w-[20%] shrink-1"] do
- whenJust tr $ \t ->
- tracePage pid t spanRecs
- unless (isJust tr)
- $ div_ [class_ "flex flex-col items-center justify-center h-48"] do
- faSprite_ "inbox-full" "regular" "w-6 h-6 text-textWeak"
- span_ [class_ "mt-2 text-sm text-textWeak"] "No trace data available for this error."
- div_ [class_ "transition-opacity duration-200 mx-1", id_ "resizer-details_width-wrapper"] $ resizer_ "log_details_container" "details_width" False
- div_ [class_ "grow-0 relative shrink-0 overflow-y-auto overflow-x-hidden max-h-[500px] w-1/2 w-c-scroll overflow-x-hidden overflow-y-auto", id_ "log_details_container"] do
- span_ [class_ "htmx-indicator query-indicator absolute loading left-1/2 -translate-x-1/2 loading-dots absoute z-10 top-10", id_ "details_indicator"] ""
- let (spanId, createdAt) = case spanRecs V.!? 0 of
- Just sr -> (sr.uSpanId, formatUTC sr.timestamp)
- Nothing -> ("", "")
- let url = "/p/" <> pid.toText <> "/log_explorer/" <> spanId <> "/" <> createdAt <> "/detailed"
- div_ [hxGet_ url, hxTarget_ "#log_details_container", hxSwap_ "innerHtml", hxTrigger_ "intersect one", hxIndicator_ "#details_indicator", term "hx-sync" "this:replace"] pass
+ div_ [class_ "surface-raised h-max rounded-2xl overflow-hidden", id_ "error-details-container"] do
+ div_ [class_ "px-4 border-b border-b-strokeWeak flex items-center justify-between"] do
+ div_ [class_ "flex items-center gap-2"] do
+ faSprite_ "magnifying-glass-chart" "regular" "w-4 h-4 text-iconNeutral"
+ h4_ [class_ "text-textStrong text-lg font-medium"] "Investigation"
+ div_ [class_ "flex items-center"] do
+ let aUrl = "/p/" <> pid.toText <> "/anomalies/" <> issueId <> ""
+ a_ [href_ $ aUrl <> "?first_occurrence=true", class_ $ (if isFirst then "text-textBrand font-medium" else "text-textWeak hover:text-textStrong") <> " text-xs py-3 px-3 cursor-pointer transition-colors", term "data-tippy-content" "Show first trace the error occured"] "First"
+ a_ [href_ aUrl, class_ $ (if isFirst then "text-textWeak hover:text-textStrong" else "text-textBrand font-medium") <> " text-xs py-3 px-3 cursor-pointer transition-colors", term "data-tippy-content" "Show recent trace the error occured"] "Recent"
+ span_ [class_ "mx-4 w-px h-4 bg-strokeWeak"] pass
+ button_ [class_ "text-xs py-3 px-3 cursor-pointer err-tab t-tab-active font-medium", onclick_ "navigatable(this, '#span-content', '#error-details-container', 't-tab-active', 'err')"] "Trace"
+ button_ [class_ "text-xs py-3 px-3 cursor-pointer err-tab font-medium", onclick_ "navigatable(this, '#log-content', '#error-details-container', 't-tab-active', 'err')"] "Logs"
+ button_ [class_ "text-xs py-3 px-3 cursor-pointer err-tab font-medium", onclick_ "navigatable(this, '#replay-content', '#error-details-container', 't-tab-active', 'err')"] "Replay"
+ div_ [class_ "p-2 w-full overflow-x-hidden"] do
+ div_ [class_ "flex w-full err-tab-content", id_ "span-content"] do
+ div_ [id_ "trace_container", class_ "grow-1 max-w-[80%] w-1/2 min-w-[20%] shrink-1"] do
+ whenJust tr $ \t ->
+ tracePage pid t spanRecs
+ unless (isJust tr)
+ $ div_ [class_ "flex flex-col items-center justify-center h-48"] do
+ faSprite_ "inbox-full" "regular" "w-6 h-6 text-textWeak"
+ span_ [class_ "mt-2 text-sm text-textWeak"] "No trace data available for this error."
+ div_ [class_ "transition-opacity duration-200 mx-1", id_ "resizer-details_width-wrapper"] $ resizer_ "log_details_container" "details_width" False
+ div_ [class_ "grow-0 relative shrink-0 overflow-y-auto overflow-x-hidden max-h-[500px] w-1/2 w-c-scroll overflow-x-hidden overflow-y-auto", id_ "log_details_container"] do
+ span_ [class_ "htmx-indicator query-indicator absolute loading left-1/2 -translate-x-1/2 loading-dots absoute z-10 top-10", id_ "details_indicator"] ""
+ let (spanId, createdAt) = case spanRecs V.!? 0 of
+ Just sr -> (sr.uSpanId, formatUTC sr.timestamp)
+ Nothing -> ("", "")
+ let url = "/p/" <> pid.toText <> "/log_explorer/" <> spanId <> "/" <> createdAt <> "/detailed"
+ div_ [hxGet_ url, hxTarget_ "#log_details_container", hxSwap_ "innerHtml", hxTrigger_ "intersect one", hxIndicator_ "#details_indicator", term "hx-sync" "this:replace"] pass
- div_ [id_ "log-content", class_ "hidden err-tab-content"] do
- div_ [class_ "flex flex-col gap-4"] do
- virtualTable pid (Just ("/p/" <> pid.toText <> "/log_explorer?json=true&query=" <> toUriStr ("kind==\"log\" AND context___trace_id==\"" <> fromMaybe "" (errM >>= (\x -> x.recentTraceId)) <> "\"")))
+ div_ [id_ "log-content", class_ "hidden err-tab-content"] do
+ div_ [class_ "flex flex-col gap-4"] do
+ virtualTable pid (Just ("/p/" <> pid.toText <> "/log_explorer?json=true&query=" <> toUriStr ("kind==\"log\" AND context___trace_id==\"" <> fromMaybe "" (errM >>= (\x -> x.recentTraceId)) <> "\"")))
- div_ [id_ "replay-content", class_ "hidden err-tab-content"] do
- let withSessionIds = V.catMaybes $ V.map (\sr -> (`lookupValueText` "id") =<< Map.lookup "session" =<< sr.attributes) spanRecs
- unless (V.null withSessionIds) do
- let sessionId = V.head withSessionIds
- div_ [class_ "border border-r border-l w-max mx-auto"]
- $ termRaw "session-replay" [id_ "sessionReplay", term "initialSession" sessionId, class_ "shrink-1 flex flex-col", term "projectId" pid.toText, term "containerId" "sessionPlayerWrapper"] ("" :: Text)
+ div_ [id_ "replay-content", class_ "hidden err-tab-content"] do
+ let withSessionIds = V.catMaybes $ V.map (\sr -> (`lookupValueText` "id") =<< Map.lookup "session" =<< sr.attributes) spanRecs
+ unless (V.null withSessionIds) do
+ let sessionId = V.head withSessionIds
+ div_ [class_ "border border-r border-l w-max mx-auto"]
+ $ termRaw "session-replay" [id_ "sessionReplay", term "initialSession" sessionId, class_ "shrink-1 flex flex-col", term "projectId" pid.toText, term "containerId" "sessionPlayerWrapper"] ("" :: Text)
- when (V.null withSessionIds)
- $ div_ [class_ "flex flex-col gap-4"] do
- emptyState_ "No Replay Available" "No session replays associated with this trace" (Just "https://monoscope.tech/docs/sdks/Javascript/browser/") "Session Replay Guide"
+ when (V.null withSessionIds)
+ $ div_ [class_ "flex flex-col gap-4"] do
+ emptyState_ "No Replay Available" "No session replays associated with this trace" (Just "https://monoscope.tech/docs/sdks/Javascript/browser/") "Session Replay Guide"
-- AI Chat section (inline with page content)
anomalyAIChat_ pid issue.id
@@ -410,6 +705,12 @@ newtype AIChatForm = AIChatForm {query :: Text}
deriving anyclass (FromForm)
+-- | Form for error assignment
+newtype ErrorAssignForm = ErrorAssignForm {assigneeId :: UserId}
+ deriving stock (Generic, Show)
+ deriving anyclass (FromForm)
+
+
-- | AI response structure from OpenAI
data AIInvestigationResponse = AIInvestigationResponse
{ explanation :: Text
@@ -429,9 +730,9 @@ buildAIContext issue errM trDataM spans =
, Just $ "- **Title**: " <> issue.title
, Just $ "- **Type**: " <> show issue.issueType
, Just $ "- **Severity**: " <> issue.severity
- , Just $ "- **Service**: " <> issue.service
- , Just $ "- **Affected Requests**: " <> show issue.affectedRequests
- , Just $ "- **Affected Clients**: " <> show issue.affectedClients
+ , Just $ "- **Service**: " <> fromMaybe "" issue.service
+ , Just $ "- **Affected Requests**: 0"
+ , Just $ "- **Affected Clients**: 0"
, Just $ "- **Recommended Action**: " <> issue.recommendedAction
, errM >>= \err ->
Just
@@ -530,6 +831,14 @@ aiChatPostH pid issueId form
Just issue -> do
errorM <- case issue.issueType of
Issues.RuntimeException -> Anomalies.errorByHash pid issue.endpointHash
+ Issues.ErrorEscalating ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.ErrorEscalatingData) -> Anomalies.errorByHash pid d.errorHash
+ _ -> pure Nothing
+ Issues.ErrorRegressed ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.ErrorRegressedData) -> Anomalies.errorByHash pid d.errorHash
+ _ -> pure Nothing
_ -> pure Nothing
(traceDataM, spans) <- case errorM of
@@ -545,7 +854,23 @@ aiChatPostH pid issueId form
pure (Just trData, V.fromList spanRecs)
Nothing -> pure (Nothing, V.empty)
Nothing -> pure (Nothing, V.empty)
- Nothing -> pure (Nothing, V.empty)
+ Nothing ->
+ -- For EndpointLatencyDegradation, try to fetch trace from sampleTraceIds
+ case issue.issueType of
+ Issues.EndpointLatencyDegradation ->
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.EndpointLatencyDegradationData) ->
+ case V.headM d.sampleTraceIds of
+ Just tId -> do
+ trM <- Telemetry.getTraceDetails pid tId Nothing now
+ case trM of
+ Just trData -> do
+ spanRecs <- Telemetry.getSpanRecordsByTraceId pid trData.traceId (Just trData.traceStartTime) now
+ pure (Just trData, V.fromList spanRecs)
+ Nothing -> pure (Nothing, V.empty)
+ Nothing -> pure (Nothing, V.empty)
+ _ -> pure (Nothing, V.empty)
+ _ -> pure (Nothing, V.empty)
let context = buildAIContext issue errorM traceDataM spans
anomalyContext = unlines [anomalySystemPrompt, "", "--- ISSUE CONTEXT ---", context]
@@ -588,6 +913,48 @@ aiChatHistoryGetH pid issueId = do
addRespHeaders $ aiChatHistoryView_ pid messages
+-- =============================================================================
+-- Error-specific action handlers
+-- =============================================================================
+
+errorActionPostH :: Projects.ProjectId -> Text -> Text -> ATAuthCtx (RespHeaders (Html ()))
+errorActionPostH pid errorHash action =
+ case action of
+ "resolve" -> do
+ _ <- Errors.updateErrorStateByProjectAndHash pid errorHash Errors.ESResolved
+ addSuccessToast "Error resolved" Nothing
+ addRespHeaders $ errorResolveButton_ pid errorHash True
+ "unresolve" -> do
+ _ <- Errors.updateErrorStateByProjectAndHash pid errorHash Errors.ESOngoing
+ addSuccessToast "Error unresolved" Nothing
+ addRespHeaders $ errorResolveButton_ pid errorHash False
+ _ -> do
+ addErrorToast "Unknown action" Nothing
+ addRespHeaders pass
+
+
+-- | Assign an error to a user
+errorAssignPostH :: Projects.ProjectId -> Text -> ErrorAssignForm -> ATAuthCtx (RespHeaders (Html ()))
+errorAssignPostH pid errorHash form =
+ withError pid errorHash "Error assigned" (`Errors.assignError` form.assigneeId)
+ $ errorAssignButton_ pid errorHash (Just form.assigneeId)
+
+
+-- | Helper for error actions
+withError :: Projects.ProjectId -> Text -> Text -> (Errors.ErrorId -> ATAuthCtx Int64) -> Html () -> ATAuthCtx (RespHeaders (Html ()))
+withError pid errorHash successMsg action responseHtml = do
+ _ <- Sessions.sessionAndProject pid
+ errorM <- Errors.getErrorByHash pid errorHash
+ case errorM of
+ Nothing -> do
+ addErrorToast "Error not found" Nothing
+ addRespHeaders pass
+ Just err -> do
+ _ <- action err.id
+ addSuccessToast successMsg Nothing
+ addRespHeaders responseHtml
+
+
-- | Render a single chat response (user question + AI answer)
aiChatResponse_ :: Projects.ProjectId -> Text -> Text -> Maybe [Widget.Widget] -> Html ()
aiChatResponse_ pid userQuery explanation widgetsM =
@@ -677,19 +1044,7 @@ anomalyAIChat_ pid issueId = do
$ toHtml txt
-anomalyListGetH
- :: Projects.ProjectId
- -> Maybe Text
- -> Maybe Text
- -> Maybe Text
- -> Maybe Text
- -> Maybe Text
- -> Maybe Text
- -> Maybe Text
- -> Maybe Endpoints.EndpointId
- -> Maybe Text
- -> Maybe Text
- -> ATAuthCtx (RespHeaders AnomalyListGet)
+anomalyListGetH :: Projects.ProjectId -> Maybe Text -> Maybe Text -> Maybe Text -> Maybe Text -> Maybe Text -> Maybe Text -> Maybe Text -> Maybe Endpoints.EndpointId -> Maybe Text -> Maybe Text -> ATAuthCtx (RespHeaders AnomalyListGet)
anomalyListGetH pid layoutM filterTM sortM timeFilter pageM perPageM loadM endpointM hxRequestM hxBoostedM = do
(sess, project) <- Sessions.sessionAndProject pid
appCtx <- ask @AuthContext
@@ -925,47 +1280,46 @@ renderIssueMainCol pid (IssueVM hideByDefault isWidget currTime timeFilter issue
-- Metadata row (method, endpoint, service, time)
div_ [class_ "flex items-center gap-4 text-sm text-textWeak mb-3 flex-wrap"] do
-- Method and endpoint (for API changes)
- when (issue.issueType == Issues.APIChange) do
- case AE.fromJSON (getAeson issue.issueData) of
- AE.Success (apiData :: Issues.APIChangeData) -> do
- div_ [class_ "flex items-center gap-2"] do
- span_ [class_ $ "badge " <> methodFillColor apiData.endpointMethod] $ toHtml apiData.endpointMethod
- span_ [class_ "monospace bg-fillWeak px-2 py-1 rounded text-xs text-textStrong"] $ toHtml apiData.endpointPath
- _ -> pass
+ case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: IssueEndpointInfo) -> do
+ div_ [class_ "flex items-center gap-2"] do
+ span_ [class_ $ "badge " <> methodFillColor d.endpointMethod] $ toHtml d.endpointMethod
+ span_ [class_ "monospace bg-fillWeak px-2 py-1 rounded text-xs text-textStrong"] $ toHtml d.endpointPath
+ _ -> pass
-- Service badge
span_ [class_ "flex items-center gap-1"] do
div_ [class_ "w-3 h-3 bg-fillYellow rounded-sm"] ""
- span_ [class_ "text-textStrong"] $ toHtml issue.service
+ span_ [class_ "text-textStrong"] $ toHtml $ fromMaybe "" issue.service
-- Time since
span_ [class_ "text-textWeak"] $ toHtml timeSinceString
- -- Statistics row (only for API changes)
- when (issue.issueType == Issues.APIChange) do
- let allChanges = getAeson issue.requestPayloads <> getAeson issue.responsePayloads :: [Anomalies.PayloadChange]
- countChange (!b, !i, !t) c = case c.changeType of
- Anomalies.Breaking -> (b + 1, i, t + 1)
- Anomalies.Incremental -> (b, i + 1, t + 1)
- _ -> (b, i, t + 1)
- (breakingChanges, incrementalChanges, totalChanges) = foldl' countChange (0, 0, 0) allChanges
- div_ [class_ "flex items-center gap-4 text-sm mb-4 p-3 bg-fillWeak rounded-lg"] do
- span_ [class_ "text-textWeak"] do
- strong_ [class_ "text-textStrong"] $ toHtml $ show totalChanges
- " total changes"
- div_ [class_ "w-px h-4 bg-strokeWeak"] ""
- span_ [class_ "text-textWeak"] do
- strong_ [class_ "text-fillError-strong"] $ toHtml $ show breakingChanges
- " breaking"
- when (breakingChanges > 0 && totalChanges > 0) $ span_ [class_ "text-xs ml-1 bg-fillError-weak text-fillError-strong px-1.5 py-0.5 rounded"] $ toHtml $ show (round (fromIntegral breakingChanges / fromIntegral totalChanges * 100 :: Float) :: Int) <> "%"
- div_ [class_ "w-px h-4 bg-strokeWeak"] ""
- span_ [class_ "text-textWeak"] do
- strong_ [class_ "text-fillSuccess-strong"] $ toHtml $ show incrementalChanges
- " incremental"
- div_ [class_ "w-px h-4 bg-strokeWeak"] ""
- span_ [class_ "text-textWeak"] do
- strong_ [class_ "text-textBrand"] $ toHtml $ show totalChanges
- " payloads affected"
+ -- Statistics row (only for API changes)
+ when (issue.issueType `elem` [Issues.NewShape]) do
+ let allChanges = getAeson issue.requestPayloads <> getAeson issue.responsePayloads :: [Anomalies.PayloadChange]
+ countChange (!b, !i, !t) c = case c.changeType of
+ Anomalies.Breaking -> (b + 1, i, t + 1)
+ Anomalies.Incremental -> (b, i + 1, t + 1)
+ _ -> (b, i, t + 1)
+ (breakingChanges, incrementalChanges, totalChanges) = foldl' countChange (0, 0, 0) allChanges
+ div_ [class_ "flex items-center gap-4 text-sm mb-4 p-3 bg-fillWeak rounded-lg"] do
+ span_ [class_ "text-textWeak"] do
+ strong_ [class_ "text-textStrong"] $ toHtml $ show totalChanges
+ " total changes"
+ div_ [class_ "w-px h-4 bg-strokeWeak"] ""
+ span_ [class_ "text-textWeak"] do
+ strong_ [class_ "text-fillError-strong"] $ toHtml $ show breakingChanges
+ " breaking"
+ when (breakingChanges > 0 && totalChanges > 0) $ span_ [class_ "text-xs ml-1 bg-fillError-weak text-fillError-strong px-1.5 py-0.5 rounded"] $ toHtml $ show (round (fromIntegral breakingChanges / fromIntegral totalChanges * 100 :: Float) :: Int) <> "%"
+ div_ [class_ "w-px h-4 bg-strokeWeak"] ""
+ span_ [class_ "text-textWeak"] do
+ strong_ [class_ "text-fillSuccess-strong"] $ toHtml $ show incrementalChanges
+ " incremental"
+ div_ [class_ "w-px h-4 bg-strokeWeak"] ""
+ span_ [class_ "text-textWeak"] do
+ strong_ [class_ "text-textBrand"] $ toHtml $ show totalChanges
+ " payloads affected"
- -- Stack trace for runtime exceptions or Query for alerts
+ -- Issue-specific details
case issue.issueType of
Issues.RuntimeException -> case AE.fromJSON (getAeson issue.issueData) of
AE.Success (exceptionData :: Issues.RuntimeExceptionData) ->
@@ -982,32 +1336,140 @@ renderIssueMainCol pid (IssueVM hideByDefault isWidget currTime timeFilter issue
span_ [class_ "text-sm text-textWeak mb-2 block font-medium"] "Query:"
div_ [class_ "bg-fillInformation-weak border border-strokeInformation-weak rounded-lg p-3 text-sm monospace text-fillInformation-strong max-w-2xl overflow-x-auto"] $ toHtml alertData.queryExpression
_ -> pass
- _ -> pass
+ Issues.NewEndpoint -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.NewEndpointData) ->
+ let shapeCount = V.length d.initialShapes
+ in div_ [class_ "mb-4 p-3 bg-fillInformation-weak border border-strokeInformation-weak rounded-lg"] do
+ div_ [class_ "flex items-center justify-between"] do
+ div_ [class_ "flex items-center gap-3"] do
+ div_ [class_ "flex items-center gap-1.5 text-sm"] do
+ faSprite_ "square-dashed" "regular" "h-3.5 w-3.5 text-fillInformation-strong"
+ span_ [class_ "font-medium text-fillInformation-strong"] $ toHtml $ show shapeCount <> " shape" <> (if shapeCount == 1 then "" else "s")
+ div_ [class_ "flex items-center gap-1.5 text-sm text-textWeak"] do
+ faSprite_ "server" "regular" "h-3 w-3"
+ span_ [] $ toHtml d.endpointHost
+ _ -> pass
+ Issues.NewShape -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.NewShapeData) -> do
+ div_ [class_ "mb-4"] do
+ div_ [class_ "flex items-center gap-2 text-sm mb-2"] do
+ span_ [class_ "font-medium text-textStrong"] $ toHtml d.endpointMethod
+ span_ [class_ "text-textWeak"] $ toHtml d.endpointPath
+ span_ [class_ "text-xs px-2 py-0.5 rounded bg-fillWeak"] $ toHtml $ "Status " <> show d.statusCode
+ unless (V.null d.newFields) do
+ div_ [class_ "text-xs text-textWeak"] $ toHtml $ "+" <> show (V.length d.newFields) <> " new fields"
+ unless (V.null d.deletedFields) do
+ div_ [class_ "text-xs text-textError"] $ toHtml $ "-" <> show (V.length d.deletedFields) <> " deleted fields"
+ _ -> pass
+ Issues.FieldChange -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.FieldChangeData) ->
+ div_ [class_ "mb-4 p-3 bg-fillWeak border border-strokeWeak rounded-lg"] do
+ div_ [class_ "text-sm"] do
+ span_ [class_ "text-textWeak"] "Field: "
+ span_ [class_ "font-mono text-textStrong"] $ toHtml d.keyPath
+ div_ [class_ "text-xs text-textWeak mt-1"] $ toHtml $ d.changeType <> " on " <> d.endpointMethod <> " " <> d.endpointPath
+ _ -> pass
+ Issues.LogPattern -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.LogPatternData) ->
+ div_ [class_ "border border-strokeWeak rounded-lg mb-4"] do
+ label_ [class_ "text-sm text-textWeak font-semibold rounded-lg p-2 flex gap-2 items-center cursor-pointer"] do
+ toHtml $ fromMaybe "LOG" d.logLevel <> " pattern (" <> show d.occurrenceCount <> " occurrences)"
+ div_ [class_ "bg-fillWeak p-4 overflow-x-scroll group-has-[.lp-input:checked]/lp:block text-sm monospace text-textStrong"] $ pre_ [class_ "whitespace-pre-wrap"] $ toHtml d.logPattern
+ _ -> pass
+ Issues.ErrorEscalating -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.ErrorEscalatingData) ->
+ div_ [class_ "mb-4 p-3 bg-fillError-weak border border-strokeError-weak rounded-lg"] do
+ div_ [class_ "text-sm text-fillError-strong font-medium"] $ toHtml d.errorType
+ div_ [class_ "text-xs text-textWeak mt-1"] $ toHtml $ show d.escalationRate <> "x increase in " <> d.escalationWindow
+ _ -> pass
+ Issues.ErrorRegressed -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.ErrorRegressedData) ->
+ div_ [class_ "mb-4 p-3 bg-fillError-weak border border-strokeError-weak rounded-lg"] do
+ div_ [class_ "text-sm text-fillError-strong font-medium"] $ toHtml d.errorType
+ div_ [class_ "text-xs text-textWeak mt-1"] $ toHtml $ "Regressed after " <> show (d.quietPeriodMinutes `div` 60) <> " hours quiet"
+ _ -> pass
+ Issues.LogPatternRateChange -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.LogPatternRateChangeData) ->
+ div_ [class_ "border border-strokeWeak rounded-lg group/lpr mb-4"] do
+ label_ [class_ "text-sm text-textWeak font-semibold rounded-lg p-2 flex gap-2 items-center cursor-pointer"] do
+ faSprite_ "chevron-right" "regular" "h-3 w-3 group-has-[.lpr-input:checked]/lpr:rotate-90"
+ toHtml $ "Rate " <> d.changeDirection <> " (" <> show (round d.changePercent :: Int) <> "%)"
+ input_ [class_ "lpr-input w-0 h-0 opacity-0", type_ "checkbox"]
+ div_ [class_ "bg-fillWeak p-4 overflow-x-scroll hidden group-has-[.lpr-input:checked]/lpr:block text-sm monospace text-textStrong"] $ pre_ [class_ "whitespace-pre-wrap"] $ toHtml d.logPattern
+ _ -> pass
+ Issues.EndpointLatencyDegradation -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.EndpointLatencyDegradationData) ->
+ let cards =
+ [ MetricCard "Percentile" "chart-line" (toHtml d.percentile) CardNeutral Nothing
+ , MetricCard "Baseline" "clock" (toHtml $ getDurationNSMS (round d.baselineLatencyMs)) CardNeutral Nothing
+ , MetricCard "Current" "chart-line" (toHtml $ getDurationNSMS (round d.currentLatencyMs)) CardHighlight Nothing
+ , MetricCard "Change" "circle-exclamation" (toHtml $ "+" <> show (round d.degradationPercent :: Int) <> "%") CardWarning (Just "arrow-up")
+ ]
+ in anomalyMetricsSection cards CardWarning "Performance Degradation Detected" issue.recommendedAction
+ _ -> pass
+ Issues.EndpointErrorRateSpike -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.EndpointErrorRateSpikeData) ->
+ let cards =
+ [ MetricCard "Error Rate" "percent" (toHtml $ show (round (d.currentErrorRate * 100) :: Int) <> "%") CardNeutral Nothing
+ , MetricCard "Baseline" "clock" (toHtml $ show (round (d.baselineErrorRate * 100) :: Int) <> "%") CardNeutral Nothing
+ , MetricCard "Errors" "hashtag" (toHtml $ show d.errorCount <> "/" <> show d.totalRequests) CardNeutral Nothing
+ , MetricCard "Spike" "circle-exclamation" (toHtml $ "+" <> show (round d.spikePercent :: Int) <> "%") CardError (Just "arrow-up")
+ ]
+ in anomalyMetricsSection cards CardError "Error Rate Spike Detected" issue.recommendedAction
+ _ -> pass
+ Issues.EndpointVolumeRateChange -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.EndpointVolumeRateChangeData) ->
+ let isDrop = d.changeDirection == "drop"
+ cardStyle = if isDrop then CardWarning else CardInfo
+ arrowIcon = if isDrop then "arrow-down" else "arrow-up"
+ changeSign = if isDrop then "" else "+"
+ alertTitle = if isDrop then "Traffic Drop Detected" else "Traffic Surge Detected"
+ cards =
+ [ MetricCard "Direction" arrowIcon (span_ [class_ "capitalize"] $ toHtml d.changeDirection) cardStyle Nothing
+ , MetricCard "Current" "gauge-high" (toHtml $ show (round d.currentRatePerHour :: Int) <> "/hr") CardNeutral Nothing
+ , MetricCard "Baseline" "clock" (toHtml $ show (round d.baselineRatePerHour :: Int) <> "/hr") CardNeutral Nothing
+ , MetricCard "Change" "circle-exclamation" (toHtml $ changeSign <> show (round (abs d.changePercent) :: Int) <> "%") cardStyle (Just arrowIcon)
+ ]
+ in anomalyMetricsSection cards cardStyle alertTitle issue.recommendedAction
+ _ -> pass
- -- Recommended action
- div_ [class_ "border-l-4 border-strokeBrand pl-4 mb-4"] $ p_ [class_ "text-sm text-textStrong leading-relaxed"] $ toHtml issue.recommendedAction
-
- -- Collapsible payload changes (only for API changes)
- when (issue.issueType == Issues.APIChange) $ details_ [class_ "group mb-4"] do
- summary_ [class_ "inline-flex items-center cursor-pointer whitespace-nowrap text-sm font-medium transition-all rounded-md gap-1.5 text-textBrand hover:text-textBrand/80 list-none"] do
- faSprite_ "chevron-right" "regular" "h-4 w-4 mr-1 transition-transform group-open:rotate-90"
- "View detailed payload changes"
- div_ [class_ "mt-4 border border-strokeWeak rounded-lg overflow-hidden bg-bgRaised"] $ renderPayloadChanges issue
+ -- Recommended action (only show for issue types that don't have built-in alert boxes)
+ unless (issue.issueType `elem` [Issues.EndpointLatencyDegradation, Issues.EndpointErrorRateSpike, Issues.EndpointVolumeRateChange]) do
+ div_ [class_ "border-l-4 border-strokeBrand pl-4 mb-4"] $ p_ [class_ "text-sm text-textStrong leading-relaxed"] $ toHtml issue.recommendedAction
-- Action buttons
+ let logsQuery = case issue.issueType of
+ Issues.NewEndpoint -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.NewEndpointData) -> Just $ "attributes.http.request.method==\"" <> d.endpointMethod <> "\" AND attributes.http.route==\"" <> d.endpointPath <> "\""
+ _ -> Nothing
+ Issues.NewShape -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.NewShapeData) -> Just $ "attributes.http.request.method==\"" <> d.endpointMethod <> "\" AND attributes.http.route==\"" <> d.endpointPath <> "\""
+ _ -> Nothing
+ Issues.LogPattern -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.LogPatternData) -> Just $ "log_pattern=\"" <> d.logPattern <> "\""
+ _ -> Nothing
+ Issues.LogPatternRateChange -> case AE.fromJSON (getAeson issue.issueData) of
+ AE.Success (d :: Issues.LogPatternRateChangeData) -> Just $ "log_pattern=\"" <> d.logPattern <> "\""
+ _ -> Nothing
+ _ -> Nothing
+ logsUrl = (\q -> "/p/" <> pid.toText <> "/log_explorer?query=" <> toUriStr q) <$> logsQuery
+
div_ [class_ "flex items-center gap-3 mt-4 pt-4 border-t border-strokeWeak"] do
- button_ [class_ "inline-flex items-center justify-center whitespace-nowrap text-sm font-medium transition-all h-8 rounded-md gap-1.5 px-3 text-textBrand hover:text-textBrand/80 hover:bg-fillBrand-weak"] do
- faSprite_ "eye" "regular" "w-4 h-4"
- span_ [class_ "leading-none"] "view related logs"
- button_ [class_ "inline-flex items-center justify-center whitespace-nowrap text-sm font-medium transition-all h-8 rounded-md gap-1.5 px-3 border bg-background hover:text-accent-foreground text-textBrand border-strokeBrand-strong hover:bg-fillBrand-weak"] do
- faSprite_ "code" "regular" "w-4 h-4"
- span_ [class_ "leading-none"] "View Full Schema"
+ whenJust logsUrl \url ->
+ a_ [href_ url, class_ "inline-flex items-center justify-center whitespace-nowrap text-sm font-medium transition-all h-8 rounded-md gap-1.5 px-3 text-textBrand hover:text-textBrand/80 hover:bg-fillBrand-weak"] do
+ faSprite_ "eye" "regular" "w-4 h-4"
+ span_ [class_ "leading-none"] "View related logs"
+ -- View Full Schema button (only for schema-related issue types)
+ when (issue.issueType `elem` [Issues.NewEndpoint, Issues.NewShape, Issues.FieldChange]) do
+ button_ [class_ "inline-flex items-center justify-center whitespace-nowrap text-sm font-medium transition-all h-8 rounded-md gap-1.5 px-3 border bg-background hover:text-accent-foreground text-textBrand border-strokeBrand-strong hover:bg-fillBrand-weak"] do
+ faSprite_ "code" "regular" "w-4 h-4"
+ span_ [class_ "leading-none"] "View Full Schema"
-- Acknowledge button
let isAcknowledged = isJust issue.acknowledgedAt
let acknowledgeEndpoint = "/p/" <> issue.projectId.toText <> "/anomalies/" <> Issues.issueIdText issue.id <> if isAcknowledged then "/unacknowledge" else "/acknowledge"
button_
- [ class_ $ "inline-flex items-center justify-center whitespace-nowrap text-sm font-medium transition-all h-8 rounded-md gap-1.5 px-3 " <> if isAcknowledged then "bg-fillSuccess-weak text-fillSuccess-strong border border-strokeSuccess-weak hover:bg-fillSuccess-weak/80" else "bg-fillPrimary text-textInverse-strong hover:bg-fillPrimary/90"
- , hxGet_ acknowledgeEndpoint
+ [ class_ ("inline-flex items-center justify-center whitespace-nowrap text-sm font-medium transition-all h-8 rounded-md gap-1.5 px-3 " <> if isAcknowledged then "bg-fillSuccess-weak text-fillSuccess-strong border border-strokeSuccess-weak hover:bg-fillSuccess-weak/80" else "bg-fillBrand-strong text-white hover:bg-fillBrand-weak")
+ , hxPost_ acknowledgeEndpoint
, hxSwap_ "outerHTML"
, hxTarget_ "closest .itemsListItem"
]
@@ -1028,60 +1490,6 @@ renderIssueMainCol pid (IssueVM hideByDefault isWidget currTime timeFilter issue
span_ [class_ "leading-none"] $ if isArchived then "Unarchive" else "Archive"
--- Render payload changes section
-renderPayloadChanges :: Issues.IssueL -> Html ()
-renderPayloadChanges issue =
- when (issue.issueType == Issues.APIChange) do
- let requestChanges = getAeson issue.requestPayloads :: [Anomalies.PayloadChange]
- let responseChanges = getAeson issue.responsePayloads :: [Anomalies.PayloadChange]
-
- when (not (null requestChanges) || not (null responseChanges)) do
- div_ [class_ "border border-strokeWeak rounded-lg overflow-hidden bg-bgRaised group/payloadtabs"] do
- div_ [class_ "flex flex-col gap-2"] do
- -- Tab navigation using radio buttons
- div_ [role_ "tablist", Aria.orientation_ "horizontal", class_ "text-muted-foreground h-9 items-center justify-center rounded-xl p-[3px] w-full grid grid-cols-2 bg-fillWeak"] do
- -- Response tab (default active)
- label_
- [ role_ "tab"
- , class_ "h-[calc(100%-1px)] flex-1 justify-center rounded-xl border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap transition-all flex items-center gap-2 cursor-pointer has-[:checked]:bg-bgRaised has-[:checked]:text-textStrong bg-transparent text-textWeak"
- ]
- do
- input_ [type_ "radio", name_ ("payload-tab-" <> Issues.issueIdText issue.id), class_ "hidden payload-tab-response", checked_]
- faSprite_ "arrow-right" "regular" "w-4 h-4"
- span_ [] $ "Response Payloads (" <> show (length responseChanges) <> ")"
-
- -- Request tab
- label_
- [ role_ "tab"
- , class_ "h-[calc(100%-1px)] flex-1 justify-center rounded-xl border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap transition-all flex items-center gap-2 cursor-pointer has-[:checked]:bg-bgRaised has-[:checked]:text-textStrong bg-transparent text-textWeak"
- ]
- do
- input_ [type_ "radio", name_ ("payload-tab-" <> Issues.issueIdText issue.id), class_ "hidden payload-tab-request"]
- faSprite_ "arrow-right" "regular" "w-4 h-4 rotate-180"
- span_ [] $ "Request Payloads (" <> show (length requestChanges) <> ")"
-
- -- Tab panels
- -- Response panel (visible when response tab is selected)
- div_
- [ role_ "tabpanel"
- , class_ "flex-1 outline-none p-4 space-y-4 hidden group-has-[.payload-tab-response:checked]/payloadtabs:block"
- ]
- do
- if null responseChanges
- then div_ [class_ "text-center py-8 text-textWeak"] "No response payload changes"
- else forM_ responseChanges (renderPayloadChange True)
-
- -- Request panel (visible when request tab is selected)
- div_
- [ role_ "tabpanel"
- , class_ "flex-1 outline-none p-4 space-y-4 hidden group-has-[.payload-tab-request:checked]/payloadtabs:block"
- ]
- do
- if null requestChanges
- then div_ [class_ "text-center py-8 text-textWeak"] "No request payload changes"
- else forM_ requestChanges (renderPayloadChange False)
-
-
-- Render individual payload change
renderPayloadChange :: Bool -> Anomalies.PayloadChange -> Html ()
renderPayloadChange isResponse change =
@@ -1213,7 +1621,9 @@ anomalyAcknowledgeButton :: Projects.ProjectId -> Issues.IssueId -> Bool -> Text
anomalyAcknowledgeButton pid aid acked host = do
let acknowledgeAnomalyEndpoint = "/p/" <> pid.toText <> "/anomalies/" <> Issues.issueIdText aid <> if acked then "/unacknowledge" else "/acknowledge?host=" <> host
a_
- [ class_ $ "btn btn-sm gap-1.5 " <> if acked then "bg-fillSuccess-weak text-textSuccess border-strokeSuccess-weak" else "btn-primary"
+ [ class_
+ $ "inline-flex items-center gap-2 cursor-pointer py-2 px-3 rounded-xl "
+ <> (if acked then "bg-fillSuccess-weak text-textSuccess" else "btn-primary")
, term "data-tippy-content" "acknowledge issue"
, hxGet_ acknowledgeAnomalyEndpoint
, hxSwap_ "outerHTML"
@@ -1227,7 +1637,9 @@ anomalyArchiveButton :: Projects.ProjectId -> Issues.IssueId -> Bool -> Html ()
anomalyArchiveButton pid aid archived = do
let archiveAnomalyEndpoint = "/p/" <> pid.toText <> "/anomalies/" <> Issues.issueIdText aid <> if archived then "/unarchive" else "/archive"
a_
- [ class_ $ "btn btn-sm btn-ghost gap-1.5 " <> if archived then "bg-fillWarning-weak text-textWarning border-strokeWarning-weak" else ""
+ [ class_
+ $ "inline-flex items-center gap-2 cursor-pointer py-2 px-3 rounded-xl "
+ <> (if archived then " bg-fillSuccess-weak text-textSuccess" else "btn-primary")
, term "data-tippy-content" $ if archived then "unarchive" else "archive"
, hxGet_ archiveAnomalyEndpoint
, hxSwap_ "outerHTML"
@@ -1237,13 +1649,64 @@ anomalyArchiveButton pid aid archived = do
if archived then "Unarchive" else "Archive"
+-- | Resolve/Unresolve error button
+errorResolveButton_ :: Projects.ProjectId -> Text -> Bool -> Html ()
+errorResolveButton_ pid errorHash isResolved = do
+ let endpoint = "/p/" <> pid.toText <> "/anomalies/errors/" <> errorHash <> "/actions/" <> if isResolved then "unresolve" else "resolve"
+ a_
+ [ class_
+ $ "inline-flex items-center gap-2 cursor-pointer py-2 px-3 rounded-xl "
+ <> (if isResolved then "bg-fillSuccess-weak text-textSuccess" else "btn-primary")
+ , term "data-tippy-content" $ if isResolved then "Reopen error" else "Resolve error"
+ , hxPost_ endpoint
+ , hxSwap_ "outerHTML"
+ ]
+ do
+ faSprite_ (if isResolved then "rotate-left" else "circle-check") "regular" "w-4 h-4"
+ if isResolved then "Resolved" else "Resolve"
+
+
+-- | Assign error button (dropdown)
+errorAssignButton_ :: Projects.ProjectId -> Text -> Maybe UserId -> Html ()
+errorAssignButton_ pid errorHash assigneeM = do
+ let isAssigned = isJust assigneeM
+ div_ [class_ "relative group/assign"] do
+ button_
+ [ class_
+ $ "inline-flex items-center gap-2 cursor-pointer py-2 px-3 rounded-xl "
+ <> (if isAssigned then "bg-fillBrand-weak text-textBrand" else "btn-primary")
+ , type_ "button"
+ ]
+ do
+ faSprite_ "user-plus" "regular" "w-4 h-4"
+ if isAssigned then "Assigned" else "Assign"
+ -- Dropdown will be populated with team members via HTMX if needed
+ div_
+ [ class_ "absolute right-0 top-full mt-1 bg-fill rounded-lg shadow-lg border border-strokeWeak hidden group-hover/assign:block min-w-[200px] z-10"
+ , id_ $ "assign-dropdown-" <> errorHash
+ ]
+ do
+ div_ [class_ "p-2 text-xs text-textWeak"] "Click to see assignees"
+
+
issueTypeBadge :: Issues.IssueType -> Bool -> Html ()
issueTypeBadge issueType critical = badge cls icon txt
where
(cls, icon, txt) = case issueType of
- Issues.RuntimeException -> ("bg-fillError-strong", "triangle-alert", "ERROR")
+ Issues.RuntimeException -> ("bg-fillError-strong", "triangle-alert", "Error")
Issues.QueryAlert -> ("bg-fillWarning-strong", "zap", "ALERT")
- Issues.APIChange
- | critical -> ("bg-fillError-strong", "exclamation-triangle", "BREAKING")
- | otherwise -> ("bg-fillInformation-strong", "info", "Incremental")
+ Issues.NewEndpoint -> ("bg-fillInformation-strong", "plus-circle", "Info")
+ Issues.NewShape
+ | critical -> ("bg-fillError-strong", "exclamation-triangle", "Breaking")
+ | otherwise -> ("bg-fillInformation-strong", "shapes", "Info")
+ Issues.FieldChange
+ | critical -> ("bg-fillError-strong", "exclamation-triangle", "Breaking")
+ | otherwise -> ("bg-fillWarning-strong", "pen-to-square", "Incremental")
+ Issues.LogPattern -> ("bg-fillInformation-strong", "file-lines", "Info")
+ Issues.ErrorEscalating -> ("bg-fillError-strong", "arrow-trend-up", "E")
+ Issues.ErrorRegressed -> ("bg-fillError-strong", "rotate-left", "Alert")
+ Issues.LogPatternRateChange -> ("bg-fillWarning-strong", "chart-line", "RATE CHANGE")
+ Issues.EndpointLatencyDegradation -> ("bg-fillWarning-strong", "clock", "Alert")
+ Issues.EndpointErrorRateSpike -> ("bg-fillError-strong", "chart-line-up", "Alert")
+ Issues.EndpointVolumeRateChange -> ("bg-fillWarning-strong", "arrows-up-down", "Alert")
badge c i t = span_ [class_ $ "badge " <> c] do faSprite_ i "regular" "w-3 h-3"; t
diff --git a/src/Pkg/DeriveUtils.hs b/src/Pkg/DeriveUtils.hs
index 277d34ca1..3b3f6f103 100644
--- a/src/Pkg/DeriveUtils.hs
+++ b/src/Pkg/DeriveUtils.hs
@@ -1,5 +1,6 @@
module Pkg.DeriveUtils (
AesonText (..),
+ BaselineState (..),
PGTextArray (..),
UUIDId (..),
idToText,
@@ -9,7 +10,7 @@ module Pkg.DeriveUtils (
) where
import Data.Aeson qualified as AE
-import Data.Default (Default)
+import Data.Default (Default (..))
import Data.Default.Instances ()
import Data.UUID qualified as UUID
import Data.Vector qualified as V
@@ -17,9 +18,11 @@ import Database.PostgreSQL.Simple (FromRow, ResultError (ConversionFailed), ToRo
import Database.PostgreSQL.Simple.FromField (Conversion (..), FromField (..), fromField, returnError)
import Database.PostgreSQL.Simple.Newtypes (Aeson (..))
import Database.PostgreSQL.Simple.ToField (ToField (..))
+import Deriving.Aeson qualified as DAE
import GHC.Records (HasField (getField))
import GHC.TypeLits (Symbol)
import Language.Haskell.TH.Syntax qualified as THS
+import Pkg.DBUtils (WrappedEnumSC (..))
import Relude
import Web.HttpApiData (FromHttpApiData)
@@ -97,3 +100,17 @@ idToText = UUID.toText . unUUIDId
-- | Parse Text to a UUID-based ID
idFromText :: Text -> Maybe (UUIDId name)
idFromText = fmap UUIDId . UUID.fromText
+
+
+-- | Baseline state for anomaly detection.
+-- Baselines start in 'Learning' state until enough data is collected,
+-- then transition to 'Established' once statistically significant.
+data BaselineState = BSLearning | BSEstablished
+ deriving stock (Eq, Generic, Read, Show)
+ deriving anyclass (NFData)
+ deriving (AE.FromJSON, AE.ToJSON) via DAE.CustomJSON '[DAE.ConstructorTagModifier '[DAE.StripPrefix "BS", DAE.CamelToSnake]] BaselineState
+ deriving (FromField, ToField) via WrappedEnumSC "BS" BaselineState
+
+
+instance Default BaselineState where
+ def = BSLearning
diff --git a/src/Pkg/Drain.hs b/src/Pkg/Drain.hs
index cca3908b2..3a5cb9626 100644
--- a/src/Pkg/Drain.hs
+++ b/src/Pkg/Drain.hs
@@ -18,6 +18,7 @@ import RequestMessages (replaceAllFormats)
data LogGroup = LogGroup
{ template :: V.Vector Text
, templateStr :: Text
+ , exampleLog :: Text
, logIds :: V.Vector Text
, frequency :: Int
, firstSeen :: UTCTime
@@ -88,6 +89,7 @@ createLogGroup templateTokens templateString logId now =
{ template = templateTokens
, templateStr = templateString
, logIds = V.singleton logId
+ , exampleLog = templateString
, frequency = 1
, firstSeen = now
, lastSeen = now
@@ -105,9 +107,9 @@ calculateSimilarity tokens1 tokens2
in fromIntegral matches / fromIntegral total
-updateTreeWithLog :: DrainTree -> Int -> Text -> V.Vector Text -> Text -> Text -> UTCTime -> DrainTree
-updateTreeWithLog tree tokenCount firstToken tokensVec logId logContent now =
- let (updatedChildren, wasUpdated) = updateOrCreateLevelOne (children tree) tokenCount firstToken tokensVec logId logContent now (config tree)
+updateTreeWithLog :: DrainTree -> Int -> Text -> V.Vector Text -> Text -> Bool -> Text -> UTCTime -> DrainTree
+updateTreeWithLog tree tokenCount firstToken tokensVec logId isSampleLog logContent now =
+ let (updatedChildren, wasUpdated) = updateOrCreateLevelOne (children tree) tokenCount firstToken tokensVec logId isSampleLog logContent now (config tree)
newTotalLogs = totalLogs tree + 1
newTotalPatterns = if wasUpdated then totalPatterns tree else totalPatterns tree + 1
in tree
@@ -117,12 +119,12 @@ updateTreeWithLog tree tokenCount firstToken tokensVec logId logContent now =
}
-updateOrCreateLevelOne :: V.Vector DrainLevelOne -> Int -> Text -> V.Vector Text -> Text -> Text -> UTCTime -> DrainConfig -> (V.Vector DrainLevelOne, Bool)
-updateOrCreateLevelOne levelOnes targetCount firstToken tokensVec logId logContent now config =
+updateOrCreateLevelOne :: V.Vector DrainLevelOne -> Int -> Text -> V.Vector Text -> Text -> Bool -> Text -> UTCTime -> DrainConfig -> (V.Vector DrainLevelOne, Bool)
+updateOrCreateLevelOne levelOnes targetCount firstToken tokensVec logId isSampleLog logContent now config =
case V.findIndex (\level -> tokenCount level == targetCount) levelOnes of
Just index ->
let existingLevel = levelOnes V.! index
- (updatedChildren, wasUpdated) = updateOrCreateLevelTwo (nodes existingLevel) firstToken tokensVec logId logContent now config
+ (updatedChildren, wasUpdated) = updateOrCreateLevelTwo (nodes existingLevel) firstToken tokensVec logId isSampleLog logContent now config
updatedLevel = existingLevel{nodes = updatedChildren}
updatedLevelOnes = levelOnes V.// [(index, updatedLevel)]
in (updatedLevelOnes, wasUpdated)
@@ -134,12 +136,12 @@ updateOrCreateLevelOne levelOnes targetCount firstToken tokensVec logId logConte
in (updatedLevelOnes, False)
-updateOrCreateLevelTwo :: V.Vector DrainLevelTwo -> Text -> V.Vector Text -> Text -> Text -> UTCTime -> DrainConfig -> (V.Vector DrainLevelTwo, Bool)
-updateOrCreateLevelTwo levelTwos targetToken tokensVec logId logContent now config =
+updateOrCreateLevelTwo :: V.Vector DrainLevelTwo -> Text -> V.Vector Text -> Text -> Bool -> Text -> UTCTime -> DrainConfig -> (V.Vector DrainLevelTwo, Bool)
+updateOrCreateLevelTwo levelTwos targetToken tokensVec logId isSampleLog logContent now config =
case V.findIndex (\level -> firstToken level == targetToken) levelTwos of
Just index ->
let existingLevel = levelTwos V.! index
- (updatedLogGroups, wasUpdated) = updateOrCreateLogGroup (logGroups existingLevel) tokensVec logId logContent now config
+ (updatedLogGroups, wasUpdated) = updateOrCreateLogGroup (logGroups existingLevel) tokensVec logId isSampleLog logContent now config
updatedLevel = existingLevel{logGroups = updatedLogGroups}
updatedLevelTwos = levelTwos V.// [(index, updatedLevel)]
in (updatedLevelTwos, wasUpdated)
@@ -166,15 +168,15 @@ leastRecentlyUsedIndex logGroups =
& maybe 0 fst
-updateOrCreateLogGroup :: V.Vector LogGroup -> V.Vector Text -> Text -> Text -> UTCTime -> DrainConfig -> (V.Vector LogGroup, Bool)
-updateOrCreateLogGroup logGroups tokensVec logId logContent now config =
+updateOrCreateLogGroup :: V.Vector LogGroup -> V.Vector Text -> Text -> Bool -> Text -> UTCTime -> DrainConfig -> (V.Vector LogGroup, Bool)
+updateOrCreateLogGroup logGroups tokensVec logId isSampleLog logContent now config =
case findBestMatch logGroups tokensVec (similarityThreshold config) of
Just (index, bestGroup) ->
let updatedTemplate =
if V.length tokensVec == V.length (template bestGroup)
then mergeTemplates (template bestGroup) tokensVec (wildcardToken config)
else template bestGroup
- updatedGroup = updateLogGroupWithTemplate bestGroup updatedTemplate logId logContent now
+ updatedGroup = updateLogGroupWithTemplate bestGroup updatedTemplate logId isSampleLog logContent now
updatedGroups = logGroups V.// [(index, updatedGroup)]
in (updatedGroups, True)
Nothing ->
@@ -213,23 +215,24 @@ mergeTemplates template1 template2 wildcardToken =
-- Update log group with new template and log information
-updateLogGroupWithTemplate :: LogGroup -> V.Vector Text -> Text -> Text -> UTCTime -> LogGroup
-updateLogGroupWithTemplate group' newTemplate logId originalLog now =
+updateLogGroupWithTemplate :: LogGroup -> V.Vector Text -> Text -> Bool -> Text -> UTCTime -> LogGroup
+updateLogGroupWithTemplate group' newTemplate logId isSampleLog originalLog now =
group'
{ template = newTemplate
, templateStr = unwords $ V.toList newTemplate
+ , exampleLog = if isSampleLog then originalLog else exampleLog group'
, logIds = V.cons logId (logIds group')
, frequency = frequency group' + 1
, lastSeen = now
}
-getAllLogGroups :: DrainTree -> V.Vector (Text, V.Vector Text)
+getAllLogGroups :: DrainTree -> V.Vector (Text, Text, V.Vector Text)
getAllLogGroups tree =
let levelOnes = children tree
levelTwos = V.concatMap nodes levelOnes
allLogGroups = V.concatMap logGroups levelTwos
- in V.map (\grp -> (templateStr grp, logIds grp)) allLogGroups
+ in V.map (\grp -> (grp.exampleLog, templateStr grp, logIds grp)) allLogGroups
looksLikeJson :: T.Text -> Bool
diff --git a/src/Pkg/Mail.hs b/src/Pkg/Mail.hs
index d5cadefd7..81aab26c3 100644
--- a/src/Pkg/Mail.hs
+++ b/src/Pkg/Mail.hs
@@ -16,6 +16,7 @@ import Effectful (
)
import Effectful.Log (Log)
import Effectful.Reader.Static (Reader, ask)
+import Models.Apis.Errors qualified as Errors
import Models.Apis.RequestDumps qualified as RequestDumps
import Models.Apis.Slack (DiscordData (..), SlackData (..), getDiscordDataByProjectId, getProjectSlackData)
import Models.Projects.Projects qualified as Projects
@@ -43,8 +44,8 @@ sendSlackMessage pid message = do
data NotificationAlerts
- = EndpointAlert {project :: Text, endpoints :: V.Vector Text, endpointHash :: Text}
- | RuntimeErrorAlert {issueId :: Text, errorData :: RequestDumps.ATError}
+ = EndpointAlert {project :: Text, method :: Text, urlPath :: Text, endpointHash :: Text}
+ | RuntimeErrorAlert {issueId :: Text, errorData :: Errors.ATError}
| ShapeAlert
| ReportAlert
{ reportType :: Text
@@ -77,7 +78,7 @@ sendDiscordAlert alert pid pTitle channelIdM' = do
let projectUrl = appCtx.env.hostUrl <> "p/" <> pid.toText
case alert of
RuntimeErrorAlert{..} -> send $ discordErrorAlert errorData pTitle projectUrl
- EndpointAlert{..} -> send $ discordNewEndpointAlert project endpoints endpointHash projectUrl
+ EndpointAlert{..} -> send $ discordNewEndpointAlert project method urlPath endpointHash projectUrl
ShapeAlert -> pass
ReportAlert{..} -> send $ discordReportAlert reportType startTime endTime totalErrors totalEvents breakDown pTitle reportUrl allChartUrl errorChartUrl
MonitorsAlert{..} ->
@@ -104,7 +105,7 @@ sendSlackAlert alert pid pTitle channelM = do
let projectUrl = appCtx.env.hostUrl <> "p/" <> pid.toText
case alert of
RuntimeErrorAlert{..} -> sendAlert cid $ slackErrorAlert errorData pTitle cid projectUrl
- EndpointAlert{..} -> sendAlert cid $ slackNewEndpointsAlert project endpoints cid endpointHash projectUrl
+ EndpointAlert{..} -> sendAlert cid $ slackNewEndpointsAlert project method urlPath cid endpointHash projectUrl
ShapeAlert -> pass
ReportAlert{..} -> sendAlert cid $ slackReportAlert reportType startTime endTime totalErrors totalEvents breakDown pTitle cid reportUrl allChartUrl errorChartUrl
MonitorsAlert{..} ->
@@ -122,14 +123,14 @@ sendWhatsAppAlert alert pid pTitle tos = do
case alert of
RuntimeErrorAlert{..} -> do
let template = appCtx.config.whatsappErrorTemplate
- url = pid.toText <> "/anomalies/by_hash/" <> fromMaybe "" errorData.hash
+ url = pid.toText <> "/anomalies/by_hash/" <> errorData.hash
contentVars = AE.object ["1" AE..= ("*" <> pTitle <> "*"), "2" AE..= ("*" <> errorData.errorType <> "*"), "3" AE..= ("`" <> errorData.message <> "`"), "4" AE..= url]
sendAlert template contentVars
pass
EndpointAlert{..} -> do
let template = appCtx.config.whatsappEndpointTemplate
url = pid.toText <> "/anomalies/by_hash/" <> endpointHash
- contentVars = AE.object ["1" AE..= ("*" <> pTitle <> "*"), "2" AE..= T.intercalate "." ((\x -> "`" <> x <> "`") <$> V.toList endpoints), "3" AE..= url]
+ contentVars = AE.object ["1" AE..= ("*" <> pTitle <> "*"), "2" AE..= ("`" <> method <> " " <> urlPath <> "`"), "3" AE..= url]
sendAlert template contentVars
pass
ReportAlert{..} -> do
@@ -199,7 +200,7 @@ slackReportAlert reportType startTime endTime totalErrors totalEvents breakDown
sumr = V.take 10 $ V.map (\(name, errCount, evCount) -> AE.object ["type" AE..= "mrkdwn", "text" AE..= ("*" <> name <> ":* Errors-" <> toText (show errCount) <> ", Total-" <> toText (show evCount))]) breakDown
-slackErrorAlert :: RequestDumps.ATError -> Text -> Text -> Text -> AE.Value
+slackErrorAlert :: Errors.ATError -> Text -> Text -> Text -> AE.Value
slackErrorAlert err project channelId projectUrl =
AE.object
[ "blocks"
@@ -209,7 +210,7 @@ slackErrorAlert err project channelId projectUrl =
, AE.object ["type" AE..= "section", "text" AE..= AE.object ["type" AE..= "mrkdwn", "text" AE..= ("```" <> err.message <> "\n```")]]
, AE.object
[ "type" AE..= "context"
- , "elements" AE..= AE.Array (V.fromList $ AE.object ["type" AE..= "mrkdwn", "text" AE..= ("*Stack:* `" <> fromMaybe "" err.stack <> "`")] : [AE.object ["type" AE..= "mrkdwn", "text" AE..= ("*Endpoint:* " <> enp)]])
+ , "elements" AE..= AE.Array (V.fromList $ AE.object ["type" AE..= "mrkdwn", "text" AE..= ("*Stack:* `" <> fromMaybe "" err.runtime <> "`")] : [AE.object ["type" AE..= "mrkdwn", "text" AE..= ("*Endpoint:* " <> enp)]])
]
, AE.object
[ "type" AE..= "context"
@@ -245,16 +246,16 @@ slackErrorAlert err project channelId projectUrl =
firstSeen = toText $ formatTime defaultTimeLocale "%b %-e, %Y, %-l:%M:%S %p" err.when
-slackNewEndpointsAlert :: Text -> V.Vector Text -> Text -> Text -> Text -> AE.Value
-slackNewEndpointsAlert projectName endpoints channelId hash projectUrl =
+slackNewEndpointsAlert :: Text -> Text -> Text -> Text -> Text -> Text -> AE.Value
+slackNewEndpointsAlert projectName method urlPath channelId hash projectUrl =
AE.object
[ "blocks"
AE..= AE.Array
( V.fromList
- [ AE.object ["type" AE..= "section", "text" AE..= AE.object ["type" AE..= "mrkdwn", "text" AE..= ("<" <> targetUrl <> "|:large_blue_circle: New Endpoint(s) Detected>")]]
- , AE.object ["type" AE..= "section", "text" AE..= AE.object ["type" AE..= "mrkdwn", "text" AE..= ("We've detected *" <> toText (show $ length endpoints) <> "* new endpoint(s) in *" <> projectName <> "*.")]]
+ [ AE.object ["type" AE..= "section", "text" AE..= AE.object ["type" AE..= "mrkdwn", "text" AE..= ("<" <> targetUrl <> "|:large_blue_circle: New Endpoint Detected>")]]
+ , AE.object ["type" AE..= "section", "text" AE..= AE.object ["type" AE..= "mrkdwn", "text" AE..= ("We've detected a new endpoint in *" <> projectName <> "*.")]]
, AE.object ["type" AE..= "divider"]
- , AE.object ["type" AE..= "section", "text" AE..= AE.object ["type" AE..= "mrkdwn", "text" AE..= ("*Endpoints:*\n\n" <> enps)]]
+ , AE.object ["type" AE..= "section", "text" AE..= AE.object ["type" AE..= "mrkdwn", "text" AE..= ("*Endpoint:*\n\n" <> enps)]]
, AE.object
[ "type" AE..= "actions"
, "elements"
@@ -266,10 +267,9 @@ slackNewEndpointsAlert projectName endpoints channelId hash projectUrl =
]
where
targetUrl = projectUrl <> "/anomalies/by_hash/" <> hash
- enp = (\x -> "\"" <> x <> "\"") . T.dropWhile (/= ' ') <$> V.toList endpoints
- query = urlEncode True $ encodeUtf8 $ "attributes.http.route in (" <> T.intercalate "," enp <> ")"
+ query = urlEncode True $ encodeUtf8 $ "method=\"" <> method <> "\" AND attributes.http.route in (\"" <> urlPath <> "\")"
explorerUrl = projectUrl <> "/log_explorer?query=" <> decodeUtf8 query
- enps = T.intercalate "\n\n" $ (\x -> "`" <> x <> "`") <$> V.toList endpoints
+ enps = "`" <> method <> " " <> urlPath <> "`"
discordReportAlert :: Text -> Text -> Text -> Int -> Int -> V.Vector (Text, Int, Int) -> Text -> Text -> Text -> Text -> AE.Value
@@ -305,7 +305,7 @@ discordReportAlert reportType startTime endTime totalErrors totalEvents breakDow
T.intercalate "\n" $ V.toList $ V.take 10 $ V.map (\(name, errCount, evCount) -> "* **" <> name <> "**: Total errors-" <> show errCount <> ", Total events-" <> show evCount) breakDown
-discordErrorAlert :: RequestDumps.ATError -> Text -> Text -> AE.Value
+discordErrorAlert :: Errors.ATError -> Text -> Text -> AE.Value
discordErrorAlert err project projectUrl =
[aesonQQ|{
"embeds": [
@@ -326,7 +326,7 @@ discordErrorAlert err project projectUrl =
"content": "**🔴 New Runtime Error**"
}|]
where
- url = projectUrl <> "/anomalies/by_hash/" <> fromMaybe "" err.hash
+ url = projectUrl <> "/anomalies/by_hash/" <> err.hash
msg = "```" <> err.message <> "```"
method = fromMaybe "" err.requestMethod
path = fromMaybe "" err.requestPath
@@ -338,8 +338,8 @@ discordErrorAlert err project projectUrl =
serviceName = fromMaybe "" err.serviceName
-discordNewEndpointAlert :: Text -> V.Vector Text -> Text -> Text -> AE.Value
-discordNewEndpointAlert projectName endpoints hash projectUrl =
+discordNewEndpointAlert :: Text -> Text -> Text -> Text -> Text -> AE.Value
+discordNewEndpointAlert projectName method urlPath hash projectUrl =
[aesonQQ|
{
"embeds": [
@@ -348,22 +348,20 @@ discordNewEndpointAlert projectName endpoints hash projectUrl =
"description": #{description},
"color": 263167,
"fields": [
- {"name": "Endpoints","value": #{enps},"inline": false},
+ {"name": "Endpoint","value": #{enp},"inline": false},
{"name": "···","value": #{explorerLink},"inline": false}
],
"footer": { "text": "{···}"},
"url": #{url}
}
],
- "content" : "🔵 New Endpoint(s) Detected"
+ "content" : "🔵 New Endpoint Detected"
}
|]
where
- endpointsCount = length endpoints
- description = "We've detected **" <> show endpointsCount <> " new endpoints** in the **" <> projectName <> "** project."
+ description = "We've detected a new endpoint in the **" <> projectName <> "** project."
url = projectUrl <> "/anomalies/by_hash/" <> hash
- enp = (\x -> "\"" <> x <> "\"") . T.dropWhile (/= ' ') <$> V.toList endpoints
- query = urlEncode True $ encodeUtf8 $ "attributes.http.route in (" <> T.intercalate "," enp <> ")"
+ query = urlEncode True $ encodeUtf8 $ "method=\"" <> method <> "\" AND attributes.http.route in (\"" <> urlPath <> "\")"
explorerUrl = projectUrl <> "/log_explorer?query=" <> decodeUtf8 query
explorerLink = "[View in Explorer](" <> explorerUrl <> ")"
- enps = T.intercalate "\n\n" $ (\x -> "`" <> x <> "`") <$> V.toList endpoints
+ enp = "`" <> method <> " " <> urlPath <> "`"
diff --git a/src/RequestMessages.hs b/src/RequestMessages.hs
index 40bc935b1..3cb8a99db 100644
--- a/src/RequestMessages.hs
+++ b/src/RequestMessages.hs
@@ -43,6 +43,7 @@ import Data.Vector.Algorithms.Intro qualified as VA
import Database.PostgreSQL.Simple (Query)
import Deriving.Aeson qualified as DAE
import Models.Apis.Anomalies qualified as Anomalies
+import Models.Apis.Errors qualified as Errors
import Models.Apis.Fields.Types qualified as Fields (
Field (..),
FieldCategoryEnum (..),
@@ -57,7 +58,7 @@ import Relude
import Relude.Unsafe as Unsafe (read)
import Text.RE.Replace (matched)
import Text.RE.TDFA (RE, SearchReplace, ed, re, (*=~/), (?=~))
-import Utils (DBField (), toXXHash)
+import Utils (DBField (), replaceAllFormats, toXXHash)
-- $setup
@@ -149,19 +150,10 @@ replaceNullChars = T.replace "\\u0000" ""
-- | Process errors with optional HTTP-specific fields
-- If HTTP fields are not provided, they remain as Nothing in the error record
-processErrors :: Projects.ProjectId -> Maybe RequestDumps.SDKTypes -> Maybe Text -> Maybe Text -> RequestDumps.ATError -> (RequestDumps.ATError, Query, [DBField])
-processErrors pid maybeSdkType maybeMethod maybePath err = (normalizedError, q, params)
+processErrors :: Projects.ProjectId -> Maybe RequestDumps.SDKTypes -> Maybe Text -> Maybe Text -> Errors.ATError -> (Query, [DBField])
+processErrors pid maybeSdkType maybeMethod maybePath err = (q, params)
where
- (q, params) = Anomalies.insertErrorQueryAndParams pid normalizedError
- normalizedError =
- err
- { RequestDumps.projectId = Just pid
- , RequestDumps.hash = Just $ fromMaybe defaultHash err.hash
- , RequestDumps.technology = maybeSdkType <|> err.technology
- , RequestDumps.requestMethod = maybeMethod <|> err.requestMethod
- , RequestDumps.requestPath = maybePath <|> err.requestPath
- }
- defaultHash = toXXHash (pid.toText <> fromMaybe "" err.serviceName <> err.errorType <> replaceAllFormats (err.message <> err.stackTrace) <> maybe "" show maybeSdkType)
+ (q, params) = Errors.upsertErrorQueryAndParam pid err
sortVector :: Ord a => V.Vector a -> V.Vector a
@@ -367,145 +359,6 @@ commonFormatPatterns =
]
--- | Replaces all format patterns in the input text with their format names
--- This function applies all regex patterns and replaces matches until no more replacements are made
---
--- >>> map replaceAllFormats ["123", "c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd", "550e8400-e29b-41d4-a716-446655440000", "507f1f77bcf86cd799439011"]
--- ["{integer}","{uuid}","{uuid}","{uuid}"]
---
--- >>> map replaceAllFormats ["e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "356a192b7913b04c54574d18c28d46e6395428ab", "5d41402abc4b2a76b9719d911017c592"]
--- ["{sha256}","{sha1}","{md5}"]
---
--- >>> replaceAllFormats "User 123 accessed endpoint c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd"
--- "User {integer} accessed endpoint {uuid}"
---
--- >>> replaceAllFormats "Error at 192.168.0.1:8080 with status 404"
--- "Error at {ipv4}{port} with status {integer}"
---
--- >>> map replaceAllFormats ["Server on :8080", "localhost:3000", "api.com:443", ":22"]
--- ["Server on {port}","localhost{port}","api.com{port}","{port}"]
---
--- >>> replaceAllFormats "Connected to 10.0.0.1:443 and 192.168.1.100:22"
--- "Connected to {ipv4}{port} and {ipv4}{port}"
---
--- >>> replaceAllFormats "Responses: 200, 301, 404, 500"
--- "Responses: {integer}, {integer}, {integer}, {integer}"
---
--- >>> replaceAllFormats "GET /api/v2/users/123/orders/456 returned 200"
--- "GET /api/v{integer}/users/{integer}/orders/{integer} returned {integer}"
---
--- >>> replaceAllFormats "User 550e8400-e29b-41d4-a716-446655440000 connected from 192.168.1.50:9876"
--- "User {uuid} connected from {ipv4}{port}"
---
--- >>> replaceAllFormats "Hash: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3, Status: 403, Port: :8443"
--- "Hash: {sha1}, Status: {integer}, Port: {port}"
---
--- >>> replaceAllFormats "Processing request 123456 at 2023-10-15:3000"
--- "Processing request {integer} at {integer}-{integer}-{integer}{port}"
---
--- >>> map replaceAllFormats ["Mixed: 192.168.1.1 123 :80 404", "0xDEADBEEF", "Values: 999, 1000, 1001"]
--- ["Mixed: {ipv4} {integer} {port} {integer}","{hex}","Values: {integer}, {integer}, {integer}"]
---
--- >>> map replaceAllFormats ["10.0.0.1", "172.16.0.1", "192.168.1.1", "255.255.255.0"]
--- ["{ipv4}","{ipv4}","{ipv4}","{ipv4}"]
---
--- >>> map replaceAllFormats ["Multiple formats: 123 abc def456789 :9000", "Log entry 404 at 10.0.0.1:8080"]
--- ["Multiple formats: {integer} abc def{integer} {port}","Log entry {integer} at {ipv4}{port}"]
-replaceAllFormats :: Text -> Text
-replaceAllFormats input = restorePlaceholders $ processPatterns input formatPatternsForReplacement
- where
- -- Process patterns sequentially with (*=~/)
- -- Use special Unicode characters as temporary placeholders to prevent re-matching
- processPatterns :: Text -> [SearchReplace RE Text] -> Text
- processPatterns txt [] = txt
- processPatterns txt (sr : rest) =
- let newTxt = txt *=~/ sr
- in processPatterns newTxt rest
-
- -- Restore the Unicode placeholders to the final format
- restorePlaceholders :: Text -> Text
- restorePlaceholders txt =
- txt
- *=~/ [ed|〖×UUID×〗///{uuid}|]
- *=~/ [ed|〖×SHA-TWO-FIVE-SIX×〗///{sha256}|]
- *=~/ [ed|〖×SHA-ONE×〗///{sha1}|]
- *=~/ [ed|〖×MD-FIVE×〗///{md5}|]
- *=~/ [ed|〖×IPV-FOUR×〗///{ipv4}|]
- *=~/ [ed|〖×PORT×〗///{port}|]
- *=~/ [ed|〖×HEX×〗///{hex}|]
- *=~/ [ed|〖×INTEGER×〗///{integer}|]
-
- -- Use a subset of patterns that should be replaced in text
- -- Using Unicode characters with no alphanumeric content that won't match any patterns
- formatPatternsForReplacement :: [SearchReplace RE Text]
- formatPatternsForReplacement =
- [ -- UUIDs and hashes first (most specific)
- [ed|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}///〖×UUID×〗|]
- , [ed|[a-fA-F0-9]{64}///〖×SHA-TWO-FIVE-SIX×〗|]
- , [ed|[a-fA-F0-9]{40}///〖×SHA-ONE×〗|]
- , [ed|[a-fA-F0-9]{32}///〖×MD-FIVE×〗|]
- , [ed|[0-9a-fA-F]{24}///〖×UUID×〗|]
- , -- Visa 13 or 16 digits
- [ed|5[1-5][0-9]{14}///{credit_card}|]
- , -- Mastercard
- [ed|3[47][0-9]{13}///{credit_card}|]
- , -- Amex
- [ed|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+///{jwt}|]
- , -- JWT
- -- IBAN (moved before base64)
- [ed|[A-Z]{2}[0-9]{2}[A-Za-z0-9]{4}[0-9]{7}[A-Za-z0-9]{0,16}///{iban}|]
- , -- Date patterns (before file paths to avoid conflicts)
- [ed|(Mon|Tue|Wed|Thu|Fri|Sat|Sun), [0-9]{1,2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [+\-][0-9]{4}///{rfc2822}|]
- , [ed|[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|[+\-][0-9]{2}:[0-9]{2})?///{YYYY-MM-DDThh:mm:ss.sTZD}|]
- , -- ISO 8601
- [ed|[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}///{YYYY-MM-DD HH:MM:SS}|]
- , -- MySQL datetime
- [ed|[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}///{MM/DD/YYYY HH:MM:SS}|]
- , -- US datetime
- [ed|[0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}///{MM-DD-YYYY HH:MM:SS}|]
- , [ed|[0-9]{2}\.[0-9]{2}\.[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}///{DD.MM.YYYY HH:MM:SS}|]
- , -- European datetime
- [ed|(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)[0-9][0-9]///{dd/mm/yyyy}|]
- , -- European date
- [ed|(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-(19|20)[0-9][0-9]///{dd-mm-yyyy}|]
- , [ed|(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)[0-9][0-9]///{dd.mm.yyyy}|]
- , [ed|(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)[0-9][0-9]///{mm/dd/yyyy}|]
- , -- US date
- [ed|(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)[0-9][0-9]///{mm-dd-yyyy}|]
- , [ed|(0[1-9]|1[012])\.(0[1-9]|[12][0-9]|3[01])\.(19|20)[0-9][0-9]///{mm.dd.yyyy}|]
- , -- Note: Removed [0-9]{4}-[0-9]{2}-[0-9]{2} pattern to avoid matching dates followed by ports (e.g. 2023-10-15:3000)
- -- The more specific datetime pattern [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} handles actual dates with times
- [ed|[0-9]{4}/[0-9]{2}/[0-9]{2}///{YYYY/MM/DD}|]
- , -- Compact date
- [ed|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{1,2}, [0-9]{4}///{Mon DD, YYYY}|]
- , -- Long month
- [ed|[0-9]{1,2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-[0-9]{4}///{DD-Mon-YYYY}|]
- , -- Oracle date
- -- Time patterns
- [ed|[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}///{HH:MM:SS.mmm}|]
- , -- Time with milliseconds
- [ed|[0-9]{2}:[0-9]{2}:[0-9]{2}///{HH:MM:SS}|]
- , -- Time only
- [ed|[0-9]{1,2}:[0-9]{2} (AM|PM|am|pm)///{H:MM AM/PM}|]
- , -- Personal identifiers
- [ed|[0-9]{3}-[0-9]{2}-[0-9]{4}///{ssn}|]
- , [ed|\+1 \([0-9]{3}\) [0-9]{3}-[0-9]{4}///{phone}|]
- , -- Network patterns
- [ed|(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)///〖×IPV-FOUR×〗|]
- , [ed|:[0-9]{1,5}///〖×PORT×〗|]
- , [ed|https?://[^\s]+///{url}|]
- , [ed|([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}///{ipv6}|]
- , [ed|([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}///{mac}|]
- , [ed|[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}///{email}|]
- , -- Hex numbers (must come before general integers)
- [ed|0x[0-9A-Fa-f]+///〖×HEX×〗|]
- , --- float
- [ed|[+-]?[0-9]+\.[0-9]+///{float}|]
- , -- Numbers (including HTTP status codes)
- [ed|[0-9]+///〖×INTEGER×〗|]
- ]
-
-
-- valueToFormatStr will take a string and try to find a format which matches that string best.
-- At the moment it takes a text and returns a generic mask that represents the format of that text
--
diff --git a/src/Utils.hs b/src/Utils.hs
index cd1d3e14e..f5ef4c4e5 100644
--- a/src/Utils.hs
+++ b/src/Utils.hs
@@ -47,6 +47,7 @@ module Utils (
methodFillColor,
levelFillColor,
changeTypeFillColor,
+ replaceAllFormats,
)
where
@@ -87,6 +88,7 @@ import Pkg.THUtils (hashFile)
import Relude hiding (notElem, show)
import Servant
import Text.Printf (printf)
+import Text.RE.TDFA (RE, SearchReplace, ed, re, (*=~/), (?=~))
import Text.Regex.TDFA ((=~))
import Text.Show
import "base64" Data.ByteString.Base64 qualified as B64
@@ -767,3 +769,142 @@ getAlertStatusColor status = case status of
"Alerting" -> "badge-error"
"Warning" -> "badge-warning"
_ -> "badge-success"
+
+
+-- | Replaces all format patterns in the input text with their format names
+-- This function applies all regex patterns and replaces matches until no more replacements are made
+--
+-- >>> map replaceAllFormats ["123", "c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd", "550e8400-e29b-41d4-a716-446655440000", "507f1f77bcf86cd799439011"]
+-- ["{integer}","{uuid}","{uuid}","{uuid}"]
+--
+-- >>> map replaceAllFormats ["e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "356a192b7913b04c54574d18c28d46e6395428ab", "5d41402abc4b2a76b9719d911017c592"]
+-- ["{sha256}","{sha1}","{md5}"]
+--
+-- >>> replaceAllFormats "User 123 accessed endpoint c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd"
+-- "User {integer} accessed endpoint {uuid}"
+--
+-- >>> replaceAllFormats "Error at 192.168.0.1:8080 with status 404"
+-- "Error at {ipv4}{port} with status {integer}"
+--
+-- >>> map replaceAllFormats ["Server on :8080", "localhost:3000", "api.com:443", ":22"]
+-- ["Server on {port}","localhost{port}","api.com{port}","{port}"]
+--
+-- >>> replaceAllFormats "Connected to 10.0.0.1:443 and 192.168.1.100:22"
+-- "Connected to {ipv4}{port} and {ipv4}{port}"
+--
+-- >>> replaceAllFormats "Responses: 200, 301, 404, 500"
+-- "Responses: {integer}, {integer}, {integer}, {integer}"
+--
+-- >>> replaceAllFormats "GET /api/v2/users/123/orders/456 returned 200"
+-- "GET /api/v{integer}/users/{integer}/orders/{integer} returned {integer}"
+--
+-- >>> replaceAllFormats "User 550e8400-e29b-41d4-a716-446655440000 connected from 192.168.1.50:9876"
+-- "User {uuid} connected from {ipv4}{port}"
+--
+-- >>> replaceAllFormats "Hash: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3, Status: 403, Port: :8443"
+-- "Hash: {sha1}, Status: {integer}, Port: {port}"
+--
+-- >>> replaceAllFormats "Processing request 123456 at 2023-10-15:3000"
+-- "Processing request {integer} at {integer}-{integer}-{integer}{port}"
+--
+-- >>> map replaceAllFormats ["Mixed: 192.168.1.1 123 :80 404", "0xDEADBEEF", "Values: 999, 1000, 1001"]
+-- ["Mixed: {ipv4} {integer} {port} {integer}","{hex}","Values: {integer}, {integer}, {integer}"]
+--
+-- >>> map replaceAllFormats ["10.0.0.1", "172.16.0.1", "192.168.1.1", "255.255.255.0"]
+-- ["{ipv4}","{ipv4}","{ipv4}","{ipv4}"]
+--
+-- >>> map replaceAllFormats ["Multiple formats: 123 abc def456789 :9000", "Log entry 404 at 10.0.0.1:8080"]
+-- ["Multiple formats: {integer} abc def{integer} {port}","Log entry {integer} at {ipv4}{port}"]
+replaceAllFormats :: Text -> Text
+replaceAllFormats input = restorePlaceholders $ processPatterns input formatPatternsForReplacement
+ where
+ -- Process patterns sequentially with (*=~/)
+ -- Use special Unicode characters as temporary placeholders to prevent re-matching
+ processPatterns :: Text -> [SearchReplace RE Text] -> Text
+ processPatterns txt [] = txt
+ processPatterns txt (sr : rest) =
+ let newTxt = txt *=~/ sr
+ in processPatterns newTxt rest
+
+ -- Restore the Unicode placeholders to the final format
+ restorePlaceholders :: Text -> Text
+ restorePlaceholders txt =
+ txt
+ *=~/ [ed|〖×UUID×〗///{uuid}|]
+ *=~/ [ed|〖×SHA-TWO-FIVE-SIX×〗///{sha256}|]
+ *=~/ [ed|〖×SHA-ONE×〗///{sha1}|]
+ *=~/ [ed|〖×MD-FIVE×〗///{md5}|]
+ *=~/ [ed|〖×IPV-FOUR×〗///{ipv4}|]
+ *=~/ [ed|〖×PORT×〗///{port}|]
+ *=~/ [ed|〖×HEX×〗///{hex}|]
+ *=~/ [ed|〖×INTEGER×〗///{integer}|]
+
+ -- Use a subset of patterns that should be replaced in text
+ -- Using Unicode characters with no alphanumeric content that won't match any patterns
+ formatPatternsForReplacement :: [SearchReplace RE Text]
+ formatPatternsForReplacement =
+ [ -- UUIDs and hashes first (most specific)
+ [ed|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}///〖×UUID×〗|]
+ , [ed|[a-fA-F0-9]{64}///〖×SHA-TWO-FIVE-SIX×〗|]
+ , [ed|[a-fA-F0-9]{40}///〖×SHA-ONE×〗|]
+ , [ed|[a-fA-F0-9]{32}///〖×MD-FIVE×〗|]
+ , [ed|[0-9a-fA-F]{24}///〖×UUID×〗|]
+ , -- Visa 13 or 16 digits
+ [ed|5[1-5][0-9]{14}///{credit_card}|]
+ , -- Mastercard
+ [ed|3[47][0-9]{13}///{credit_card}|]
+ , -- Amex
+ [ed|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+///{jwt}|]
+ , -- JWT
+ -- IBAN (moved before base64)
+ [ed|[A-Z]{2}[0-9]{2}[A-Za-z0-9]{4}[0-9]{7}[A-Za-z0-9]{0,16}///{iban}|]
+ , -- Date patterns (before file paths to avoid conflicts)
+ [ed|(Mon|Tue|Wed|Thu|Fri|Sat|Sun), [0-9]{1,2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [+\-][0-9]{4}///{rfc2822}|]
+ , [ed|[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|[+\-][0-9]{2}:[0-9]{2})?///{YYYY-MM-DDThh:mm:ss.sTZD}|]
+ , -- ISO 8601
+ [ed|[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}///{YYYY-MM-DD HH:MM:SS}|]
+ , -- MySQL datetime
+ [ed|[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}///{MM/DD/YYYY HH:MM:SS}|]
+ , -- US datetime
+ [ed|[0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}///{MM-DD-YYYY HH:MM:SS}|]
+ , [ed|[0-9]{2}\.[0-9]{2}\.[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}///{DD.MM.YYYY HH:MM:SS}|]
+ , -- European datetime
+ [ed|(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)[0-9][0-9]///{dd/mm/yyyy}|]
+ , -- European date
+ [ed|(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-(19|20)[0-9][0-9]///{dd-mm-yyyy}|]
+ , [ed|(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)[0-9][0-9]///{dd.mm.yyyy}|]
+ , [ed|(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)[0-9][0-9]///{mm/dd/yyyy}|]
+ , -- US date
+ [ed|(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)[0-9][0-9]///{mm-dd-yyyy}|]
+ , [ed|(0[1-9]|1[012])\.(0[1-9]|[12][0-9]|3[01])\.(19|20)[0-9][0-9]///{mm.dd.yyyy}|]
+ , -- Note: Removed [0-9]{4}-[0-9]{2}-[0-9]{2} pattern to avoid matching dates followed by ports (e.g. 2023-10-15:3000)
+ -- The more specific datetime pattern [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} handles actual dates with times
+ [ed|[0-9]{4}/[0-9]{2}/[0-9]{2}///{YYYY/MM/DD}|]
+ , -- Compact date
+ [ed|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{1,2}, [0-9]{4}///{Mon DD, YYYY}|]
+ , -- Long month
+ [ed|[0-9]{1,2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-[0-9]{4}///{DD-Mon-YYYY}|]
+ , -- Oracle date
+ -- Time patterns
+ [ed|[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}///{HH:MM:SS.mmm}|]
+ , -- Time with milliseconds
+ [ed|[0-9]{2}:[0-9]{2}:[0-9]{2}///{HH:MM:SS}|]
+ , -- Time only
+ [ed|[0-9]{1,2}:[0-9]{2} (AM|PM|am|pm)///{H:MM AM/PM}|]
+ , -- Personal identifiers
+ [ed|[0-9]{3}-[0-9]{2}-[0-9]{4}///{ssn}|]
+ , [ed|\+1 \([0-9]{3}\) [0-9]{3}-[0-9]{4}///{phone}|]
+ , -- Network patterns
+ [ed|(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)///〖×IPV-FOUR×〗|]
+ , [ed|:[0-9]{1,5}///〖×PORT×〗|]
+ , [ed|https?://[^\s]+///{url}|]
+ , [ed|([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}///{ipv6}|]
+ , [ed|([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}///{mac}|]
+ , [ed|[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}///{email}|]
+ , -- Hex numbers (must come before general integers)
+ [ed|0x[0-9A-Fa-f]+///〖×HEX×〗|]
+ , --- float
+ [ed|[+-]?[0-9]+\.[0-9]+///{float}|]
+ , -- Numbers (including HTTP status codes)
+ [ed|[0-9]+///〖×INTEGER×〗|]
+ ]
diff --git a/src/Web/Routes.hs b/src/Web/Routes.hs
index fcb9d1b7d..8f55f0bba 100644
--- a/src/Web/Routes.hs
+++ b/src/Web/Routes.hs
@@ -57,6 +57,7 @@ import Data.CaseInsensitive qualified as CI
import Data.Effectful.Wreq qualified as Wreq
import Data.Text qualified as T
import Models.Apis.Anomalies qualified as Anomalies
+import Models.Apis.Errors qualified as Errors
import Models.Apis.Monitors qualified as Monitors
import Models.Apis.Reports qualified as ReportsM
import Models.Projects.Dashboards qualified as Dashboards
@@ -300,6 +301,9 @@ data AnomaliesRoutes' mode = AnomaliesRoutes'
, anomalyHashGet :: mode :- "by_hash" :> Capture "anomalyHash" Text :> QPT "first_occurrence" :> Get '[HTML] (RespHeaders (PageCtx (Html ())))
, aiChatPost :: mode :- Capture "issueID" Anomalies.IssueId :> "ai_chat" :> ReqBody '[FormUrlEncoded] AnomalyList.AIChatForm :> Post '[HTML] (RespHeaders (Html ()))
, aiChatHistoryGet :: mode :- Capture "issueID" Anomalies.IssueId :> "ai_chat" :> "history" :> Get '[HTML] (RespHeaders (Html ()))
+ , -- Error-specific actions
+ errorActionPost :: mode :- "errors" :> Capture "errorHash" Text :> "actions" :> Capture "action" Text :> Post '[HTML] (RespHeaders (Html ()))
+ , errorAssignPost :: mode :- "errors" :> Capture "errorHash" Text :> "assign" :> ReqBody '[FormUrlEncoded] AnomalyList.ErrorAssignForm :> Post '[HTML] (RespHeaders (Html ()))
}
deriving stock (Generic)
@@ -518,6 +522,9 @@ anomaliesServer pid =
, anomalyHashGet = AnomalyList.anomalyDetailHashGetH pid
, aiChatPost = AnomalyList.aiChatPostH pid
, aiChatHistoryGet = AnomalyList.aiChatHistoryGetH pid
+ , -- Error-specific handlers
+ errorActionPost = AnomalyList.errorActionPostH pid
+ , errorAssignPost = AnomalyList.errorAssignPostH pid
}
diff --git a/static/migrations/0026_anomaly_detection_baselines.sql b/static/migrations/0026_anomaly_detection_baselines.sql
new file mode 100644
index 000000000..bf32c94b6
--- /dev/null
+++ b/static/migrations/0026_anomaly_detection_baselines.sql
@@ -0,0 +1,121 @@
+BEGIN;
+
+
+
+-- ============================================================================
+-- 3. ERRORS TABLE (RECREATED WITH LIFECYCLE)
+-- ============================================================================
+-- Recreate errors table with lifecycle tracking.
+-- States: new, escalating, ongoing, resolved, regressed
+
+-- Drop existing trigger and table
+DROP TRIGGER IF EXISTS error_created_anomaly ON apis.errors;
+DROP TABLE IF EXISTS apis.errors;
+
+CREATE OR REPLACE FUNCTION apis.new_error_proc() RETURNS trigger AS $$
+BEGIN
+ IF TG_WHEN <> 'AFTER' THEN
+ RAISE EXCEPTION 'apis.new_error_proc() may only run as an AFTER trigger';
+ END IF;
+ -- Create a job for the new error
+ -- JSON format matches Aeson's derived FromJSON for:
+ -- NewErrorDetected Projects.ProjectId Text
+ INSERT INTO background_jobs (run_at, status, payload)
+ VALUES (
+ NOW(),
+ 'queued',
+ jsonb_build_object(
+ 'tag', 'NewErrorDetected',
+ 'contents', jsonb_build_array(NEW.project_id, NEW.hash)
+ )
+ );
+ RETURN NULL;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TABLE apis.errors (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ project_id UUID NOT NULL REFERENCES projects.projects(id) ON DELETE CASCADE,
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+
+ exception_type TEXT NOT NULL,
+ message TEXT NOT NULL,
+ stacktrace TEXT NOT NULL,
+ hash TEXT NOT NULL,
+
+ environment TEXT NOT NULL,
+ service TEXT,
+ runtime TEXT,
+ error_data JSONB NOT NULL DEFAULT '{}',
+ first_event_id UUID,
+ last_event_id UUID,
+ state TEXT NOT NULL DEFAULT 'new',
+ assignee_id UUID,
+ assigned_at TIMESTAMPTZ,
+ resolved_at TIMESTAMPTZ,
+ regressed_at TIMESTAMPTZ,
+
+ occurrences_1m INT NOT NULL DEFAULT 0,
+ occurrences_5m INT NOT NULL DEFAULT 0,
+ occurrences_1h INT NOT NULL DEFAULT 0,
+ occurrences_24h INT NOT NULL DEFAULT 0,
+
+ quiet_minutes INT NOT NULL DEFAULT 0,
+ resolution_threshold_minutes INT NOT NULL DEFAULT 30,
+
+ baseline_state TEXT NOT NULL DEFAULT 'learning',
+ baseline_samples INT NOT NULL DEFAULT 0,
+ baseline_error_rate_mean FLOAT,
+ baseline_error_rate_stddev FLOAT,
+ baseline_updated_at TIMESTAMPTZ,
+
+ is_ignored BOOLEAN DEFAULT false,
+ ignored_until TIMESTAMPTZ
+);
+SELECT manage_updated_at('apis.errors');
+
+CREATE UNIQUE INDEX uniq_error_group ON apis.errors (project_id, hash, environment);
+CREATE INDEX idx_errors_project_state ON apis.errors (project_id, state);
+CREATE INDEX idx_errors_last_seen ON apis.errors (project_id, last_event_id);
+CREATE UNIQUE INDEX idx_apis_errors_project_id_hash ON apis.errors(project_id, hash);
+CREATE INDEX idx_apis_errors_project_id ON apis.errors(project_id);
+CREATE INDEX idx_errors_active ON apis.errors(project_id, state) WHERE state != 'resolved';
+CREATE INDEX idx_errors_state ON apis.errors(project_id, state);
+CREATE TRIGGER error_created_anomaly AFTER INSERT ON apis.errors FOR EACH ROW EXECUTE PROCEDURE apis.new_error_proc('runtime_exception', 'created', 'skip_anomaly_record');
+
+-- all data here that is not in errors (top level) go into error_data jsonb
+-- Whenever the same error occurs, we update the error_data with latest values for these fields
+-- Then an on update trigger on errors can update these fields from error_data if needed
+
+CREATE TABLE apis.error_events (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ project_id UUID NOT NULL REFERENCES projects.projects(id) ON DELETE CASCADE,
+ occurred_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+ target_hash TEXT NOT NULL, -- references apis.errors.hash (no FK due to composite unique)
+ exception_type TEXT NOT NULL,
+ message TEXT NOT NULL,
+ stack_trace TEXT NOT NULL,
+ service_name TEXT NOT NULL,
+
+ release TEXT,
+ environment TEXT,
+ request_method TEXT,
+ request_path TEXT,
+ endpoint_hash TEXT,
+ trace_id TEXT,
+ span_id TEXT,
+ parent_span_id TEXT,
+ user_id TEXT,
+ user_email TEXT,
+ user_ip INET,
+ session_id TEXT,
+ sample_rate FLOAT NOT NULL DEFAULT 1.0
+);
+
+-- Indexes for efficient queries
+CREATE INDEX idx_error_events_project ON apis.error_events (project_id, occurred_at DESC);
+CREATE INDEX idx_error_event_error_hash ON apis.error_events (project_id, target_hash);
+CREATE INDEX idx_error_events_service ON apis.error_events (service_name);
+
+COMMIT;
diff --git a/static/migrations/0027_log_pattern_monitoring.sql b/static/migrations/0027_log_pattern_monitoring.sql
new file mode 100644
index 000000000..fab4dfaac
--- /dev/null
+++ b/static/migrations/0027_log_pattern_monitoring.sql
@@ -0,0 +1,75 @@
+BEGIN;
+
+CREATE TABLE IF NOT EXISTS apis.log_patterns (
+ id BIGSERIAL PRIMARY KEY,
+ project_id UUID NOT NULL REFERENCES projects.projects(id) ON DELETE CASCADE,
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+
+ log_pattern TEXT NOT NULL,
+ pattern_hash TEXT NOT NULL,
+
+ service_name TEXT,
+ log_level TEXT,
+ sample_message TEXT,
+
+ first_seen_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+ last_seen_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+ occurrence_count BIGINT NOT NULL DEFAULT 1,
+
+ state TEXT NOT NULL DEFAULT 'new', -- 'new', 'acknowledged', 'ignored'
+ acknowledged_by UUID REFERENCES users.users(id),
+ acknowledged_at TIMESTAMPTZ,
+
+ -- Baseline for volume spike detection
+ baseline_state TEXT NOT NULL DEFAULT 'learning', -- 'learning', 'established'
+ baseline_volume_hourly_mean FLOAT,
+ baseline_volume_hourly_stddev FLOAT,
+ baseline_samples INT NOT NULL DEFAULT 0,
+ baseline_updated_at TIMESTAMPTZ,
+
+ UNIQUE(project_id, pattern_hash)
+);
+
+SELECT manage_updated_at('apis.log_patterns');
+
+CREATE INDEX IF NOT EXISTS idx_log_patterns_project ON apis.log_patterns(project_id);
+CREATE INDEX IF NOT EXISTS idx_log_patterns_project_state ON apis.log_patterns(project_id, state);
+CREATE INDEX IF NOT EXISTS idx_log_patterns_last_seen ON apis.log_patterns(project_id, last_seen_at DESC);
+CREATE INDEX IF NOT EXISTS idx_log_patterns_service ON apis.log_patterns(project_id, service_name);
+
+CREATE OR REPLACE FUNCTION apis.new_log_pattern_proc() RETURNS trigger AS $$
+BEGIN
+ IF TG_WHEN <> 'AFTER' THEN
+ RAISE EXCEPTION 'apis.new_log_pattern_proc() may only run as an AFTER trigger';
+ END IF;
+ INSERT INTO background_jobs (run_at, status, payload)
+ VALUES (
+ NOW(),
+ 'queued',
+ jsonb_build_object(
+ 'tag', 'NewLogPatternDetected',
+ 'contents', jsonb_build_array(NEW.project_id, NEW.pattern_hash)
+ )
+ );
+ RETURN NULL;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE TRIGGER log_pattern_created_notify AFTER INSERT ON apis.log_patterns FOR EACH ROW EXECUTE PROCEDURE apis.new_log_pattern_proc();
+
+
+ALTER TABLE apis.endpoints
+ADD COLUMN IF NOT EXISTS baseline_state TEXT NOT NULL DEFAULT 'learning',
+ADD COLUMN IF NOT EXISTS baseline_samples INT NOT NULL DEFAULT 0,
+ADD COLUMN IF NOT EXISTS baseline_updated_at TIMESTAMPTZ,
+ADD COLUMN IF NOT EXISTS baseline_error_rate_mean FLOAT,
+ADD COLUMN IF NOT EXISTS baseline_error_rate_stddev FLOAT,
+ADD COLUMN IF NOT EXISTS baseline_latency_mean FLOAT,
+ADD COLUMN IF NOT EXISTS baseline_latency_stddev FLOAT,
+ADD COLUMN IF NOT EXISTS baseline_latency_p95 FLOAT,
+ADD COLUMN IF NOT EXISTS baseline_latency_p99 FLOAT,
+ADD COLUMN IF NOT EXISTS baseline_volume_hourly_mean FLOAT,
+ADD COLUMN IF NOT EXISTS baseline_volume_hourly_stddev FLOAT;
+
+COMMIT;
diff --git a/static/migrations/0028_rebuild_issues_table.sql b/static/migrations/0028_rebuild_issues_table.sql
new file mode 100644
index 000000000..8066be765
--- /dev/null
+++ b/static/migrations/0028_rebuild_issues_table.sql
@@ -0,0 +1,65 @@
+BEGIN;
+
+
+ALTER TYPE apis.issue_type ADD VALUE IF NOT EXISTS 'log_pattern';
+ALTER TYPE apis.issue_type ADD VALUE IF NOT EXISTS 'error_escalating';
+ALTER TYPE apis.issue_type ADD VALUE IF NOT EXISTS 'error_regressed';
+ALTER TYPE apis.issue_type ADD VALUE IF NOT EXISTS 'log_pattern_rate_change';
+ALTER TYPE apis.issue_type ADD VALUE IF NOT EXISTS 'endpoint_latency_degradation';
+ALTER TYPE apis.issue_type ADD VALUE IF NOT EXISTS 'endpoint_error_rate_spike';
+ALTER TYPE apis.issue_type ADD VALUE IF NOT EXISTS 'endpoint_volume_rate_change';
+
+DROP TABLE IF EXISTS apis.issues CASCADE;
+
+CREATE TABLE apis.issues (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ project_id UUID NOT NULL REFERENCES projects.projects(id) ON DELETE CASCADE,
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+
+ issue_type apis.issue_type NOT NULL,
+ source_type TEXT NOT NULL, -- 'error', 'log_pattern', 'endpoint', 'shape'
+ target_hash TEXT NOT NULL, -- links to error.hash, log_pattern.pattern_hash, endpoint.hash
+ endpoint_hash TEXT, -- for endpoint-related issues
+ title TEXT NOT NULL DEFAULT '',
+ service TEXT,
+ environment TEXT,
+ severity TEXT NOT NULL DEFAULT 'warning', -- 'critical', 'warning', 'info'
+ critical BOOLEAN NOT NULL DEFAULT FALSE,
+ -- Lifecycle
+ first_seen_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+ last_seen_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
+ acknowledged_at TIMESTAMPTZ,
+ acknowledged_by UUID REFERENCES users.users(id) ON DELETE SET NULL,
+ recommended_action TEXT NOT NULL DEFAULT '',
+ migration_complexity TEXT NOT NULL DEFAULT 'n/a', -- 'low', 'medium', 'high', 'n/a'
+ request_payloads JSONB NOT NULL DEFAULT '[]'::jsonb,
+ response_payloads JSONB NOT NULL DEFAULT '[]'::jsonb,
+ archived_at TIMESTAMPTZ,
+ resolved_at TIMESTAMPTZ,
+ llm_enhanced_at TIMESTAMPTZ,
+ llm_enhancement_version INT,
+ -- Flexible category-specific data
+ issue_data JSONB NOT NULL DEFAULT '{}'
+);
+
+SELECT manage_updated_at('apis.issues');
+
+-- Indexes
+CREATE INDEX idx_issues_project_id ON apis.issues(project_id);
+CREATE INDEX idx_issues_project_created ON apis.issues(project_id, created_at DESC);
+CREATE INDEX idx_issues_issue_type ON apis.issues(project_id, issue_type);
+CREATE INDEX idx_issues_source ON apis.issues(project_id, source_type, target_hash);
+CREATE INDEX idx_issues_severity ON apis.issues(project_id, severity);
+CREATE INDEX idx_issues_critical ON apis.issues(project_id, critical) WHERE critical = TRUE;
+CREATE INDEX idx_issues_open ON apis.issues(project_id, acknowledged_at, archived_at)
+ WHERE acknowledged_at IS NULL AND archived_at IS NULL;
+CREATE INDEX idx_issues_unresolved ON apis.issues(project_id, resolved_at)
+ WHERE resolved_at IS NULL AND archived_at IS NULL;
+
+-- Dedupe: one open issue per source per issue type
+CREATE UNIQUE INDEX idx_issues_unique_open
+ ON apis.issues(project_id, issue_type, target_hash)
+ WHERE resolved_at IS NULL AND archived_at IS NULL;
+
+COMMIT;
diff --git a/static/migrations/0030_error_events_trigger.sql b/static/migrations/0030_error_events_trigger.sql
new file mode 100644
index 000000000..86b55f3b1
--- /dev/null
+++ b/static/migrations/0030_error_events_trigger.sql
@@ -0,0 +1,68 @@
+BEGIN;
+
+-- Trigger function to create error_events from error_data JSONB on errors table
+-- The error_data column contains ATError structure with event-specific details
+CREATE OR REPLACE FUNCTION apis.create_error_event_proc() RETURNS trigger AS $$
+BEGIN
+ IF TG_WHEN <> 'AFTER' THEN
+ RAISE EXCEPTION 'apis.create_error_event_proc() may only run as an AFTER trigger';
+ END IF;
+
+ IF NEW.error_data IS NOT NULL AND NEW.error_data != '{}'::jsonb THEN
+ INSERT INTO apis.error_events (
+ project_id,
+ occurred_at,
+ target_hash,
+ exception_type,
+ message,
+ stack_trace,
+ service_name,
+ environment,
+ request_method,
+ request_path,
+ endpoint_hash,
+ trace_id,
+ span_id,
+ parent_span_id,
+ user_id,
+ user_email,
+ user_ip,
+ session_id,
+ sample_rate
+ ) VALUES (
+ NEW.project_id,
+ COALESCE((NEW.error_data->>'when')::timestamptz, NOW()),
+ NEW.hash,
+ COALESCE(NEW.error_data->>'root_exception_type', NEW.error_data->>'error_type', NEW.error_type),
+ COALESCE(NEW.error_data->>'root_exception_message', NEW.error_data->>'message', NEW.message),
+ COALESCE(NEW.error_data->>'stack_trace', NEW.stacktrace),
+ COALESCE(NEW.error_data->>'service_name', NEW.service, 'unknown'),
+ COALESCE(NEW.error_data->>'environment', NEW.environment),
+ NEW.error_data->>'request_method',
+ NEW.error_data->>'request_path',
+ NEW.error_data->>'endpoint_hash',
+ NEW.error_data->>'trace_id',
+ NEW.error_data->>'span_id',
+ NEW.error_data->>'parent_span_id',
+ NEW.error_data->>'user_id',
+ NEW.error_data->>'user_email',
+ -- Handle user_ip carefully - convert to INET if valid, otherwise NULL
+ CASE
+ WHEN NEW.error_data->>'user_ip' IS NOT NULL
+ AND NEW.error_data->>'user_ip' != ''
+ THEN (NEW.error_data->>'user_ip')::inet
+ ELSE NULL
+ END,
+ NEW.error_data->>'session_id',
+ 1.0 -- default sample rate
+ );
+ END IF;
+
+ RETURN NULL;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER error_insert_create_event AFTER INSERT ON apis.errors FOR EACH ROW EXECUTE PROCEDURE apis.create_error_event_proc();
+CREATE TRIGGER error_update_create_event AFTER UPDATE OF error_data ON apis.errors FOR EACH ROW EXECUTE PROCEDURE apis.create_error_event_proc();
+
+COMMIT;
diff --git a/static/migrations/0032_drop_issue_index.sql b/static/migrations/0032_drop_issue_index.sql
new file mode 100644
index 000000000..a8f75b4ab
--- /dev/null
+++ b/static/migrations/0032_drop_issue_index.sql
@@ -0,0 +1,12 @@
+BEGIN;
+ DROP INDEX IF EXISTS apis.idx_issues_unresolved;
+ DROP INDEX IF EXISTS apis.idx_issues_unique_open;
+ CREATE UNIQUE INDEX issues_project_target_type_open_idx
+ ON apis.issues (project_id, target_hash, issue_type)
+ WHERE acknowledged_at IS NULL AND archived_at IS NULL;
+ ALTER TABLE apis.errors RENAME COLUMN exception_type TO error_type;
+ ALTER TABLE apis.errors
+ ADD COLUMN first_trace_id TEXT,
+ ADD COLUMN recent_trace_id TEXT;
+ ALTER TABLE apis.log_patterns ADD COLUMN trace_id TEXT;
+COMMIT;
\ No newline at end of file
diff --git a/test/unit/Pkg/DrainSpec.hs b/test/unit/Pkg/DrainSpec.hs
index 276a8d37d..6c0149c74 100644
--- a/test/unit/Pkg/DrainSpec.hs
+++ b/test/unit/Pkg/DrainSpec.hs
@@ -24,7 +24,7 @@ processNewLog logId logContent now tree = do
firstToken = if V.null tokensVec then "" else V.head tokensVec
in if tokenCount == 0
then tree -- Skip empty logs
- else updateTreeWithLog tree tokenCount firstToken tokensVec logId logContent now
+ else updateTreeWithLog tree tokenCount firstToken tokensVec logId True logContent now
processBatch :: V.Vector (Text, Text) -> UTCTime -> DrainTree -> DrainTree
processBatch logBatch now initialTree = do
V.foldl (\tree (logId, logContent) -> processNewLog logId logContent now tree) initialTree logBatch
@@ -37,23 +37,23 @@ spec = describe "DRAIN updateTreeWithLog" $ do
updatedTree = processBatch (V.fromList basicHttpLogs) (testTimeOffset 0) initialTree
logGroups = getAllLogGroups updatedTree
length logGroups `shouldBe` 3
- let patterns = V.map fst logGroups
- V.toList patterns `shouldMatchList`
+ let patterns = V.map (\(_, template, _) -> template) logGroups
+ V.toList patterns `shouldMatchList`
[ "DELETE /api/users/{integer} HTTP/{float} {integer}"
, "POST /api/users HTTP/{float} {integer}"
, "GET <*> HTTP/{float} {integer}"
- ]
- let log1 = V.find (\(tmp, logIds) -> tmp == "GET <*> HTTP/{float} {integer}") logGroups
+ ]
+ let log1 = V.find (\(_, tmp, _) -> tmp == "GET <*> HTTP/{float} {integer}") logGroups
case log1 of
- Just (_, lg) -> lg `shouldBe` V.fromList ["log5", "log3", "log2", "log1"]
+ Just (_, _, lg) -> lg `shouldBe` V.fromList ["log5", "log3", "log2", "log1"]
Nothing -> error "log1 pattern not found"
- let log2 = V.find (\(tmp, logIds) -> tmp == "POST /api/users HTTP/{float} {integer}") logGroups
+ let log2 = V.find (\(_, tmp, _) -> tmp == "POST /api/users HTTP/{float} {integer}") logGroups
case log2 of
- Just (_, lg) -> lg `shouldBe` V.fromList ["log4"]
+ Just (_, _, lg) -> lg `shouldBe` V.fromList ["log4"]
Nothing -> error "log2 pattern not found"
- let log3 = V.find (\(tmp, logIds) -> tmp == "DELETE /api/users/{integer} HTTP/{float} {integer}") logGroups
+ let log3 = V.find (\(_, tmp, _) -> tmp == "DELETE /api/users/{integer} HTTP/{float} {integer}") logGroups
case log3 of
- Just (_, lg) -> lg `shouldBe` V.fromList ["log6"]
+ Just (_, _, lg) -> lg `shouldBe` V.fromList ["log6"]
Nothing -> error "log3 pattern not found"
pass
@@ -61,23 +61,23 @@ spec = describe "DRAIN updateTreeWithLog" $ do
let initialTree = emptyDrainTree
updatedTree = processBatch (V.fromList databaseLogs) (testTimeOffset 0) initialTree
logGroups = getAllLogGroups updatedTree
- let patterns = V.map fst logGroups
+ let patterns = V.map (\(_, template, _) -> template) logGroups
V.toList patterns `shouldMatchList`
[ "Connected to database <*>"
, "Database query executed in {integer}ms"
, "Connection pool exhausted max={integer} active={integer}"
]
- let log1 = V.find (\(tmp, logIds) -> tmp == "Connected to database <*>") logGroups
+ let log1 = V.find (\(_, tmp, _) -> tmp == "Connected to database <*>") logGroups
case log1 of
- Just (_, lg) -> lg `shouldBe` V.fromList ["db3", "db2", "db1"]
+ Just (_, _, lg) -> lg `shouldBe` V.fromList ["db3", "db2", "db1"]
Nothing -> error "db1 pattern not found"
- let log2 = V.find (\(tmp, logIds) -> tmp == "Database query executed in {integer}ms") logGroups
+ let log2 = V.find (\(_, tmp, _) -> tmp == "Database query executed in {integer}ms") logGroups
case log2 of
- Just (_, lg) -> lg `shouldBe` V.fromList ["db6", "db5", "db4"]
+ Just (_, _, lg) -> lg `shouldBe` V.fromList ["db6", "db5", "db4"]
Nothing -> error "db2 pattern not found"
- let log3 = V.find (\(tmp, logIds) -> tmp == "Connection pool exhausted max={integer} active={integer}") logGroups
+ let log3 = V.find (\(_, tmp, _) -> tmp == "Connection pool exhausted max={integer} active={integer}") logGroups
case log3 of
- Just (_, lg) -> lg `shouldBe` V.fromList ["db8", "db7"]
+ Just (_, _, lg) -> lg `shouldBe` V.fromList ["db8", "db7"]
Nothing -> error "db3 pattern not found"
pass
@@ -86,28 +86,28 @@ spec = describe "DRAIN updateTreeWithLog" $ do
updatedTree = processBatch (V.fromList startupLogs) (testTimeOffset 0) initialTree
logGroups = getAllLogGroups updatedTree
length logGroups `shouldBe` 4
- let patterns = V.map fst logGroups
+ let patterns = V.map (\(_, template, _) -> template) logGroups
V.toList patterns `shouldMatchList`
[ "Initializing Redis connection <*>"
, "Application ready to serve requests"
, "Loading configuration from <*>"
, "Starting application on port {integer}"
]
- let log1 = V.find (\(tmp, logIds) -> tmp == "Starting application on port {integer}") logGroups
+ let log1 = V.find (\(_, tmp, _) -> tmp == "Starting application on port {integer}") logGroups
case log1 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["start3", "start2", "start1"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["start3", "start2", "start1"]
Nothing -> error "start1 pattern not found"
- let log2 = V.find (\(tmp, logIds) -> tmp == "Loading configuration from <*>") logGroups
+ let log2 = V.find (\(_, tmp, _) -> tmp == "Loading configuration from <*>") logGroups
case log2 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["start4", "start5"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["start4", "start5"]
Nothing -> error "start4 pattern not found"
- let log3 = V.find (\(tmp, logIds) -> tmp == "Initializing Redis connection <*>") logGroups
+ let log3 = V.find (\(_, tmp, _) -> tmp == "Initializing Redis connection <*>") logGroups
case log3 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["start6", "start7"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["start6", "start7"]
Nothing -> error "start6 pattern not found"
- let log4 = V.find (\(tmp, logIds) -> tmp == "Application ready to serve requests") logGroups
+ let log4 = V.find (\(_, tmp, _) -> tmp == "Application ready to serve requests") logGroups
case log4 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["start9", "start8"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["start9", "start8"]
Nothing -> error "start8 pattern not found"
pass
@@ -116,28 +116,28 @@ spec = describe "DRAIN updateTreeWithLog" $ do
updatedTree = processBatch (V.fromList errorLogs) (testTimeOffset 0) initialTree
logGroups = getAllLogGroups updatedTree
length logGroups `shouldBe` 4
- let patterns = V.map fst logGroups
+ let patterns = V.map (\(_, template, _) -> template) logGroups
V.toList patterns `shouldMatchList`
[ "ERROR Failed to authenticate user {email}"
, "ERROR Database connection timeout after {integer}ms"
, "WARN Retrying failed request attempt {integer} of {integer}"
, "FATAL Out of memory heap size {integer}MB exceeded"
]
- let log1 = V.find (\(tmp, logIds) -> tmp == "WARN Retrying failed request attempt {integer} of {integer}") logGroups
+ let log1 = V.find (\(_, tmp, _) -> tmp == "WARN Retrying failed request attempt {integer} of {integer}") logGroups
case log1 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["err7", "err6", "err5"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["err7", "err6", "err5"]
Nothing -> error "err1 pattern not found"
- let log2 = V.find (\(tmp, logIds) -> tmp == "ERROR Failed to authenticate user {email}") logGroups
+ let log2 = V.find (\(_, tmp, _) -> tmp == "ERROR Failed to authenticate user {email}") logGroups
case log2 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["err2", "err1"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["err2", "err1"]
Nothing -> error "err2 pattern not found"
- let log3 = V.find (\(tmp, logIds) -> tmp == "ERROR Database connection timeout after {integer}ms") logGroups
+ let log3 = V.find (\(_, tmp, _) -> tmp == "ERROR Database connection timeout after {integer}ms") logGroups
case log3 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["err4", "err3"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["err4", "err3"]
Nothing -> error "err3 pattern not found"
- let log4 = V.find (\(tmp, logIds) -> tmp == "FATAL Out of memory heap size {integer}MB exceeded") logGroups
+ let log4 = V.find (\(_, tmp, _) -> tmp == "FATAL Out of memory heap size {integer}MB exceeded") logGroups
case log4 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["err9", "err8"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["err9", "err8"]
Nothing -> error "pattern not found"
pass
@@ -145,49 +145,49 @@ spec = describe "DRAIN updateTreeWithLog" $ do
let initialTree = emptyDrainTree
updatedTree = processBatch (V.fromList timestampedLogs) (testTimeOffset 0) initialTree
logGroups = getAllLogGroups updatedTree
- let patterns = V.map fst logGroups
+ let patterns = V.map (\(_, template, _) -> template) logGroups
V.toList patterns `shouldMatchList`
[ "{YYYY-MM-DDThh:mm:ss.sTZD} INFO User <*> <*> <*>"
, "{YYYY-MM-DDThh:mm:ss.sTZD} ERROR Invalid token provided <*>"
, "{YYYY-MM-DDThh:mm:ss.sTZD} WARN Rate limit exceeded client={ipv4}"
]
- let log1 = V.find (\(tmp, logIds) -> tmp == "{YYYY-MM-DDThh:mm:ss.sTZD} INFO User <*> <*> <*>") logGroups
+ let log1 = V.find (\(_, tmp, _) -> tmp == "{YYYY-MM-DDThh:mm:ss.sTZD} INFO User <*> <*> <*>") logGroups
case log1 of
- Just (_, lg) -> do
+ Just (_, _, lg) -> do
"ts1" `V.elem` lg `shouldBe` True
"ts2" `V.elem` lg `shouldBe` True
Nothing -> error "ts1 pattern not found"
- let log3 = V.find (\(tmp, logIds) -> tmp == "{YYYY-MM-DDThh:mm:ss.sTZD} ERROR Invalid token provided <*>") logGroups
+ let log3 = V.find (\(_, tmp, _) -> tmp == "{YYYY-MM-DDThh:mm:ss.sTZD} ERROR Invalid token provided <*>") logGroups
case log3 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["ts5", "ts6"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["ts5", "ts6"]
Nothing -> error "ts5 pattern not found"
- let log4 = V.find (\(tmp, logIds) -> tmp == "{YYYY-MM-DDThh:mm:ss.sTZD} WARN Rate limit exceeded client={ipv4}") logGroups
+ let log4 = V.find (\(_, tmp, _) -> tmp == "{YYYY-MM-DDThh:mm:ss.sTZD} WARN Rate limit exceeded client={ipv4}") logGroups
case log4 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["ts7", "ts8"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["ts7", "ts8"]
Nothing -> error "ts7 pattern not found"
it "should get correct log patterns for microservice logs" $ do
let initialTree = emptyDrainTree
updatedTree = processBatch (V.fromList microserviceLogs) (testTimeOffset 0) initialTree
logGroups = getAllLogGroups updatedTree
- let patterns = V.map fst logGroups
+ let patterns = V.map (\(_, template, _) -> template) logGroups
V.toList patterns `shouldMatchList`
[ "payment-service processing payment amount={float} <*>"
, "auth-service JWT validation successful for user <*>"
, "user-service database query SELECT * FROM users WHERE id={integer} took {integer}ms"
, "user-service received request <*> <*> <*>"
]
- let log1 = V.find (\(tmp, logIds) -> tmp == "user-service received request <*> <*> <*>" ) logGroups
+ let log1 = V.find (\(_, tmp, _) -> tmp == "user-service received request <*> <*> <*>" ) logGroups
case log1 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["svc2", "svc1"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["svc2", "svc1"]
Nothing -> error "svc1 pattern not found"
- let log3 = V.find (\(tmp, logIds) -> tmp == "user-service database query SELECT * FROM users WHERE id={integer} took {integer}ms") logGroups
+ let log3 = V.find (\(_, tmp, _) -> tmp == "user-service database query SELECT * FROM users WHERE id={integer} took {integer}ms") logGroups
case log3 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["svc4", "svc5"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["svc4", "svc5"]
Nothing -> error "svc4 pattern not found"
- let log4 = V.find (\(tmp, logIds) -> tmp == "auth-service JWT validation successful for user <*>") logGroups
+ let log4 = V.find (\(_, tmp, _) -> tmp == "auth-service JWT validation successful for user <*>") logGroups
case log4 of
- Just (_, lg) -> V.toList lg `shouldMatchList` ["svc7", "svc8"]
+ Just (_, _, lg) -> V.toList lg `shouldMatchList` ["svc7", "svc8"]
Nothing -> error "svc7 pattern not found"
basicHttpLogs :: [(Text, Text)]