Skip to content

Commit

Permalink
Analyze Trace Logs (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay288 authored Jan 13, 2023
1 parent cbd4f87 commit 87c82ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
28 changes: 23 additions & 5 deletions backend/src/analyze-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,17 @@ const analyze = async (
queryRunner: QueryRunner,
newEndpoint?: boolean,
) => {
const traceUUID = uuidv4()
mlog.debug(`Analyzing Trace: ${traceUUID}`)
const prevRiskScore = apiEndpoint.riskScore
const prevLastActive = apiEndpoint.lastActive
endpointUpdateDates(trace.createdAt, apiEndpoint)
mlog.debug(`Analyzing Trace - Updated Dates: ${traceUUID}`)

const start1 = performance.now()
const dataFields = await findDataFieldsToSave(ctx, trace, apiEndpoint)
mlog.time("analyzer.find_data_fields", performance.now() - start1)
mlog.debug(`Analyzing Trace - Found Datafields: ${traceUUID}`)

const start2 = performance.now()
let alerts = await SpecService.findOpenApiSpecDiff(
Expand All @@ -132,6 +136,7 @@ const analyze = async (
queryRunner,
)
mlog.time("analyzer.find_openapi_spec_diff", performance.now() - start2)
mlog.debug(`Analyzing Trace - Found OpenAPI Spec Diffs: ${traceUUID}`)

const start3 = performance.now()
const sensitiveDataAlerts = await AlertService.createDataFieldAlerts(
Expand All @@ -144,6 +149,7 @@ const analyze = async (
)
alerts = alerts?.concat(sensitiveDataAlerts)
mlog.time("analyzer.create_data_field_alerts", performance.now() - start3)
mlog.debug(`Analyzing Trace - Created Data Field Alerts: ${traceUUID}`)

if (newEndpoint) {
const newEndpointAlert = await AlertService.createAlert(
Expand Down Expand Up @@ -176,6 +182,7 @@ const analyze = async (
5,
)
mlog.time("analyzer.insert_api_trace_query", performance.now() - start4)
mlog.debug(`Analyzing Trace - Inserted API Trace: ${traceUUID}`)

const start5 = performance.now()
await retryTypeormTransaction(
Expand All @@ -186,6 +193,7 @@ const analyze = async (
5,
)
mlog.time("analyzer.insert_data_fields_query", performance.now() - start5)
mlog.debug(`Analyzing Trace - Inserted Data Fields: ${traceUUID}`)

const start6 = performance.now()
await retryTypeormTransaction(
Expand All @@ -195,14 +203,19 @@ const analyze = async (
),
5,
)
mlog.time("analyzer.update_data_fields_query", performance.now() - start6)
mlog.debug(`Analyzing Trace - Updated Data Fields: ${traceUUID}`)

const start7 = performance.now()
await retryTypeormTransaction(
() =>
insertValuesBuilder(ctx, queryRunner, Alert, alerts).orIgnore().execute(),
5,
)
mlog.time("analyzer.insert_alerts_query", performance.now() - start6)
mlog.time("analyzer.insert_alerts_query", performance.now() - start7)
mlog.debug(`Analyzing Trace - Inserted Alerts: ${traceUUID}`)

const start7 = performance.now()
const start8 = performance.now()
if (shouldUpdateEndpoint(prevRiskScore, prevLastActive, apiEndpoint)) {
await retryTypeormTransaction(
() =>
Expand All @@ -218,14 +231,19 @@ const analyze = async (
5,
)
}
mlog.time("analyzer.update_api_endpoint_query", performance.now() - start7)
mlog.time("analyzer.update_api_endpoint_query", performance.now() - start8)
mlog.debug(`Analyzing Trace - Updated API Endpoint: ${traceUUID}`)

const start8 = performance.now()
const start9 = performance.now()
await updateIPs(ctx, trace, apiEndpoint, queryRunner)
mlog.time("analyzer.update_ips", performance.now() - start8)
mlog.time("analyzer.update_ips", performance.now() - start9)
mlog.debug(`Analyzing Trace - Updated IPs: ${traceUUID}`)
await queryRunner.commitTransaction()

const start10 = performance.now()
await sendWebhookRequests(ctx, alerts, apiEndpoint)
mlog.time("analyzer.sent_webhook_requests", performance.now() - start10)
mlog.debug(`Analyzing Trace - Sent Webhook Requests: ${traceUUID}`)
}

const generateEndpoint = async (
Expand Down
3 changes: 3 additions & 0 deletions backend/src/api/collector/log-request/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mlog from "logger"
import { Response } from "express"
import { LogRequestService } from "services/log-request"
import { TraceParams } from "@common/types"
Expand All @@ -8,6 +9,7 @@ export const logRequestSingleHandler = async (
req: MetloRequest,
res: Response,
): Promise<void> => {
mlog.debug("Called Log Request Single Handler")
const traceParams: TraceParams = req.body
try {
await LogRequestService.logRequest(req.ctx, traceParams)
Expand All @@ -21,6 +23,7 @@ export const logRequestBatchHandler = async (
req: MetloRequest,
res: Response,
): Promise<void> => {
mlog.debug("Called Log Request Batch Handler")
const traceParamsBatch: TraceParams[] = req.body
try {
await LogRequestService.logRequestBatch(req.ctx, traceParamsBatch)
Expand Down
7 changes: 6 additions & 1 deletion backend/src/services/log-request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@ export class LogRequestService {
ctx: MetloContext,
traceParams: TraceParams,
): Promise<void> {
mlog.debug("Called Log Request Service Func")
const unsafeRedisClient = RedisClient.getInstance()
try {
/** Log Request in ApiTrace table **/
let queueLength = 0
try {
queueLength = await unsafeRedisClient.llen(TRACES_QUEUE)
} catch {}
} catch (err) {
mlog.withErr(err).debug(`Error checking queue length`)
}
mlog.debug(`Trace queue length ${queueLength}`)
if (queueLength > 1000) {
mlog.debug("Trace queue overloaded")
return
}

const validPath = getValidPath(traceParams?.request?.url?.path)
if (!validPath.isValid) {
mlog.debug(`Invalid Path: ${traceParams?.request?.url?.path}`)
throw new Error400BadRequest(
`Invalid path ${traceParams?.request?.url?.path}: ${validPath.errMsg}`,
)
Expand Down

0 comments on commit 87c82ec

Please sign in to comment.