Skip to content

Commit 84cce75

Browse files
authored
refactor(otlp-transformer): re-structure package to prepare for separate entrypoints (#5264)
1 parent 6d31a18 commit 84cce75

38 files changed

+746
-495
lines changed

experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ All notable changes to experimental packages in this project will be documented
1717

1818
### :house: (Internal)
1919

20+
* refactor(otlp-transformer): re-structure package to prepare for separate entrypoints [#5264](https://github.com/open-telemetry/opentelemetry-js/pull/5264) @pichlermarc
21+
2022
## 0.56.0
2123

2224
### :boom: Breaking Change

experimental/packages/otlp-transformer/src/common/types.ts renamed to experimental/packages/otlp-transformer/src/common/internal-types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17+
/** Properties of a Resource. */
18+
export interface IResource {
19+
/** Resource attributes */
20+
attributes: IKeyValue[];
21+
22+
/** Resource droppedAttributesCount */
23+
droppedAttributesCount: number;
24+
}
25+
1726
/** Properties of an InstrumentationScope. */
1827
export interface IInstrumentationScope {
1928
/** InstrumentationScope name */

experimental/packages/otlp-transformer/src/common/internal.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,22 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import type { IAnyValue, IInstrumentationScope, IKeyValue } from './types';
16+
import type {
17+
IAnyValue,
18+
IInstrumentationScope,
19+
IKeyValue,
20+
IResource,
21+
} from './internal-types';
1722
import { Attributes } from '@opentelemetry/api';
1823
import { InstrumentationScope } from '@opentelemetry/core';
24+
import { IResource as ISdkResource } from '@opentelemetry/resources';
25+
26+
export function createResource(resource: ISdkResource): IResource {
27+
return {
28+
attributes: toAttributes(resource.attributes),
29+
droppedAttributesCount: 0,
30+
};
31+
}
1932

2033
export function createInstrumentationScope(
2134
scope: InstrumentationScope

experimental/packages/otlp-transformer/src/common/index.ts renamed to experimental/packages/otlp-transformer/src/common/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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

experimental/packages/otlp-transformer/src/index.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,19 @@
1717
export {
1818
IExportMetricsPartialSuccess,
1919
IExportMetricsServiceResponse,
20-
} from './metrics/types';
20+
} from './metrics';
2121
export {
2222
IExportTracePartialSuccess,
2323
IExportTraceServiceResponse,
24-
} from './trace/types';
25-
export {
26-
IExportLogsServiceResponse,
27-
IExportLogsPartialSuccess,
28-
} from './logs/types';
24+
} from './trace';
25+
export { IExportLogsServiceResponse, IExportLogsPartialSuccess } from './logs';
2926

30-
export {
31-
ProtobufLogsSerializer,
32-
ProtobufMetricsSerializer,
33-
ProtobufTraceSerializer,
34-
} from './protobuf/serializers';
27+
export { ProtobufLogsSerializer } from './logs/protobuf';
28+
export { ProtobufMetricsSerializer } from './metrics/protobuf';
29+
export { ProtobufTraceSerializer } from './trace/protobuf';
3530

36-
export {
37-
JsonTraceSerializer,
38-
JsonLogsSerializer,
39-
JsonMetricsSerializer,
40-
} from './json/serializers';
31+
export { JsonLogsSerializer } from './logs/json';
32+
export { JsonMetricsSerializer } from './metrics/json';
33+
export { JsonTraceSerializer } from './trace/json';
4134

42-
export { ISerializer } from './common/i-serializer';
35+
export { ISerializer } from './i-serializer';

experimental/packages/otlp-transformer/src/json/serializers.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export interface IExportLogsServiceResponse {
18+
/** ExportLogsServiceResponse partialSuccess */
19+
partialSuccess?: IExportLogsPartialSuccess;
20+
}
21+
22+
export interface IExportLogsPartialSuccess {
23+
/** ExportLogsPartialSuccess rejectedLogRecords */
24+
rejectedLogRecords?: number;
25+
26+
/** ExportLogsPartialSuccess errorMessage */
27+
errorMessage?: string;
28+
}

experimental/packages/otlp-transformer/src/logs/index.ts

Lines changed: 5 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -14,105 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { ReadableLogRecord } from '@opentelemetry/sdk-logs';
18-
import {
19-
ESeverityNumber,
20-
IExportLogsServiceRequest,
21-
ILogRecord,
22-
IResourceLogs,
23-
} from './types';
24-
import { IResource } from '@opentelemetry/resources';
25-
import { Encoder, getOtlpEncoder } from '../common';
26-
import {
27-
createInstrumentationScope,
28-
toAnyValue,
29-
toKeyValue,
30-
} from '../common/internal';
31-
import { SeverityNumber } from '@opentelemetry/api-logs';
32-
import { OtlpEncodingOptions, IKeyValue } from '../common/types';
33-
import { LogAttributes } from '@opentelemetry/api-logs';
34-
import { createResource } from '../resource/internal';
35-
36-
export function createExportLogsServiceRequest(
37-
logRecords: ReadableLogRecord[],
38-
options?: OtlpEncodingOptions
39-
): IExportLogsServiceRequest {
40-
const encoder = getOtlpEncoder(options);
41-
return {
42-
resourceLogs: logRecordsToResourceLogs(logRecords, encoder),
43-
};
44-
}
45-
46-
function createResourceMap(
47-
logRecords: ReadableLogRecord[]
48-
): Map<IResource, Map<string, ReadableLogRecord[]>> {
49-
const resourceMap: Map<
50-
IResource,
51-
Map<string, ReadableLogRecord[]>
52-
> = new Map();
53-
54-
for (const record of logRecords) {
55-
const {
56-
resource,
57-
instrumentationScope: { name, version = '', schemaUrl = '' },
58-
} = record;
59-
60-
let ismMap = resourceMap.get(resource);
61-
if (!ismMap) {
62-
ismMap = new Map();
63-
resourceMap.set(resource, ismMap);
64-
}
65-
66-
const ismKey = `${name}@${version}:${schemaUrl}`;
67-
let records = ismMap.get(ismKey);
68-
if (!records) {
69-
records = [];
70-
ismMap.set(ismKey, records);
71-
}
72-
records.push(record);
73-
}
74-
return resourceMap;
75-
}
76-
77-
function logRecordsToResourceLogs(
78-
logRecords: ReadableLogRecord[],
79-
encoder: Encoder
80-
): IResourceLogs[] {
81-
const resourceMap = createResourceMap(logRecords);
82-
return Array.from(resourceMap, ([resource, ismMap]) => ({
83-
resource: createResource(resource),
84-
scopeLogs: Array.from(ismMap, ([, scopeLogs]) => {
85-
return {
86-
scope: createInstrumentationScope(scopeLogs[0].instrumentationScope),
87-
logRecords: scopeLogs.map(log => toLogRecord(log, encoder)),
88-
schemaUrl: scopeLogs[0].instrumentationScope.schemaUrl,
89-
};
90-
}),
91-
schemaUrl: undefined,
92-
}));
93-
}
94-
95-
function toLogRecord(log: ReadableLogRecord, encoder: Encoder): ILogRecord {
96-
return {
97-
timeUnixNano: encoder.encodeHrTime(log.hrTime),
98-
observedTimeUnixNano: encoder.encodeHrTime(log.hrTimeObserved),
99-
severityNumber: toSeverityNumber(log.severityNumber),
100-
severityText: log.severityText,
101-
body: toAnyValue(log.body),
102-
attributes: toLogAttributes(log.attributes),
103-
droppedAttributesCount: log.droppedAttributesCount,
104-
flags: log.spanContext?.traceFlags,
105-
traceId: encoder.encodeOptionalSpanContext(log.spanContext?.traceId),
106-
spanId: encoder.encodeOptionalSpanContext(log.spanContext?.spanId),
107-
};
108-
}
109-
110-
function toSeverityNumber(
111-
severityNumber: SeverityNumber | undefined
112-
): ESeverityNumber | undefined {
113-
return severityNumber as number | undefined as ESeverityNumber | undefined;
114-
}
115-
116-
export function toLogAttributes(attributes: LogAttributes): IKeyValue[] {
117-
return Object.keys(attributes).map(key => toKeyValue(key, attributes[key]));
118-
}
17+
// IMPORTANT: exports added here are public
18+
export {
19+
IExportLogsServiceResponse,
20+
IExportLogsPartialSuccess,
21+
} from './export-response';

0 commit comments

Comments
 (0)