Skip to content

Commit

Permalink
refactor(otlp-transformer): re-struture package to prepare for separa…
Browse files Browse the repository at this point in the history
…te entrypoints
  • Loading branch information
pichlermarc committed Dec 13, 2024
1 parent 6d31a18 commit 5ad61ec
Show file tree
Hide file tree
Showing 37 changed files with 744 additions and 495 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
* limitations under the License.
*/

/** Properties of a Resource. */
export interface IResource {
/** Resource attributes */
attributes: IKeyValue[];

/** Resource droppedAttributesCount */
droppedAttributesCount: number;
}

/** Properties of an InstrumentationScope. */
export interface IInstrumentationScope {
/** InstrumentationScope name */
Expand Down
15 changes: 14 additions & 1 deletion experimental/packages/otlp-transformer/src/common/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { IAnyValue, IInstrumentationScope, IKeyValue } from './types';
import type {
IAnyValue,
IInstrumentationScope,
IKeyValue,
IResource,
} from './internal-types';
import { Attributes } from '@opentelemetry/api';
import { InstrumentationScope } from '@opentelemetry/core';
import { IResource as ISdkResource } from '@opentelemetry/resources';

export function createResource(resource: ISdkResource): IResource {
return {
attributes: toAttributes(resource.attributes),
droppedAttributesCount: 0,
};
}

export function createInstrumentationScope(
scope: InstrumentationScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { OtlpEncodingOptions, Fixed64, LongBits } from './types';
import type { OtlpEncodingOptions, Fixed64, LongBits } from './internal-types';
import { HrTime } from '@opentelemetry/api';
import { hexToBinary, hrTimeToNanoseconds } from '@opentelemetry/core';

Expand Down
27 changes: 10 additions & 17 deletions experimental/packages/otlp-transformer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,19 @@
export {
IExportMetricsPartialSuccess,
IExportMetricsServiceResponse,
} from './metrics/types';
} from './metrics';
export {
IExportTracePartialSuccess,
IExportTraceServiceResponse,
} from './trace/types';
export {
IExportLogsServiceResponse,
IExportLogsPartialSuccess,
} from './logs/types';
} from './trace';
export { IExportLogsServiceResponse, IExportLogsPartialSuccess } from './logs';

export {
ProtobufLogsSerializer,
ProtobufMetricsSerializer,
ProtobufTraceSerializer,
} from './protobuf/serializers';
export { ProtobufLogsSerializer } from './logs/protobuf';
export { ProtobufMetricsSerializer } from './metrics/protobuf';
export { ProtobufTraceSerializer } from './trace/protobuf';

export {
JsonTraceSerializer,
JsonLogsSerializer,
JsonMetricsSerializer,
} from './json/serializers';
export { JsonLogsSerializer } from './logs/json';
export { JsonMetricsSerializer } from './metrics/json';
export { JsonTraceSerializer } from './trace/json';

export { ISerializer } from './common/i-serializer';
export { ISerializer } from './i-serializer';
81 changes: 0 additions & 81 deletions experimental/packages/otlp-transformer/src/json/serializers.ts

This file was deleted.

28 changes: 28 additions & 0 deletions experimental/packages/otlp-transformer/src/logs/export-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export interface IExportLogsServiceResponse {
/** ExportLogsServiceResponse partialSuccess */
partialSuccess?: IExportLogsPartialSuccess;
}

export interface IExportLogsPartialSuccess {
/** ExportLogsPartialSuccess rejectedLogRecords */
rejectedLogRecords?: number;

/** ExportLogsPartialSuccess errorMessage */
errorMessage?: string;
}
107 changes: 5 additions & 102 deletions experimental/packages/otlp-transformer/src/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,105 +14,8 @@
* limitations under the License.
*/

import type { ReadableLogRecord } from '@opentelemetry/sdk-logs';
import {
ESeverityNumber,
IExportLogsServiceRequest,
ILogRecord,
IResourceLogs,
} from './types';
import { IResource } from '@opentelemetry/resources';
import { Encoder, getOtlpEncoder } from '../common';
import {
createInstrumentationScope,
toAnyValue,
toKeyValue,
} from '../common/internal';
import { SeverityNumber } from '@opentelemetry/api-logs';
import { OtlpEncodingOptions, IKeyValue } from '../common/types';
import { LogAttributes } from '@opentelemetry/api-logs';
import { createResource } from '../resource/internal';

