diff --git a/Ingestor/API/OTelIngest.ts b/Ingestor/API/OTelIngest.ts index 1c051ddbcd..6283d95ec0 100644 --- a/Ingestor/API/OTelIngest.ts +++ b/Ingestor/API/OTelIngest.ts @@ -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'); diff --git a/Ingestor/Service/OTelIngest.ts b/Ingestor/Service/OTelIngest.ts index aab2b8800a..1caa1e71ae 100644 --- a/Ingestor/Service/OTelIngest.ts +++ b/Ingestor/Service/OTelIngest.ts @@ -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; - dbMetric.explicitBounds = datapoint['explicitBounds'] as Array; + newDbMetric.bucketCounts = datapoint['bucketCounts'] as Array; + newDbMetric.explicitBounds = datapoint[ + 'explicitBounds' + ] as Array; - 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; } }