Files
oneuptime/App/FeatureSet/Telemetry/Services/OtelIngestBaseService.ts
Nawaz Dhandala 5f398bdb31 Add utility classes for telemetry: Monitor, StackTrace, and Syslog parsing
- Implemented MonitorUtil for managing monitor secrets and populating them in monitor steps and tests.
- Created StackTraceParser to parse and structure stack traces from various programming languages.
- Developed SyslogParser to handle and parse syslog messages in both RFC 5424 and RFC 3164 formats.
2026-04-02 14:04:13 +01:00

118 lines
3.2 KiB
TypeScript

import { ExpressRequest } from "Common/Server/Utils/Express";
import CaptureSpan from "Common/Server/Utils/Telemetry/CaptureSpan";
import { JSONArray, JSONObject, JSONValue } from "Common/Types/JSON";
import ObjectID from "Common/Types/ObjectID";
import KubernetesClusterService from "Common/Server/Services/KubernetesClusterService";
import KubernetesCluster from "Common/Models/DatabaseModels/KubernetesCluster";
import logger from "Common/Server/Utils/Logger";
export default abstract class OtelIngestBaseService {
@CaptureSpan()
protected static getServiceNameFromAttributes(
req: ExpressRequest,
attributes: JSONArray,
): string {
for (const attribute of attributes) {
if (
attribute["key"] === "service.name" &&
attribute["value"] &&
(attribute["value"] as JSONObject)["stringValue"]
) {
if (
typeof (attribute["value"] as JSONObject)["stringValue"] === "string"
) {
return (attribute["value"] as JSONObject)["stringValue"] as string;
}
}
}
const serviceName: string = req.headers[
"x-oneuptime-service-name"
] as string;
if (serviceName) {
return serviceName;
}
return "Unknown Service";
}
@CaptureSpan()
protected static getClusterNameFromAttributes(
attributes: JSONArray,
): string | null {
for (const attribute of attributes) {
if (
attribute["key"] === "k8s.cluster.name" &&
attribute["value"] &&
(attribute["value"] as JSONObject)["stringValue"]
) {
const value: JSONValue = (attribute["value"] as JSONObject)[
"stringValue"
];
if (typeof value === "string" && value.trim()) {
return value.trim();
}
}
}
return null;
}
@CaptureSpan()
protected static async autoDiscoverKubernetesCluster(data: {
projectId: ObjectID;
attributes: JSONArray;
}): Promise<void> {
try {
const clusterName: string | null = this.getClusterNameFromAttributes(
data.attributes,
);
if (!clusterName) {
return;
}
const cluster: KubernetesCluster =
await KubernetesClusterService.findOrCreateByClusterIdentifier({
projectId: data.projectId,
clusterIdentifier: clusterName,
});
if (cluster._id) {
await KubernetesClusterService.updateLastSeen(
new ObjectID(cluster._id.toString()),
);
}
} catch (err) {
logger.error(
"Error auto-discovering Kubernetes cluster: " + (err as Error).message,
);
}
}
@CaptureSpan()
protected static getServiceNameFromHeaders(
req: ExpressRequest,
defaultName: string = "Unknown Service",
): string {
const headerValue: string | string[] | undefined =
req.headers["x-oneuptime-service-name"];
if (typeof headerValue === "string" && headerValue.trim()) {
return headerValue.trim();
}
if (Array.isArray(headerValue) && headerValue.length > 0) {
const value: string = headerValue.find((item: string) => {
return item && item.trim();
}) as string;
if (value && value.trim()) {
return value.trim();
}
}
return defaultName;
}
}