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

feat(otlp-transformer): consolidate scope/resource creation in transformer #4600

Merged
merged 8 commits into from
Apr 4, 2024
5 changes: 2 additions & 3 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(otlp-transformer): consolidate scope/resource creation in transformer [#4600](https://github.com/open-telemetry/opentelemetry-js/pull/4600)

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand All @@ -31,9 +33,6 @@ All notable changes to experimental packages in this project will be documented
* 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
* `compression`
* 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.
* feat(api-events)!: removed domain from the Events API [#4569](https://github.com/open-telemetry/opentelemetry-js/pull/4569) @martinkuba
* fix(api-events)!: renamed EventEmitter to EventLogger in the Events API [#4569](https://github.com/open-telemetry/opentelemetry-js/pull/4568) @martinkuba
* feat(api-logs)!: changed LogRecord body data type to AnyValue and AnyValueMap types [#4575](https://github.com/open-telemetry/opentelemetry-js/pull/4575) @martinkuba

### :rocket: (Enhancement)

Expand Down
12 changes: 11 additions & 1 deletion experimental/packages/otlp-transformer/src/common/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { IAnyValue, IKeyValue } from './types';
import type { IAnyValue, IInstrumentationScope, IKeyValue } from './types';
import { Attributes } from '@opentelemetry/api';
import { InstrumentationScope } from '@opentelemetry/core';

export function createInstrumentationScope(
scope: InstrumentationScope
): IInstrumentationScope {
return {
name: scope.name,
version: scope.version,
};
}

export function toAttributes(attributes: Attributes): IKeyValue[] {
return Object.keys(attributes).map(key => toKeyValue(key, attributes[key]));
Expand Down
19 changes: 9 additions & 10 deletions experimental/packages/otlp-transformer/src/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ import {
} from './types';
import { IResource } from '@opentelemetry/resources';
import { Encoder, getOtlpEncoder } from '../common';
import { toAnyValue, toAttributes, toKeyValue } from '../common/internal';
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[],
Expand Down Expand Up @@ -75,18 +80,12 @@ function logRecordsToResourceLogs(
): IResourceLogs[] {
const resourceMap = createResourceMap(logRecords);
return Array.from(resourceMap, ([resource, ismMap]) => ({
resource: {
attributes: toAttributes(resource.attributes),
droppedAttributesCount: 0,
},
resource: createResource(resource),
scopeLogs: Array.from(ismMap, ([, scopeLogs]) => {
const {
instrumentationScope: { name, version, schemaUrl },
} = scopeLogs[0];
return {
scope: { name, version },
scope: createInstrumentationScope(scopeLogs[0].instrumentationScope),
logRecords: scopeLogs.map(log => toLogRecord(log, encoder)),
schemaUrl,
schemaUrl: scopeLogs[0].instrumentationScope.schemaUrl,
};
}),
schemaUrl: undefined,
Expand Down
13 changes: 4 additions & 9 deletions experimental/packages/otlp-transformer/src/metrics/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
ResourceMetrics,
ScopeMetrics,
} from '@opentelemetry/sdk-metrics';
import { toAttributes } from '../common/internal';
import {
EAggregationTemporality,
IExponentialHistogramDataPoint,
Expand All @@ -36,17 +35,16 @@ import {
IScopeMetrics,
} from './types';
import { Encoder, getOtlpEncoder } from '../common';
import { createInstrumentationScope, toAttributes } from '../common/internal';
import { createResource } from '../resource/internal';

export function toResourceMetrics(
resourceMetrics: ResourceMetrics,
options?: OtlpEncodingOptions
): IResourceMetrics {
const encoder = getOtlpEncoder(options);
return {
resource: {
attributes: toAttributes(resourceMetrics.resource.attributes),
droppedAttributesCount: 0,
},
resource: createResource(resourceMetrics.resource),
schemaUrl: undefined,
scopeMetrics: toScopeMetrics(resourceMetrics.scopeMetrics, encoder),
};
Expand All @@ -58,10 +56,7 @@ export function toScopeMetrics(
): IScopeMetrics[] {
return Array.from(
scopeMetrics.map(metrics => ({
scope: {
name: metrics.scope.name,
version: metrics.scope.version,
},
scope: createInstrumentationScope(metrics.scope),
metrics: metrics.metrics.map(metricData => toMetric(metricData, encoder)),
schemaUrl: metrics.scope.schemaUrl,
}))
Expand Down
25 changes: 25 additions & 0 deletions experimental/packages/otlp-transformer/src/resource/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.
*/
import { IResource as ISdkResource } from '@opentelemetry/resources';
import { toAttributes } from '../common/internal';
import { IResource } from './types';

export function createResource(resource: ISdkResource): IResource {
return {
attributes: toAttributes(resource.attributes),
droppedAttributesCount: 0,
};
}
16 changes: 7 additions & 9 deletions experimental/packages/otlp-transformer/src/trace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
import type { IResource } from '@opentelemetry/resources';
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import type { OtlpEncodingOptions } from '../common/types';
import { toAttributes } from '../common/internal';
import { sdkSpanToOtlpSpan } from './internal';
import {
IExportTraceServiceRequest,
IResourceSpans,
IScopeSpans,
} from './types';
import { Encoder, getOtlpEncoder } from '../common';
import { createInstrumentationScope } from '../common/internal';
import { createResource } from '../resource/internal';

export function createExportTraceServiceRequest(
spans: ReadableSpan[],
Expand Down Expand Up @@ -79,26 +80,23 @@ function spanRecordsToResourceSpans(
while (!ilmEntry.done) {
const scopeSpans = ilmEntry.value;
if (scopeSpans.length > 0) {
const { name, version, schemaUrl } =
scopeSpans[0].instrumentationLibrary;
const spans = scopeSpans.map(readableSpan =>
sdkSpanToOtlpSpan(readableSpan, encoder)
);

scopeResourceSpans.push({
scope: { name, version },
scope: createInstrumentationScope(
scopeSpans[0].instrumentationLibrary
),
spans: spans,
schemaUrl: schemaUrl,
schemaUrl: scopeSpans[0].instrumentationLibrary.schemaUrl,
});
}
ilmEntry = ilmIterator.next();
}
// TODO SDK types don't provide resource schema URL at this time
const transformedSpans: IResourceSpans = {
resource: {
attributes: toAttributes(resource.attributes),
droppedAttributesCount: 0,
},
resource: createResource(resource),
scopeSpans: scopeResourceSpans,
schemaUrl: undefined,
};
Expand Down
5 changes: 4 additions & 1 deletion experimental/packages/otlp-transformer/test/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ function createExpectedLogJson(useHex: boolean): IExportLogsServiceRequest {
schemaUrl: undefined,
scopeLogs: [
{
scope: { name: 'scope_name_1', version: '0.1.0' },
scope: {
name: 'scope_name_1',
version: '0.1.0',
},
logRecords: [
{
timeUnixNano: { low: 4132445859, high: 391214506 },
Expand Down
5 changes: 4 additions & 1 deletion experimental/packages/otlp-transformer/test/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ function createExpectedSpanJson(options: OtlpEncodingOptions) {
schemaUrl: undefined,
scopeSpans: [
{
scope: { name: 'myLib', version: '0.1.0' },
scope: {
name: 'myLib',
version: '0.1.0',
},
spans: [
{
traceId: traceId,
Expand Down