Skip to content

Commit

Permalink
refactor(otlp-transformer): json schema based validation
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Dec 13, 2024
1 parent c0d0966 commit 6063edb
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 537 deletions.
2 changes: 1 addition & 1 deletion api/test/common/diag/logLevel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('LogLevelFilter DiagLogger', () => {

const levelMap: Array<{
message: string;
level: DiagLogLevel;
level: number | DiagLogLevel;
ignoreFuncs: Array<keyof DiagLogger>;
}> = [
{ message: 'ALL', level: DiagLogLevel.ALL, ignoreFuncs: [] },
Expand Down
3 changes: 2 additions & 1 deletion experimental/packages/otlp-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@
"nyc": "15.1.0",
"protobufjs-cli": "1.1.3",
"ts-loader": "9.5.1",
"typescript": "4.4.4",
"typescript": "5.7.2",
"webpack": "5.96.1"
},
"dependencies": {
"@sinclair/typebox": "0.34.11",
"@opentelemetry/api-logs": "0.56.0",
"@opentelemetry/core": "1.29.0",
"@opentelemetry/resources": "1.29.0",
Expand Down
134 changes: 68 additions & 66 deletions experimental/packages/otlp-transformer/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,74 @@
* limitations under the License.
*/

/** Properties of an InstrumentationScope. */
export interface IInstrumentationScope {
/** InstrumentationScope name */
name: string;

/** InstrumentationScope version */
version?: string;

/** InstrumentationScope attributes */
attributes?: IKeyValue[];

/** InstrumentationScope droppedAttributesCount */
droppedAttributesCount?: number;
}

/** Properties of a KeyValue. */
export interface IKeyValue {
/** KeyValue key */
key: string;

/** KeyValue value */
value: IAnyValue;
}

/** Properties of an AnyValue. */
export interface IAnyValue {
/** AnyValue stringValue */
stringValue?: string | null;

/** AnyValue boolValue */
boolValue?: boolean | null;

/** AnyValue intValue */
intValue?: number | null;

/** AnyValue doubleValue */
doubleValue?: number | null;

/** AnyValue arrayValue */
arrayValue?: IArrayValue;

/** AnyValue kvlistValue */
kvlistValue?: IKeyValueList;

/** AnyValue bytesValue */
bytesValue?: Uint8Array;
}

/** Properties of an ArrayValue. */
export interface IArrayValue {
/** ArrayValue values */
values: IAnyValue[];
}

/** Properties of a KeyValueList. */
export interface IKeyValueList {
/** KeyValueList values */
values: IKeyValue[];
}

export interface LongBits {
low: number;
high: number;
}

export type Fixed64 = LongBits | string | number;
import { Type, type Static } from "@sinclair/typebox";

export const OtelCommonTypes = Type.Module({
ResourceAttributes: Type.Object({
key: Type.String(),
value: Type.Any(),
}),
IResource: Type.Object({
attributes: Type.Array(Type.Ref("ResourceAttributes")),
}),
IKeyValue: Type.Object({
key: Type.String(),
value: Type.Ref("IAnyValue"),
}),
IKeyValueList: Type.Object({
values: Type.Array(Type.Ref("IKeyValue")),
}),
IArrayValue: Type.Object({
values: Type.Array(Type.Ref("IAnyValue")),
}),
IAnyValue: Type.Object({
stringValue: Type.Optional(Type.Union([Type.String(), Type.Null()])),
boolValue: Type.Optional(Type.Union([Type.Boolean(), Type.Null()])),
intValue: Type.Optional(Type.Union([Type.Number(), Type.Null()])),
doubleValue: Type.Optional(Type.Union([Type.Number(), Type.Null()])),
arrayValue: Type.Optional(Type.Ref("IArrayValue")),
kvlistValue: Type.Optional(Type.Ref("IKeyValueList")),
bytesValue: Type.Optional(Type.Uint8Array()),
}),
IInstrumentationScope: Type.Object({
name: Type.String(),
version: Type.Optional(Type.String()),
attributes: Type.Optional(Type.Array(Type.Ref("IKeyValue"))),
droppedAttributesCount: Type.Optional(Type.Number()),
}),
LongBits: Type.Object({
low: Type.Number(),
high: Type.Number(),
}),
Fixed64: Type.Union([Type.Ref("LongBits"), Type.String(), Type.Number()]),
});

export const TResourceAttributes = OtelCommonTypes.Import("ResourceAttributes");
export type ResourceAttributes = Static<typeof TResourceAttributes>;

export const TResource = OtelCommonTypes.Import("IResource");
export type IResource = Static<typeof TResource>;

export const TKeyValue = OtelCommonTypes.Import("IKeyValue");
export type IKeyValue = Static<typeof TKeyValue>;

export const TKeyValueList = OtelCommonTypes.Import("IKeyValueList");
export type IKeyValueList = Static<typeof TKeyValueList>;

export const TArrayValue = OtelCommonTypes.Import("IArrayValue");
export type IArrayValue = Static<typeof TArrayValue>;

export const TAnyValue = OtelCommonTypes.Import("IAnyValue");
export type IAnyValue = Static<typeof TAnyValue>;

export const TInstrumentationScope = OtelCommonTypes.Import("IInstrumentationScope");
export type IInstrumentationScope = Static<typeof TInstrumentationScope>;

export const TLongBits = OtelCommonTypes.Import("LongBits");
export type LongBits = Static<typeof TLongBits>;

export const TFixed64 = OtelCommonTypes.Import("Fixed64");
export type Fixed64 = Static<typeof TFixed64>;

export interface OtlpEncodingOptions {
/** Convert trace and span IDs to hex strings. */
Expand Down
115 changes: 44 additions & 71 deletions experimental/packages/otlp-transformer/src/logs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,9 @@
* limitations under the License.
*/

import type {
Fixed64,
IAnyValue,
IInstrumentationScope,
IKeyValue,
} from '../common/types';
import type { IResource } from '../resource/types';

/** Properties of an ExportLogsServiceRequest. */
export interface IExportLogsServiceRequest {
/** ExportLogsServiceRequest resourceLogs */
resourceLogs?: IResourceLogs[];
}
import { Type, type Static } from "@sinclair/typebox";
import { TKeyValue, TFixed64, TAnyValue, TInstrumentationScope } from "../common/types";
import { TResource } from '../resource/types';

export interface IExportLogsServiceResponse {
/** ExportLogsServiceResponse partialSuccess */
Expand All @@ -41,67 +31,10 @@ export interface IExportLogsPartialSuccess {
errorMessage?: string;
}

/** Properties of a ResourceLogs. */
export interface IResourceLogs {
/** ResourceLogs resource */
resource?: IResource;

/** ResourceLogs scopeLogs */
scopeLogs: IScopeLogs[];

/** ResourceLogs schemaUrl */
schemaUrl?: string;
}

/** Properties of an ScopeLogs. */
export interface IScopeLogs {
/** IScopeLogs scope */
scope?: IInstrumentationScope;

/** IScopeLogs logRecords */
logRecords?: ILogRecord[];

/** IScopeLogs schemaUrl */
schemaUrl?: string | null;
}

/** Properties of a LogRecord. */
export interface ILogRecord {
/** LogRecord timeUnixNano */
timeUnixNano: Fixed64;

/** LogRecord observedTimeUnixNano */
observedTimeUnixNano: Fixed64;

/** LogRecord severityNumber */
severityNumber?: ESeverityNumber;

/** LogRecord severityText */
severityText?: string;

/** LogRecord body */
body?: IAnyValue;

/** LogRecord attributes */
attributes: IKeyValue[];

/** LogRecord droppedAttributesCount */
droppedAttributesCount: number;

/** LogRecord flags */
flags?: number;

/** LogRecord traceId */
traceId?: string | Uint8Array;

/** LogRecord spanId */
spanId?: string | Uint8Array;
}

/**
* Numerical value of the severity, normalized to values described in Log Data Model.
*/
export const enum ESeverityNumber {
export enum ESeverityNumber {
/** Unspecified. Do NOT use as default */
SEVERITY_NUMBER_UNSPECIFIED = 0,
SEVERITY_NUMBER_TRACE = 1,
Expand Down Expand Up @@ -129,3 +62,43 @@ export const enum ESeverityNumber {
SEVERITY_NUMBER_FATAL3 = 23,
SEVERITY_NUMBER_FATAL4 = 24,
}

export const OtelLogTypes = Type.Module({
ILogRecord: Type.Object({
timeUnixNano: TFixed64,
observedTimeUnixNano: TFixed64,
severityNumber: Type.Optional(Type.Enum(ESeverityNumber)),
severityText: Type.Optional(Type.String()),
body: Type.Optional(TAnyValue),
attributes: Type.Array(TKeyValue),
droppedAttributesCount: Type.Number(),
flags: Type.Optional(Type.Number()),
traceId: Type.Optional(Type.Union([Type.String(), Type.Uint8Array()])),
spanId: Type.Optional(Type.Union([Type.String(), Type.Uint8Array()])),
}),
IScopeLogs: Type.Object({
scope: Type.Optional(TInstrumentationScope),
logRecords: Type.Optional(Type.Array(Type.Ref("ILogRecord"))),
schemaUrl: Type.Optional(Type.Union([Type.String(), Type.Null()])),
}),
IResourceLogs: Type.Object({
resource: Type.Optional(TResource),
scopeLogs: Type.Array(Type.Ref("IScopeLogs")),
schemaUrl: Type.Optional(Type.String()),
}),
IExportLogsServiceRequest: Type.Object({
resourceLogs: Type.Optional(Type.Array(Type.Ref("IResourceLogs"))),
}),
});

export const TLogRecord = OtelLogTypes.Import("ILogRecord");
export type ILogRecord = Static<typeof TLogRecord>;

export const TScopeLogs = OtelLogTypes.Import("IScopeLogs");
export type IScopeLogs = Static<typeof TScopeLogs>;

export const TResourceLogs = OtelLogTypes.Import("IResourceLogs");
export type IResourceLogs = Static<typeof TResourceLogs>;

export const TExportLogsServiceRequest = OtelLogTypes.Import("IExportLogsServiceRequest");
export type IExportLogsServiceRequest = Static<typeof TExportLogsServiceRequest>;
Loading

0 comments on commit 6063edb

Please sign in to comment.