Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogParser: update types to eliminate the dreaded any, add Json type #200

Merged
merged 8 commits into from
Jan 12, 2024
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 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be JSONBlob

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try it and see ;) It'll raise a circular definition error, at least when I did, it did.

Copy link
Contributor

@wrn14897 wrn14897 Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah...good point. didn't notice the circular ref. its no big deal. but I'd use Record<string, Json> instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also circular, weirdly enough. It has to be a primitive literal in order to hit the circular reference type edge case.


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
jaggederest marked this conversation as resolved.
Show resolved Hide resolved
) => {
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[]) {
jaggederest marked this conversation as resolved.
Show resolved Hide resolved
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,
jaggederest marked this conversation as resolved.
Show resolved Hide resolved
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
Loading