Skip to content

Commit

Permalink
LogParser: update types to eliminate the dreaded any, add Json type (
Browse files Browse the repository at this point in the history
  • Loading branch information
jaggederest authored Jan 12, 2024
1 parent cf6eb69 commit 3311001
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
18 changes: 13 additions & 5 deletions packages/api/src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
export type JSONBlob = Record<string, Json>;

export type Json =
| string
| number
| boolean
| null
| Json[]
| { [key: string]: Json };

export const useTry = <T>(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];
Expand All @@ -10,9 +20,7 @@ export const useTry = <T>(fn: () => T): [null | Error | unknown, null | T] => {
}
};

export const tryJSONStringify = (
json: Record<string, unknown> | Record<string, unknown>[],
) => {
export const tryJSONStringify = (json: Json) => {
const [_, result] = useTry<string>(() => JSON.stringify(json));
return result;
};
Expand Down
25 changes: 10 additions & 15 deletions packages/api/src/utils/logParser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import _ from 'lodash';

import type { Json, JSONBlob } from './common';
import { tryJSONStringify } from './common';

export type JSONBlob = Record<string, any>;

export type KeyPath = string[];

export enum AggregationTemporality {
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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`;
Expand Down Expand Up @@ -229,14 +227,11 @@ export type VectorMetric = {
v: number; // value
};

abstract class ParsingInterface<T> {
abstract _parse(
log: T,
...args: any[]
): LogStreamModel | MetricModel | RrwebEventModel;
abstract class ParsingInterface<T, S> {
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));
Expand All @@ -249,7 +244,7 @@ abstract class ParsingInterface<T> {
}
}

class VectorLogParser extends ParsingInterface<VectorLog> {
class VectorLogParser extends ParsingInterface<VectorLog, LogStreamModel> {
getType(log: VectorLog): LogType {
if (log.hdx_platform === LogPlatform.OtelTraces) {
return LogType.Span;
Expand Down Expand Up @@ -282,10 +277,10 @@ class VectorLogParser extends ParsingInterface<VectorLog> {
}
}

class VectorMetricParser extends ParsingInterface<VectorMetric> {
class VectorMetricParser extends ParsingInterface<VectorMetric, MetricModel> {
_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,
Expand All @@ -297,7 +292,7 @@ class VectorMetricParser extends ParsingInterface<VectorMetric> {
}
}

class VectorRrwebParser extends ParsingInterface<VectorLog> {
class VectorRrwebParser extends ParsingInterface<VectorLog, RrwebEventModel> {
_parse(log: VectorLog): RrwebEventModel {
return {
...mapObjectToKeyValuePairs(log.b),
Expand Down

0 comments on commit 3311001

Please sign in to comment.