Skip to content

Commit b78d443

Browse files
feat(otlp-transformer): consolidate scope/resource creation in transformer (#4600)
* [chore] consolidate scope/resource creation in transformer Signed-off-by: Bogdan Drutu <[email protected]> * fixup! [chore] consolidate scope/resource creation in transformer * fixup! [chore] consolidate scope/resource creation in transformer * chore: add changelog entry * fixup! [chore] consolidate scope/resource creation in transformer --------- Signed-off-by: Bogdan Drutu <[email protected]> Co-authored-by: Bogdan Drutu <[email protected]>
1 parent e01f493 commit b78d443

File tree

8 files changed

+66
-34
lines changed

8 files changed

+66
-34
lines changed

experimental/CHANGELOG.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented
88

99
### :rocket: (Enhancement)
1010

11+
* feat(otlp-transformer): consolidate scope/resource creation in transformer [#4600](https://github.com/open-telemetry/opentelemetry-js/pull/4600)
12+
1113
### :bug: (Bug Fix)
1214

1315
### :books: (Refine Doc)
@@ -31,9 +33,6 @@ All notable changes to experimental packages in this project will be documented
3133
* was used internally to keep track of the service client used by the exporter, as a side effect it allowed end-users to modify the gRPC service client that was used
3234
* `compression`
3335
* was used internally to keep track of the compression to use but was unintentionally exposed to the users. It allowed to read and write the value, writing, however, would have no effect.
34-
* feat(api-events)!: removed domain from the Events API [#4569](https://github.com/open-telemetry/opentelemetry-js/pull/4569) @martinkuba
35-
* fix(api-events)!: renamed EventEmitter to EventLogger in the Events API [#4569](https://github.com/open-telemetry/opentelemetry-js/pull/4568) @martinkuba
36-
* feat(api-logs)!: changed LogRecord body data type to AnyValue and AnyValueMap types [#4575](https://github.com/open-telemetry/opentelemetry-js/pull/4575) @martinkuba
3736

3837
### :rocket: (Enhancement)
3938

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import type { IAnyValue, IKeyValue } from './types';
16+
import type { IAnyValue, IInstrumentationScope, IKeyValue } from './types';
1717
import { Attributes } from '@opentelemetry/api';
18+
import { InstrumentationScope } from '@opentelemetry/core';
19+
20+
export function createInstrumentationScope(
21+
scope: InstrumentationScope
22+
): IInstrumentationScope {
23+
return {
24+
name: scope.name,
25+
version: scope.version,
26+
};
27+
}
1828

1929
export function toAttributes(attributes: Attributes): IKeyValue[] {
2030
return Object.keys(attributes).map(key => toKeyValue(key, attributes[key]));

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ import {
2323
} from './types';
2424
import { IResource } from '@opentelemetry/resources';
2525
import { Encoder, getOtlpEncoder } from '../common';
26-
import { toAnyValue, toAttributes, toKeyValue } from '../common/internal';
26+
import {
27+
createInstrumentationScope,
28+
toAnyValue,
29+
toKeyValue,
30+
} from '../common/internal';
2731
import { SeverityNumber } from '@opentelemetry/api-logs';
2832
import { OtlpEncodingOptions, IKeyValue } from '../common/types';
2933
import { LogAttributes } from '@opentelemetry/api-logs';
34+
import { createResource } from '../resource/internal';
3035

3136
export function createExportLogsServiceRequest(
3237
logRecords: ReadableLogRecord[],
@@ -75,18 +80,12 @@ function logRecordsToResourceLogs(
7580
): IResourceLogs[] {
7681
const resourceMap = createResourceMap(logRecords);
7782
return Array.from(resourceMap, ([resource, ismMap]) => ({
78-
resource: {
79-
attributes: toAttributes(resource.attributes),
80-
droppedAttributesCount: 0,
81-
},
83+
resource: createResource(resource),
8284
scopeLogs: Array.from(ismMap, ([, scopeLogs]) => {
83-
const {
84-
instrumentationScope: { name, version, schemaUrl },
85-
} = scopeLogs[0];
8685
return {
87-
scope: { name, version },
86+
scope: createInstrumentationScope(scopeLogs[0].instrumentationScope),
8887
logRecords: scopeLogs.map(log => toLogRecord(log, encoder)),
89-
schemaUrl,
88+
schemaUrl: scopeLogs[0].instrumentationScope.schemaUrl,
9089
};
9190
}),
9291
schemaUrl: undefined,

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
ResourceMetrics,
2626
ScopeMetrics,
2727
} from '@opentelemetry/sdk-metrics';
28-
import { toAttributes } from '../common/internal';
2928
import {
3029
EAggregationTemporality,
3130
IExponentialHistogramDataPoint,
@@ -36,17 +35,16 @@ import {
3635
IScopeMetrics,
3736
} from './types';
3837
import { Encoder, getOtlpEncoder } from '../common';
38+
import { createInstrumentationScope, toAttributes } from '../common/internal';
39+
import { createResource } from '../resource/internal';
3940

4041
export function toResourceMetrics(
4142
resourceMetrics: ResourceMetrics,
4243
options?: OtlpEncodingOptions
4344
): IResourceMetrics {
4445
const encoder = getOtlpEncoder(options);
4546
return {
46-
resource: {
47-
attributes: toAttributes(resourceMetrics.resource.attributes),
48-
droppedAttributesCount: 0,
49-
},
47+
resource: createResource(resourceMetrics.resource),
5048
schemaUrl: undefined,
5149
scopeMetrics: toScopeMetrics(resourceMetrics.scopeMetrics, encoder),
5250
};
@@ -58,10 +56,7 @@ export function toScopeMetrics(
5856
): IScopeMetrics[] {
5957
return Array.from(
6058
scopeMetrics.map(metrics => ({
61-
scope: {
62-
name: metrics.scope.name,
63-
version: metrics.scope.version,
64-
},
59+
scope: createInstrumentationScope(metrics.scope),
6560
metrics: metrics.metrics.map(metricData => toMetric(metricData, encoder)),
6661
schemaUrl: metrics.scope.schemaUrl,
6762
}))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
import { IResource as ISdkResource } from '@opentelemetry/resources';
17+
import { toAttributes } from '../common/internal';
18+
import { IResource } from './types';
19+
20+
export function createResource(resource: ISdkResource): IResource {
21+
return {
22+
attributes: toAttributes(resource.attributes),
23+
droppedAttributesCount: 0,
24+
};
25+
}

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
import type { IResource } from '@opentelemetry/resources';
1717
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
1818
import type { OtlpEncodingOptions } from '../common/types';
19-
import { toAttributes } from '../common/internal';
2019
import { sdkSpanToOtlpSpan } from './internal';
2120
import {
2221
IExportTraceServiceRequest,
2322
IResourceSpans,
2423
IScopeSpans,
2524
} from './types';
2625
import { Encoder, getOtlpEncoder } from '../common';
26+
import { createInstrumentationScope } from '../common/internal';
27+
import { createResource } from '../resource/internal';
2728

2829
export function createExportTraceServiceRequest(
2930
spans: ReadableSpan[],
@@ -79,26 +80,23 @@ function spanRecordsToResourceSpans(
7980
while (!ilmEntry.done) {
8081
const scopeSpans = ilmEntry.value;
8182
if (scopeSpans.length > 0) {
82-
const { name, version, schemaUrl } =
83-
scopeSpans[0].instrumentationLibrary;
8483
const spans = scopeSpans.map(readableSpan =>
8584
sdkSpanToOtlpSpan(readableSpan, encoder)
8685
);
8786

8887
scopeResourceSpans.push({
89-
scope: { name, version },
88+
scope: createInstrumentationScope(
89+
scopeSpans[0].instrumentationLibrary
90+
),
9091
spans: spans,
91-
schemaUrl: schemaUrl,
92+
schemaUrl: scopeSpans[0].instrumentationLibrary.schemaUrl,
9293
});
9394
}
9495
ilmEntry = ilmIterator.next();
9596
}
9697
// TODO SDK types don't provide resource schema URL at this time
9798
const transformedSpans: IResourceSpans = {
98-
resource: {
99-
attributes: toAttributes(resource.attributes),
100-
droppedAttributesCount: 0,
101-
},
99+
resource: createResource(resource),
102100
scopeSpans: scopeResourceSpans,
103101
schemaUrl: undefined,
104102
};

experimental/packages/otlp-transformer/test/logs.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ function createExpectedLogJson(useHex: boolean): IExportLogsServiceRequest {
4646
schemaUrl: undefined,
4747
scopeLogs: [
4848
{
49-
scope: { name: 'scope_name_1', version: '0.1.0' },
49+
scope: {
50+
name: 'scope_name_1',
51+
version: '0.1.0',
52+
},
5053
logRecords: [
5154
{
5255
timeUnixNano: { low: 4132445859, high: 391214506 },

experimental/packages/otlp-transformer/test/trace.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ function createExpectedSpanJson(options: OtlpEncodingOptions) {
6868
schemaUrl: undefined,
6969
scopeSpans: [
7070
{
71-
scope: { name: 'myLib', version: '0.1.0' },
71+
scope: {
72+
name: 'myLib',
73+
version: '0.1.0',
74+
},
7275
spans: [
7376
{
7477
traceId: traceId,

0 commit comments

Comments
 (0)