diff --git a/packages/api/src/utils/common.ts b/packages/api/src/utils/common.ts index 0888f8c87..293a72354 100644 --- a/packages/api/src/utils/common.ts +++ b/packages/api/src/utils/common.ts @@ -1,6 +1,16 @@ +export type JSONBlob = Record; + +export type Json = + | string + | number + | boolean + | null + | Json[] + | { [key: string]: Json }; + export const useTry = (fn: () => T): [null | Error | unknown, null | T] => { - let output: T | null = null; - let error: any = null; + let output: null | T = null; + let error: null | Error | unknown = null; try { output = fn(); return [error, output]; @@ -10,9 +20,7 @@ export const useTry = (fn: () => T): [null | Error | unknown, null | T] => { } }; -export const tryJSONStringify = ( - json: Record | Record[], -) => { +export const tryJSONStringify = (json: Json) => { const [_, result] = useTry(() => JSON.stringify(json)); return result; }; diff --git a/packages/api/src/utils/logParser.ts b/packages/api/src/utils/logParser.ts index 9041cfd05..1e96488d8 100644 --- a/packages/api/src/utils/logParser.ts +++ b/packages/api/src/utils/logParser.ts @@ -1,9 +1,7 @@ import _ from 'lodash'; +import type { Json, JSONBlob } from './common'; import { tryJSONStringify } from './common'; - -export type JSONBlob = Record; - export type KeyPath = string[]; export enum AggregationTemporality { @@ -85,7 +83,7 @@ export function* traverseJson( currentNode: JSONBlob, depth = 1, keyPathArray?: KeyPath, -): IterableIterator<[KeyPath, any]> { +): IterableIterator<[KeyPath, Json]> { for (const [key, value] of Object.entries(currentNode)) { const keyPath = keyPathArray ? [...keyPathArray, key] : [key]; @@ -119,7 +117,7 @@ export const mapObjectToKeyValuePairs = ( const pushArray = ( type: 'bool' | 'number' | 'string', keyPath: string, - value: any, + value: number | string, // Note that booleans are converted to 0 or 1 ) => { const keyNames = `${type}.names`; const keyValues = `${type}.values`; @@ -229,14 +227,11 @@ export type VectorMetric = { v: number; // value }; -abstract class ParsingInterface { - abstract _parse( - log: T, - ...args: any[] - ): LogStreamModel | MetricModel | RrwebEventModel; +abstract class ParsingInterface { + abstract _parse(log: T, ...args: any[]): S; parse(logs: T[], ...args: any[]) { - const parsedLogs: any[] = []; + const parsedLogs: S[] = []; for (const log of logs) { try { parsedLogs.push(this._parse(log, ...args)); @@ -249,7 +244,7 @@ abstract class ParsingInterface { } } -class VectorLogParser extends ParsingInterface { +class VectorLogParser extends ParsingInterface { getType(log: VectorLog): LogType { if (log.hdx_platform === LogPlatform.OtelTraces) { return LogType.Span; @@ -282,10 +277,10 @@ class VectorLogParser extends ParsingInterface { } } -class VectorMetricParser extends ParsingInterface { +class VectorMetricParser extends ParsingInterface { _parse(metric: VectorMetric): MetricModel { return { - _string_attributes: metric.b, + _string_attributes: metric.b as any, // TODO: fix conversion of metric.b to proper string map data_type: metric.dt, is_delta: metric.at === AggregationTemporality.Delta, is_monotonic: metric.im, @@ -297,7 +292,7 @@ class VectorMetricParser extends ParsingInterface { } } -class VectorRrwebParser extends ParsingInterface { +class VectorRrwebParser extends ParsingInterface { _parse(log: VectorLog): RrwebEventModel { return { ...mapObjectToKeyValuePairs(log.b),