export function createExportLogsServiceRequest(
logRecords: ReadableLogRecord[],
options?: OtlpEncodingOptions
): IExportLogsServiceRequest {
const encoder = getOtlpEncoder(options);
return {
resourceLogs: logRecordsToResourceLogs(logRecords, encoder),
};
}

function createResourceMap(
logRecords: ReadableLogRecord[]
): Map<IResource, Map<string, ReadableLogRecord[]>> {
const resourceMap: Map<
IResource,
Map<string, ReadableLogRecord[]>
> = new Map();

for (const record of logRecords) {
const {
resource,
instrumentationScope: { name, version = '', schemaUrl = '' },
} = record;

let ismMap = resourceMap.get(resource);
if (!ismMap) {
ismMap = new Map();
resourceMap.set(resource, ismMap);
}

const ismKey = `${name}@${version}:${schemaUrl}`;
let records = ismMap.get(ismKey);
if (!records) {
records = [];
ismMap.set(ismKey, records);
}
records.push(record);
}
return resourceMap;
}

function logRecordsToResourceLogs(
logRecords: ReadableLogRecord[],
encoder: Encoder
): IResourceLogs[] {
const resourceMap = createResourceMap(logRecords);
return Array.from(resourceMap, ([resource, ismMap]) => ({
resource: createResource(resource),
scopeLogs: Array.from(ismMap, ([, scopeLogs]) => {
return {
scope: createInstrumentationScope(scopeLogs[0].instrumentationScope),
logRecords: scopeLogs.map(log => toLogRecord(log, encoder)),
schemaUrl: scopeLogs[0].instrumentationScope.schemaUrl,
};
}),
schemaUrl: undefined,
}));
}

function toLogRecord(log: ReadableLogRecord, encoder: Encoder): ILogRecord {
return {
timeUnixNano: encoder.encodeHrTime(log.hrTime),
observedTimeUnixNano: encoder.encodeHrTime(log.hrTimeObserved),
severityNumber: toSeverityNumber(log.severityNumber),
severityText: log.severityText,
body: toAnyValue(log.body),
attributes: toLogAttributes(log.attributes),
droppedAttributesCount: log.droppedAttributesCount,
flags: log.spanContext?.traceFlags,
traceId: encoder.encodeOptionalSpanContext(log.spanContext?.traceId),
spanId: encoder.encodeOptionalSpanContext(log.spanContext?.spanId),
};
}

function toSeverityNumber(
severityNumber: SeverityNumber | undefined
): ESeverityNumber | undefined {
return severityNumber as number | undefined as ESeverityNumber | undefined;
}

export function toLogAttributes(attributes: LogAttributes): IKeyValue[] {
return Object.keys(attributes).map(key => toKeyValue(key, attributes[key]));
}
// IMPORTANT: exports added here are public
export {
IExportLogsServiceResponse,
IExportLogsPartialSuccess,
} from './export-response';
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,15 @@ import type {
IAnyValue,
IInstrumentationScope,
IKeyValue,
} from '../common/types';
import type { IResource } from '../resource/types';
IResource,
} from '../common/internal-types';

/** Properties of an ExportLogsServiceRequest. */
export interface IExportLogsServiceRequest {
/** ExportLogsServiceRequest resourceLogs */
resourceLogs?: IResourceLogs[];
}

export interface IExportLogsServiceResponse {
/** ExportLogsServiceResponse partialSuccess */
partialSuccess?: IExportLogsPartialSuccess;
}

export interface IExportLogsPartialSuccess {
/** ExportLogsPartialSuccess rejectedLogRecords */
rejectedLogRecords?: number;

/** ExportLogsPartialSuccess errorMessage */
errorMessage?: string;
}

/** Properties of a ResourceLogs. */
export interface IResourceLogs {
/** ResourceLogs resource */
Expand Down
Loading

0 comments on commit 5ad61ec

Please sign in to comment.