forked from OneUptime/oneuptime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request OneUptime#1625 from OneUptime/log-monitors
Log monitors
- Loading branch information
Showing
38 changed files
with
688 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
App/FeatureSet/Workers/Jobs/TelemetryMonitor/MonitorTelemetryMonitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import OneUptimeDate from "Common/Types/Date"; | ||
import RunCron from "../../Utils/Cron"; | ||
import LIMIT_MAX from "Common/Types/Database/LimitMax"; | ||
import IncomingMonitorRequest from "Common/Types/Monitor/IncomingMonitor/IncomingMonitorRequest"; | ||
import MonitorType from "Common/Types/Monitor/MonitorType"; | ||
import { EVERY_MINUTE } from "Common/Utils/CronTime"; | ||
import MonitorService from "CommonServer/Services/MonitorService"; | ||
import QueryHelper from "CommonServer/Types/Database/QueryHelper"; | ||
import logger from "CommonServer/Utils/Logger"; | ||
import MonitorResourceUtil from "CommonServer/Utils/Monitor/MonitorResource"; | ||
import Monitor from "Model/Models/Monitor"; | ||
import CronTab from "CommonServer/Utils/CronTab"; | ||
|
||
RunCron( | ||
"LogMonitor:MonitorLogMonitor", | ||
{ schedule: EVERY_MINUTE, runOnStartup: false }, | ||
async () => { | ||
logger.debug("Checking LogMonitor:MonitorLogMonitor"); | ||
|
||
const telemetryMonitors: Array<Monitor> = await MonitorService.findBy({ | ||
query: { | ||
monitorType: QueryHelper.any([ | ||
MonitorType.Logs, | ||
MonitorType.Traces, | ||
MonitorType.Metrics, | ||
]), | ||
telemetryMonitorNextMonitorAt: QueryHelper.lessThanEqualToOrNull( | ||
OneUptimeDate.getCurrentDate(), | ||
), | ||
}, | ||
props: { | ||
isRoot: true, | ||
}, | ||
select: { | ||
_id: true, | ||
monitorSteps: true, | ||
createdAt: true, | ||
monitoringInterval: true, | ||
}, | ||
limit: LIMIT_MAX, | ||
skip: 0, | ||
}); | ||
|
||
const updatePromises: Array<Promise<void>> = []; | ||
|
||
for (const telemetryMonitor of telemetryMonitors) { | ||
if (!telemetryMonitor.monitoringInterval) { | ||
continue; | ||
} | ||
|
||
let nextPing: Date = OneUptimeDate.addRemoveMinutes( | ||
OneUptimeDate.getCurrentDate(), | ||
1, | ||
); | ||
|
||
try { | ||
nextPing = CronTab.getNextExecutionTime( | ||
telemetryMonitor?.monitoringInterval as string, | ||
); | ||
} catch (err) { | ||
logger.error(err); | ||
} | ||
|
||
updatePromises.push( | ||
MonitorService.updateOneById({ | ||
id: telemetryMonitor.id!, | ||
data: { | ||
telemetryMonitorLastMonitorAt: OneUptimeDate.getCurrentDate(), | ||
telemetryMonitorNextMonitorAt: nextPing, | ||
}, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
await Promise.all(updatePromises); | ||
|
||
logger.debug(`Found ${telemetryMonitors.length} telemetry monitors`); | ||
|
||
logger.debug(telemetryMonitors); | ||
|
||
for (const monitor of telemetryMonitors) { | ||
try { | ||
if (!monitor.monitorSteps) { | ||
logger.debug("Monitor has no steps. Skipping..."); | ||
continue; | ||
} | ||
|
||
const incomingRequest: IncomingMonitorRequest = { | ||
monitorId: monitor.id!, | ||
requestHeaders: undefined, | ||
requestBody: undefined, | ||
requestMethod: undefined, | ||
incomingRequestReceivedAt: | ||
monitor.incomingRequestReceivedAt || monitor.createdAt!, | ||
onlyCheckForIncomingRequestReceivedAt: true, | ||
}; | ||
|
||
await MonitorResourceUtil.monitorResource(incomingRequest); | ||
} catch (error) { | ||
logger.error( | ||
`Error while processing incoming request monitor: ${monitor.id?.toString()}`, | ||
); | ||
logger.error(error); | ||
} | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
enum LogSeverity { | ||
Unspecified = "Unspecified", | ||
Information = "Information", | ||
Warning = "Warning", | ||
Error = "Error", | ||
Trace = "Trace", | ||
Debug = "Debug", | ||
Fatal = "Fatal", | ||
} | ||
|
||
export default LogSeverity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Dictionary from "../Dictionary"; | ||
import { JSONObject } from "../JSON"; | ||
import LogSeverity from "../Log/LogSeverity"; | ||
import ObjectID from "../ObjectID"; | ||
|
||
export default interface MonitorStepLogMonitor { | ||
attributes: Dictionary<string | number | boolean>; | ||
body: string; | ||
severityText: Array<LogSeverity>; | ||
telemetryServiceId: Array<ObjectID>; | ||
lastXSecondsOfLogs: number; | ||
} | ||
|
||
export class MonitorStepLogMonitorUtil { | ||
public static getDefault(): MonitorStepLogMonitor { | ||
return { | ||
attributes: {}, | ||
body: "", | ||
severityText: [], | ||
telemetryServiceId: [], | ||
lastXSecondsOfLogs: 60, | ||
}; | ||
} | ||
|
||
public static fromJSON(json: JSONObject): MonitorStepLogMonitor { | ||
return { | ||
attributes: | ||
(json["attributes"] as Dictionary<string | number | boolean>) || {}, | ||
body: json["body"] as string, | ||
severityText: json["severityText"] as Array<LogSeverity>, | ||
telemetryServiceId: ObjectID.fromJSONArray( | ||
json["telemetryServiceId"] as Array<JSONObject>, | ||
), | ||
lastXSecondsOfLogs: json["lastXSecondsOfLogs"] as number, | ||
}; | ||
} | ||
|
||
public static toJSON(monitor: MonitorStepLogMonitor): JSONObject { | ||
return { | ||
attributes: monitor.attributes, | ||
body: monitor.body, | ||
severityText: monitor.severityText, | ||
telemetryServiceId: ObjectID.toJSONArray(monitor.telemetryServiceId), | ||
lastXSecondsOfLogs: monitor.lastXSecondsOfLogs, | ||
}; | ||
} | ||
} |
Oops, something went wrong.