From b78d4432d8ad26bc31c99444c5b7a70fae93d5ec Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Thu, 4 Apr 2024 15:21:54 +0200 Subject: [PATCH 1/2] feat(otlp-transformer): consolidate scope/resource creation in transformer (#4600) * [chore] consolidate scope/resource creation in transformer Signed-off-by: Bogdan Drutu * 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 Co-authored-by: Bogdan Drutu --- experimental/CHANGELOG.md | 5 ++-- .../otlp-transformer/src/common/internal.ts | 12 ++++++++- .../otlp-transformer/src/logs/index.ts | 19 +++++++------- .../otlp-transformer/src/metrics/internal.ts | 13 +++------- .../otlp-transformer/src/resource/internal.ts | 25 +++++++++++++++++++ .../otlp-transformer/src/trace/index.ts | 16 ++++++------ .../otlp-transformer/test/logs.test.ts | 5 +++- .../otlp-transformer/test/trace.test.ts | 5 +++- 8 files changed, 66 insertions(+), 34 deletions(-) create mode 100644 experimental/packages/otlp-transformer/src/resource/internal.ts diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 920044e357..c8d631e6c1 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -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) @@ -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) diff --git a/experimental/packages/otlp-transformer/src/common/internal.ts b/experimental/packages/otlp-transformer/src/common/internal.ts index 0fe649f525..8755362bb3 100644 --- a/experimental/packages/otlp-transformer/src/common/internal.ts +++ b/experimental/packages/otlp-transformer/src/common/internal.ts @@ -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])); diff --git a/experimental/packages/otlp-transformer/src/logs/index.ts b/experimental/packages/otlp-transformer/src/logs/index.ts index b690484a71..73013c0e26 100644 --- a/experimental/packages/otlp-transformer/src/logs/index.ts +++ b/experimental/packages/otlp-transformer/src/logs/index.ts @@ -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[], @@ -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, diff --git a/experimental/packages/otlp-transformer/src/metrics/internal.ts b/experimental/packages/otlp-transformer/src/metrics/internal.ts index 1a621a4213..fc13a80799 100644 --- a/experimental/packages/otlp-transformer/src/metrics/internal.ts +++ b/experimental/packages/otlp-transformer/src/metrics/internal.ts @@ -25,7 +25,6 @@ import { ResourceMetrics, ScopeMetrics, } from '@opentelemetry/sdk-metrics'; -import { toAttributes } from '../common/internal'; import { EAggregationTemporality, IExponentialHistogramDataPoint, @@ -36,6 +35,8 @@ 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, @@ -43,10 +44,7 @@ export function toResourceMetrics( ): 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), }; @@ -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, })) diff --git a/experimental/packages/otlp-transformer/src/resource/internal.ts b/experimental/packages/otlp-transformer/src/resource/internal.ts new file mode 100644 index 0000000000..93b6964508 --- /dev/null +++ b/experimental/packages/otlp-transformer/src/resource/internal.ts @@ -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, + }; +} diff --git a/experimental/packages/otlp-transformer/src/trace/index.ts b/experimental/packages/otlp-transformer/src/trace/index.ts index 5db18a082e..9fef398c54 100644 --- a/experimental/packages/otlp-transformer/src/trace/index.ts +++ b/experimental/packages/otlp-transformer/src/trace/index.ts @@ -16,7 +16,6 @@ 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, @@ -24,6 +23,8 @@ import { IScopeSpans, } from './types'; import { Encoder, getOtlpEncoder } from '../common'; +import { createInstrumentationScope } from '../common/internal'; +import { createResource } from '../resource/internal'; export function createExportTraceServiceRequest( spans: ReadableSpan[], @@ -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, }; diff --git a/experimental/packages/otlp-transformer/test/logs.test.ts b/experimental/packages/otlp-transformer/test/logs.test.ts index ea8dd7e82b..278607ae23 100644 --- a/experimental/packages/otlp-transformer/test/logs.test.ts +++ b/experimental/packages/otlp-transformer/test/logs.test.ts @@ -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 }, diff --git a/experimental/packages/otlp-transformer/test/trace.test.ts b/experimental/packages/otlp-transformer/test/trace.test.ts index 65b23ddc2d..30aeeec158 100644 --- a/experimental/packages/otlp-transformer/test/trace.test.ts +++ b/experimental/packages/otlp-transformer/test/trace.test.ts @@ -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, From c0468673cb0cfe61d12edf69745fabfdf940d7b0 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 4 Apr 2024 15:24:53 -0700 Subject: [PATCH 2/2] fix(resources): ensure BrowserDetector does not think Node.js v21 is a browser (#4604) Fixes: #4561 --- CHANGELOG.md | 1 + .../src/detectors/BrowserDetectorSync.ts | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index becf83bc68..0c3d6bfe7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :bug: (Bug Fix) * fix(sdk-trace-web): fix invalid timings in span events [#4486](https://github.com/open-telemetry/opentelemetry-js/pull/4486) @Abinet18 +* fix(resources): ensure BrowserDetector does not think Node.js v21 is a browser [#4561](https://github.com/open-telemetry/opentelemetry-js/issues/4561) @trentm ### :books: (Refine Doc) diff --git a/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts b/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts index b58fea94c8..66a4801067 100644 --- a/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts +++ b/packages/opentelemetry-resources/src/detectors/BrowserDetectorSync.ts @@ -24,7 +24,12 @@ import { diag } from '@opentelemetry/api'; */ class BrowserDetectorSync implements DetectorSync { detect(config?: ResourceDetectionConfig): IResource { - const isBrowser = typeof navigator !== 'undefined'; + const isBrowser = + typeof navigator !== 'undefined' && + global.process?.versions?.node === undefined && // Node.js v21 adds `navigator` + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore don't have Bun types + global.Bun?.version === undefined; // Bun (bun.sh) defines `navigator` if (!isBrowser) { return Resource.empty(); }