refactor: Update OTelIngest.ts to improve code readability and maintainability

This commit is contained in:
Simon Larsen 2024-06-07 21:27:11 +01:00
parent 0f49e6e100
commit 0a16f6bf44
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
2 changed files with 71 additions and 29 deletions

View File

@ -5,7 +5,6 @@ import OTelIngestService from '../Service/OTelIngest';
import OneUptimeDate from 'Common/Types/Date';
import BadRequestException from 'Common/Types/Exception/BadRequestException';
import { JSONArray, JSONObject } from 'Common/Types/JSON';
import JSONFunctions from 'Common/Types/JSONFunctions';
import ProductType from 'Common/Types/MeteredPlan/ProductType';
import Text from 'Common/Types/Text';
import LogService from 'CommonServer/Services/LogService';
@ -320,7 +319,7 @@ router.post(
const metricUnit: string = metric['unit'] as string;
let dbMetric: Metric = new Metric();
const dbMetric: Metric = new Metric();
dbMetric.projectId = (
req as TelemetryRequest
@ -351,14 +350,25 @@ router.post(
}
if (
resourceMetric['attributes'] &&
resourceMetric['attributes'] instanceof Array &&
resourceMetric['attributes'].length > 0
resourceMetric['resource'] &&
(resourceMetric['resource'] as JSONObject)[
'attributes'
] &&
((resourceMetric['resource'] as JSONObject)[
'attributes'
] as JSONArray) instanceof Array &&
(
(resourceMetric['resource'] as JSONObject)[
'attributes'
] as JSONArray
).length > 0
) {
attributesObject = {
...attributesObject,
...OTelIngestService.getAttributes(
resourceMetric['attributes'] as JSONArray
(resourceMetric['resource'] as JSONObject)[
'attributes'
] as JSONArray
),
};
}
@ -369,13 +379,14 @@ router.post(
) {
attributesObject = {
...attributesObject,
...((scopeMetric['scope'] as JSONObject) || {}),
...{
scope:
(scopeMetric['scope'] as JSONObject) ||
{},
},
};
}
dbMetric.attributes =
JSONFunctions.flattenObject(attributesObject);
if (
metric['sum'] &&
(metric['sum'] as JSONObject)['dataPoints'] &&
@ -388,12 +399,13 @@ router.post(
for (const datapoint of (
metric['sum'] as JSONObject
)['dataPoints'] as JSONArray) {
dbMetric =
const sumMetric: Metric =
OTelIngestService.getMetricFromDatapoint(
dbMetric,
datapoint
);
dbMetrics.push(dbMetric);
dbMetrics.push(sumMetric);
}
} else if (
metric['gauge'] &&
@ -407,12 +419,13 @@ router.post(
for (const datapoint of (
metric['gauge'] as JSONObject
)['dataPoints'] as JSONArray) {
dbMetric =
const guageMetric: Metric =
OTelIngestService.getMetricFromDatapoint(
dbMetric,
datapoint
);
dbMetrics.push(dbMetric);
dbMetrics.push(guageMetric);
}
} else if (
metric['histogram'] &&
@ -426,12 +439,13 @@ router.post(
for (const datapoint of (
metric['histogram'] as JSONObject
)['dataPoints'] as JSONArray) {
dbMetric =
const histogramMetric: Metric =
OTelIngestService.getMetricFromDatapoint(
dbMetric,
datapoint
);
dbMetrics.push(dbMetric);
dbMetrics.push(histogramMetric);
}
} else {
logger.warn('Unknown metric type');

View File

@ -32,31 +32,59 @@ export default class OTelIngestService {
dbMetric: Metric,
datapoint: JSONObject
): Metric {
dbMetric.startTimeUnixNano = datapoint['startTimeUnixNano'] as number;
dbMetric.startTime = OneUptimeDate.fromUnixNano(
const newDbMetric: Metric = Metric.fromJSON(
dbMetric.toJSON(),
Metric
) as Metric;
newDbMetric.startTimeUnixNano = datapoint[
'startTimeUnixNano'
] as number;
newDbMetric.startTime = OneUptimeDate.fromUnixNano(
datapoint['startTimeUnixNano'] as number
);
dbMetric.timeUnixNano = datapoint['timeUnixNano'] as number;
dbMetric.time = OneUptimeDate.fromUnixNano(
newDbMetric.timeUnixNano = datapoint['timeUnixNano'] as number;
newDbMetric.time = OneUptimeDate.fromUnixNano(
datapoint['timeUnixNano'] as number
);
if (Object.keys(datapoint).includes('asInt')) {
dbMetric.value = datapoint['asInt'] as number;
newDbMetric.value = datapoint['asInt'] as number;
} else if (Object.keys(datapoint).includes('asDouble')) {
dbMetric.value = datapoint['asDouble'] as number;
newDbMetric.value = datapoint['asDouble'] as number;
}
dbMetric.count = datapoint['count'] as number;
dbMetric.sum = datapoint['sum'] as number;
newDbMetric.count = datapoint['count'] as number;
newDbMetric.sum = datapoint['sum'] as number;
dbMetric.min = datapoint['min'] as number;
dbMetric.max = datapoint['max'] as number;
newDbMetric.min = datapoint['min'] as number;
newDbMetric.max = datapoint['max'] as number;
dbMetric.bucketCounts = datapoint['bucketCounts'] as Array<number>;
dbMetric.explicitBounds = datapoint['explicitBounds'] as Array<number>;
newDbMetric.bucketCounts = datapoint['bucketCounts'] as Array<number>;
newDbMetric.explicitBounds = datapoint[
'explicitBounds'
] as Array<number>;
return dbMetric;
// attrbutes
if (Object.keys(datapoint).includes('attributes')) {
if (!newDbMetric.attributes) {
newDbMetric.attributes = {};
}
newDbMetric.attributes = {
...(newDbMetric.attributes || {}),
...this.getAttributes(datapoint['attributes'] as JSONArray),
};
}
if (newDbMetric.attributes) {
newDbMetric.attributes = JSONFunctions.flattenObject(
newDbMetric.attributes
);
}
return newDbMetric;
}
}