From f6e2f5f944d2a3bfe065dcc430af4c9ba81a475a Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Mon, 25 Apr 2022 12:13:16 +0200 Subject: [PATCH 01/29] chore: updating submodule for otlp-grpc-exporter-base --- experimental/packages/otlp-grpc-exporter-base/protos | 2 +- experimental/packages/otlp-proto-exporter-base/submodule.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/experimental/packages/otlp-grpc-exporter-base/protos b/experimental/packages/otlp-grpc-exporter-base/protos index 59c488bfb8..cc4ed55c08 160000 --- a/experimental/packages/otlp-grpc-exporter-base/protos +++ b/experimental/packages/otlp-grpc-exporter-base/protos @@ -1 +1 @@ -Subproject commit 59c488bfb8fb6d0458ad6425758b70259ff4a2bd +Subproject commit cc4ed55c082cb75e084d40b4ddf3805eda099f97 diff --git a/experimental/packages/otlp-proto-exporter-base/submodule.md b/experimental/packages/otlp-proto-exporter-base/submodule.md index c2838b1ba5..32c3e291c8 100644 --- a/experimental/packages/otlp-proto-exporter-base/submodule.md +++ b/experimental/packages/otlp-proto-exporter-base/submodule.md @@ -40,7 +40,7 @@ Knowing this if you want to change the submodule to point to a different version 8. Now thing which is very important. You have to commit this to apply these changes ```shell script - git commit -am "chore: updating submodule for opentelemetry-proto" + git commit -am "chore: updating submodule for opentelemetry-proto-exporter-base" ``` 9. If you look now at git log you will notice that the folder `protos` has been changed and it will show what was the previous sha and what is current one. From a8f045db73cbd0a967f5290e09ca4bcfbdf42083 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Mon, 25 Apr 2022 16:15:24 +0200 Subject: [PATCH 02/29] chore: updating submodule for otlp-proto-exporter-base --- experimental/packages/otlp-proto-exporter-base/protos | 2 +- experimental/packages/otlp-transformer/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/experimental/packages/otlp-proto-exporter-base/protos b/experimental/packages/otlp-proto-exporter-base/protos index 59c488bfb8..cc4ed55c08 160000 --- a/experimental/packages/otlp-proto-exporter-base/protos +++ b/experimental/packages/otlp-proto-exporter-base/protos @@ -1 +1 @@ -Subproject commit 59c488bfb8fb6d0458ad6425758b70259ff4a2bd +Subproject commit cc4ed55c082cb75e084d40b4ddf3805eda099f97 diff --git a/experimental/packages/otlp-transformer/package.json b/experimental/packages/otlp-transformer/package.json index 9ec6ec2d84..389b9592e2 100644 --- a/experimental/packages/otlp-transformer/package.json +++ b/experimental/packages/otlp-transformer/package.json @@ -1,6 +1,6 @@ { "name": "@opentelemetry/otlp-transformer", - "private": true, + "private": false, "publishConfig": { "access": "restricted" }, From 606870a65d62043529346ab6b260640a0bc136f9 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 27 Apr 2022 09:02:10 +0200 Subject: [PATCH 03/29] feat(otlp-metrics-exporter): update to proto 0.14. --- .../package.json | 2 +- .../src/OTLPMetricExporter.ts | 16 +- .../test/OTLPMetricExporter.test.ts | 18 +- .../test/metricsHelper.ts | 41 ++-- .../tsconfig.json | 6 +- .../package.json | 2 +- .../src/OTLPMetricExporterBase.ts | 5 +- .../src/index.ts | 1 - .../platform/browser/OTLPMetricExporter.ts | 14 +- .../src/platform/node/OTLPMetricExporter.ts | 14 +- .../src/transformMetrics.ts | 232 ------------------ .../browser/CollectorMetricExporter.test.ts | 6 +- .../common/CollectorMetricExporter.test.ts | 6 +- .../test/common/transformMetrics.test.ts | 189 -------------- .../test/metricsHelper.ts | 90 +++---- .../test/node/CollectorMetricExporter.test.ts | 7 +- .../tsconfig.json | 6 +- .../package.json | 2 +- .../src/OTLPMetricExporter.ts | 17 +- .../test/OTLPMetricExporter.test.ts | 26 +- .../test/metricsHelper.ts | 26 +- .../tsconfig.json | 6 +- .../otlp-grpc-exporter-base/package.json | 4 +- .../test/traceHelper.ts | 12 +- .../otlp-grpc-exporter-base/tsconfig.json | 4 +- .../packages/otlp-transformer/package.json | 1 + .../packages/otlp-transformer/src/index.ts | 4 +- .../otlp-transformer/src/metrics/index.ts | 2 +- .../otlp-transformer/src/metrics/types.ts | 2 +- 29 files changed, 163 insertions(+), 598 deletions(-) delete mode 100644 experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/transformMetrics.ts delete mode 100644 experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/transformMetrics.test.ts diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/package.json b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/package.json index 0d05d93ec0..0b143e0cf1 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/package.json +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/package.json @@ -72,7 +72,7 @@ "@opentelemetry/core": "1.2.0", "@opentelemetry/exporter-metrics-otlp-http": "0.28.0", "@opentelemetry/otlp-grpc-exporter-base": "0.28.0", - "@opentelemetry/exporter-trace-otlp-http": "0.28.0", + "@opentelemetry/otlp-transformer": "0.28.0", "@opentelemetry/resources": "1.2.0", "@opentelemetry/sdk-metrics-base": "0.28.0" } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts index af5960eace..23b0fd23ce 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts @@ -14,12 +14,11 @@ * limitations under the License. */ -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { defaultExporterTemporality, defaultOptions, - OTLPMetricExporterBase, OTLPMetricExporterOptions, - toOTLPExportMetricServiceRequest + OTLPMetricExporterBase, + OTLPMetricExporterOptions } from '@opentelemetry/exporter-metrics-otlp-http'; import { AggregationTemporality, ResourceMetrics } from '@opentelemetry/sdk-metrics-base'; import { @@ -30,12 +29,13 @@ import { } from '@opentelemetry/otlp-grpc-exporter-base'; import { baggageUtils, getEnv } from '@opentelemetry/core'; import { Metadata } from '@grpc/grpc-js'; +import { createExportMetricsServiceRequest, IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; const DEFAULT_COLLECTOR_URL = 'localhost:4317'; class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase { + IExportMetricsServiceRequest> { protected readonly _aggregationTemporality: AggregationTemporality; constructor(config: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions= defaultOptions) { @@ -66,12 +66,8 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase let collectorExporter: OTLPMetricExporter; let server: grpc.Server; let exportedData: - | otlpTypes.opentelemetryProto.metrics.v1.ResourceMetrics[] + | IResourceMetrics[] | undefined; let metrics: ResourceMetrics; let reqMetadata: grpc.Metadata | undefined; @@ -82,7 +82,7 @@ const testOTLPMetricExporter = (params: TestParams) => .MetricsService.service, { Export: (data: { - request: otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest; + request: IExportMetricsServiceRequest; metadata: grpc.Metadata; }) => { try { @@ -220,18 +220,18 @@ const testOTLPMetricExporter = (params: TestParams) => exportedData[0].instrumentationLibraryMetrics[0].metrics[2]; ensureExportedCounterIsCorrect( counter, - counter.intSum?.dataPoints[0].timeUnixNano, - counter.intSum?.dataPoints[0].startTimeUnixNano + counter.sum?.dataPoints[0].timeUnixNano, + counter.sum?.dataPoints[0].startTimeUnixNano ); ensureExportedObservableGaugeIsCorrect( observableGauge, - observableGauge.doubleGauge?.dataPoints[0].timeUnixNano, - observableGauge.doubleGauge?.dataPoints[0].startTimeUnixNano + observableGauge.gauge?.dataPoints[0].timeUnixNano, + observableGauge.gauge?.dataPoints[0].startTimeUnixNano ); ensureExportedHistogramIsCorrect( histogram, - histogram.intHistogram?.dataPoints[0].timeUnixNano, - histogram.intHistogram?.dataPoints[0].startTimeUnixNano, + histogram.histogram?.dataPoints[0].timeUnixNano, + histogram.histogram?.dataPoints[0].startTimeUnixNano, [0, 100], ['0', '2', '0'] ); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts index fcb0b4db50..c3161a8bdf 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts @@ -15,12 +15,12 @@ */ import { Counter, Histogram, ObservableResult, ValueType } from '@opentelemetry/api-metrics'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; import * as grpc from '@grpc/grpc-js'; import { VERSION } from '@opentelemetry/core'; import { ExplicitBucketHistogramAggregation, MeterProvider, MetricReader } from '@opentelemetry/sdk-metrics-base'; +import { IKeyValue, IMetric, IResource } from '@opentelemetry/otlp-transformer'; export class TestMetricReader extends MetricReader { protected onForceFlush(): Promise { @@ -95,7 +95,7 @@ export function mockHistogram(): Histogram { } export function ensureExportedAttributesAreCorrect( - attributes: otlpTypes.opentelemetryProto.common.v1.KeyValue[] + attributes: IKeyValue[] ) { assert.deepStrictEqual( attributes, @@ -113,7 +113,7 @@ export function ensureExportedAttributesAreCorrect( } export function ensureExportedCounterIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time?: number, startTime?: number ) { @@ -121,13 +121,15 @@ export function ensureExportedCounterIsCorrect( name: 'int-counter', description: 'sample counter description', unit: '1', - data: 'intSum', - intSum: { + data: 'sum', + sum: { dataPoints: [ { - labels: [], + attributes: [], exemplars: [], - value: '1', + value: 'asInt', + asInt: '1', + flags: 0, startTimeUnixNano: String(startTime), timeUnixNano: String(time), }, @@ -139,7 +141,7 @@ export function ensureExportedCounterIsCorrect( } export function ensureExportedObservableGaugeIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time?: number, startTime?: number ) { @@ -147,13 +149,15 @@ export function ensureExportedObservableGaugeIsCorrect( name: 'double-observable-gauge', description: 'sample observable gauge description', unit: '1', - data: 'doubleGauge', - doubleGauge: { + data: 'gauge', + gauge: { dataPoints: [ { - labels: [], + attributes: [], exemplars: [], - value: 6, + value: 'asDouble', + asDouble: 6, + flags: 0, startTimeUnixNano: String(startTime), timeUnixNano: String(time), }, @@ -163,7 +167,7 @@ export function ensureExportedObservableGaugeIsCorrect( } export function ensureExportedHistogramIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time?: number, startTime?: number, explicitBounds: number[] = [Infinity], @@ -173,13 +177,14 @@ export function ensureExportedHistogramIsCorrect( name: 'int-histogram', description: 'sample histogram description', unit: '1', - data: 'intHistogram', - intHistogram: { + data: 'histogram', + histogram: { dataPoints: [ { - labels: [], + attributes: [], exemplars: [], - sum: '21', + flags: 0, + sum: 21, count: '2', startTimeUnixNano: String(startTime), timeUnixNano: String(time), @@ -193,7 +198,7 @@ export function ensureExportedHistogramIsCorrect( } export function ensureResourceIsCorrect( - resource: otlpTypes.opentelemetryProto.resource.v1.Resource + resource: IResource ) { assert.deepStrictEqual(resource, { attributes: [ diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/tsconfig.json b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/tsconfig.json index 6380e3f172..77f463edd1 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/tsconfig.json +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/tsconfig.json @@ -15,9 +15,6 @@ { "path": "../../../packages/opentelemetry-resources" }, - { - "path": "../exporter-trace-otlp-http" - }, { "path": "../opentelemetry-api-metrics" }, @@ -29,6 +26,9 @@ }, { "path": "../otlp-grpc-exporter-base" + }, + { + "path": "../otlp-transformer" } ] } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/package.json b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/package.json index d896cc02ec..fa25fe0707 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/package.json +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/package.json @@ -96,7 +96,7 @@ "dependencies": { "@opentelemetry/api-metrics": "0.28.0", "@opentelemetry/core": "1.2.0", - "@opentelemetry/exporter-trace-otlp-http": "0.28.0", + "@opentelemetry/otlp-transformer": "0.28.0", "@opentelemetry/otlp-exporter-base": "0.28.0", "@opentelemetry/resources": "1.2.0", "@opentelemetry/sdk-metrics-base": "0.28.0" diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts index 75a0617bec..5d06cd60bd 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts @@ -16,13 +16,13 @@ import { ExportResult } from '@opentelemetry/core'; import { AggregationTemporality, PushMetricExporter, ResourceMetrics } from '@opentelemetry/sdk-metrics-base'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { defaultOptions, OTLPMetricExporterOptions } from './OTLPMetricExporterOptions'; import { OTLPExporterBase } from '@opentelemetry/otlp-exporter-base'; +import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; export class OTLPMetricExporterBase> + IExportMetricsServiceRequest>> implements PushMetricExporter { public _otlpExporter: T; protected _preferredAggregationTemporality: AggregationTemporality; @@ -48,5 +48,4 @@ implements PushMetricExporter { getPreferredAggregationTemporality(): AggregationTemporality { return this._preferredAggregationTemporality; } - } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/index.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/index.ts index c1deb8d977..ee1b1574a6 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/index.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/index.ts @@ -17,4 +17,3 @@ export * from './platform'; export * from './OTLPMetricExporterOptions'; export * from './OTLPMetricExporterBase'; -export { toOTLPExportMetricServiceRequest } from './transformMetrics'; diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts index 6daa90742e..3495da06b7 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts @@ -15,10 +15,6 @@ */ import { AggregationTemporality, ResourceMetrics } from '@opentelemetry/sdk-metrics-base'; -import { - otlpTypes -} from '@opentelemetry/exporter-trace-otlp-http'; -import { toOTLPExportMetricServiceRequest } from '../../transformMetrics'; import { baggageUtils, getEnv } from '@opentelemetry/core'; import { defaultExporterTemporality, defaultOptions, OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions'; import { OTLPMetricExporterBase } from '../../OTLPMetricExporterBase'; @@ -27,12 +23,13 @@ import { OTLPExporterBrowserBase, OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base'; +import { createExportMetricsServiceRequest, IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/metrics'; const DEFAULT_COLLECTOR_URL = `http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`; class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase { + IExportMetricsServiceRequest> { protected readonly _aggregationTemporality: AggregationTemporality; constructor(config: OTLPMetricExporterOptions & OTLPExporterConfigBase = defaultOptions) { @@ -58,11 +55,10 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase { + IExportMetricsServiceRequest> { protected readonly _aggregationTemporality: AggregationTemporality; constructor(config: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions = defaultOptions) { @@ -48,11 +45,10 @@ class OTLPExporterNodeProxy extends OTLPExporterNodeBase { - return { key, value: String(value) }; - }); -} - -/** - * Convert {@link AggregationTemporality} to a collector-compatible format. - * @param aggregationTemporality - */ -export function toAggregationTemporality( - aggregationTemporality: AggregationTemporality -): otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality { - if (aggregationTemporality === AggregationTemporality.CUMULATIVE) { - return otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_CUMULATIVE; - } - if (aggregationTemporality === AggregationTemporality.DELTA) { - return otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_DELTA; - } - - return otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_UNSPECIFIED; -} - -/** - * Convert {@link MetricData} of {@link DataPointType.SINGULAR} to a collector-compatible format. - * @param metricData - */ -export function toSingularDataPoints( - metricData: MetricData -): otlpTypes.opentelemetryProto.metrics.v1.DataPoint[] { - return Array.from(metricData.dataPoints.map(dataPoint => { - return { - labels: toCollectorAttributes(dataPoint.attributes), - value: dataPoint.value as number, - startTimeUnixNano: core.hrTimeToNanoseconds( - dataPoint.startTime - ), - timeUnixNano: core.hrTimeToNanoseconds( - dataPoint.endTime - ), - }; - })); -} - -/** - * Convert {@link MetricData} of {@link DataPointType.HISTOGRAM} to a collector-compatible format. - * @param metricData - */ -export function toHistogramDataPoints( - metricData: MetricData -): otlpTypes.opentelemetryProto.metrics.v1.HistogramDataPoint[] { - return Array.from(metricData.dataPoints.map(dataPoints => { - const histogram = dataPoints.value as Histogram; - return { - labels: toCollectorAttributes(dataPoints.attributes), - sum: histogram.sum, - count: histogram.count, - startTimeUnixNano: core.hrTimeToNanoseconds( - dataPoints.startTime - ), - timeUnixNano: core.hrTimeToNanoseconds( - dataPoints.endTime - ), - bucketCounts: histogram.buckets.counts, - explicitBounds: histogram.buckets.boundaries, - }; - })); -} - -/** - * Converts {@link MetricData} to a collector-compatible format. - * @param metricData - * @param aggregationTemporality - */ -export function toCollectorMetric( - metricData: MetricData, - aggregationTemporality: AggregationTemporality -): otlpTypes.opentelemetryProto.metrics.v1.Metric { - const metricCollector: otlpTypes.opentelemetryProto.metrics.v1.Metric = { - name: metricData.descriptor.name, - description: metricData.descriptor.description, - unit: metricData.descriptor.unit, - }; - - if (metricData.dataPointType === DataPointType.SINGULAR) { - const result = { - dataPoints: toSingularDataPoints(metricData), - isMonotonic: - metricData.descriptor.type === InstrumentType.COUNTER || - metricData.descriptor.type === InstrumentType.OBSERVABLE_COUNTER, - aggregationTemporality: toAggregationTemporality(aggregationTemporality), - }; - - if ( - metricData.descriptor.type === InstrumentType.COUNTER || - metricData.descriptor.type === InstrumentType.OBSERVABLE_COUNTER || - metricData.descriptor.type === InstrumentType.UP_DOWN_COUNTER || - metricData.descriptor.type === InstrumentType.OBSERVABLE_UP_DOWN_COUNTER - ) { - if (metricData.descriptor.valueType === ValueType.INT) { - metricCollector.intSum = result; - } else { - metricCollector.doubleSum = result; - } - } else{ - // Instrument is a gauge. - if (metricData.descriptor.valueType === ValueType.INT) { - metricCollector.intGauge = result; - } else { - metricCollector.doubleGauge = result; - } - } - } else if (metricData.dataPointType === DataPointType.HISTOGRAM) { - const result = { - dataPoints: toHistogramDataPoints(metricData), - aggregationTemporality: toAggregationTemporality(aggregationTemporality) - }; - if (metricData.descriptor.valueType === ValueType.INT) { - metricCollector.intHistogram = result; - } else { - metricCollector.doubleHistogram = result; - } - } - - // TODO: Add support for exponential histograms when they're ready. - - return metricCollector; -} - -/** - * Prepares metric service request to be sent to collector - * @param metrics metrics - * @param aggregationTemporality - * @param collectorExporterBase - */ -export function toOTLPExportMetricServiceRequest( - metrics: ResourceMetrics, - aggregationTemporality: AggregationTemporality, - collectorExporterBase: OTLPExporterBase -): otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest { - const additionalAttributes = Object.assign( - {}, - collectorExporterBase.attributes - ); - return { - resourceMetrics: toCollectorResourceMetrics( - metrics, - additionalAttributes, - aggregationTemporality - ), - }; -} - -/** - * Convert to InstrumentationLibraryMetrics - * @param instrumentationLibrary - * @param metrics - * @param aggregationTemporality - */ -function toCollectorInstrumentationLibraryMetrics( - instrumentationLibrary: core.InstrumentationLibrary, - metrics: MetricData[], - aggregationTemporality: AggregationTemporality -): otlpTypes.opentelemetryProto.metrics.v1.InstrumentationLibraryMetrics { - return { - metrics: metrics.map(metric => toCollectorMetric(metric, aggregationTemporality)), - instrumentationLibrary, - }; -} - -/** - * Returns a list of resource metrics which will be exported to the collector - * @param resourceMetrics - * @param baseAttributes - * @param aggregationTemporality - */ -function toCollectorResourceMetrics( - resourceMetrics: ResourceMetrics, - baseAttributes: SpanAttributes, - aggregationTemporality: AggregationTemporality -): otlpTypes.opentelemetryProto.metrics.v1.ResourceMetrics[] { - return [{ - resource: toCollectorResource(resourceMetrics.resource, baseAttributes), - instrumentationLibraryMetrics: Array.from(resourceMetrics.instrumentationLibraryMetrics.map( - instrumentationLibraryMetrics => toCollectorInstrumentationLibraryMetrics( - instrumentationLibraryMetrics.instrumentationLibrary, - instrumentationLibraryMetrics.metrics, - aggregationTemporality - ))) - }]; -} diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts index fb76700f59..caa9a70cce 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts @@ -21,7 +21,6 @@ import { AggregationTemporality, ResourceMetrics, } from '@opentelemetry/sdk-met import * as assert from 'assert'; import * as sinon from 'sinon'; import { OTLPMetricExporter } from '../../src/platform/browser'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { collect, ensureCounterIsCorrect, @@ -37,6 +36,7 @@ import { } from '../metricsHelper'; import { OTLPMetricExporterOptions } from '../../src'; import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base'; +import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; describe('OTLPMetricExporter - web', () => { let collectorExporter: OTLPMetricExporter; @@ -108,7 +108,7 @@ describe('OTLPMetricExporter - web', () => { const body = await blob.text(); const json = JSON.parse( body - ) as otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest; + ) as IExportMetricsServiceRequest; const metric1 = json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[0]; const metric2 = @@ -226,7 +226,7 @@ describe('OTLPMetricExporter - web', () => { const body = request.requestBody; const json = JSON.parse( body - ) as otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest; + ) as IExportMetricsServiceRequest; const metric1 = json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[0]; const metric2 = diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/CollectorMetricExporter.test.ts index 9c2b4e4c89..d4029853ca 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/CollectorMetricExporter.test.ts @@ -20,15 +20,15 @@ import { } from '@opentelemetry/sdk-metrics-base'; import * as assert from 'assert'; import * as sinon from 'sinon'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { collect, mockCounter, mockObservableGauge, setUp, shutdown } from '../metricsHelper'; import { OTLPExporterBase, OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base'; +import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; type CollectorExporterConfig = OTLPExporterConfigBase; class OTLPMetricExporter extends OTLPExporterBase< CollectorExporterConfig, ResourceMetrics, - otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest + IExportMetricsServiceRequest > { onInit() {} onShutdown() {} @@ -38,7 +38,7 @@ class OTLPMetricExporter extends OTLPExporterBase< } convert( metrics: ResourceMetrics[] - ): otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest { + ): IExportMetricsServiceRequest { return { resourceMetrics: [] }; } } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/transformMetrics.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/transformMetrics.test.ts deleted file mode 100644 index 6232752304..0000000000 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/transformMetrics.test.ts +++ /dev/null @@ -1,189 +0,0 @@ -/* - * 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 { hrTimeToNanoseconds, hrTime } from '@opentelemetry/core'; -import { AggregationTemporality, DataPointType, InstrumentType } from '@opentelemetry/sdk-metrics-base'; -import * as assert from 'assert'; -import * as transform from '../../src/transformMetrics'; -import { - collect, - ensureCounterIsCorrect, - ensureDoubleCounterIsCorrect, - ensureHistogramIsCorrect, - ensureObservableCounterIsCorrect, - ensureObservableGaugeIsCorrect, - ensureObservableUpDownCounterIsCorrect, - mockCounter, - mockDoubleCounter, - mockHistogram, - mockObservableCounter, - mockObservableGauge, - mockObservableUpDownCounter, - setUp, -} from '../metricsHelper'; - -describe('transformMetrics', () => { - describe('toCollectorMetric', async () => { - - function getValue(count: number) { - if (count % 2 === 0) { - return 3; - } - return -1; - } - - beforeEach(() => { - setUp(); - }); - - it('should convert counter', async () => { - const counter = mockCounter(); - counter.add(1); - const metrics = (await collect()); - - const metric = metrics.instrumentationLibraryMetrics[0].metrics[0]; - ensureCounterIsCorrect( - transform.toCollectorMetric(metric, AggregationTemporality.CUMULATIVE), - hrTimeToNanoseconds(metric.dataPoints[0].endTime), - hrTimeToNanoseconds(metric.dataPoints[0].startTime) - ); - } - ); - - it('should convert double counter', async () => { - const doubleCounter = mockDoubleCounter(); - doubleCounter.add(8); - const metrics = (await collect()); - - const metric = metrics.instrumentationLibraryMetrics[0].metrics[0]; - ensureDoubleCounterIsCorrect( - transform.toCollectorMetric(metric, AggregationTemporality.CUMULATIVE), - hrTimeToNanoseconds(metric.dataPoints[0].endTime), - hrTimeToNanoseconds(metric.dataPoints[0].startTime), - ); - } - ); - - it('should convert observable gauge', async () => { - let count = 0; - mockObservableGauge(observableResult => { - count++; - observableResult.observe(getValue(count), {}); - }); - - // collect three times. - await collect(); - await collect(); - const metrics = (await collect()); - - const metric = metrics.instrumentationLibraryMetrics[0].metrics[0]; - ensureObservableGaugeIsCorrect( - transform.toCollectorMetric(metric, AggregationTemporality.CUMULATIVE), - hrTimeToNanoseconds(metric.dataPoints[0].endTime), - hrTimeToNanoseconds(metric.dataPoints[0].startTime), - -1, - ); - } - ); - - - it('should convert observable counter', async () => { - mockObservableCounter(observableResult => { - observableResult.observe(1, {}); - }); - - // collect three times. - await collect(); - await collect(); - const metrics = (await collect()); - // TODO: Collect seems to not deliver the last observation -> why? - - const metric = metrics.instrumentationLibraryMetrics[0].metrics[0]; - ensureObservableCounterIsCorrect( - transform.toCollectorMetric(metric, AggregationTemporality.CUMULATIVE), - hrTimeToNanoseconds(metric.dataPoints[0].endTime), - hrTimeToNanoseconds(metric.dataPoints[0].startTime), - 2, - ); - } - ); - - it('should convert observable up-down counter', async () => { - mockObservableUpDownCounter(observableResult => { - observableResult.observe(1, {}); - }); - - // collect three times. - await collect(); - await collect(); - const metrics = (await collect()); - // TODO: Collect seems to not deliver the last observation -> why? - - const metric = metrics.instrumentationLibraryMetrics[0].metrics[0]; - ensureObservableUpDownCounterIsCorrect( - transform.toCollectorMetric(metric, AggregationTemporality.CUMULATIVE), - hrTimeToNanoseconds(metric.dataPoints[0].endTime), - hrTimeToNanoseconds(metric.dataPoints[0].startTime), - 2, - ); - } - ); - - it('should convert observable histogram', async () => { - const histogram = mockHistogram(); - histogram.record(7); - histogram.record(14); - - const metrics = (await collect()); - - const metric = metrics.instrumentationLibraryMetrics[0].metrics[0]; - ensureHistogramIsCorrect( - transform.toCollectorMetric(metric, AggregationTemporality.CUMULATIVE), - hrTimeToNanoseconds(metric.dataPoints[0].endTime), - hrTimeToNanoseconds(metric.dataPoints[0].startTime), - [0, 100], - [0, 2, 0] - ); - } - ); - - it('should convert metric attributes value to string', () => { - const metric = transform.toCollectorMetric( - { - descriptor: { - name: 'name', - description: 'description', - unit: 'unit', - type: InstrumentType.COUNTER, - valueType: 0, - }, - dataPoints: [ - { - value: 1, - attributes: { foo: (1 as unknown) as string }, - startTime: hrTime(), - endTime: hrTime(), - } - ], - dataPointType: DataPointType.SINGULAR, - }, - AggregationTemporality.CUMULATIVE - ); - const collectorMetric = metric.intSum?.dataPoints[0]; - assert.strictEqual(collectorMetric?.labels[0].value, '1'); - }); - }); -}); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts index a38db6ca83..7bd302cce7 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts @@ -14,12 +14,26 @@ * limitations under the License. */ -import { Counter, Histogram, ObservableResult, ValueType, } from '@opentelemetry/api-metrics'; -import { InstrumentationLibrary, VERSION } from '@opentelemetry/core'; -import { ExplicitBucketHistogramAggregation, MeterProvider, MetricReader } from '@opentelemetry/sdk-metrics-base'; +import { + Counter, + ObservableResult, + Histogram, + ValueType, +} from '@opentelemetry/api-metrics'; import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; +import { InstrumentationLibrary, VERSION } from '@opentelemetry/core'; +import { + ExplicitBucketHistogramAggregation, + MeterProvider, + MetricReader +} from '@opentelemetry/sdk-metrics-base'; +import { + IExportMetricsServiceRequest, + IKeyValue, + IMetric, + IResource +} from '@opentelemetry/otlp-transformer'; if (typeof Buffer === 'undefined') { (window as any).Buffer = { @@ -164,7 +178,7 @@ export const mockedInstrumentationLibraries: InstrumentationLibrary[] = [ ]; export function ensureAttributesAreCorrect( - attributes: otlpTypes.opentelemetryProto.common.v1.KeyValue[] + attributes: IKeyValue[] ) { assert.deepStrictEqual( attributes, @@ -181,7 +195,7 @@ export function ensureAttributesAreCorrect( } export function ensureWebResourceIsCorrect( - resource: otlpTypes.opentelemetryProto.resource.v1.Resource + resource: IResource ) { assert.strictEqual(resource.attributes.length, 7); assert.strictEqual(resource.attributes[0].key, 'service.name'); @@ -202,33 +216,31 @@ export function ensureWebResourceIsCorrect( } export function ensureCounterIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, - endTime: number, - startTime: number + metric: IMetric, + time?: number, + startTime?: number ) { assert.deepStrictEqual(metric, { name: 'int-counter', description: 'sample counter description', unit: '1', - intSum: { + sum: { dataPoints: [ { - labels: [], - value: 1, + attributes: [], + asInt: 1, startTimeUnixNano: startTime, - timeUnixNano: endTime, + timeUnixNano: time, }, ], isMonotonic: true, - aggregationTemporality: - otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_CUMULATIVE, + aggregationTemporality: 2, }, }); } export function ensureDoubleCounterIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time: number, endTime: number ) { @@ -246,15 +258,13 @@ export function ensureDoubleCounterIsCorrect( }, ], isMonotonic: true, - aggregationTemporality: - otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_CUMULATIVE, + aggregationTemporality: 2, }, }); } export function ensureObservableGaugeIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time: number, startTime: number, value: number, @@ -264,25 +274,21 @@ export function ensureObservableGaugeIsCorrect( name, description: 'sample observable gauge description', unit: '1', - doubleGauge: { + gauge: { dataPoints: [ { - labels: [], - value, + attributes: [], + asDouble: value, startTimeUnixNano: startTime, timeUnixNano: time, }, - ], - aggregationTemporality: - otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_CUMULATIVE, - isMonotonic: false, + ] }, }); } export function ensureObservableCounterIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time: number, startTime: number, value: number, @@ -296,21 +302,19 @@ export function ensureObservableCounterIsCorrect( isMonotonic: true, dataPoints: [ { - labels: [], + attributes: [], value, startTimeUnixNano: startTime, timeUnixNano: time, }, ], - aggregationTemporality: - otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_CUMULATIVE, + aggregationTemporality: 2 }, }); } export function ensureObservableUpDownCounterIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time: number, startTime: number, value: number, @@ -330,15 +334,13 @@ export function ensureObservableUpDownCounterIsCorrect( timeUnixNano: time, }, ], - aggregationTemporality: - otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_CUMULATIVE, + aggregationTemporality: 2 }, }); } export function ensureHistogramIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time: number, startTime: number, explicitBounds: (number | null)[] = [Infinity], @@ -348,10 +350,10 @@ export function ensureHistogramIsCorrect( name: 'int-histogram', description: 'sample histogram description', unit: '1', - intHistogram: { + histogram: { dataPoints: [ { - labels: [], + attributes: [], sum: 21, count: 2, startTimeUnixNano: startTime, @@ -360,15 +362,13 @@ export function ensureHistogramIsCorrect( explicitBounds, }, ], - aggregationTemporality: - otlpTypes.opentelemetryProto.metrics.v1.AggregationTemporality - .AGGREGATION_TEMPORALITY_CUMULATIVE, + aggregationTemporality: 2 }, }); } export function ensureExportMetricsServiceRequestIsSet( - json: otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest + json: IExportMetricsServiceRequest ) { const resourceMetrics = json.resourceMetrics; assert.strictEqual( diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts index d5eb13986d..9f1ba8fe03 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts @@ -27,7 +27,6 @@ import { import { OTLPMetricExporter } from '../../src/platform/node'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { ensureCounterIsCorrect, ensureExportMetricsServiceRequestIsSet, @@ -41,6 +40,7 @@ import { import { MockedResponse } from './nodeHelpers'; import { AggregationTemporality, ResourceMetrics } from '@opentelemetry/sdk-metrics-base'; import { OTLPExporterError, OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base'; +import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; const fakeRequest = { end: function () { @@ -230,9 +230,10 @@ describe('OTLPMetricExporter - node with json over http', () => { setTimeout(() => { const writeArgs = stubWrite.args[0]; - const json = JSON.parse( + const myJson = JSON.parse( writeArgs[0] - ) as otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest; + ); + const json = myJson as IExportMetricsServiceRequest; const metric1 = json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[0]; const metric2 = diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/tsconfig.json b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/tsconfig.json index db701b3310..69019ed409 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/tsconfig.json +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/tsconfig.json @@ -15,9 +15,6 @@ { "path": "../../../packages/opentelemetry-resources" }, - { - "path": "../exporter-trace-otlp-http" - }, { "path": "../opentelemetry-api-metrics" }, @@ -26,6 +23,9 @@ }, { "path": "../otlp-exporter-base" + }, + { + "path": "../otlp-transformer" } ] } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/package.json b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/package.json index a69ed4d616..dafcc23c94 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/package.json +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/package.json @@ -72,7 +72,7 @@ "@opentelemetry/exporter-metrics-otlp-http": "0.28.0", "@opentelemetry/otlp-proto-exporter-base": "0.28.0", "@opentelemetry/otlp-exporter-base": "0.28.0", - "@opentelemetry/exporter-trace-otlp-http": "0.28.0", + "@opentelemetry/otlp-transformer": "0.28.0", "@opentelemetry/resources": "1.2.0", "@opentelemetry/sdk-metrics-base": "0.28.0", "protobufjs": "^6.9.0" diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts index 105f0c2cf3..e7f97c7618 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts @@ -13,28 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -import { - otlpTypes -} from '@opentelemetry/exporter-trace-otlp-http'; import { defaultExporterTemporality, defaultOptions, - OTLPMetricExporterOptions, - toOTLPExportMetricServiceRequest + OTLPMetricExporterOptions } from '@opentelemetry/exporter-metrics-otlp-http'; import { ServiceClientType, OTLPProtoExporterNodeBase } from '@opentelemetry/otlp-proto-exporter-base'; import { getEnv, baggageUtils} from '@opentelemetry/core'; import { AggregationTemporality, ResourceMetrics} from '@opentelemetry/sdk-metrics-base'; import { OTLPMetricExporterBase } from '@opentelemetry/exporter-metrics-otlp-http'; import { appendResourcePathToUrlIfNotPresent, OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base'; +import { createExportMetricsServiceRequest, IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer'; const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/metrics'; const DEFAULT_COLLECTOR_URL = `http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`; class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase { + IExportMetricsServiceRequest> { protected readonly _aggregationTemporality: AggregationTemporality; constructor(config: OTLPExporterNodeConfigBase & OTLPMetricExporterOptions = defaultOptions) { @@ -50,11 +46,10 @@ class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase { }); it('should successfully send metrics', done => { - collectorExporter.export(metrics, () => { - }); - sinon.stub(http, 'request').returns({ write: () => {}, on: () => {}, end: (...writeArgs: any[]) => { const ExportTraceServiceRequestProto = getExportRequestProto(); const data = ExportTraceServiceRequestProto?.decode(writeArgs[0]); - const json = data?.toJSON() as otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest; + const json = data?.toJSON() as IExportMetricsServiceRequest; const metric1 = json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[0]; @@ -194,14 +189,14 @@ describe('OTLPMetricExporter - node with proto over http', () => { assert.ok(typeof metric1 !== 'undefined', "counter doesn't exist"); ensureExportedCounterIsCorrect( metric1, - metric1.intSum?.dataPoints[0].timeUnixNano, - metric1.intSum?.dataPoints[0].startTimeUnixNano + metric1.sum?.dataPoints[0].timeUnixNano, + metric1.sum?.dataPoints[0].startTimeUnixNano ); assert.ok(typeof metric2 !== 'undefined', "observable gauge doesn't exist"); ensureExportedObservableGaugeIsCorrect( metric2, - metric2.doubleGauge?.dataPoints[0].timeUnixNano, - metric2.doubleGauge?.dataPoints[0].startTimeUnixNano + metric2.gauge?.dataPoints[0].timeUnixNano, + metric2.gauge?.dataPoints[0].startTimeUnixNano ); assert.ok( typeof metric3 !== 'undefined', @@ -209,17 +204,20 @@ describe('OTLPMetricExporter - node with proto over http', () => { ); ensureExportedHistogramIsCorrect( metric3, - metric3.intHistogram?.dataPoints[0].timeUnixNano, - metric3.intHistogram?.dataPoints[0].startTimeUnixNano, + metric3.histogram?.dataPoints[0].timeUnixNano, + metric3.histogram?.dataPoints[0].startTimeUnixNano, [0, 100], ['0', '2', '0'] ); ensureExportMetricsServiceRequestIsSet(json); - done(); }, } as any); + + collectorExporter.export(metrics, result => { + done(result.error); + }); }); it('should log the successful message', done => { diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts index 8daa508726..2b6a4d3a8a 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts @@ -20,15 +20,15 @@ import { Histogram, ValueType, } from '@opentelemetry/api-metrics'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { Resource } from '@opentelemetry/resources'; import * as assert from 'assert'; -import { Stream } from 'stream'; import { ExplicitBucketHistogramAggregation, MeterProvider, MetricReader } from '@opentelemetry/sdk-metrics-base'; +import { IExportMetricsServiceRequest, IKeyValue, IMetric } from '@opentelemetry/otlp-transformer'; +import { Stream } from 'stream'; export class TestMetricReader extends MetricReader { protected onForceFlush(): Promise { @@ -103,7 +103,7 @@ export function mockHistogram(): Histogram { } export function ensureProtoAttributesAreCorrect( - attributes: otlpTypes.opentelemetryProto.common.v1.KeyValue[] + attributes: IKeyValue[] ) { assert.deepStrictEqual( attributes, @@ -120,7 +120,7 @@ export function ensureProtoAttributesAreCorrect( } export function ensureExportedCounterIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time?: number, startTime?: number ) { @@ -128,10 +128,10 @@ export function ensureExportedCounterIsCorrect( name: 'int-counter', description: 'sample counter description', unit: '1', - intSum: { + sum: { dataPoints: [ { - value: '1', + asInt: '1', startTimeUnixNano: String(startTime), timeUnixNano: String(time), }, @@ -143,7 +143,7 @@ export function ensureExportedCounterIsCorrect( } export function ensureExportedObservableGaugeIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time?: number, startTime?: number ) { @@ -151,10 +151,10 @@ export function ensureExportedObservableGaugeIsCorrect( name: 'double-observable-gauge', description: 'sample observable gauge description', unit: '1', - doubleGauge: { + gauge: { dataPoints: [ { - value: 6, + asDouble: 6, startTimeUnixNano: String(startTime), timeUnixNano: String(time), }, @@ -164,7 +164,7 @@ export function ensureExportedObservableGaugeIsCorrect( } export function ensureExportedHistogramIsCorrect( - metric: otlpTypes.opentelemetryProto.metrics.v1.Metric, + metric: IMetric, time?: number, startTime?: number, explicitBounds: number[] = [Infinity], @@ -174,10 +174,10 @@ export function ensureExportedHistogramIsCorrect( name: 'int-histogram', description: 'sample histogram description', unit: '1', - intHistogram: { + histogram: { dataPoints: [ { - sum: '21', + sum: 21, count: '2', startTimeUnixNano: String(startTime), timeUnixNano: String(time), @@ -191,7 +191,7 @@ export function ensureExportedHistogramIsCorrect( } export function ensureExportMetricsServiceRequestIsSet( - json: otlpTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest + json: IExportMetricsServiceRequest ) { const resourceMetrics = json.resourceMetrics; assert.strictEqual( diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/tsconfig.json b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/tsconfig.json index d81d9c0a67..1023f08037 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/tsconfig.json +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/tsconfig.json @@ -15,9 +15,6 @@ { "path": "../../../packages/opentelemetry-resources" }, - { - "path": "../exporter-trace-otlp-http" - }, { "path": "../opentelemetry-api-metrics" }, @@ -32,6 +29,9 @@ }, { "path": "../otlp-proto-exporter-base" + }, + { + "path": "../otlp-transformer" } ] } diff --git a/experimental/packages/otlp-grpc-exporter-base/package.json b/experimental/packages/otlp-grpc-exporter-base/package.json index e3c34d8c02..26486d265c 100644 --- a/experimental/packages/otlp-grpc-exporter-base/package.json +++ b/experimental/packages/otlp-grpc-exporter-base/package.json @@ -64,8 +64,8 @@ "ts-mocha": "9.0.2", "typescript": "4.4.4", "@opentelemetry/sdk-trace-base": "1.2.0", - "@opentelemetry/resources": "1.2.0", - "@opentelemetry/exporter-trace-otlp-http": "0.28.0" + "@opentelemetry/otlp-transformer": "0.28.0", + "@opentelemetry/resources": "1.2.0" }, "peerDependencies": { "@opentelemetry/api": "^1.0.0" diff --git a/experimental/packages/otlp-grpc-exporter-base/test/traceHelper.ts b/experimental/packages/otlp-grpc-exporter-base/test/traceHelper.ts index 83578d3c12..7a2d7a3022 100644 --- a/experimental/packages/otlp-grpc-exporter-base/test/traceHelper.ts +++ b/experimental/packages/otlp-grpc-exporter-base/test/traceHelper.ts @@ -15,12 +15,12 @@ */ import { SpanStatusCode, TraceFlags } from '@opentelemetry/api'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { Resource } from '@opentelemetry/resources'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; import * as grpc from '@grpc/grpc-js'; import { VERSION } from '@opentelemetry/core'; +import { IEvent, IKeyValue, ILink, IResource, ISpan } from '@opentelemetry/otlp-transformer'; const traceIdArr = [ 31, @@ -101,7 +101,7 @@ export const mockedReadableSpan: ReadableSpan = { }; export function ensureExportedEventsAreCorrect( - events: otlpTypes.opentelemetryProto.trace.v1.Span.Event[] + events: IEvent[] ) { assert.deepStrictEqual( events, @@ -160,7 +160,7 @@ export function ensureExportedEventsAreCorrect( } export function ensureExportedAttributesAreCorrect( - attributes: otlpTypes.opentelemetryProto.common.v1.KeyValue[] + attributes: IKeyValue[] ) { assert.deepStrictEqual( attributes, @@ -178,7 +178,7 @@ export function ensureExportedAttributesAreCorrect( } export function ensureExportedLinksAreCorrect( - attributes: otlpTypes.opentelemetryProto.trace.v1.Span.Link[] + attributes: ILink[] ) { assert.deepStrictEqual( attributes, @@ -204,7 +204,7 @@ export function ensureExportedLinksAreCorrect( } export function ensureExportedSpanIsCorrect( - span: otlpTypes.opentelemetryProto.trace.v1.Span + span: ISpan ) { if (span.attributes) { ensureExportedAttributesAreCorrect(span.attributes); @@ -262,7 +262,7 @@ export function ensureExportedSpanIsCorrect( } export function ensureResourceIsCorrect( - resource: otlpTypes.opentelemetryProto.resource.v1.Resource + resource: IResource ) { assert.deepStrictEqual(resource, { attributes: [ diff --git a/experimental/packages/otlp-grpc-exporter-base/tsconfig.json b/experimental/packages/otlp-grpc-exporter-base/tsconfig.json index c8e54e3690..087c804079 100644 --- a/experimental/packages/otlp-grpc-exporter-base/tsconfig.json +++ b/experimental/packages/otlp-grpc-exporter-base/tsconfig.json @@ -19,10 +19,10 @@ "path": "../../../packages/opentelemetry-sdk-trace-base" }, { - "path": "../exporter-trace-otlp-http" + "path": "../otlp-exporter-base" }, { - "path": "../otlp-exporter-base" + "path": "../otlp-transformer" } ] } diff --git a/experimental/packages/otlp-transformer/package.json b/experimental/packages/otlp-transformer/package.json index 389b9592e2..bdd9aced4d 100644 --- a/experimental/packages/otlp-transformer/package.json +++ b/experimental/packages/otlp-transformer/package.json @@ -9,6 +9,7 @@ "module": "build/esm/index.js", "esnext": "build/esnext/index.js", "types": "build/src/index.d.ts", + "main": "build/src/index.js", "repository": "open-telemetry/opentelemetry-js", "scripts": { "compile": "tsc --build tsconfig.all.json", diff --git a/experimental/packages/otlp-transformer/src/index.ts b/experimental/packages/otlp-transformer/src/index.ts index b7052c8f54..51408c10d8 100644 --- a/experimental/packages/otlp-transformer/src/index.ts +++ b/experimental/packages/otlp-transformer/src/index.ts @@ -15,9 +15,9 @@ */ export * from './common/types'; -// export * from './metrics/types'; +export * from './metrics/types'; export * from './resource/types'; export * from './trace/types'; export { createExportTraceServiceRequest } from './trace'; -// export { createExportMetricsServiceRequest } from './metrics'; +export { createExportMetricsServiceRequest } from './metrics'; diff --git a/experimental/packages/otlp-transformer/src/metrics/index.ts b/experimental/packages/otlp-transformer/src/metrics/index.ts index 9022048c29..a714a0cd14 100644 --- a/experimental/packages/otlp-transformer/src/metrics/index.ts +++ b/experimental/packages/otlp-transformer/src/metrics/index.ts @@ -20,7 +20,7 @@ import type { IExportMetricsServiceRequest } from './types'; import { AggregationTemporality } from '@opentelemetry/sdk-metrics-base'; export function createExportMetricsServiceRequest(resourceMetrics: ResourceMetrics, - aggregationTemporality: AggregationTemporality): IExportMetricsServiceRequest | null { + aggregationTemporality: AggregationTemporality): IExportMetricsServiceRequest { return { resourceMetrics: [{ resource: { diff --git a/experimental/packages/otlp-transformer/src/metrics/types.ts b/experimental/packages/otlp-transformer/src/metrics/types.ts index b5900c31e0..55282afefe 100644 --- a/experimental/packages/otlp-transformer/src/metrics/types.ts +++ b/experimental/packages/otlp-transformer/src/metrics/types.ts @@ -100,7 +100,7 @@ export interface ISum { /** Properties of a Histogram. */ export interface IHistogram { /** Histogram dataPoints */ - dataPoints?: IHistogramDataPoint[] + dataPoints: IHistogramDataPoint[] /** Histogram aggregationTemporality */ aggregationTemporality?: EAggregationTemporality From ca54530588fb65dcacfa04a6ca70828780104696 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Tue, 26 Apr 2022 20:32:39 +0200 Subject: [PATCH 04/29] feat(otlp-trace-exporter): update to proto 0.14. --- .../exporter-trace-otlp-grpc/package.json | 2 +- .../src/OTLPTraceExporter.ts | 11 +- .../test/OTLPTraceExporter.test.ts | 27 +- .../test/traceHelper.ts | 13 +- .../exporter-trace-otlp-grpc/tsconfig.json | 6 +- .../exporter-trace-otlp-http/package.json | 3 +- .../exporter-trace-otlp-http/src/index.ts | 2 - .../src/platform/browser/OTLPTraceExporter.ts | 9 +- .../src/platform/node/OTLPTraceExporter.ts | 9 +- .../exporter-trace-otlp-http/src/transform.ts | 367 ------------------ .../exporter-trace-otlp-http/src/types.ts | 326 ---------------- .../browser/CollectorTraceExporter.test.ts | 14 +- .../test/browser/index-webpack.ts | 3 - .../test/common/transform.test.ts | 218 ----------- .../test/node/CollectorTraceExporter.test.ts | 10 +- .../test/traceHelper.ts | 33 +- .../exporter-trace-otlp-http/tsconfig.json | 3 + .../exporter-trace-otlp-proto/package.json | 3 +- .../src/OTLPTraceExporter.ts | 11 +- .../test/OTLPTraceExporter.test.ts | 12 +- .../test/traceHelper.ts | 20 +- .../exporter-trace-otlp-proto/tsconfig.json | 3 + .../otlp-transformer/src/trace/index.ts | 4 +- .../otlp-transformer/src/trace/internal.ts | 17 +- .../otlp-transformer/test/trace.test.ts | 22 +- 25 files changed, 120 insertions(+), 1028 deletions(-) delete mode 100644 experimental/packages/exporter-trace-otlp-http/src/transform.ts delete mode 100644 experimental/packages/exporter-trace-otlp-http/src/types.ts delete mode 100644 experimental/packages/exporter-trace-otlp-http/test/common/transform.test.ts diff --git a/experimental/packages/exporter-trace-otlp-grpc/package.json b/experimental/packages/exporter-trace-otlp-grpc/package.json index c63e084cff..fcc3bd3eff 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/package.json +++ b/experimental/packages/exporter-trace-otlp-grpc/package.json @@ -70,7 +70,7 @@ "@opentelemetry/core": "1.2.0", "@grpc/grpc-js": "^1.5.9", "@grpc/proto-loader": "^0.6.9", - "@opentelemetry/exporter-trace-otlp-http": "0.28.0", + "@opentelemetry/otlp-transformer": "0.28.0", "@opentelemetry/otlp-grpc-exporter-base": "0.28.0", "@opentelemetry/resources": "1.2.0", "@opentelemetry/sdk-trace-base": "1.2.0" diff --git a/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts index d08e679e99..0acd3ecc94 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts @@ -15,10 +15,6 @@ */ import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base'; -import { - otlpTypes, - toOTLPExportTraceServiceRequest, -} from '@opentelemetry/exporter-trace-otlp-http'; import { baggageUtils, getEnv } from '@opentelemetry/core'; import { Metadata } from '@grpc/grpc-js'; import { @@ -27,6 +23,7 @@ import { ServiceClientType, validateAndNormalizeUrl } from '@opentelemetry/otlp-grpc-exporter-base'; +import { createExportTraceServiceRequest, IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer'; const DEFAULT_COLLECTOR_URL = 'localhost:4317'; @@ -35,7 +32,7 @@ const DEFAULT_COLLECTOR_URL = 'localhost:4317'; */ export class OTLPTraceExporter extends OTLPGRPCExporterNodeBase + IExportTraceServiceRequest> implements SpanExporter { constructor(config: OTLPGRPCExporterConfigNode = {}) { @@ -49,8 +46,8 @@ export class OTLPTraceExporter convert( spans: ReadableSpan[] - ): otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest { - return toOTLPExportTraceServiceRequest(spans, this); + ): IExportTraceServiceRequest { + return createExportTraceServiceRequest(spans); } getDefaultUrl(config: OTLPGRPCExporterConfigNode) { diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index fc4383f183..dfb01074ba 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -15,7 +15,6 @@ */ import * as protoLoader from '@grpc/proto-loader'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { diag } from '@opentelemetry/api'; import { BasicTracerProvider, @@ -37,6 +36,7 @@ import { } from './traceHelper'; import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base'; import { GrpcCompressionAlgorithm } from '@opentelemetry/otlp-grpc-exporter-base'; +import { IExportTraceServiceRequest, IResourceSpans } from '@opentelemetry/otlp-transformer'; const traceServiceProtoPath = 'opentelemetry/proto/collector/trace/v1/trace_service.proto'; @@ -59,7 +59,7 @@ const testCollectorExporter = (params: TestParams) => let collectorExporter: OTLPTraceExporter; let server: grpc.Server; let exportedData: - | otlpTypes.opentelemetryProto.trace.v1.ResourceSpans + | IResourceSpans | undefined; let reqMetadata: grpc.Metadata | undefined; @@ -83,15 +83,13 @@ const testCollectorExporter = (params: TestParams) => .service, { Export: (data: { - request: otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest; + request: IExportTraceServiceRequest; metadata: grpc.Metadata; }) => { - try { + if (data.request.resourceSpans != null) { exportedData = data.request.resourceSpans[0]; - reqMetadata = data.metadata; - } catch (e) { - exportedData = undefined; } + reqMetadata = data.metadata; }, } ); @@ -180,8 +178,9 @@ const testCollectorExporter = (params: TestParams) => ); let spans; let resource; - if (exportedData) { + if (exportedData && exportedData.instrumentationLibrarySpans[0].spans) { spans = exportedData.instrumentationLibrarySpans[0].spans; + resource = exportedData.resource; ensureExportedSpanIsCorrect(spans[0]); @@ -230,7 +229,7 @@ const testCollectorExporter = (params: TestParams) => ); let spans; let resource; - if (exportedData) { + if (exportedData && exportedData.instrumentationLibrarySpans[0].spans) { spans = exportedData.instrumentationLibrarySpans[0].spans; resource = exportedData.resource; ensureExportedSpanIsCorrect(spans[0]); @@ -261,7 +260,7 @@ const testCollectorExporter = (params: TestParams) => ) : undefined; - envSource.OTEL_EXPORTER_OTLP_COMPRESSION='gzip'; + envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'gzip'; collectorExporter = new OTLPTraceExporter({ url: 'grpcs://' + address, credentials, @@ -334,8 +333,10 @@ describe('when configuring via environment', () => { }); }); -testCollectorExporter({ useTLS: true }); -testCollectorExporter({ useTLS: false }); -testCollectorExporter({ metadata }); +describe('', () => { + testCollectorExporter({ useTLS: true }); + testCollectorExporter({ useTLS: false }); + testCollectorExporter({ metadata }); +}); diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts index 83578d3c12..b7476f45f0 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts @@ -15,12 +15,12 @@ */ import { SpanStatusCode, TraceFlags } from '@opentelemetry/api'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { Resource } from '@opentelemetry/resources'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; import * as grpc from '@grpc/grpc-js'; import { VERSION } from '@opentelemetry/core'; +import { IEvent, IKeyValue, ILink, IResource, ISpan } from '@opentelemetry/otlp-transformer'; const traceIdArr = [ 31, @@ -101,7 +101,7 @@ export const mockedReadableSpan: ReadableSpan = { }; export function ensureExportedEventsAreCorrect( - events: otlpTypes.opentelemetryProto.trace.v1.Span.Event[] + events: IEvent[] ) { assert.deepStrictEqual( events, @@ -160,7 +160,7 @@ export function ensureExportedEventsAreCorrect( } export function ensureExportedAttributesAreCorrect( - attributes: otlpTypes.opentelemetryProto.common.v1.KeyValue[] + attributes: IKeyValue[] ) { assert.deepStrictEqual( attributes, @@ -178,7 +178,7 @@ export function ensureExportedAttributesAreCorrect( } export function ensureExportedLinksAreCorrect( - attributes: otlpTypes.opentelemetryProto.trace.v1.Span.Link[] + attributes: ILink[] ) { assert.deepStrictEqual( attributes, @@ -204,7 +204,7 @@ export function ensureExportedLinksAreCorrect( } export function ensureExportedSpanIsCorrect( - span: otlpTypes.opentelemetryProto.trace.v1.Span + span: ISpan ) { if (span.attributes) { ensureExportedAttributesAreCorrect(span.attributes); @@ -254,7 +254,6 @@ export function ensureExportedSpanIsCorrect( span.status, { code: 'STATUS_CODE_OK', - deprecatedCode: 'DEPRECATED_STATUS_CODE_OK', message: '', }, 'status is wrong' @@ -262,7 +261,7 @@ export function ensureExportedSpanIsCorrect( } export function ensureResourceIsCorrect( - resource: otlpTypes.opentelemetryProto.resource.v1.Resource + resource: IResource ) { assert.deepStrictEqual(resource, { attributes: [ diff --git a/experimental/packages/exporter-trace-otlp-grpc/tsconfig.json b/experimental/packages/exporter-trace-otlp-grpc/tsconfig.json index 4a71bbfef6..94d47b6e19 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/tsconfig.json +++ b/experimental/packages/exporter-trace-otlp-grpc/tsconfig.json @@ -18,14 +18,14 @@ { "path": "../../../packages/opentelemetry-sdk-trace-base" }, - { - "path": "../exporter-trace-otlp-http" - }, { "path": "../otlp-exporter-base" }, { "path": "../otlp-grpc-exporter-base" + }, + { + "path": "../otlp-transformer" } ] } diff --git a/experimental/packages/exporter-trace-otlp-http/package.json b/experimental/packages/exporter-trace-otlp-http/package.json index f7528a5349..f973ae4b43 100644 --- a/experimental/packages/exporter-trace-otlp-http/package.json +++ b/experimental/packages/exporter-trace-otlp-http/package.json @@ -97,6 +97,7 @@ "@opentelemetry/core": "1.2.0", "@opentelemetry/resources": "1.2.0", "@opentelemetry/sdk-trace-base": "1.2.0", - "@opentelemetry/otlp-exporter-base": "0.28.0" + "@opentelemetry/otlp-exporter-base": "0.28.0", + "@opentelemetry/otlp-transformer": "0.28.0" } } diff --git a/experimental/packages/exporter-trace-otlp-http/src/index.ts b/experimental/packages/exporter-trace-otlp-http/src/index.ts index d22bb9029d..52ec5f71f5 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/index.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/index.ts @@ -15,5 +15,3 @@ */ export * from './platform'; -export * as otlpTypes from './types'; -export { toCollectorResource, toOTLPExportTraceServiceRequest } from './transform'; diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts index 1f78f66ef6..966d774341 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts @@ -16,10 +16,9 @@ import { appendResourcePathToUrlIfNotPresent, OTLPExporterBrowserBase } from '@opentelemetry/otlp-exporter-base'; import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base'; -import { toOTLPExportTraceServiceRequest } from '../../transform'; -import * as otlpTypes from '../../types'; import { getEnv, baggageUtils } from '@opentelemetry/core'; import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base'; +import { createExportTraceServiceRequest, IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer'; const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/traces'; const DEFAULT_COLLECTOR_URL=`http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`; @@ -30,7 +29,7 @@ const DEFAULT_COLLECTOR_URL=`http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_P export class OTLPTraceExporter extends OTLPExporterBrowserBase< ReadableSpan, - otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest + IExportTraceServiceRequest > implements SpanExporter { constructor(config: OTLPExporterConfigBase = {}) { @@ -44,8 +43,8 @@ export class OTLPTraceExporter } convert( spans: ReadableSpan[] - ): otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest { - return toOTLPExportTraceServiceRequest(spans, this, true); + ): IExportTraceServiceRequest { + return createExportTraceServiceRequest(spans); } getDefaultUrl(config: OTLPExporterConfigBase): string { diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts index d4a6e21a93..1b2cd89219 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts @@ -15,14 +15,13 @@ */ import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base'; -import * as otlpTypes from '../../types'; -import { toOTLPExportTraceServiceRequest } from '../../transform'; import { getEnv, baggageUtils } from '@opentelemetry/core'; import { OTLPExporterNodeBase } from '@opentelemetry/otlp-exporter-base'; import { OTLPExporterNodeConfigBase, appendResourcePathToUrlIfNotPresent } from '@opentelemetry/otlp-exporter-base'; +import { createExportTraceServiceRequest, IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer'; const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/traces'; const DEFAULT_COLLECTOR_URL = `http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`; @@ -32,7 +31,7 @@ const DEFAULT_COLLECTOR_URL = `http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE */ export class OTLPTraceExporter extends OTLPExporterNodeBase + IExportTraceServiceRequest> implements SpanExporter { constructor(config: OTLPExporterNodeConfigBase = {}) { super(config); @@ -46,8 +45,8 @@ export class OTLPTraceExporter convert( spans: ReadableSpan[] - ): otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest { - return toOTLPExportTraceServiceRequest(spans, this, true); + ): IExportTraceServiceRequest { + return createExportTraceServiceRequest(spans, true); } getDefaultUrl(config: OTLPExporterNodeConfigBase): string { diff --git a/experimental/packages/exporter-trace-otlp-http/src/transform.ts b/experimental/packages/exporter-trace-otlp-http/src/transform.ts deleted file mode 100644 index 5224c527fa..0000000000 --- a/experimental/packages/exporter-trace-otlp-http/src/transform.ts +++ /dev/null @@ -1,367 +0,0 @@ -/* - * 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 { - SpanAttributes, - Link, - SpanKind, - SpanStatus, - TraceState, -} from '@opentelemetry/api'; -import * as core from '@opentelemetry/core'; -import { Resource } from '@opentelemetry/resources'; -import { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'; -import { - OTLP_SPAN_KIND_MAPPING, - opentelemetryProto, -} from './types'; -import { OTLPExporterBase, OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base'; - -const MAX_INTEGER_VALUE = 2147483647; -const MIN_INTEGER_VALUE = -2147483648; - -/** - * Converts attributes to KeyValue array - * @param attributes - */ -export function toCollectorAttributes( - attributes: SpanAttributes -): opentelemetryProto.common.v1.KeyValue[] { - return Object.keys(attributes).map(key => { - return toCollectorAttributeKeyValue(key, attributes[key]); - }); -} - -/** - * Converts array of unknown value to ArrayValue - * @param values - */ -export function toCollectorArrayValue( - values: unknown[] -): opentelemetryProto.common.v1.ArrayValue { - return { - values: values.map(value => toCollectorAnyValue(value)), - }; -} - -/** - * Converts attributes to KeyValueList - * @param attributes - */ -export function toCollectorKeyValueList( - attributes: SpanAttributes -): opentelemetryProto.common.v1.KeyValueList { - return { - values: toCollectorAttributes(attributes), - }; -} - -/** - * Converts key and unknown value to KeyValue - * @param value event value - */ -export function toCollectorAttributeKeyValue( - key: string, - value: unknown -): opentelemetryProto.common.v1.KeyValue { - const anyValue = toCollectorAnyValue(value); - return { - key, - value: anyValue, - }; -} - -/** - * Converts unknown value to AnyValue - * @param value - */ -export function toCollectorAnyValue( - value: unknown -): opentelemetryProto.common.v1.AnyValue { - const anyValue: opentelemetryProto.common.v1.AnyValue = {}; - if (typeof value === 'string') { - anyValue.stringValue = value; - } else if (typeof value === 'boolean') { - anyValue.boolValue = value; - } else if ( - typeof value === 'number' && - value <= MAX_INTEGER_VALUE && - value >= MIN_INTEGER_VALUE && - Number.isInteger(value) - ) { - anyValue.intValue = value; - } else if (typeof value === 'number') { - anyValue.doubleValue = value; - } else if (Array.isArray(value)) { - anyValue.arrayValue = toCollectorArrayValue(value); - } else if (value) { - anyValue.kvlistValue = toCollectorKeyValueList(value as SpanAttributes); - } - return anyValue; -} - -/** - * - * Converts events - * @param events array of events - */ -export function toCollectorEvents( - timedEvents: TimedEvent[] -): opentelemetryProto.trace.v1.Span.Event[] { - return timedEvents.map(timedEvent => { - const timeUnixNano = core.hrTimeToNanoseconds(timedEvent.time); - const name = timedEvent.name; - const attributes = toCollectorAttributes(timedEvent.attributes || {}); - const droppedAttributesCount = 0; - - const protoEvent: opentelemetryProto.trace.v1.Span.Event = { - timeUnixNano, - name, - attributes, - droppedAttributesCount, - }; - - return protoEvent; - }); -} - -/** - * Converts links - * @param span - * @param useHex - if ids should be kept as hex without converting to base64 - */ -function toCollectorLinks( - span: ReadableSpan, - useHex?: boolean -): opentelemetryProto.trace.v1.Span.Link[] { - return span.links.map((link: Link) => { - const protoLink: opentelemetryProto.trace.v1.Span.Link = { - traceId: useHex - ? link.context.traceId - : core.hexToBase64(link.context.traceId), - spanId: useHex - ? link.context.spanId - : core.hexToBase64(link.context.spanId), - attributes: toCollectorAttributes(link.attributes || {}), - droppedAttributesCount: 0, - }; - return protoLink; - }); -} - -/** - * Converts span - * @param span - * @param useHex - if ids should be kept as hex without converting to base64 - */ -export function toCollectorSpan( - span: ReadableSpan, - useHex?: boolean -): opentelemetryProto.trace.v1.Span { - return { - traceId: useHex - ? span.spanContext().traceId - : core.hexToBase64(span.spanContext().traceId), - spanId: useHex - ? span.spanContext().spanId - : core.hexToBase64(span.spanContext().spanId), - parentSpanId: span.parentSpanId - ? useHex - ? span.parentSpanId - : core.hexToBase64(span.parentSpanId) - : undefined, - traceState: toCollectorTraceState(span.spanContext().traceState), - name: span.name, - kind: toCollectorKind(span.kind), - startTimeUnixNano: core.hrTimeToNanoseconds(span.startTime), - endTimeUnixNano: core.hrTimeToNanoseconds(span.endTime), - attributes: toCollectorAttributes(span.attributes), - droppedAttributesCount: 0, - events: toCollectorEvents(span.events), - droppedEventsCount: 0, - status: toCollectorStatus(span.status), - links: toCollectorLinks(span, useHex), - droppedLinksCount: 0, - }; -} - -/** - * Converts status - * @param status - */ -export function toCollectorStatus( - status: SpanStatus -): opentelemetryProto.trace.v1.SpanStatus { - const spanStatus: opentelemetryProto.trace.v1.SpanStatus = { - code: status.code, - }; - if (typeof status.message !== 'undefined') { - spanStatus.message = status.message; - } - return spanStatus; -} - -/** - * Converts resource - * @param resource - * @param additionalAttributes - */ -export function toCollectorResource( - resource?: Resource, - additionalAttributes: { [key: string]: unknown } = {} -): opentelemetryProto.resource.v1.Resource { - const attr = Object.assign( - {}, - additionalAttributes, - resource ? resource.attributes : {} - ); - const resourceProto: opentelemetryProto.resource.v1.Resource = { - attributes: toCollectorAttributes(attr), - droppedAttributesCount: 0, - }; - - return resourceProto; -} - -/** - * Converts span kind - * @param kind - */ -export function toCollectorKind( - kind: SpanKind -): opentelemetryProto.trace.v1.Span.SpanKind { - const collectorKind = OTLP_SPAN_KIND_MAPPING[kind]; - return typeof collectorKind === 'number' - ? collectorKind - : opentelemetryProto.trace.v1.Span.SpanKind.SPAN_KIND_UNSPECIFIED; -} - -/** - * Converts traceState - * @param traceState - */ -export function toCollectorTraceState( - traceState?: TraceState -): opentelemetryProto.trace.v1.Span.TraceState | undefined { - if (!traceState) return undefined; - return traceState.serialize(); -} - -/** - * Prepares trace service request to be sent to collector - * @param spans spans - * @param collectorExporterBase - * @param useHex - if ids should be kept as hex without converting to base64 - */ -export function toOTLPExportTraceServiceRequest< - T extends OTLPExporterConfigBase ->( - spans: ReadableSpan[], - collectorTraceExporterBase: OTLPExporterBase< - T, - ReadableSpan, - opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest - >, - useHex?: boolean -): opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest { - const groupedSpans: Map< - Resource, - Map - > = groupSpansByResourceAndLibrary(spans); - - const additionalAttributes = Object.assign( - {}, - collectorTraceExporterBase.attributes - ); - - return { - resourceSpans: toCollectorResourceSpans( - groupedSpans, - additionalAttributes, - useHex - ), - }; -} - -/** - * Takes an array of spans and groups them by resource and instrumentation - * library - * @param spans spans - */ -export function groupSpansByResourceAndLibrary( - spans: ReadableSpan[] -): Map> { - return spans.reduce((spanMap, span) => { - //group by resource - let resourceSpans = spanMap.get(span.resource); - if (!resourceSpans) { - resourceSpans = new Map(); - spanMap.set(span.resource, resourceSpans); - } - //group by instrumentation library - let libSpans = resourceSpans.get(span.instrumentationLibrary); - if (!libSpans) { - libSpans = new Array(); - resourceSpans.set(span.instrumentationLibrary, libSpans); - } - libSpans.push(span); - return spanMap; - }, new Map>()); -} - -/** - * Convert to InstrumentationLibrarySpans - * @param instrumentationLibrary - * @param spans - * @param useHex - if ids should be kept as hex without converting to base64 - */ -function toCollectorInstrumentationLibrarySpans( - instrumentationLibrary: core.InstrumentationLibrary, - spans: ReadableSpan[], - useHex?: boolean -): opentelemetryProto.trace.v1.InstrumentationLibrarySpans { - return { - spans: spans.map(span => toCollectorSpan(span, useHex)), - instrumentationLibrary, - }; -} - -/** - * Returns a list of resource spans which will be exported to the collector - * @param groupedSpans - * @param baseAttributes - * @param useHex - if ids should be kept as hex without converting to base64 - */ -function toCollectorResourceSpans( - groupedSpans: Map>, - baseAttributes: SpanAttributes, - useHex?: boolean -): opentelemetryProto.trace.v1.ResourceSpans[] { - return Array.from(groupedSpans, ([resource, libSpans]) => { - return { - resource: toCollectorResource(resource, baseAttributes), - instrumentationLibrarySpans: Array.from( - libSpans, - ([instrumentationLibrary, spans]) => - toCollectorInstrumentationLibrarySpans( - instrumentationLibrary, - spans, - useHex - ) - ), - }; - }); -} diff --git a/experimental/packages/exporter-trace-otlp-http/src/types.ts b/experimental/packages/exporter-trace-otlp-http/src/types.ts deleted file mode 100644 index 02be0ed2e4..0000000000 --- a/experimental/packages/exporter-trace-otlp-http/src/types.ts +++ /dev/null @@ -1,326 +0,0 @@ -/* - * 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 { SpanKind, SpanStatusCode } from '@opentelemetry/api'; - -/* eslint-disable @typescript-eslint/no-namespace */ -/* eslint-disable @typescript-eslint/no-unused-vars */ - -export namespace opentelemetryProto { - export namespace collector { - export namespace trace.v1 { - export interface TraceService { - service: opentelemetryProto.collector.trace.v1.TraceService; - } - - export interface ExportTraceServiceRequest { - resourceSpans: opentelemetryProto.trace.v1.ResourceSpans[]; - } - } - export namespace metrics.v1 { - export interface ExportMetricsServiceRequest { - resourceMetrics: opentelemetryProto.metrics.v1.ResourceMetrics[]; - } - } - } - - export namespace resource.v1 { - export interface Resource { - attributes: opentelemetryProto.common.v1.KeyValue[]; - droppedAttributesCount: number; - } - } - - export namespace metrics.v1 { - export interface Metric { - name: string; - description: string; - unit: string; - // data: - intGauge?: opentelemetryProto.metrics.v1.Gauge; - doubleGauge?: opentelemetryProto.metrics.v1.Gauge; - intSum?: opentelemetryProto.metrics.v1.Sum; - doubleSum?: opentelemetryProto.metrics.v1.Sum; - intHistogram?: opentelemetryProto.metrics.v1.Histogram; - doubleHistogram?: opentelemetryProto.metrics.v1.Histogram; - } - - export interface Gauge { - dataPoints: opentelemetryProto.metrics.v1.DataPoint[]; - } - - export interface Sum { - dataPoints: opentelemetryProto.metrics.v1.DataPoint[]; - aggregationTemporality: opentelemetryProto.metrics.v1.AggregationTemporality; - isMonotonic: boolean; - } - - export interface Histogram { - dataPoints: opentelemetryProto.metrics.v1.HistogramDataPoint[]; - aggregationTemporality: opentelemetryProto.metrics.v1.AggregationTemporality; - } - - export interface DataPoint { - labels: opentelemetryProto.common.v1.StringKeyValue[]; - startTimeUnixNano: number; - timeUnixNano: number; - value: number; - exemplars?: opentelemetryProto.metrics.v1.Exemplar[]; - } - - export interface Exemplar { - filteredLabels: opentelemetryProto.common.v1.StringKeyValue[]; - timeUnixNano: number; - value: number; - spanId: Uint8Array; - traceId: Uint8Array; - } - - export interface HistogramDataPoint { - labels: opentelemetryProto.common.v1.StringKeyValue[]; - startTimeUnixNano: number; - timeUnixNano: number; - count: number; - sum: number; - bucketCounts?: number[]; - explicitBounds?: number[]; - exemplars?: opentelemetryProto.metrics.v1.Exemplar[][]; - } - - export interface InstrumentationLibraryMetrics { - instrumentationLibrary?: opentelemetryProto.common.v1.InstrumentationLibrary; - metrics: opentelemetryProto.metrics.v1.Metric[]; - } - - export interface ResourceMetrics { - resource?: opentelemetryProto.resource.v1.Resource; - instrumentationLibraryMetrics: opentelemetryProto.metrics.v1.InstrumentationLibraryMetrics[]; - } - - export enum AggregationTemporality { - // UNSPECIFIED is the default AggregationTemporality, it MUST not be used. - AGGREGATION_TEMPORALITY_UNSPECIFIED = 0, - - // DELTA is an AggregationTemporality for a metric aggregator which reports - // changes since last report time. Successive metrics contain aggregation of - // values from continuous and non-overlapping intervals. - // - // The values for a DELTA metric are based only on the time interval - // associated with one measurement cycle. There is no dependency on - // previous measurements like is the case for CUMULATIVE metrics. - // - // For example, consider a system measuring the number of requests that - // it receives and reports the sum of these requests every second as a - // DELTA metric: - // - // 1. The system starts receiving at time=t_0. - // 2. A request is received, the system measures 1 request. - // 3. A request is received, the system measures 1 request. - // 4. A request is received, the system measures 1 request. - // 5. The 1 second collection cycle ends. A metric is exported for the - // number of requests received over the interval of time t_0 to - // t_0+1 with a value of 3. - // 6. A request is received, the system measures 1 request. - // 7. A request is received, the system measures 1 request. - // 8. The 1 second collection cycle ends. A metric is exported for the - // number of requests received over the interval of time t_0+1 to - // t_0+2 with a value of 2. - AGGREGATION_TEMPORALITY_DELTA = 1, - - // CUMULATIVE is an AggregationTemporality for a metric aggregator which - // reports changes since a fixed start time. This means that current values - // of a CUMULATIVE metric depend on all previous measurements since the - // start time. Because of this, the sender is required to retain this state - // in some form. If this state is lost or invalidated, the CUMULATIVE metric - // values MUST be reset and a new fixed start time following the last - // reported measurement time sent MUST be used. - // - // For example, consider a system measuring the number of requests that - // it receives and reports the sum of these requests every second as a - // CUMULATIVE metric: - // - // 1. The system starts receiving at time=t_0. - // 2. A request is received, the system measures 1 request. - // 3. A request is received, the system measures 1 request. - // 4. A request is received, the system measures 1 request. - // 5. The 1 second collection cycle ends. A metric is exported for the - // number of requests received over the interval of time t_0 to - // t_0+1 with a value of 3. - // 6. A request is received, the system measures 1 request. - // 7. A request is received, the system measures 1 request. - // 8. The 1 second collection cycle ends. A metric is exported for the - // number of requests received over the interval of time t_0 to - // t_0+2 with a value of 5. - // 9. The system experiences a fault and loses state. - // 10. The system recovers and resumes receiving at time=t_1. - // 11. A request is received, the system measures 1 request. - // 12. The 1 second collection cycle ends. A metric is exported for the - // number of requests received over the interval of time t_1 to - // t_0+1 with a value of 1. - // - // Note: Even though, when reporting changes since last report time, using - // CUMULATIVE is valid, it is not recommended. This may cause problems for - // systems that do not use start_time to determine when the aggregation - // value was reset (e.g. Prometheus). - AGGREGATION_TEMPORALITY_CUMULATIVE = 2, - } - } - - export namespace trace.v1 { - export namespace ConstantSampler { - export enum ConstantDecision { - ALWAYS_OFF = 0, - ALWAYS_ON = 1, - ALWAYS_PARENT = 2, - } - } - export namespace Span { - export interface Event { - timeUnixNano: number; - name: string; - attributes?: opentelemetryProto.common.v1.KeyValue[]; - droppedAttributesCount: number; - } - - export interface Link { - traceId: string; - spanId: string; - traceState?: opentelemetryProto.trace.v1.Span.TraceState; - attributes?: opentelemetryProto.common.v1.KeyValue[]; - droppedAttributesCount: number; - } - - // eslint-disable-next-line @typescript-eslint/no-shadow - export enum SpanKind { - SPAN_KIND_UNSPECIFIED, - SPAN_KIND_INTERNAL, - SPAN_KIND_SERVER, - SPAN_KIND_CLIENT, - SPAN_KIND_PRODUCER, - SPAN_KIND_CONSUMER, - } - - export type TraceState = string | undefined; - } - - export interface ConstantSampler { - decision?: opentelemetryProto.trace.v1.ConstantSampler.ConstantDecision; - } - - export interface InstrumentationLibrarySpans { - instrumentationLibrary?: opentelemetryProto.common.v1.InstrumentationLibrary; - spans: opentelemetryProto.trace.v1.Span[]; - } - - export interface ProbabilitySampler { - samplingProbability?: number | null; - } - - export interface RateLimitingSampler { - qps?: number | null; - } - - export interface ResourceSpans { - resource?: opentelemetryProto.resource.v1.Resource; - instrumentationLibrarySpans: opentelemetryProto.trace.v1.InstrumentationLibrarySpans[]; - } - - export interface Span { - traceId: string; - spanId: string; - traceState: opentelemetryProto.trace.v1.Span.TraceState; - parentSpanId?: string; - name?: string; - kind?: opentelemetryProto.trace.v1.Span.SpanKind; - startTimeUnixNano?: number; - endTimeUnixNano?: number; - attributes?: opentelemetryProto.common.v1.KeyValue[]; - droppedAttributesCount: number; - events?: opentelemetryProto.trace.v1.Span.Event[]; - droppedEventsCount: number; - links?: opentelemetryProto.trace.v1.Span.Link[]; - droppedLinksCount: number; - status?: SpanStatus; - } - - export interface SpanStatus { - /** The status code of this message. */ - code: SpanStatusCode; - /** A developer-facing error message. */ - message?: string; - } - - export interface TraceConfig { - constantSampler?: ConstantSampler | null; - probabilitySampler?: ProbabilitySampler | null; - rateLimitingSampler?: RateLimitingSampler | null; - } - } - export namespace common.v1 { - export interface KeyValue { - key: string; - value: AnyValue; - } - - export type ArrayValue = { - values: AnyValue[]; - }; - - export interface KeyValueList { - values: KeyValue[]; - } - - export type AnyValue = { - stringValue?: string; - boolValue?: boolean; - intValue?: number; - doubleValue?: number; - arrayValue?: ArrayValue; - kvlistValue?: KeyValueList; - }; - - export interface InstrumentationLibrary { - name: string; - version?: string; - } - - export interface StringKeyValue { - key: string; - value: string; - } - - export enum ValueType { - STRING, - INT, - DOUBLE, - BOOL, - } - } -} - -/** - * Mapping between api SpanKind and proto SpanKind - */ -export const OTLP_SPAN_KIND_MAPPING = { - [SpanKind.INTERNAL]: - opentelemetryProto.trace.v1.Span.SpanKind.SPAN_KIND_INTERNAL, - [SpanKind.SERVER]: opentelemetryProto.trace.v1.Span.SpanKind.SPAN_KIND_SERVER, - [SpanKind.CLIENT]: opentelemetryProto.trace.v1.Span.SpanKind.SPAN_KIND_CLIENT, - [SpanKind.PRODUCER]: - opentelemetryProto.trace.v1.Span.SpanKind.SPAN_KIND_PRODUCER, - [SpanKind.CONSUMER]: - opentelemetryProto.trace.v1.Span.SpanKind.SPAN_KIND_CONSUMER, -}; diff --git a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts index 1f57a5e959..ba08f359c0 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts @@ -20,7 +20,6 @@ import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; import * as sinon from 'sinon'; import { OTLPTraceExporter } from '../../src/platform/browser/index'; -import * as otlpTypes from '../../src/types'; import { ensureSpanIsCorrect, @@ -30,6 +29,7 @@ import { mockedReadableSpan, } from '../traceHelper'; import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base'; +import { IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer'; describe('OTLPTraceExporter - web', () => { let collectorTraceExporter: OTLPTraceExporter; @@ -109,16 +109,16 @@ describe('OTLPTraceExporter - web', () => { const body = await blob.text(); const json = JSON.parse( body - ) as otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest; + ) as IExportTraceServiceRequest; const span1 = - json.resourceSpans[0].instrumentationLibrarySpans[0].spans[0]; + json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureSpanIsCorrect(span1); } - const resource = json.resourceSpans[0].resource; + const resource = json.resourceSpans?.[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); if (resource) { ensureWebResourceIsCorrect(resource); @@ -200,16 +200,16 @@ describe('OTLPTraceExporter - web', () => { const body = request.requestBody; const json = JSON.parse( body - ) as otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest; + ) as IExportTraceServiceRequest; const span1 = - json.resourceSpans[0].instrumentationLibrarySpans[0].spans[0]; + json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureSpanIsCorrect(span1); } - const resource = json.resourceSpans[0].resource; + const resource = json.resourceSpans?.[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); if (resource) { ensureWebResourceIsCorrect(resource); diff --git a/experimental/packages/exporter-trace-otlp-http/test/browser/index-webpack.ts b/experimental/packages/exporter-trace-otlp-http/test/browser/index-webpack.ts index 99100a0f6e..ae7d4b5a9d 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/browser/index-webpack.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/browser/index-webpack.ts @@ -16,8 +16,5 @@ const testsContext = require.context('../browser', true, /test$/); testsContext.keys().forEach(testsContext); -const testsContextCommon = require.context('../common', true, /test$/); -testsContextCommon.keys().forEach(testsContextCommon); - const srcContext = require.context('.', true, /src$/); srcContext.keys().forEach(srcContext); diff --git a/experimental/packages/exporter-trace-otlp-http/test/common/transform.test.ts b/experimental/packages/exporter-trace-otlp-http/test/common/transform.test.ts deleted file mode 100644 index 94932201b7..0000000000 --- a/experimental/packages/exporter-trace-otlp-http/test/common/transform.test.ts +++ /dev/null @@ -1,218 +0,0 @@ -/* - * 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 { SpanAttributes, SpanStatusCode } from '@opentelemetry/api'; -import { TimedEvent } from '@opentelemetry/sdk-trace-base'; -import * as assert from 'assert'; -import * as transform from '../../src/transform'; -import { - ensureSpanIsCorrect, - mockedReadableSpan, - mockedResources, - mockedInstrumentationLibraries, - multiResourceTrace, - multiInstrumentationLibraryTrace, -} from '../traceHelper'; -import { Resource } from '@opentelemetry/resources'; - -describe('transform', () => { - describe('toCollectorAttributes', () => { - it('should convert attribute string', () => { - const attributes: SpanAttributes = { - foo: 'bar', - }; - assert.deepStrictEqual(transform.toCollectorAttributes(attributes), [ - { key: 'foo', value: { stringValue: 'bar' } }, - ]); - }); - - it('should convert attribute integer to integer', () => { - const attributes: SpanAttributes = { - foo: 13, - }; - assert.deepStrictEqual(transform.toCollectorAttributes(attributes), [ - { key: 'foo', value: { intValue: 13 } }, - ]); - }); - - it('should convert attribute integer to double', () => { - const attributes: SpanAttributes = { - foo: 2247483647, - }; - assert.deepStrictEqual(transform.toCollectorAttributes(attributes), [ - { key: 'foo', value: { doubleValue: 2247483647 } }, - ]); - }); - - it('should convert attribute boolean', () => { - const attributes: SpanAttributes = { - foo: true, - }; - assert.deepStrictEqual(transform.toCollectorAttributes(attributes), [ - { key: 'foo', value: { boolValue: true } }, - ]); - }); - - it('should convert attribute double', () => { - const attributes: SpanAttributes = { - foo: 1.34, - }; - assert.deepStrictEqual(transform.toCollectorAttributes(attributes), [ - { key: 'foo', value: { doubleValue: 1.34 } }, - ]); - }); - }); - - describe('toCollectorEvents', () => { - it('should convert events to otc events', () => { - const events: TimedEvent[] = [ - { name: 'foo', time: [123, 123], attributes: { a: 'b' } }, - { - name: 'foo2', - time: [321, 321], - attributes: { c: 'd' }, - }, - ]; - assert.deepStrictEqual(transform.toCollectorEvents(events), [ - { - timeUnixNano: 123000000123, - name: 'foo', - attributes: [{ key: 'a', value: { stringValue: 'b' } }], - droppedAttributesCount: 0, - }, - { - timeUnixNano: 321000000321, - name: 'foo2', - attributes: [{ key: 'c', value: { stringValue: 'd' } }], - droppedAttributesCount: 0, - }, - ]); - }); - }); - - describe('toCollectorAnyValue', () => { - it('should use correct type on array', () => { - assert.deepStrictEqual(transform.toCollectorAnyValue(['string', true, 1]), { - arrayValue: { - values: - [ - { stringValue: 'string' }, - { boolValue: true }, - { intValue: 1 } - ] - } - }); - }); - - it('should use correct type on kvlist', () => { - assert.deepStrictEqual(transform.toCollectorAnyValue({ string: 'string', boolean: true, integer: 1 }), { - kvlistValue: { - values: - [ - { key: 'string', value: { stringValue: 'string' } }, - { key: 'boolean', value: { boolValue: true } }, - { key: 'integer', value: { intValue: 1 } } - ] - } - }); - }); - }); - - describe('toCollectorStatus', () => { - it('should set message if status is not undefined', () => { - const result = transform.toCollectorStatus({ - code: SpanStatusCode.OK, - message: 'message' - }); - assert.deepStrictEqual(result.message, 'message'); - }); - }); - - describe('toCollectorSpan', () => { - it('should convert span using hex', () => { - ensureSpanIsCorrect(transform.toCollectorSpan(mockedReadableSpan, true)); - }); - it('should convert span using base64', () => { - ensureSpanIsCorrect(transform.toCollectorSpan(mockedReadableSpan), false); - }); - }); - - describe('toCollectorResource', () => { - it('should convert resource', () => { - const resource = transform.toCollectorResource( - new Resource({ - service: 'ui', - version: 1.0, - success: true, - }) - ); - assert.deepStrictEqual(resource, { - attributes: [ - { - key: 'service', - value: { stringValue: 'ui' }, - }, - { - key: 'version', - value: { intValue: 1 }, - }, - { key: 'success', value: { boolValue: true } }, - ], - droppedAttributesCount: 0, - }); - }); - }); - describe('groupSpansByResourceAndLibrary', () => { - it('should group by resource', () => { - const [resource1, resource2] = mockedResources; - const [instrumentationLibrary] = mockedInstrumentationLibraries; - const [span1, span2, span3] = multiResourceTrace; - - const expected = new Map([ - [resource1, new Map([[instrumentationLibrary, [span1]]])], - [resource2, new Map([[instrumentationLibrary, [span2, span3]]])], - ]); - - const result = transform.groupSpansByResourceAndLibrary( - multiResourceTrace - ); - - assert.deepStrictEqual(result, expected); - }); - - it('should group by instrumentation library', () => { - const [resource] = mockedResources; - const [lib1, lib2] = mockedInstrumentationLibraries; - const [span1, span2, span3] = multiInstrumentationLibraryTrace; - - const expected = new Map([ - [ - resource, - new Map([ - [lib1, [span1, span2]], - [lib2, [span3]], - ]), - ], - ]); - - const result = transform.groupSpansByResourceAndLibrary( - multiInstrumentationLibraryTrace - ); - - assert.deepStrictEqual(result, expected); - }); - }); -}); diff --git a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts index 6ddd63a0fc..f4f18251a6 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts @@ -30,13 +30,13 @@ import * as zlib from 'zlib'; import { OTLPTraceExporter } from '../../src/platform/node'; -import * as otlpTypes from '../../src/types'; import { ensureExportTraceServiceRequestIsSet, ensureSpanIsCorrect, mockedReadableSpan } from '../traceHelper'; import { MockedResponse } from './nodeHelpers'; +import { IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer'; let fakeRequest: PassThrough; @@ -229,9 +229,9 @@ describe('OTLPTraceExporter - node with json over http', () => { const json = JSON.parse( responseBody - ) as otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest; + ) as IExportTraceServiceRequest; const span1 = - json.resourceSpans[0].instrumentationLibrarySpans[0].spans[0]; + json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureSpanIsCorrect(span1); @@ -325,9 +325,9 @@ describe('OTLPTraceExporter - node with json over http', () => { const json = JSON.parse( responseBody - ) as otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest; + ) as IExportTraceServiceRequest; const span1 = - json.resourceSpans[0].instrumentationLibrarySpans[0].spans[0]; + json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureSpanIsCorrect(span1); diff --git a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts index 1ad9e891a7..85ba76b9e6 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts @@ -19,8 +19,15 @@ import { hexToBase64, InstrumentationLibrary, VERSION } from '@opentelemetry/cor import { Resource } from '@opentelemetry/resources'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; -import * as otlpTypes from '../src/types'; -import { opentelemetryProto } from '../src/types'; +import { + ESpanKind, + IEvent, + IExportTraceServiceRequest, + IKeyValue, + ILink, + IResource, + ISpan +} from '@opentelemetry/otlp-transformer'; if (typeof Buffer === 'undefined') { (window as any).Buffer = { @@ -208,7 +215,7 @@ export const multiInstrumentationLibraryTrace: ReadableSpan[] = [ ]; export function ensureEventsAreCorrect( - events: opentelemetryProto.trace.v1.Span.Event[] + events: IEvent[] ) { assert.deepStrictEqual( events, @@ -267,7 +274,7 @@ export function ensureEventsAreCorrect( } export function ensureAttributesAreCorrect( - attributes: opentelemetryProto.common.v1.KeyValue[] + attributes: IKeyValue[] ) { assert.deepStrictEqual( attributes, @@ -284,7 +291,7 @@ export function ensureAttributesAreCorrect( } export function ensureLinksAreCorrect( - attributes: opentelemetryProto.trace.v1.Span.Link[], + attributes: ILink[], useHex?: boolean ) { assert.deepStrictEqual( @@ -309,7 +316,7 @@ export function ensureLinksAreCorrect( } export function ensureSpanIsCorrect( - span: otlpTypes.opentelemetryProto.trace.v1.Span, + span: ISpan, useHex = true ) { if (span.attributes) { @@ -339,7 +346,7 @@ export function ensureSpanIsCorrect( assert.strictEqual(span.name, 'documentFetch', 'name is wrong'); assert.strictEqual( span.kind, - opentelemetryProto.trace.v1.Span.SpanKind.SPAN_KIND_INTERNAL, + ESpanKind.SPAN_KIND_INTERNAL, 'kind is wrong' ); assert.strictEqual( @@ -367,7 +374,7 @@ export function ensureSpanIsCorrect( } export function ensureWebResourceIsCorrect( - resource: otlpTypes.opentelemetryProto.resource.v1.Resource + resource: IResource ) { assert.strictEqual(resource.attributes.length, 7); assert.strictEqual(resource.attributes[0].key, 'service.name'); @@ -388,7 +395,7 @@ export function ensureWebResourceIsCorrect( } export function ensureExportTraceServiceRequestIsSet( - json: otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest + json: IExportTraceServiceRequest ) { const resourceSpans = json.resourceSpans; assert.strictEqual( @@ -397,11 +404,11 @@ export function ensureExportTraceServiceRequestIsSet( 'resourceSpans is missing' ); - const resource = resourceSpans[0].resource; + const resource = resourceSpans?.[0].resource; assert.strictEqual(!!resource, true, 'resource is missing'); const instrumentationLibrarySpans = - resourceSpans[0].instrumentationLibrarySpans; + resourceSpans?.[0].instrumentationLibrarySpans; assert.strictEqual( instrumentationLibrarySpans && instrumentationLibrarySpans.length, 1, @@ -409,14 +416,14 @@ export function ensureExportTraceServiceRequestIsSet( ); const instrumentationLibrary = - instrumentationLibrarySpans[0].instrumentationLibrary; + instrumentationLibrarySpans?.[0].instrumentationLibrary; assert.strictEqual( !!instrumentationLibrary, true, 'instrumentationLibrary is missing' ); - const spans = instrumentationLibrarySpans[0].spans; + const spans = instrumentationLibrarySpans?.[0].spans; assert.strictEqual(spans && spans.length, 1, 'spans are missing'); } diff --git a/experimental/packages/exporter-trace-otlp-http/tsconfig.json b/experimental/packages/exporter-trace-otlp-http/tsconfig.json index 82df98e67b..087c804079 100644 --- a/experimental/packages/exporter-trace-otlp-http/tsconfig.json +++ b/experimental/packages/exporter-trace-otlp-http/tsconfig.json @@ -20,6 +20,9 @@ }, { "path": "../otlp-exporter-base" + }, + { + "path": "../otlp-transformer" } ] } diff --git a/experimental/packages/exporter-trace-otlp-proto/package.json b/experimental/packages/exporter-trace-otlp-proto/package.json index 7367f0efd5..89f8155ba6 100644 --- a/experimental/packages/exporter-trace-otlp-proto/package.json +++ b/experimental/packages/exporter-trace-otlp-proto/package.json @@ -73,6 +73,7 @@ "@opentelemetry/sdk-trace-base": "1.2.0", "@opentelemetry/otlp-exporter-base": "0.28.0", "@opentelemetry/otlp-proto-exporter-base": "0.28.0", - "protobufjs": "^6.9.0" + "protobufjs": "^6.9.0", + "@opentelemetry/otlp-transformer": "0.28.0" } } diff --git a/experimental/packages/exporter-trace-otlp-proto/src/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-proto/src/OTLPTraceExporter.ts index e2e98de304..403083e506 100644 --- a/experimental/packages/exporter-trace-otlp-proto/src/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-proto/src/OTLPTraceExporter.ts @@ -15,13 +15,10 @@ */ import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base'; -import { - otlpTypes, - toOTLPExportTraceServiceRequest -} from '@opentelemetry/exporter-trace-otlp-http'; import { getEnv, baggageUtils } from '@opentelemetry/core'; import { appendResourcePathToUrlIfNotPresent, OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base'; import { OTLPProtoExporterNodeBase, ServiceClientType } from '@opentelemetry/otlp-proto-exporter-base'; +import { createExportTraceServiceRequest, IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer'; const DEFAULT_COLLECTOR_RESOURCE_PATH = '/v1/traces'; const DEFAULT_COLLECTOR_URL=`http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_PATH}`; @@ -32,7 +29,7 @@ const DEFAULT_COLLECTOR_URL=`http://localhost:4318${DEFAULT_COLLECTOR_RESOURCE_P export class OTLPTraceExporter extends OTLPProtoExporterNodeBase< ReadableSpan, - otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest + IExportTraceServiceRequest > implements SpanExporter { constructor(config: OTLPExporterNodeConfigBase = {}) { @@ -47,8 +44,8 @@ export class OTLPTraceExporter convert( spans: ReadableSpan[] - ): otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest { - return toOTLPExportTraceServiceRequest(spans, this); + ): IExportTraceServiceRequest { + return createExportTraceServiceRequest(spans); } getDefaultUrl(config: OTLPExporterNodeConfigBase) { diff --git a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts index 9554fea1d8..1cd8121c6e 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts @@ -16,9 +16,6 @@ import { diag } from '@opentelemetry/api'; import { ExportResultCode } from '@opentelemetry/core'; -import { - otlpTypes -} from '@opentelemetry/exporter-trace-otlp-http'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; import * as http from 'http'; @@ -34,6 +31,7 @@ import { } from './traceHelper'; import { CompressionAlgorithm, OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base'; import { getExportRequestProto } from '@opentelemetry/otlp-proto-exporter-base'; +import { IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer'; const fakeRequest = { end: function () { }, @@ -155,9 +153,9 @@ describe('OTLPTraceExporter - node with proto over http', () => { fakeRequest.on('end', () => { const ExportTraceServiceRequestProto = getExportRequestProto(); const data = ExportTraceServiceRequestProto?.decode(buff); - const json = data?.toJSON() as otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest; + const json = data?.toJSON() as IExportTraceServiceRequest; const span1 = - json.resourceSpans[0].instrumentationLibrarySpans[0].spans[0]; + json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureProtoSpanIsCorrect(span1); @@ -242,9 +240,9 @@ describe('OTLPTraceExporter - node with proto over http', () => { const unzippedBuff = zlib.gunzipSync(buff); const ExportTraceServiceRequestProto = getExportRequestProto(); const data = ExportTraceServiceRequestProto?.decode(unzippedBuff); - const json = data?.toJSON() as otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest; + const json = data?.toJSON() as IExportTraceServiceRequest; const span1 = - json.resourceSpans[0].instrumentationLibrarySpans[0].spans[0]; + json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureProtoSpanIsCorrect(span1); diff --git a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts index 86ff5dc32c..9c8e7216ea 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts @@ -16,11 +16,11 @@ import { SpanStatusCode, TraceFlags } from '@opentelemetry/api'; import { hexToBase64 } from '@opentelemetry/core'; -import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http'; import { Resource } from '@opentelemetry/resources'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; import { Stream } from 'stream'; +import { IEvent, IExportTraceServiceRequest, IKeyValue, ILink, ISpan } from '@opentelemetry/otlp-transformer'; const traceIdHex = '1f1008dc8e270e85c40a0d7c3939b278'; const spanIdHex = '5e107261f64fa53e'; @@ -84,7 +84,7 @@ export const mockedReadableSpan: ReadableSpan = { }; export function ensureProtoEventsAreCorrect( - events: otlpTypes.opentelemetryProto.trace.v1.Span.Event[] + events: IEvent[] ) { assert.deepStrictEqual( events, @@ -135,7 +135,7 @@ export function ensureProtoEventsAreCorrect( } export function ensureProtoAttributesAreCorrect( - attributes: otlpTypes.opentelemetryProto.common.v1.KeyValue[] + attributes: IKeyValue[] ) { assert.deepStrictEqual( attributes, @@ -152,7 +152,7 @@ export function ensureProtoAttributesAreCorrect( } export function ensureProtoLinksAreCorrect( - attributes: otlpTypes.opentelemetryProto.trace.v1.Span.Link[] + attributes: ILink[] ) { assert.deepStrictEqual( attributes, @@ -176,7 +176,7 @@ export function ensureProtoLinksAreCorrect( } export function ensureProtoSpanIsCorrect( - span: otlpTypes.opentelemetryProto.trace.v1.Span + span: ISpan ) { if (span.attributes) { ensureProtoAttributesAreCorrect(span.attributes); @@ -229,7 +229,7 @@ export function ensureProtoSpanIsCorrect( } export function ensureExportTraceServiceRequestIsSet( - json: otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest + json: IExportTraceServiceRequest ) { const resourceSpans = json.resourceSpans; assert.strictEqual( @@ -238,11 +238,11 @@ export function ensureExportTraceServiceRequestIsSet( 'resourceSpans is missing' ); - const resource = resourceSpans[0].resource; + const resource = resourceSpans?.[0].resource; assert.strictEqual(!!resource, true, 'resource is missing'); const instrumentationLibrarySpans = - resourceSpans[0].instrumentationLibrarySpans; + resourceSpans?.[0].instrumentationLibrarySpans; assert.strictEqual( instrumentationLibrarySpans && instrumentationLibrarySpans.length, 1, @@ -250,14 +250,14 @@ export function ensureExportTraceServiceRequestIsSet( ); const instrumentationLibrary = - instrumentationLibrarySpans[0].instrumentationLibrary; + instrumentationLibrarySpans?.[0].instrumentationLibrary; assert.strictEqual( !!instrumentationLibrary, true, 'instrumentationLibrary is missing' ); - const spans = instrumentationLibrarySpans[0].spans; + const spans = instrumentationLibrarySpans?.[0].spans; assert.strictEqual(spans && spans.length, 1, 'spans are missing'); } diff --git a/experimental/packages/exporter-trace-otlp-proto/tsconfig.json b/experimental/packages/exporter-trace-otlp-proto/tsconfig.json index 69212c4878..6ba9e30e47 100644 --- a/experimental/packages/exporter-trace-otlp-proto/tsconfig.json +++ b/experimental/packages/exporter-trace-otlp-proto/tsconfig.json @@ -26,6 +26,9 @@ }, { "path": "../otlp-proto-exporter-base" + }, + { + "path": "../otlp-transformer" } ] } diff --git a/experimental/packages/otlp-transformer/src/trace/index.ts b/experimental/packages/otlp-transformer/src/trace/index.ts index 1329fe7f03..95495dd67a 100644 --- a/experimental/packages/otlp-transformer/src/trace/index.ts +++ b/experimental/packages/otlp-transformer/src/trace/index.ts @@ -20,7 +20,7 @@ import { toAttributes } from '../common/internal'; import { sdkSpanToOtlpSpan } from './internal'; import { IExportTraceServiceRequest } from './types'; -export function createExportTraceServiceRequest(spans: ReadableSpan[]): IExportTraceServiceRequest | null { +export function createExportTraceServiceRequest(spans: ReadableSpan[], useHex?: boolean): IExportTraceServiceRequest { return { resourceSpans: spanRecordsToResourceSpans(spans).map(({ resource, resourceSpans, resourceSchemaUrl }) => ({ resource: { @@ -29,7 +29,7 @@ export function createExportTraceServiceRequest(spans: ReadableSpan[]): IExportT }, instrumentationLibrarySpans: resourceSpans.map(({ instrumentationLibrary, instrumentationLibrarySpans, librarySchemaUrl }) => ({ instrumentationLibrary, - spans: instrumentationLibrarySpans.map(sdkSpanToOtlpSpan), + spans: instrumentationLibrarySpans.map(span => sdkSpanToOtlpSpan(span, useHex)), schemaUrl: librarySchemaUrl, })), schemaUrl: resourceSchemaUrl, diff --git a/experimental/packages/otlp-transformer/src/trace/internal.ts b/experimental/packages/otlp-transformer/src/trace/internal.ts index 02fe74af96..87a8182c3f 100644 --- a/experimental/packages/otlp-transformer/src/trace/internal.ts +++ b/experimental/packages/otlp-transformer/src/trace/internal.ts @@ -18,16 +18,19 @@ import { hrTimeToNanoseconds } from '@opentelemetry/core'; import type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base'; import { toAttributes } from '../common/internal'; import { EStatusCode, IEvent, ILink, ISpan } from './types'; +import * as core from '@opentelemetry/core'; export function sdkSpanToOtlpSpan( span: ReadableSpan, + useHex?: boolean ): ISpan { const ctx = span.spanContext(); const status = span.status; + const parentSpanId = useHex? span.parentSpanId : span.parentSpanId != null? core.hexToBase64(span.parentSpanId): undefined; return { - traceId: ctx.traceId, - spanId: ctx.spanId, - parentSpanId: span.parentSpanId, + traceId: useHex? ctx.traceId : core.hexToBase64(ctx.traceId), + spanId: useHex? ctx.spanId : core.hexToBase64(ctx.spanId), + parentSpanId: parentSpanId, name: span.name, // Span kind is offset by 1 because the API does not define a value for unset kind: span.kind == null ? 0 : span.kind + 1, @@ -42,16 +45,16 @@ export function sdkSpanToOtlpSpan( code: status.code as unknown as EStatusCode, message: status.message, }, - links: span.links.map(toOtlpLink), + links: span.links.map(link => toOtlpLink(link, useHex)), droppedLinksCount: 0, }; } -export function toOtlpLink(link: Link): ILink { +export function toOtlpLink(link: Link, useHex?: boolean): ILink { return { attributes: link.attributes ? toAttributes(link.attributes) : [], - spanId: link.context.spanId, - traceId: link.context.traceId, + spanId: useHex? link.context.spanId : core.hexToBase64(link.context.spanId), + traceId: useHex? link.context.traceId : core.hexToBase64(link.context.traceId), droppedAttributesCount: 0, }; } diff --git a/experimental/packages/otlp-transformer/test/trace.test.ts b/experimental/packages/otlp-transformer/test/trace.test.ts index 3bd7be6b4c..830674ee38 100644 --- a/experimental/packages/otlp-transformer/test/trace.test.ts +++ b/experimental/packages/otlp-transformer/test/trace.test.ts @@ -161,24 +161,24 @@ describe('Trace', () => { }); it('returns null on an empty list', () => { - assert.deepStrictEqual(createExportTraceServiceRequest([]), { resourceSpans: [] }); + assert.deepStrictEqual(createExportTraceServiceRequest([], true), { resourceSpans: [] }); }); it('serializes a span', () => { - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.deepStrictEqual(exportRequest, expectedSpanJson); }); it('serializes a span', () => { - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.deepStrictEqual(exportRequest, expectedSpanJson); }); it('serializes a span without a parent', () => { (span as any).parentSpanId = undefined; - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].parentSpanId, undefined); @@ -188,7 +188,7 @@ describe('Trace', () => { it('error', () => { span.status.code = SpanStatusCode.ERROR; span.status.message = 'error message'; - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); const spanStatus = exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].status; assert.strictEqual(spanStatus?.code, EStatusCode.STATUS_CODE_ERROR); @@ -197,7 +197,7 @@ describe('Trace', () => { it('unset', () => { span.status.code = SpanStatusCode.UNSET; - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].status.code, EStatusCode.STATUS_CODE_UNSET); }); @@ -206,31 +206,31 @@ describe('Trace', () => { describe('span kind', () => { it('consumer', () => { (span as any).kind = SpanKind.CONSUMER; - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_CONSUMER); }); it('internal', () => { (span as any).kind = SpanKind.INTERNAL; - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_INTERNAL); }); it('producer', () => { (span as any).kind = SpanKind.PRODUCER; - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_PRODUCER); }); it('server', () => { (span as any).kind = SpanKind.SERVER; - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_SERVER); }); it('unspecified', () => { (span as any).kind = undefined; - const exportRequest = createExportTraceServiceRequest([span]); + const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_UNSPECIFIED); }); From c801f4208b7a8411f41390611b1e2645749f12df Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 27 Apr 2022 09:29:46 +0200 Subject: [PATCH 05/29] fix(changelog): update changelog. --- experimental/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 4eed8e2217..1f5c9f30f0 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(exporters): update proto version and use otlp-transformer #2929 @pichlermarc + ### :bug: (Bug Fix) ### :books: (Refine Doc) From 258e503b5234b6f033a2da08c31210900f39bd14 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 27 Apr 2022 10:05:32 +0200 Subject: [PATCH 06/29] feat(exporter-trace-otlp-http): use hex representation for traceId and spanId in browser exporter. --- .../src/platform/browser/OTLPTraceExporter.ts | 2 +- .../browser/CollectorTraceExporter.test.ts | 61 ++++++++++--------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts index 966d774341..87954e9529 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts @@ -44,7 +44,7 @@ export class OTLPTraceExporter convert( spans: ReadableSpan[] ): IExportTraceServiceRequest { - return createExportTraceServiceRequest(spans); + return createExportTraceServiceRequest(spans, true); } getDefaultUrl(config: OTLPExporterConfigBase): string { diff --git a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts index ba08f359c0..847d722db4 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts @@ -99,39 +99,42 @@ describe('OTLPTraceExporter - web', () => { }); it('should successfully send the spans using sendBeacon', done => { - collectorTraceExporter.export(spans, () => { + collectorTraceExporter.export(spans, result => { }); setTimeout(async () => { - const args = stubBeacon.args[0]; - const url = args[0]; - const blob: Blob = args[1]; - const body = await blob.text(); - const json = JSON.parse( - body - ) as IExportTraceServiceRequest; - const span1 = - json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; - - assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureSpanIsCorrect(span1); - } - - const resource = json.resourceSpans?.[0].resource; - assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); - if (resource) { - ensureWebResourceIsCorrect(resource); + try { + const args = stubBeacon.args[0]; + const url = args[0]; + const blob: Blob = args[1]; + const body = await blob.text(); + const json = JSON.parse( + body + ) as IExportTraceServiceRequest; + const span1 = + json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; + + assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); + if (span1) { + ensureSpanIsCorrect(span1); + } + + const resource = json.resourceSpans?.[0].resource; + assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); + if (resource) { + ensureWebResourceIsCorrect(resource); + } + + assert.strictEqual(url, 'http://foo.bar.com'); + assert.strictEqual(stubBeacon.callCount, 1); + + assert.strictEqual(stubOpen.callCount, 0); + + ensureExportTraceServiceRequestIsSet(json); + done(); + } catch (err) { + done(err); } - - assert.strictEqual(url, 'http://foo.bar.com'); - assert.strictEqual(stubBeacon.callCount, 1); - - assert.strictEqual(stubOpen.callCount, 0); - - ensureExportTraceServiceRequestIsSet(json); - - done(); }); }); From aed89cbb43580391c7b4b328c3a6a31aad5d8d6b Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 27 Apr 2022 11:01:38 +0200 Subject: [PATCH 07/29] feat(otlp-transformer): update tests for base64 trace and span id representation. --- .../otlp-transformer/test/trace.test.ts | 187 ++++++++++-------- 1 file changed, 100 insertions(+), 87 deletions(-) diff --git a/experimental/packages/otlp-transformer/test/trace.test.ts b/experimental/packages/otlp-transformer/test/trace.test.ts index 830674ee38..cf438586d1 100644 --- a/experimental/packages/otlp-transformer/test/trace.test.ts +++ b/experimental/packages/otlp-transformer/test/trace.test.ts @@ -14,17 +14,103 @@ * limitations under the License. */ import { SpanKind, SpanStatusCode } from '@opentelemetry/api'; -import { TraceState } from '@opentelemetry/core'; +import { TraceState, hexToBase64 } from '@opentelemetry/core'; import { Resource } from '@opentelemetry/resources'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; import { createExportTraceServiceRequest, ESpanKind, EStatusCode } from '../src'; +function createExpectedSpanJson(useHex: boolean){ + const traceId = useHex? '00000000000000000000000000000001' : hexToBase64('00000000000000000000000000000001'); + const spanId = useHex? '0000000000000002' : hexToBase64('0000000000000002'); + const parentSpanId = useHex? '0000000000000001' : hexToBase64('0000000000000001'); + const linkSpanId = useHex? '0000000000000003' : hexToBase64('0000000000000003'); + const linkTraceId = useHex? '00000000000000000000000000000002' : hexToBase64('00000000000000000000000000000002'); + + return { + resourceSpans: [ + { + resource: { + attributes: [ + { + key: 'resource-attribute', + value: { stringValue: 'resource attribute value' }, + }, + ], + droppedAttributesCount: 0, + }, + schemaUrl: undefined, + instrumentationLibrarySpans: [ + { + instrumentationLibrary: { name: 'myLib', version: '0.1.0' }, + spans: [ + { + traceId: traceId, + spanId: spanId, + parentSpanId: parentSpanId, + name: 'span-name', + kind: ESpanKind.SPAN_KIND_CLIENT, + links: [ + { + droppedAttributesCount: 0, + spanId: linkSpanId, + traceId: linkTraceId, + attributes: [ + { + key: 'link-attribute', + value: { + stringValue: 'string value' + } + } + ] + } + ], + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision + startTimeUnixNano: 1640715557342725388, + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision + endTimeUnixNano: 1640715558642725388, + events: [ + { + droppedAttributesCount: 0, + attributes: [ + { + key: 'event-attribute', + value: { + stringValue: 'some string value' + } + } + ], + name: 'some event', + timeUnixNano: 1640715558542725400 + } + ], + attributes: [ + { + key: 'string-attribute', + value: { stringValue: 'some attribute value' }, + }, + ], + droppedAttributesCount: 0, + droppedEventsCount: 0, + droppedLinksCount: 0, + status: { + code: EStatusCode.STATUS_CODE_OK, + message: undefined, + }, + }, + ], + schemaUrl: 'http://url.to.schema', + }, + ], + }, + ], + }; +} + describe('Trace', () => { describe('createExportTraceServiceRequest', () => { let resource: Resource; let span: ReadableSpan; - let expectedSpanJson: any; beforeEach(() => { resource = new Resource({ @@ -79,109 +165,36 @@ describe('Trace', () => { code: SpanStatusCode.OK, }, }; - - expectedSpanJson = { - resourceSpans: [ - { - resource: { - attributes: [ - { - key: 'resource-attribute', - value: { stringValue: 'resource attribute value' }, - }, - ], - droppedAttributesCount: 0, - }, - schemaUrl: undefined, - instrumentationLibrarySpans: [ - { - instrumentationLibrary: { name: 'myLib', version: '0.1.0' }, - spans: [ - { - traceId: '00000000000000000000000000000001', - spanId: '0000000000000002', - parentSpanId: '0000000000000001', - name: 'span-name', - kind: ESpanKind.SPAN_KIND_CLIENT, - links: [ - { - droppedAttributesCount: 0, - spanId: '0000000000000003', - traceId: '00000000000000000000000000000002', - attributes: [ - { - key: 'link-attribute', - value: { - stringValue: 'string value' - } - } - ] - } - ], - // eslint-disable-next-line @typescript-eslint/no-loss-of-precision - startTimeUnixNano: 1640715557342725388, - // eslint-disable-next-line @typescript-eslint/no-loss-of-precision - endTimeUnixNano: 1640715558642725388, - events: [ - { - droppedAttributesCount: 0, - attributes: [ - { - key: 'event-attribute', - value: { - stringValue: 'some string value' - } - } - ], - name: 'some event', - timeUnixNano: 1640715558542725400 - } - ], - attributes: [ - { - key: 'string-attribute', - value: { stringValue: 'some attribute value' }, - }, - ], - droppedAttributesCount: 0, - droppedEventsCount: 0, - droppedLinksCount: 0, - status: { - code: EStatusCode.STATUS_CODE_OK, - message: undefined, - }, - }, - ], - schemaUrl: 'http://url.to.schema', - }, - ], - }, - ], - }; }); it('returns null on an empty list', () => { assert.deepStrictEqual(createExportTraceServiceRequest([], true), { resourceSpans: [] }); }); - it('serializes a span', () => { + it('serializes a span with useHex = true', () => { const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); - assert.deepStrictEqual(exportRequest, expectedSpanJson); + assert.deepStrictEqual(exportRequest, createExpectedSpanJson(true)); }); - it('serializes a span', () => { - const exportRequest = createExportTraceServiceRequest([span], true); + it('serializes a span with useHex = false', () => { + const exportRequest = createExportTraceServiceRequest([span], false); assert.ok(exportRequest); - assert.deepStrictEqual(exportRequest, expectedSpanJson); + assert.deepStrictEqual(exportRequest, createExpectedSpanJson(false)); }); - it('serializes a span without a parent', () => { + it('serializes a span without a parent with useHex = true', () => { (span as any).parentSpanId = undefined; const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].parentSpanId, undefined); + }); + it('serializes a span without a parent with useHex = false', () => { + (span as any).parentSpanId = undefined; + const exportRequest = createExportTraceServiceRequest([span], false); + assert.ok(exportRequest); + assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].parentSpanId, undefined); }); describe('status code', () => { From 78720de1225f75af36373370eba16d532273f011 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 27 Apr 2022 13:31:46 +0200 Subject: [PATCH 08/29] feat(protos): update protos to 0.16 --- .../test/OTLPTraceExporter.test.ts | 8 +++---- .../browser/CollectorTraceExporter.test.ts | 4 ++-- .../test/node/CollectorTraceExporter.test.ts | 4 ++-- .../test/traceHelper.ts | 8 +++---- .../test/OTLPTraceExporter.test.ts | 4 ++-- .../test/traceHelper.ts | 12 +++++----- .../test/OTLPMetricExporter.test.ts | 6 ++--- .../test/metricsHelper.ts | 1 + .../browser/CollectorMetricExporter.test.ts | 12 +++++----- .../test/metricsHelper.ts | 18 +++++++-------- .../test/node/CollectorMetricExporter.test.ts | 6 ++--- .../test/OTLPMetricExporter.test.ts | 6 ++--- .../test/metricsHelper.ts | 18 +++++++-------- .../packages/otlp-grpc-exporter-base/protos | 2 +- .../otlp-grpc-exporter-base/submodule.md | 2 +- .../packages/otlp-proto-exporter-base/protos | 2 +- .../otlp-proto-exporter-base/submodule.md | 2 +- .../otlp-transformer/src/common/types.ts | 8 +++---- .../otlp-transformer/src/metrics/index.ts | 4 ++-- .../otlp-transformer/src/metrics/types.ts | 18 +++++++-------- .../otlp-transformer/src/trace/index.ts | 4 ++-- .../otlp-transformer/src/trace/types.ts | 10 ++++----- .../otlp-transformer/test/metrics.test.ts | 16 +++++++------- .../otlp-transformer/test/trace.test.ts | 22 +++++++++---------- .../test/common/Tracer.test.ts | 2 +- 25 files changed, 99 insertions(+), 100 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index dfb01074ba..d3b62a373c 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -178,8 +178,8 @@ const testCollectorExporter = (params: TestParams) => ); let spans; let resource; - if (exportedData && exportedData.instrumentationLibrarySpans[0].spans) { - spans = exportedData.instrumentationLibrarySpans[0].spans; + if (exportedData && exportedData.scopeSpans[0].spans) { + spans = exportedData.scopeSpans[0].spans; resource = exportedData.resource; ensureExportedSpanIsCorrect(spans[0]); @@ -229,8 +229,8 @@ const testCollectorExporter = (params: TestParams) => ); let spans; let resource; - if (exportedData && exportedData.instrumentationLibrarySpans[0].spans) { - spans = exportedData.instrumentationLibrarySpans[0].spans; + if (exportedData && exportedData.scopeSpans[0].spans) { + spans = exportedData.scopeSpans[0].spans; resource = exportedData.resource; ensureExportedSpanIsCorrect(spans[0]); diff --git a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts index 847d722db4..27692509ee 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts @@ -112,7 +112,7 @@ describe('OTLPTraceExporter - web', () => { body ) as IExportTraceServiceRequest; const span1 = - json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; + json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { @@ -205,7 +205,7 @@ describe('OTLPTraceExporter - web', () => { body ) as IExportTraceServiceRequest; const span1 = - json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; + json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { diff --git a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts index f4f18251a6..07d40484bd 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts @@ -231,7 +231,7 @@ describe('OTLPTraceExporter - node with json over http', () => { responseBody ) as IExportTraceServiceRequest; const span1 = - json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; + json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureSpanIsCorrect(span1); @@ -327,7 +327,7 @@ describe('OTLPTraceExporter - node with json over http', () => { responseBody ) as IExportTraceServiceRequest; const span1 = - json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; + json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureSpanIsCorrect(span1); diff --git a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts index 85ba76b9e6..cbe1b0e882 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts @@ -408,19 +408,19 @@ export function ensureExportTraceServiceRequestIsSet( assert.strictEqual(!!resource, true, 'resource is missing'); const instrumentationLibrarySpans = - resourceSpans?.[0].instrumentationLibrarySpans; + resourceSpans?.[0].scopeSpans; assert.strictEqual( instrumentationLibrarySpans && instrumentationLibrarySpans.length, 1, - 'instrumentationLibrarySpans is missing' + 'scopeSpans is missing' ); const instrumentationLibrary = - instrumentationLibrarySpans?.[0].instrumentationLibrary; + instrumentationLibrarySpans?.[0].scope; assert.strictEqual( !!instrumentationLibrary, true, - 'instrumentationLibrary is missing' + 'scope is missing' ); const spans = instrumentationLibrarySpans?.[0].spans; diff --git a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts index 1cd8121c6e..30878f5a98 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts @@ -155,7 +155,7 @@ describe('OTLPTraceExporter - node with proto over http', () => { const data = ExportTraceServiceRequestProto?.decode(buff); const json = data?.toJSON() as IExportTraceServiceRequest; const span1 = - json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; + json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureProtoSpanIsCorrect(span1); @@ -242,7 +242,7 @@ describe('OTLPTraceExporter - node with proto over http', () => { const data = ExportTraceServiceRequestProto?.decode(unzippedBuff); const json = data?.toJSON() as IExportTraceServiceRequest; const span1 = - json.resourceSpans?.[0].instrumentationLibrarySpans?.[0].spans?.[0]; + json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureProtoSpanIsCorrect(span1); diff --git a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts index 9c8e7216ea..ac8a04cc1f 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts @@ -241,20 +241,18 @@ export function ensureExportTraceServiceRequestIsSet( const resource = resourceSpans?.[0].resource; assert.strictEqual(!!resource, true, 'resource is missing'); - const instrumentationLibrarySpans = - resourceSpans?.[0].instrumentationLibrarySpans; + const instrumentationLibrarySpans = resourceSpans?.[0].scopeSpans; assert.strictEqual( instrumentationLibrarySpans && instrumentationLibrarySpans.length, 1, - 'instrumentationLibrarySpans is missing' + 'scopeSpans is missing' ); - const instrumentationLibrary = - instrumentationLibrarySpans?.[0].instrumentationLibrary; + const scope = instrumentationLibrarySpans?.[0].scope; assert.strictEqual( - !!instrumentationLibrary, + !!scope, true, - 'instrumentationLibrary is missing' + 'scope is missing' ); const spans = instrumentationLibrarySpans?.[0].spans; diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts index 5e833eff4a..c259dfe880 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts @@ -213,11 +213,11 @@ const testOTLPMetricExporter = (params: TestParams) => if (exportedData) { resource = exportedData[0].resource; const counter = - exportedData[0].instrumentationLibraryMetrics[0].metrics[0]; + exportedData[0].scopeMetrics[0].metrics[0]; const observableGauge = - exportedData[0].instrumentationLibraryMetrics[0].metrics[1]; + exportedData[0].scopeMetrics[0].metrics[1]; const histogram = - exportedData[0].instrumentationLibraryMetrics[0].metrics[2]; + exportedData[0].scopeMetrics[0].metrics[2]; ensureExportedCounterIsCorrect( counter, counter.sum?.dataPoints[0].timeUnixNano, diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts index c3161a8bdf..7c73fc0105 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts @@ -184,6 +184,7 @@ export function ensureExportedHistogramIsCorrect( attributes: [], exemplars: [], flags: 0, + _sum: 'sum', sum: 21, count: '2', startTimeUnixNano: String(startTime), diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts index caa9a70cce..ad1bea4fd7 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts @@ -110,11 +110,11 @@ describe('OTLPMetricExporter - web', () => { body ) as IExportMetricsServiceRequest; const metric1 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[0]; + json.resourceMetrics[0].scopeMetrics[0].metrics[0]; const metric2 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[1]; + json.resourceMetrics[0].scopeMetrics[0].metrics[1]; const metric3 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[2]; + json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist"); if (metric1) { @@ -228,11 +228,11 @@ describe('OTLPMetricExporter - web', () => { body ) as IExportMetricsServiceRequest; const metric1 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[0]; + json.resourceMetrics[0].scopeMetrics[0].metrics[0]; const metric2 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[1]; + json.resourceMetrics[0].scopeMetrics[0].metrics[1]; const metric3 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[2]; + json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist"); if (metric1) { ensureCounterIsCorrect( diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts index 7bd302cce7..68c41e6be1 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts @@ -380,23 +380,23 @@ export function ensureExportMetricsServiceRequestIsSet( const resource = resourceMetrics[0].resource; assert.strictEqual(!!resource, true, 'resource is missing'); - const instrumentationLibraryMetrics = - resourceMetrics[0].instrumentationLibraryMetrics; + const scopeMetrics = + resourceMetrics[0].scopeMetrics; assert.strictEqual( - instrumentationLibraryMetrics && instrumentationLibraryMetrics.length, + scopeMetrics && scopeMetrics.length, 1, - 'instrumentationLibraryMetrics is missing' + 'scopeMetrics is missing' ); - const instrumentationLibrary = - instrumentationLibraryMetrics[0].instrumentationLibrary; + const scope = + scopeMetrics[0].scope; assert.strictEqual( - !!instrumentationLibrary, + !!scope, true, - 'instrumentationLibrary is missing' + 'scope is missing' ); - const metrics = resourceMetrics[0].instrumentationLibraryMetrics[0].metrics; + const metrics = resourceMetrics[0].scopeMetrics[0].metrics; assert.strictEqual(metrics.length, 3, 'Metrics are missing'); } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts index 9f1ba8fe03..0b0a13fe2d 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts @@ -235,11 +235,11 @@ describe('OTLPMetricExporter - node with json over http', () => { ); const json = myJson as IExportMetricsServiceRequest; const metric1 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[0]; + json.resourceMetrics[0].scopeMetrics[0].metrics[0]; const metric2 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[1]; + json.resourceMetrics[0].scopeMetrics[0].metrics[1]; const metric3 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[2]; + json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "counter doesn't exist"); ensureCounterIsCorrect( diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/OTLPMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/OTLPMetricExporter.test.ts index 4aee2faf21..d96431bec3 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/OTLPMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/OTLPMetricExporter.test.ts @@ -180,11 +180,11 @@ describe('OTLPMetricExporter - node with proto over http', () => { const json = data?.toJSON() as IExportMetricsServiceRequest; const metric1 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[0]; + json.resourceMetrics[0].scopeMetrics[0].metrics[0]; const metric2 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[1]; + json.resourceMetrics[0].scopeMetrics[0].metrics[1]; const metric3 = - json.resourceMetrics[0].instrumentationLibraryMetrics[0].metrics[2]; + json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "counter doesn't exist"); ensureExportedCounterIsCorrect( diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts index 2b6a4d3a8a..c9e5d16851 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts @@ -203,23 +203,23 @@ export function ensureExportMetricsServiceRequestIsSet( const resource = resourceMetrics[0].resource; assert.strictEqual(!!resource, true, 'resource is missing'); - const instrumentationLibraryMetrics = - resourceMetrics[0].instrumentationLibraryMetrics; + const scopeMetrics = + resourceMetrics[0].scopeMetrics; assert.strictEqual( - instrumentationLibraryMetrics && instrumentationLibraryMetrics.length, + scopeMetrics && scopeMetrics.length, 1, - 'instrumentationLibraryMetrics is missing' + 'scopeMetrics is missing' ); - const instrumentationLibrary = - instrumentationLibraryMetrics[0].instrumentationLibrary; + const scope = + scopeMetrics[0].scope; assert.strictEqual( - !!instrumentationLibrary, + !!scope, true, - 'instrumentationLibrary is missing' + 'scope is missing' ); - const metrics = resourceMetrics[0].instrumentationLibraryMetrics[0].metrics; + const metrics = resourceMetrics[0].scopeMetrics[0].metrics; assert.strictEqual(metrics.length, 3, 'Metrics are missing'); } diff --git a/experimental/packages/otlp-grpc-exporter-base/protos b/experimental/packages/otlp-grpc-exporter-base/protos index cc4ed55c08..9657f2f707 160000 --- a/experimental/packages/otlp-grpc-exporter-base/protos +++ b/experimental/packages/otlp-grpc-exporter-base/protos @@ -1 +1 @@ -Subproject commit cc4ed55c082cb75e084d40b4ddf3805eda099f97 +Subproject commit 9657f2f70739433d27412fe99d81479676814e26 diff --git a/experimental/packages/otlp-grpc-exporter-base/submodule.md b/experimental/packages/otlp-grpc-exporter-base/submodule.md index 68f912dcd9..fd5d1a7a47 100644 --- a/experimental/packages/otlp-grpc-exporter-base/submodule.md +++ b/experimental/packages/otlp-grpc-exporter-base/submodule.md @@ -40,7 +40,7 @@ the latest sha when this guide was written is `59c488bfb8fb6d0458ad6425758b70259 8. Now thing which is very important. You have to commit this to apply these changes ```shell script - git commit -am "chore: updating submodule for opentelemetry-proto" + git commit -am "chore: updating submodule for otlp-grpc-exporter-base" ``` 9. If you look now at git log you will notice that the folder `protos` has been changed and it will show what was the previous sha and what is current one. diff --git a/experimental/packages/otlp-proto-exporter-base/protos b/experimental/packages/otlp-proto-exporter-base/protos index cc4ed55c08..9657f2f707 160000 --- a/experimental/packages/otlp-proto-exporter-base/protos +++ b/experimental/packages/otlp-proto-exporter-base/protos @@ -1 +1 @@ -Subproject commit cc4ed55c082cb75e084d40b4ddf3805eda099f97 +Subproject commit 9657f2f70739433d27412fe99d81479676814e26 diff --git a/experimental/packages/otlp-proto-exporter-base/submodule.md b/experimental/packages/otlp-proto-exporter-base/submodule.md index 32c3e291c8..2b3be0c7b7 100644 --- a/experimental/packages/otlp-proto-exporter-base/submodule.md +++ b/experimental/packages/otlp-proto-exporter-base/submodule.md @@ -40,7 +40,7 @@ Knowing this if you want to change the submodule to point to a different version 8. Now thing which is very important. You have to commit this to apply these changes ```shell script - git commit -am "chore: updating submodule for opentelemetry-proto-exporter-base" + git commit -am "chore: updating submodule for otlp-proto-exporter-base" ``` 9. If you look now at git log you will notice that the folder `protos` has been changed and it will show what was the previous sha and what is current one. diff --git a/experimental/packages/otlp-transformer/src/common/types.ts b/experimental/packages/otlp-transformer/src/common/types.ts index c706015b84..7ca7ecd1a3 100644 --- a/experimental/packages/otlp-transformer/src/common/types.ts +++ b/experimental/packages/otlp-transformer/src/common/types.ts @@ -14,12 +14,12 @@ * limitations under the License. */ -/** Properties of an InstrumentationLibrary. */ -export interface IInstrumentationLibrary { - /** InstrumentationLibrary name */ +/** Properties of an InstrumentationScope. */ +export interface IInstrumentationScope { + /** InstrumentationScope name */ name: string - /** InstrumentationLibrary version */ + /** InstrumentationScope version */ version?: string; } diff --git a/experimental/packages/otlp-transformer/src/metrics/index.ts b/experimental/packages/otlp-transformer/src/metrics/index.ts index a714a0cd14..00ce3a1b36 100644 --- a/experimental/packages/otlp-transformer/src/metrics/index.ts +++ b/experimental/packages/otlp-transformer/src/metrics/index.ts @@ -28,9 +28,9 @@ export function createExportMetricsServiceRequest(resourceMetrics: ResourceMetri droppedAttributesCount: 0 }, schemaUrl: undefined, // TODO: Schema Url does not exist yet in the SDK. - instrumentationLibraryMetrics: Array.from(resourceMetrics.instrumentationLibraryMetrics.map(metrics => { + scopeMetrics: Array.from(resourceMetrics.instrumentationLibraryMetrics.map(metrics => { return { - instrumentationLibrary: { + scope: { name: metrics.instrumentationLibrary.name, version: metrics.instrumentationLibrary.version, }, diff --git a/experimental/packages/otlp-transformer/src/metrics/types.ts b/experimental/packages/otlp-transformer/src/metrics/types.ts index 55282afefe..d4767094e6 100644 --- a/experimental/packages/otlp-transformer/src/metrics/types.ts +++ b/experimental/packages/otlp-transformer/src/metrics/types.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { IInstrumentationLibrary, IKeyValue } from '../common/types'; +import { IInstrumentationScope, IKeyValue } from '../common/types'; import { IResource } from '../resource/types'; /** Properties of an ExportMetricsServiceRequest. */ @@ -29,24 +29,24 @@ export interface IResourceMetrics { /** ResourceMetrics resource */ resource?: IResource; - /** ResourceMetrics instrumentationLibraryMetrics */ - instrumentationLibraryMetrics: IInstrumentationLibraryMetrics[] + /** ResourceMetrics scopeMetrics */ + scopeMetrics: IScopeMetrics[] /** ResourceMetrics schemaUrl */ schemaUrl?: string; } -/** Properties of an InstrumentationLibraryMetrics. */ -export interface IInstrumentationLibraryMetrics { +/** Properties of an IScopeMetrics. */ +export interface IScopeMetrics { - /** InstrumentationLibraryMetrics instrumentationLibrary */ - instrumentationLibrary?: IInstrumentationLibrary; + /** ScopeMetrics scope */ + scope?: IInstrumentationScope; - /** InstrumentationLibraryMetrics metrics */ + /** ScopeMetrics metrics */ metrics: IMetric[]; - /** InstrumentationLibraryMetrics schemaUrl */ + /** ScopeMetrics schemaUrl */ schemaUrl?: string; } diff --git a/experimental/packages/otlp-transformer/src/trace/index.ts b/experimental/packages/otlp-transformer/src/trace/index.ts index 95495dd67a..c49637be32 100644 --- a/experimental/packages/otlp-transformer/src/trace/index.ts +++ b/experimental/packages/otlp-transformer/src/trace/index.ts @@ -27,8 +27,8 @@ export function createExportTraceServiceRequest(spans: ReadableSpan[], useHex?: attributes: toAttributes(resource.attributes), droppedAttributesCount: 0, }, - instrumentationLibrarySpans: resourceSpans.map(({ instrumentationLibrary, instrumentationLibrarySpans, librarySchemaUrl }) => ({ - instrumentationLibrary, + scopeSpans: resourceSpans.map(({ instrumentationLibrary, instrumentationLibrarySpans, librarySchemaUrl }) => ({ + scope: instrumentationLibrary, spans: instrumentationLibrarySpans.map(span => sdkSpanToOtlpSpan(span, useHex)), schemaUrl: librarySchemaUrl, })), diff --git a/experimental/packages/otlp-transformer/src/trace/types.ts b/experimental/packages/otlp-transformer/src/trace/types.ts index e14437a55e..2c65f6f959 100644 --- a/experimental/packages/otlp-transformer/src/trace/types.ts +++ b/experimental/packages/otlp-transformer/src/trace/types.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { IInstrumentationLibrary, IKeyValue } from '../common/types'; +import { IInstrumentationScope, IKeyValue } from '../common/types'; import { IResource } from '../resource/types'; /** Properties of an ExportTraceServiceRequest. */ @@ -30,8 +30,8 @@ export interface IResourceSpans { /** ResourceSpans resource */ resource?: IResource; - /** ResourceSpans instrumentationLibrarySpans */ - instrumentationLibrarySpans: IInstrumentationLibrarySpans[]; + /** ResourceSpans scopeSpans */ + scopeSpans: IInstrumentationLibrarySpans[]; /** ResourceSpans schemaUrl */ schemaUrl?: string; @@ -40,8 +40,8 @@ export interface IResourceSpans { /** Properties of an InstrumentationLibrarySpans. */ export interface IInstrumentationLibrarySpans { - /** InstrumentationLibrarySpans instrumentationLibrary */ - instrumentationLibrary?: IInstrumentationLibrary; + /** InstrumentationLibrarySpans scope */ + scope?: IInstrumentationScope; /** InstrumentationLibrarySpans spans */ spans?: ISpan[] diff --git a/experimental/packages/otlp-transformer/test/metrics.test.ts b/experimental/packages/otlp-transformer/test/metrics.test.ts index 37ecdaf560..73cee96f00 100644 --- a/experimental/packages/otlp-transformer/test/metrics.test.ts +++ b/experimental/packages/otlp-transformer/test/metrics.test.ts @@ -166,9 +166,9 @@ describe('Metrics', () => { droppedAttributesCount: 0, }, schemaUrl: undefined, - instrumentationLibraryMetrics: [ + scopeMetrics: [ { - instrumentationLibrary: { + scope: { name: 'mylib', version: '0.1.0', }, @@ -228,9 +228,9 @@ describe('Metrics', () => { droppedAttributesCount: 0, }, schemaUrl: undefined, - instrumentationLibraryMetrics: [ + scopeMetrics: [ { - instrumentationLibrary: { + scope: { name: 'mylib', version: '0.1.0', }, @@ -290,9 +290,9 @@ describe('Metrics', () => { droppedAttributesCount: 0, }, schemaUrl: undefined, - instrumentationLibraryMetrics: [ + scopeMetrics: [ { - instrumentationLibrary: { + scope: { name: 'mylib', version: '0.1.0', }, @@ -350,9 +350,9 @@ describe('Metrics', () => { droppedAttributesCount: 0, }, schemaUrl: undefined, - instrumentationLibraryMetrics: [ + scopeMetrics: [ { - instrumentationLibrary: { + scope: { name: 'mylib', version: '0.1.0', }, diff --git a/experimental/packages/otlp-transformer/test/trace.test.ts b/experimental/packages/otlp-transformer/test/trace.test.ts index cf438586d1..b47dcaf1e9 100644 --- a/experimental/packages/otlp-transformer/test/trace.test.ts +++ b/experimental/packages/otlp-transformer/test/trace.test.ts @@ -40,9 +40,9 @@ function createExpectedSpanJson(useHex: boolean){ droppedAttributesCount: 0, }, schemaUrl: undefined, - instrumentationLibrarySpans: [ + scopeSpans: [ { - instrumentationLibrary: { name: 'myLib', version: '0.1.0' }, + scope: { name: 'myLib', version: '0.1.0' }, spans: [ { traceId: traceId, @@ -187,14 +187,14 @@ describe('Trace', () => { (span as any).parentSpanId = undefined; const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); - assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].parentSpanId, undefined); + assert.strictEqual(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].parentSpanId, undefined); }); it('serializes a span without a parent with useHex = false', () => { (span as any).parentSpanId = undefined; const exportRequest = createExportTraceServiceRequest([span], false); assert.ok(exportRequest); - assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].parentSpanId, undefined); + assert.strictEqual(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].parentSpanId, undefined); }); describe('status code', () => { @@ -203,7 +203,7 @@ describe('Trace', () => { span.status.message = 'error message'; const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); - const spanStatus = exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].status; + const spanStatus = exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].status; assert.strictEqual(spanStatus?.code, EStatusCode.STATUS_CODE_ERROR); assert.strictEqual(spanStatus?.message, 'error message'); }); @@ -212,7 +212,7 @@ describe('Trace', () => { span.status.code = SpanStatusCode.UNSET; const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); - assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].status.code, EStatusCode.STATUS_CODE_UNSET); + assert.strictEqual(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].status.code, EStatusCode.STATUS_CODE_UNSET); }); }); @@ -221,31 +221,31 @@ describe('Trace', () => { (span as any).kind = SpanKind.CONSUMER; const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); - assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_CONSUMER); + assert.strictEqual(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_CONSUMER); }); it('internal', () => { (span as any).kind = SpanKind.INTERNAL; const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); - assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_INTERNAL); + assert.strictEqual(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_INTERNAL); }); it('producer', () => { (span as any).kind = SpanKind.PRODUCER; const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); - assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_PRODUCER); + assert.strictEqual(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_PRODUCER); }); it('server', () => { (span as any).kind = SpanKind.SERVER; const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); - assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_SERVER); + assert.strictEqual(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_SERVER); }); it('unspecified', () => { (span as any).kind = undefined; const exportRequest = createExportTraceServiceRequest([span], true); assert.ok(exportRequest); - assert.strictEqual(exportRequest.resourceSpans?.[0].instrumentationLibrarySpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_UNSPECIFIED); + assert.strictEqual(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].kind, ESpanKind.SPAN_KIND_UNSPECIFIED); }); }); }); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts index 71453bb260..479764150a 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts @@ -142,7 +142,7 @@ describe('Tracer', () => { span.end(); }); - it('should have an instrumentationLibrary', () => { + it('should have an scope', () => { const tracer = new Tracer( { name: 'default', version: '0.0.1' }, {}, From 48b14af5b280fb0c67c1b616bbff78849fe19ddd Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 27 Apr 2022 14:25:47 +0200 Subject: [PATCH 09/29] fix(exporter): cleanup dependencies. --- experimental/packages/exporter-trace-otlp-proto/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/experimental/packages/exporter-trace-otlp-proto/package.json b/experimental/packages/exporter-trace-otlp-proto/package.json index 89f8155ba6..c0a652e869 100644 --- a/experimental/packages/exporter-trace-otlp-proto/package.json +++ b/experimental/packages/exporter-trace-otlp-proto/package.json @@ -68,7 +68,6 @@ "dependencies": { "@opentelemetry/core": "1.1.1", "@grpc/proto-loader": "^0.6.9", - "@opentelemetry/exporter-trace-otlp-http": "0.28.0", "@opentelemetry/resources": "1.2.0", "@opentelemetry/sdk-trace-base": "1.2.0", "@opentelemetry/otlp-exporter-base": "0.28.0", From 90eaaaba74bf5a1e0655a7ce138b3f7dd105e292 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 27 Apr 2022 15:19:00 +0200 Subject: [PATCH 10/29] fix(exporters): cleanup. --- .../src/OTLPTraceExporter.ts | 4 +-- .../test/OTLPTraceExporter.test.ts | 19 +++++++++++--- .../src/platform/browser/OTLPTraceExporter.ts | 4 +-- .../src/platform/node/OTLPTraceExporter.ts | 4 +-- .../browser/CollectorTraceExporter.test.ts | 2 +- .../test/node/CollectorTraceExporter.test.ts | 14 +++------- .../test/traceHelper.ts | 12 ++++----- .../src/OTLPTraceExporter.ts | 4 +-- .../test/OTLPTraceExporter.test.ts | 6 ++--- .../exporter-trace-otlp-proto/tsconfig.json | 3 --- .../browser/CollectorMetricExporter.test.ts | 26 ++++++------------- .../common/CollectorMetricExporter.test.ts | 4 +-- .../test/metricsHelper.ts | 6 ++--- .../test/node/CollectorMetricExporter.test.ts | 14 +++------- .../src/OTLPMetricExporter.ts | 4 +-- .../test/OTLPMetricExporter.test.ts | 9 +++---- .../test/metricsHelper.ts | 6 ++--- .../test/traceHelper.ts | 20 ++++---------- .../test/common/Tracer.test.ts | 2 +- 19 files changed, 58 insertions(+), 105 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts index 0acd3ecc94..5ea0829dc5 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/src/OTLPTraceExporter.ts @@ -44,9 +44,7 @@ export class OTLPTraceExporter } } - convert( - spans: ReadableSpan[] - ): IExportTraceServiceRequest { + convert(spans: ReadableSpan[]): IExportTraceServiceRequest { return createExportTraceServiceRequest(spans); } diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index d3b62a373c..e23fc81a63 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -178,11 +178,17 @@ const testCollectorExporter = (params: TestParams) => ); let spans; let resource; - if (exportedData && exportedData.scopeSpans[0].spans) { + if (exportedData) { spans = exportedData.scopeSpans[0].spans; - resource = exportedData.resource; - ensureExportedSpanIsCorrect(spans[0]); + + assert.ok( + typeof spans !== 'undefined', + 'spans do not exist' + ); + if(spans) { + ensureExportedSpanIsCorrect(spans[0]); + } assert.ok( typeof resource !== 'undefined', @@ -229,9 +235,14 @@ const testCollectorExporter = (params: TestParams) => ); let spans; let resource; - if (exportedData && exportedData.scopeSpans[0].spans) { + if (exportedData) { spans = exportedData.scopeSpans[0].spans; resource = exportedData.resource; + + assert.ok( + typeof spans !== 'undefined', + 'spans do not exist' + ); ensureExportedSpanIsCorrect(spans[0]); assert.ok( diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts index 87954e9529..21c78e8368 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts @@ -41,9 +41,7 @@ export class OTLPTraceExporter ) ); } - convert( - spans: ReadableSpan[] - ): IExportTraceServiceRequest { + convert(spans: ReadableSpan[]): IExportTraceServiceRequest { return createExportTraceServiceRequest(spans, true); } diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts index 1b2cd89219..517148717d 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts @@ -43,9 +43,7 @@ export class OTLPTraceExporter ); } - convert( - spans: ReadableSpan[] - ): IExportTraceServiceRequest { + convert(spans: ReadableSpan[]): IExportTraceServiceRequest { return createExportTraceServiceRequest(spans, true); } diff --git a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts index 27692509ee..04858ad1b5 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts @@ -99,7 +99,7 @@ describe('OTLPTraceExporter - web', () => { }); it('should successfully send the spans using sendBeacon', done => { - collectorTraceExporter.export(spans, result => { + collectorTraceExporter.export(spans, () => { }); setTimeout(async () => { diff --git a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts index 07d40484bd..2c14e406a1 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts @@ -227,11 +227,8 @@ describe('OTLPTraceExporter - node with json over http', () => { fakeRequest.on('end', () => { const responseBody = buff.toString(); - const json = JSON.parse( - responseBody - ) as IExportTraceServiceRequest; - const span1 = - json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; + const json = JSON.parse(responseBody) as IExportTraceServiceRequest; + const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureSpanIsCorrect(span1); @@ -323,11 +320,8 @@ describe('OTLPTraceExporter - node with json over http', () => { fakeRequest.on('end', () => { const responseBody = zlib.gunzipSync(buff).toString(); - const json = JSON.parse( - responseBody - ) as IExportTraceServiceRequest; - const span1 = - json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; + const json = JSON.parse(responseBody) as IExportTraceServiceRequest; + const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureSpanIsCorrect(span1); diff --git a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts index cbe1b0e882..2cefead54a 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts @@ -407,23 +407,21 @@ export function ensureExportTraceServiceRequestIsSet( const resource = resourceSpans?.[0].resource; assert.strictEqual(!!resource, true, 'resource is missing'); - const instrumentationLibrarySpans = - resourceSpans?.[0].scopeSpans; + const scopeSpans = resourceSpans?.[0].scopeSpans; assert.strictEqual( - instrumentationLibrarySpans && instrumentationLibrarySpans.length, + scopeSpans && scopeSpans.length, 1, 'scopeSpans is missing' ); - const instrumentationLibrary = - instrumentationLibrarySpans?.[0].scope; + const scope = scopeSpans?.[0].scope; assert.strictEqual( - !!instrumentationLibrary, + !!scope, true, 'scope is missing' ); - const spans = instrumentationLibrarySpans?.[0].spans; + const spans = scopeSpans?.[0].spans; assert.strictEqual(spans && spans.length, 1, 'spans are missing'); } diff --git a/experimental/packages/exporter-trace-otlp-proto/src/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-proto/src/OTLPTraceExporter.ts index 403083e506..9fcc7f5a3f 100644 --- a/experimental/packages/exporter-trace-otlp-proto/src/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-proto/src/OTLPTraceExporter.ts @@ -42,9 +42,7 @@ export class OTLPTraceExporter ); } - convert( - spans: ReadableSpan[] - ): IExportTraceServiceRequest { + convert(spans: ReadableSpan[]): IExportTraceServiceRequest { return createExportTraceServiceRequest(spans); } diff --git a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts index 30878f5a98..87c4a091a4 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts @@ -154,8 +154,7 @@ describe('OTLPTraceExporter - node with proto over http', () => { const ExportTraceServiceRequestProto = getExportRequestProto(); const data = ExportTraceServiceRequestProto?.decode(buff); const json = data?.toJSON() as IExportTraceServiceRequest; - const span1 = - json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; + const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureProtoSpanIsCorrect(span1); @@ -241,8 +240,7 @@ describe('OTLPTraceExporter - node with proto over http', () => { const ExportTraceServiceRequestProto = getExportRequestProto(); const data = ExportTraceServiceRequestProto?.decode(unzippedBuff); const json = data?.toJSON() as IExportTraceServiceRequest; - const span1 = - json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; + const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); if (span1) { ensureProtoSpanIsCorrect(span1); diff --git a/experimental/packages/exporter-trace-otlp-proto/tsconfig.json b/experimental/packages/exporter-trace-otlp-proto/tsconfig.json index 6ba9e30e47..3324a8a529 100644 --- a/experimental/packages/exporter-trace-otlp-proto/tsconfig.json +++ b/experimental/packages/exporter-trace-otlp-proto/tsconfig.json @@ -18,9 +18,6 @@ { "path": "../../../packages/opentelemetry-sdk-trace-base" }, - { - "path": "../exporter-trace-otlp-http" - }, { "path": "../otlp-exporter-base" }, diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts index ad1bea4fd7..a17cd91a84 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts @@ -106,15 +106,10 @@ describe('OTLPMetricExporter - web', () => { const url = args[0]; const blob: Blob = args[1]; const body = await blob.text(); - const json = JSON.parse( - body - ) as IExportMetricsServiceRequest; - const metric1 = - json.resourceMetrics[0].scopeMetrics[0].metrics[0]; - const metric2 = - json.resourceMetrics[0].scopeMetrics[0].metrics[1]; - const metric3 = - json.resourceMetrics[0].scopeMetrics[0].metrics[2]; + const json = JSON.parse(body) as IExportMetricsServiceRequest; + const metric1 = json.resourceMetrics[0].scopeMetrics[0].metrics[0]; + const metric2 = json.resourceMetrics[0].scopeMetrics[0].metrics[1]; + const metric3 = json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist"); if (metric1) { @@ -224,15 +219,10 @@ describe('OTLPMetricExporter - web', () => { assert.strictEqual(request.url, 'http://foo.bar.com'); const body = request.requestBody; - const json = JSON.parse( - body - ) as IExportMetricsServiceRequest; - const metric1 = - json.resourceMetrics[0].scopeMetrics[0].metrics[0]; - const metric2 = - json.resourceMetrics[0].scopeMetrics[0].metrics[1]; - const metric3 = - json.resourceMetrics[0].scopeMetrics[0].metrics[2]; + const json = JSON.parse(body) as IExportMetricsServiceRequest; + const metric1 = json.resourceMetrics[0].scopeMetrics[0].metrics[0]; + const metric2 = json.resourceMetrics[0].scopeMetrics[0].metrics[1]; + const metric3 = json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist"); if (metric1) { ensureCounterIsCorrect( diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/CollectorMetricExporter.test.ts index d4029853ca..baddaa76bc 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/common/CollectorMetricExporter.test.ts @@ -36,9 +36,7 @@ class OTLPMetricExporter extends OTLPExporterBase< getDefaultUrl(config: CollectorExporterConfig) { return config.url || ''; } - convert( - metrics: ResourceMetrics[] - ): IExportMetricsServiceRequest { + convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest { return { resourceMetrics: [] }; } } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts index 68c41e6be1..5ea7c4bf18 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts @@ -380,16 +380,14 @@ export function ensureExportMetricsServiceRequestIsSet( const resource = resourceMetrics[0].resource; assert.strictEqual(!!resource, true, 'resource is missing'); - const scopeMetrics = - resourceMetrics[0].scopeMetrics; + const scopeMetrics = resourceMetrics[0].scopeMetrics; assert.strictEqual( scopeMetrics && scopeMetrics.length, 1, 'scopeMetrics is missing' ); - const scope = - scopeMetrics[0].scope; + const scope = scopeMetrics[0].scope; assert.strictEqual( !!scope, true, diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts index 0b0a13fe2d..93c9ba70e1 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/node/CollectorMetricExporter.test.ts @@ -230,16 +230,10 @@ describe('OTLPMetricExporter - node with json over http', () => { setTimeout(() => { const writeArgs = stubWrite.args[0]; - const myJson = JSON.parse( - writeArgs[0] - ); - const json = myJson as IExportMetricsServiceRequest; - const metric1 = - json.resourceMetrics[0].scopeMetrics[0].metrics[0]; - const metric2 = - json.resourceMetrics[0].scopeMetrics[0].metrics[1]; - const metric3 = - json.resourceMetrics[0].scopeMetrics[0].metrics[2]; + const json = JSON.parse(writeArgs[0]) as IExportMetricsServiceRequest; + const metric1 = json.resourceMetrics[0].scopeMetrics[0].metrics[0]; + const metric2 = json.resourceMetrics[0].scopeMetrics[0].metrics[1]; + const metric3 = json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "counter doesn't exist"); ensureCounterIsCorrect( diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts index e7f97c7618..beb3f0c3ff 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/src/OTLPMetricExporter.ts @@ -44,9 +44,7 @@ class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase { const data = ExportTraceServiceRequestProto?.decode(writeArgs[0]); const json = data?.toJSON() as IExportMetricsServiceRequest; - const metric1 = - json.resourceMetrics[0].scopeMetrics[0].metrics[0]; - const metric2 = - json.resourceMetrics[0].scopeMetrics[0].metrics[1]; - const metric3 = - json.resourceMetrics[0].scopeMetrics[0].metrics[2]; + const metric1 = json.resourceMetrics[0].scopeMetrics[0].metrics[0]; + const metric2 = json.resourceMetrics[0].scopeMetrics[0].metrics[1]; + const metric3 = json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "counter doesn't exist"); ensureExportedCounterIsCorrect( diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts index c9e5d16851..bcaabd2bad 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts @@ -203,16 +203,14 @@ export function ensureExportMetricsServiceRequestIsSet( const resource = resourceMetrics[0].resource; assert.strictEqual(!!resource, true, 'resource is missing'); - const scopeMetrics = - resourceMetrics[0].scopeMetrics; + const scopeMetrics = resourceMetrics[0].scopeMetrics; assert.strictEqual( scopeMetrics && scopeMetrics.length, 1, 'scopeMetrics is missing' ); - const scope = - scopeMetrics[0].scope; + const scope = scopeMetrics[0].scope; assert.strictEqual( !!scope, true, diff --git a/experimental/packages/otlp-grpc-exporter-base/test/traceHelper.ts b/experimental/packages/otlp-grpc-exporter-base/test/traceHelper.ts index 7a2d7a3022..e8b0204d61 100644 --- a/experimental/packages/otlp-grpc-exporter-base/test/traceHelper.ts +++ b/experimental/packages/otlp-grpc-exporter-base/test/traceHelper.ts @@ -100,9 +100,7 @@ export const mockedReadableSpan: ReadableSpan = { instrumentationLibrary: { name: 'default', version: '0.0.1' }, }; -export function ensureExportedEventsAreCorrect( - events: IEvent[] -) { +export function ensureExportedEventsAreCorrect(events: IEvent[]) { assert.deepStrictEqual( events, [ @@ -159,9 +157,7 @@ export function ensureExportedEventsAreCorrect( ); } -export function ensureExportedAttributesAreCorrect( - attributes: IKeyValue[] -) { +export function ensureExportedAttributesAreCorrect(attributes: IKeyValue[]) { assert.deepStrictEqual( attributes, [ @@ -177,9 +173,7 @@ export function ensureExportedAttributesAreCorrect( ); } -export function ensureExportedLinksAreCorrect( - attributes: ILink[] -) { +export function ensureExportedLinksAreCorrect(attributes: ILink[]) { assert.deepStrictEqual( attributes, [ @@ -203,9 +197,7 @@ export function ensureExportedLinksAreCorrect( ); } -export function ensureExportedSpanIsCorrect( - span: ISpan -) { +export function ensureExportedSpanIsCorrect(span: ISpan) { if (span.attributes) { ensureExportedAttributesAreCorrect(span.attributes); } @@ -261,9 +253,7 @@ export function ensureExportedSpanIsCorrect( ); } -export function ensureResourceIsCorrect( - resource: IResource -) { +export function ensureResourceIsCorrect(resource: IResource) { assert.deepStrictEqual(resource, { attributes: [ { diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts index 479764150a..71453bb260 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts @@ -142,7 +142,7 @@ describe('Tracer', () => { span.end(); }); - it('should have an scope', () => { + it('should have an instrumentationLibrary', () => { const tracer = new Tracer( { name: 'default', version: '0.0.1' }, {}, From f04dcb5a36ee103971257a2f056654f4901a9c9a Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Fri, 29 Apr 2022 10:08:34 +0200 Subject: [PATCH 11/29] fix(otlp-transformer): rename IInstrumentationLibrarySpans to IScopeSpans. --- .../test/OTLPTraceExporter.test.ts | 2 -- .../src/OTLPMetricExporter.ts | 2 +- .../packages/otlp-transformer/src/trace/types.ts | 12 ++++++------ 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index e23fc81a63..2ea938e8c5 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -349,5 +349,3 @@ describe('', () => { testCollectorExporter({ useTLS: false }); testCollectorExporter({ metadata }); }); - - diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts index 23b0fd23ce..2d69200fd7 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts @@ -40,8 +40,8 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase Date: Fri, 29 Apr 2022 10:10:22 +0200 Subject: [PATCH 12/29] feat(otlp-exporter-example): update example to use new exporter version. --- .../docker/docker-compose.yaml | 2 +- examples/otlp-exporter-node/metrics.js | 33 ++++++++++--------- examples/otlp-exporter-node/package.json | 23 ++++++------- examples/otlp-exporter-node/tracing.js | 6 ++-- lerna.json | 3 +- 5 files changed, 36 insertions(+), 31 deletions(-) diff --git a/examples/otlp-exporter-node/docker/docker-compose.yaml b/examples/otlp-exporter-node/docker/docker-compose.yaml index d0b6d62e7c..c699fb8b8d 100644 --- a/examples/otlp-exporter-node/docker/docker-compose.yaml +++ b/examples/otlp-exporter-node/docker/docker-compose.yaml @@ -2,7 +2,7 @@ version: "3" services: # Collector collector: - image: otel/opentelemetry-collector-contrib:0.42.0 + image: otel/opentelemetry-collector-contrib:0.49.0 # image: otel/opentelemetry-collector-contrib:latest command: ["--config=/conf/collector-config.yaml"] volumes: diff --git a/examples/otlp-exporter-node/metrics.js b/examples/otlp-exporter-node/metrics.js index 114fd0028a..04accabd4e 100644 --- a/examples/otlp-exporter-node/metrics.js +++ b/examples/otlp-exporter-node/metrics.js @@ -1,27 +1,30 @@ 'use strict'; const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api'); -const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http'); -// const { OTLPMetricExporter } = require('@opentelemetry/exporter-otlp-grpc'); -// const { OTLPMetricExporter } = require('@opentelemetry/exporter-otlp-proto'); -const { MeterProvider } = require('@opentelemetry/sdk-metrics-base'); +// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http'); +const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc'); +// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto'); +const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics-base'); const { Resource } = require('@opentelemetry/resources'); const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); // Optional and only needed to see the internal diagnostic logging (during development) diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); -const metricExporter = new OTLPMetricExporter({ - url: 'http://localhost:4318/v1/metrics', -}); +const metricExporter = new OTLPMetricExporter({}); -const meter = new MeterProvider({ - exporter: metricExporter, - interval: 1000, +const meterProvider = new MeterProvider({ resource: new Resource({ [SemanticResourceAttributes.SERVICE_NAME]: 'basic-metric-service', }), -}).getMeter('example-exporter-collector'); +}); + +meterProvider.addMetricReader(new PeriodicExportingMetricReader({ + exporter: metricExporter, + exportIntervalMillis: 1000, +})); + +const meter = meterProvider.getMeter('example-exporter-collector'); const requestCounter = meter.createCounter('requests', { description: 'Example of a Counter', @@ -35,10 +38,10 @@ const histogram = meter.createHistogram('test_histogram', { description: 'Example of a Histogram', }); -const labels = { pid: process.pid, environment: 'staging' }; +const attributes = { pid: process.pid, environment: 'staging' }; setInterval(() => { - requestCounter.add(1, labels); - upDownCounter.add(Math.random() > 0.5 ? 1 : -1, labels); - histogram.record(Math.random(), labels); + requestCounter.add(1, attributes); + upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes); + histogram.record(Math.random(), attributes); }, 1000); diff --git a/examples/otlp-exporter-node/package.json b/examples/otlp-exporter-node/package.json index 4a3c3aacf4..a43a114437 100644 --- a/examples/otlp-exporter-node/package.json +++ b/examples/otlp-exporter-node/package.json @@ -29,17 +29,18 @@ }, "dependencies": { "@opentelemetry/api": "^1.0.2", - "@opentelemetry/core": "1.0.1", - "@opentelemetry/exporter-trace-otlp-http": "0.27.0", - "@opentelemetry/exporter-metrics-otlp-http": "0.27.0", - "@opentelemetry/exporter-trace-otlp-grpc": "0.27.0", - "@opentelemetry/exporter-metrics-otlp-grpc": "0.27.0", - "@opentelemetry/exporter-trace-otlp-proto": "0.27.0", - "@opentelemetry/exporter-metrics-otlp-proto": "0.27.0", - "@opentelemetry/resources": "1.0.1", - "@opentelemetry/semantic-conventions": "1.0.1", - "@opentelemetry/sdk-metrics-base": "0.27.0", - "@opentelemetry/sdk-trace-base": "1.0.1" + "@opentelemetry/api-metrics": "0.28.0", + "@opentelemetry/core": "1.1.1", + "@opentelemetry/exporter-trace-otlp-http": "0.28.0", + "@opentelemetry/exporter-metrics-otlp-http": "0.28.0", + "@opentelemetry/exporter-trace-otlp-grpc": "0.28.0", + "@opentelemetry/exporter-metrics-otlp-grpc": "0.28.0", + "@opentelemetry/exporter-trace-otlp-proto": "0.28.0", + "@opentelemetry/exporter-metrics-otlp-proto": "0.28.0", + "@opentelemetry/resources": "1.1.1", + "@opentelemetry/semantic-conventions": "1.1.1", + "@opentelemetry/sdk-metrics-base": "0.28.0", + "@opentelemetry/sdk-trace-base": "1.1.1" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js#readme" } diff --git a/examples/otlp-exporter-node/tracing.js b/examples/otlp-exporter-node/tracing.js index 661c419348..d193034f87 100644 --- a/examples/otlp-exporter-node/tracing.js +++ b/examples/otlp-exporter-node/tracing.js @@ -2,11 +2,11 @@ const opentelemetry = require('@opentelemetry/api'); const { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base'); -const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http'); const { Resource } = require('@opentelemetry/resources'); const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); -// const { OTLPTraceExporter } = require('@opentelemetry/exporter-otlp-grpc'); -// const { OTLPTraceExporter } = require('@opentelemetry/exporter-otlp-proto'); +// const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http'); +const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc'); +// const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto'); // opentelemetry.diag.setLogger( // new opentelemetry.DiagConsoleLogger(), diff --git a/lerna.json b/lerna.json index 7c9b4dc384..98304af897 100644 --- a/lerna.json +++ b/lerna.json @@ -6,6 +6,7 @@ "experimental/packages/*", "experimental/backwards-compatability/*", "integration-tests/*", - "selenium-tests" + "selenium-tests", + "examples/otlp-exporter-node" ] } From 7052b25839287d99ae46caa13a20cb64a531ca02 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Fri, 29 Apr 2022 10:28:01 +0200 Subject: [PATCH 13/29] docs(exporters): update readme files. --- examples/otlp-exporter-node/README.md | 9 ++-- .../exporter-trace-otlp-grpc/README.md | 7 ++-- .../exporter-trace-otlp-http/README.md | 17 ++++---- .../exporter-trace-otlp-proto/README.md | 2 +- .../README.md | 27 ++++++------ .../README.md | 42 +++++++++---------- .../README.md | 20 ++++----- 7 files changed, 61 insertions(+), 63 deletions(-) diff --git a/examples/otlp-exporter-node/README.md b/examples/otlp-exporter-node/README.md index c65b0b7947..6fb61fe6ca 100644 --- a/examples/otlp-exporter-node/README.md +++ b/examples/otlp-exporter-node/README.md @@ -1,10 +1,9 @@ # Overview - - -This example shows how to use [@opentelemetry/exporter-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.26.0/experimental/packages/opentelemetry-exporter-otlp-http) to instrument a simple Node.js application. - -This example will export spans data simultaneously using [Exporter Collector](https://github.com/open-telemetry/opentelemetry-js/tree/v0.26.0/experimental/packages/opentelemetry-exporter-otlp-http) and grpc. It will use [proto format](https://github.com/open-telemetry/opentelemetry-proto). +This example shows how to use +[@opentelemetry/exporter-trace-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.26.0/experimental/packages/opentelemetry-exporter-trace-otlp-http) +and [@opentelemetry/exporter-metrics-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.26.0/experimental/packages/opentelemetry-exporter-metrics-otlp-http) +to instrument a simple Node.js application. ## Installation diff --git a/experimental/packages/exporter-trace-otlp-grpc/README.md b/experimental/packages/exporter-trace-otlp-grpc/README.md index 1ce3b37ada..6cc3ca7c2e 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/README.md +++ b/experimental/packages/exporter-trace-otlp-grpc/README.md @@ -3,7 +3,7 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.25.0**. +This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. ## Installation @@ -135,9 +135,8 @@ OTEL_EXPORTER_OTLP_TRACES_COMPRESSION=gzip ## Running opentelemetry-collector locally to see the traces -1. Go to examples/otlp-exporter-node -2. run `npm run docker:start` -3. Open page at `http://localhost:9411/zipkin/` to observe the traces +1. Go to `examples/otlp-exporter-node` +2. Follow the instructions there to inspect traces. ## Useful links diff --git a/experimental/packages/exporter-trace-otlp-http/README.md b/experimental/packages/exporter-trace-otlp-http/README.md index f03ca83ff5..12099bc415 100644 --- a/experimental/packages/exporter-trace-otlp-http/README.md +++ b/experimental/packages/exporter-trace-otlp-http/README.md @@ -3,7 +3,7 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.25.0**. +This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. ## Installation @@ -86,19 +86,19 @@ For PROTOBUF please check [npm-url-proto] ## Configuration options as environment variables -Instead of providing options to `OTLPMetricExporter` and `OTLPTraceExporter` explicitly, environment variables may be provided instead. +Instead of providing options to `OTLPTraceExporter` explicitly, environment variables may be provided instead. ```sh -OTEL_EXPORTER_OTLP_ENDPOINT=https://localhost:4317 +OTEL_EXPORTER_OTLP_ENDPOINT=https://localhost:4318 # this will automatically append the version and signal path -# e.g. https://localhost:4317/v1/traces for `OTLPTraceExporter` and https://localhost:4317/v1/metrics for `OTLPMetricExporter` +# e.g. https://localhost:4318/v1/traces for `OTLPTraceExporter` and https://localhost:4318/v1/metrics for `OTLPMetricExporter` ``` If the trace and metric exporter endpoints have different providers, the env var for per-signal endpoints are available to use ```sh -OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://trace-service:4317/v1/traces -OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://metric-service:4317/v1/metrics +OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://trace-service:4318/v1/traces +OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://metric-service:4318/v1/metrics # version and signal needs to be explicit ``` @@ -108,9 +108,8 @@ For more details, see [OpenTelemetry Specification on Protocol Exporter][opentel ## Running opentelemetry-collector locally to see the traces -1. Go to examples/otlp-exporter-node -2. run `npm run docker:start` -3. Open page at `http://localhost:9411/zipkin/` to observe the traces +1. Go to `examples/otlp-exporter-node` +2. Follow the instructions there to inspect traces. ## Useful links diff --git a/experimental/packages/exporter-trace-otlp-proto/README.md b/experimental/packages/exporter-trace-otlp-proto/README.md index 19e91f0f46..6edba4c9b4 100644 --- a/experimental/packages/exporter-trace-otlp-proto/README.md +++ b/experimental/packages/exporter-trace-otlp-proto/README.md @@ -3,7 +3,7 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.25.0**. +This module provides exporter for node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. ## Installation diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md index 99f13136be..33ad539140 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md @@ -3,7 +3,7 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.25.0**. +This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. ## Installation @@ -19,37 +19,38 @@ To see sample code and documentation for the traces exporter, as well as instruc ## Metrics in Node - GRPC -The OTLPTraceExporter in Node expects the URL to only be the hostname. It will not work with `/v1/metrics`. All options that work with trace also work with metrics. +The OTLPMetricsExporter in Node expects the URL to only be the hostname. It will not work with `/v1/metrics`. All options that work with trace also work with metrics. ```js -const { MeterProvider } = require('@opentelemetry/sdk-metrics-base'); +const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics-base'); const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc'); const collectorOptions = { // url is optional and can be omitted - default is grpc://localhost:4317 url: 'grpc://:', }; + const exporter = new OTLPMetricExporter(collectorOptions); +const meterProvider = new MeterProvider({}); + +meterProvider.addMetricReader(new PeriodicExportingMetricReader({ + exporter: metricExporter, + exportIntervalMillis: 1000, +})); -// Register the exporter -const provider = new MeterProvider({ - exporter, - interval: 60000, -}) ['SIGINT', 'SIGTERM'].forEach(signal => { - process.on(signal, () => provider.shutdown().catch(console.error)); + process.on(signal, () => meterProvider.shutdown().catch(console.error)); }); // Now, start recording data -const meter = provider.getMeter('example-meter'); +const meter = meterProvider.getMeter('example-meter'); const counter = meter.createCounter('metric_name'); counter.add(10, { 'key': 'value' }); ``` ## Running opentelemetry-collector locally to see the metrics -1. Go to examples/otlp-exporter-node -2. run `npm run docker:start` -3. Open page at `http://localhost:9411/zipkin/` to observe the metrics +1. Go to `examples/otlp-exporter-node` +2. Follow the instructions there to observe the metrics. ## Useful links diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md index fc5ebe33b8..752ca4fb90 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md @@ -3,7 +3,7 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.25.0**. +This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. ## Installation @@ -22,7 +22,7 @@ To see sample code and documentation for the traces exporter, visit the [Collect The OTLPMetricExporter in Web expects the endpoint to end in `/v1/metrics`. ```js -import { MeterProvider } from '@opentelemetry/sdk-metrics-base'; +import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics-base'; import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http'; const collectorOptions = { url: '', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics @@ -30,37 +30,38 @@ const collectorOptions = { concurrencyLimit: 1, // an optional limit on pending requests }; const exporter = new OTLPMetricExporter(collectorOptions); +const meterProvider = new MeterProvider({}); -// Register the exporter -const meter = new MeterProvider({ - exporter, - interval: 60000, -}).getMeter('example-meter'); +meterProvider.addMetricReader(new PeriodicExportingMetricReader({ + exporter: metricExporter, + exportIntervalMillis: 1000, +})); // Now, start recording data +const meter = meterProvider.getMeter('example-meter'); const counter = meter.createCounter('metric_name'); counter.add(10, { 'key': 'value' }); - ``` ## Metrics in Node ```js -const { MeterProvider } = require('@opentelemetry/sdk-metrics-base'); +const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics-base'); const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http'); const collectorOptions = { url: '', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics concurrencyLimit: 1, // an optional limit on pending requests }; const exporter = new OTLPMetricExporter(collectorOptions); +const meterProvider = new MeterProvider({}); -// Register the exporter -const meter = new MeterProvider({ - exporter, - interval: 60000, -}).getMeter('example-meter'); +meterProvider.addMetricReader(new PeriodicExportingMetricReader({ + exporter: metricExporter, + exportIntervalMillis: 1000, +})); // Now, start recording data +const meter = meterProvider.getMeter('example-meter'); const counter = meter.createCounter('metric_name'); counter.add(10, { 'key': 'value' }); @@ -79,16 +80,16 @@ For exporting metrics with PROTOBUF please check [exporter-metrics-otlp-proto][n Instead of providing options to `OTLPMetricExporter` and `OTLPTraceExporter` explicitly, environment variables may be provided instead. ```sh -OTEL_EXPORTER_OTLP_ENDPOINT=https://localhost:4317 +OTEL_EXPORTER_OTLP_ENDPOINT=https://localhost:4318 # this will automatically append the version and signal path -# e.g. https://localhost:4317/v1/traces for `OTLPTraceExporter` and https://localhost:4317/v1/metrics for `OTLPMetricExporter` +# e.g. https://localhost:4318/v1/traces for `OTLPTraceExporter` and https://localhost:4318/v1/metrics for `OTLPMetricExporter` ``` If the trace and metric exporter endpoints have different providers, the env var for per-signal endpoints are available to use ```sh -OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://trace-service:4317/v1/traces -OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://metric-service:4317/v1/metrics +OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://trace-service:4318/v1/traces +OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://metric-service:4318/v1/metrics # version and signal needs to be explicit ``` @@ -98,9 +99,8 @@ For more details, see [OpenTelemetry Specification on Protocol Exporter][opentel ## Running opentelemetry-collector locally to see the metrics -1. Go to examples/otlp-exporter-node -2. run `npm run docker:start` -3. Open page at `http://localhost:9411/zipkin/` to observe the metrics +1. Go to `examples/otlp-exporter-node` +2. Follow the instructions there to observe the metrics. ## Useful links diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md index cbf16261e7..65ef6dd883 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md @@ -3,7 +3,7 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.25.0**. +This module provides exporter for node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. ## Installation @@ -20,20 +20,21 @@ To see sample code and documentation for the traces exporter, visit the [Collect ## Metrics in Node - PROTO over http ```js -const { MeterProvider } = require('@opentelemetry/sdk-metrics-base'); +const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics-base'); const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto'); const collectorOptions = { url: '', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics }; const exporter = new OTLPMetricExporter(collectorOptions); +const meterProvider = new MeterProvider({}); -// Register the exporter -const meter = new MeterProvider({ - exporter, - interval: 60000, -}).getMeter('example-meter'); +meterProvider.addMetricReader(new PeriodicExportingMetricReader({ + exporter: metricExporter, + exportIntervalMillis: 1000, +})); // Now, start recording data +const meter = meterProvider.getMeter('example-meter'); const counter = meter.createCounter('metric_name'); counter.add(10, { 'key': 'value' }); @@ -41,9 +42,8 @@ counter.add(10, { 'key': 'value' }); ## Running opentelemetry-collector locally to see the metrics -1. Go to examples/otlp-exporter-node -2. run `npm run docker:start` -3. Open page at `http://localhost:9411/zipkin/` to observe the metrics +1. Go to `examples/otlp-exporter-node` +2. Follow the instructions there to observe the metrics. ## Useful links From d32f440b78f2ad6b3b21211b51e1c479eeeb520b Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Fri, 29 Apr 2022 11:42:48 +0200 Subject: [PATCH 14/29] fix(otlp-metric-exporter): map over metrics directly. --- .../src/OTLPMetricExporter.ts | 2 +- .../platform/browser/OTLPMetricExporter.ts | 6 ++--- .../src/platform/node/OTLPMetricExporter.ts | 6 ++--- .../src/OTLPMetricExporter.ts | 2 +- .../otlp-transformer/src/metrics/index.ts | 23 +++-------------- .../otlp-transformer/src/metrics/internal.ts | 25 ++++++++++++++++--- .../otlp-transformer/test/metrics.test.ts | 8 +++--- 7 files changed, 35 insertions(+), 37 deletions(-) diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts index 2d69200fd7..3d7b178352 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts @@ -67,7 +67,7 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase { - return { - scope: { - name: metrics.instrumentationLibrary.name, - version: metrics.instrumentationLibrary.version, - }, - metrics: metrics.metrics.map(metricData => toMetric(metricData, aggregationTemporality)), - schemaUrl: metrics.instrumentationLibrary.schemaUrl - }; - })) - }] + resourceMetrics: resourceMetrics.map(metrics => toResourceMetrics(metrics, aggregationTemporality)) }; } diff --git a/experimental/packages/otlp-transformer/src/metrics/internal.ts b/experimental/packages/otlp-transformer/src/metrics/internal.ts index 1dab2f4417..3cae3ad222 100644 --- a/experimental/packages/otlp-transformer/src/metrics/internal.ts +++ b/experimental/packages/otlp-transformer/src/metrics/internal.ts @@ -21,11 +21,30 @@ import { DataPointType, Histogram, InstrumentType, - MetricData + MetricData, ResourceMetrics } from '@opentelemetry/sdk-metrics-base'; import { toAttributes } from '../common/internal'; -import { EAggregationTemporality, IHistogramDataPoint, IMetric, INumberDataPoint } from './types'; - +import { EAggregationTemporality, IHistogramDataPoint, IMetric, INumberDataPoint, IResourceMetrics } from './types'; + +export function toResourceMetrics(resourceMetrics: ResourceMetrics, aggregationTemporality: AggregationTemporality): IResourceMetrics { + return { + resource: { + attributes: toAttributes(resourceMetrics.resource.attributes), + droppedAttributesCount: 0 + }, + schemaUrl: undefined, // TODO: Schema Url does not exist yet in the SDK. + scopeMetrics: Array.from(resourceMetrics.instrumentationLibraryMetrics.map(metrics => { + return { + scope: { + name: metrics.instrumentationLibrary.name, + version: metrics.instrumentationLibrary.version, + }, + metrics: metrics.metrics.map(metricData => toMetric(metricData, aggregationTemporality)), + schemaUrl: metrics.instrumentationLibrary.schemaUrl + }; + })) + }; +} export function toMetric(metricData: MetricData, metricTemporality: AggregationTemporality): IMetric { const out: IMetric = { diff --git a/experimental/packages/otlp-transformer/test/metrics.test.ts b/experimental/packages/otlp-transformer/test/metrics.test.ts index 73cee96f00..c1fa7586ed 100644 --- a/experimental/packages/otlp-transformer/test/metrics.test.ts +++ b/experimental/packages/otlp-transformer/test/metrics.test.ts @@ -144,7 +144,7 @@ describe('Metrics', () => { } it('serializes a sum metric record', () => { - const metrics = createResourceMetrics([createCounterData(10)]); + const metrics = [createResourceMetrics([createCounterData(10)])]; const exportRequest = createExportMetricsServiceRequest( metrics, AggregationTemporality.DELTA @@ -208,7 +208,7 @@ describe('Metrics', () => { it('serializes an observable sum metric record', () => { const exportRequest = createExportMetricsServiceRequest( - createResourceMetrics([createObservableCounterData(10)]), + [createResourceMetrics([createObservableCounterData(10)])], AggregationTemporality.DELTA ); assert.ok(exportRequest); @@ -270,7 +270,7 @@ describe('Metrics', () => { it('serializes a gauge metric record', () => { const exportRequest = createExportMetricsServiceRequest( - createResourceMetrics([createObservableGaugeData(10.5)]), + [createResourceMetrics([createObservableGaugeData(10.5)])], AggregationTemporality.DELTA ); assert.ok(exportRequest); @@ -330,7 +330,7 @@ describe('Metrics', () => { it('serializes a histogram metric record', () => { const exportRequest = createExportMetricsServiceRequest( - createResourceMetrics([createHistogramMetrics(2, 9, [5], [1,1])]), + [createResourceMetrics([createHistogramMetrics(2, 9, [5], [1,1])])], AggregationTemporality.CUMULATIVE ); assert.ok(exportRequest); From e591ad38b8f178ff1bbd62da51bef185df1e8638 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Fri, 29 Apr 2022 15:08:06 +0200 Subject: [PATCH 15/29] fix(exporter-trace-otlp-proto): rename constant instrumentationLibrarySpans -> scopeSpans --- .../exporter-trace-otlp-proto/test/traceHelper.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts index ac8a04cc1f..d46f9cfc16 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts @@ -241,21 +241,21 @@ export function ensureExportTraceServiceRequestIsSet( const resource = resourceSpans?.[0].resource; assert.strictEqual(!!resource, true, 'resource is missing'); - const instrumentationLibrarySpans = resourceSpans?.[0].scopeSpans; + const scopeSpans = resourceSpans?.[0].scopeSpans; assert.strictEqual( - instrumentationLibrarySpans && instrumentationLibrarySpans.length, + scopeSpans && scopeSpans.length, 1, 'scopeSpans is missing' ); - const scope = instrumentationLibrarySpans?.[0].scope; + const scope = scopeSpans?.[0].scope; assert.strictEqual( !!scope, true, 'scope is missing' ); - const spans = instrumentationLibrarySpans?.[0].spans; + const spans = scopeSpans?.[0].spans; assert.strictEqual(spans && spans.length, 1, 'spans are missing'); } From 87be6625c35dcd46f89fb8d7dee7e7f1148deac1 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Fri, 29 Apr 2022 15:24:51 +0200 Subject: [PATCH 16/29] fix(otlp-exporter): use assert.ok() instead of assert.strictEqual() in tests. --- .../packages/exporter-trace-otlp-http/test/traceHelper.ts | 6 +----- .../packages/exporter-trace-otlp-proto/test/traceHelper.ts | 6 +----- .../test/metricsHelper.ts | 6 +----- .../test/metricsHelper.ts | 6 +----- 4 files changed, 4 insertions(+), 20 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts index 2cefead54a..99d2abccdc 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts @@ -415,11 +415,7 @@ export function ensureExportTraceServiceRequestIsSet( ); const scope = scopeSpans?.[0].scope; - assert.strictEqual( - !!scope, - true, - 'scope is missing' - ); + assert.ok(scope, 'scope is missing'); const spans = scopeSpans?.[0].spans; assert.strictEqual(spans && spans.length, 1, 'spans are missing'); diff --git a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts index d46f9cfc16..b8404de926 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts @@ -249,11 +249,7 @@ export function ensureExportTraceServiceRequestIsSet( ); const scope = scopeSpans?.[0].scope; - assert.strictEqual( - !!scope, - true, - 'scope is missing' - ); + assert.ok(scope, 'scope is missing'); const spans = scopeSpans?.[0].spans; assert.strictEqual(spans && spans.length, 1, 'spans are missing'); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts index 5ea7c4bf18..b44d21104d 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts @@ -388,11 +388,7 @@ export function ensureExportMetricsServiceRequestIsSet( ); const scope = scopeMetrics[0].scope; - assert.strictEqual( - !!scope, - true, - 'scope is missing' - ); + assert.ok(scope, 'scope is missing'); const metrics = resourceMetrics[0].scopeMetrics[0].metrics; assert.strictEqual(metrics.length, 3, 'Metrics are missing'); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts index bcaabd2bad..99a8a4fe07 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts @@ -211,11 +211,7 @@ export function ensureExportMetricsServiceRequestIsSet( ); const scope = scopeMetrics[0].scope; - assert.strictEqual( - !!scope, - true, - 'scope is missing' - ); + assert.ok(scope, 'scope is missing'); const metrics = resourceMetrics[0].scopeMetrics[0].metrics; assert.strictEqual(metrics.length, 3, 'Metrics are missing'); From 4eae3984b9e8d99953323830fcf2c50545a1f5a2 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Mon, 2 May 2022 10:42:39 +0200 Subject: [PATCH 17/29] feat(otlp-transformer): add support for instrumentationLibarary next to scope --- .../docker/docker-compose.yaml | 2 +- examples/otlp-exporter-node/metrics.js | 4 +- examples/otlp-exporter-node/tracing.js | 21 +++-- .../src/platform/browser/OTLPTraceExporter.ts | 2 +- .../src/platform/node/OTLPTraceExporter.ts | 2 +- .../platform/browser/OTLPMetricExporter.ts | 3 +- .../src/platform/node/OTLPMetricExporter.ts | 3 +- .../otlp-transformer/src/metrics/index.ts | 5 +- .../otlp-transformer/src/metrics/internal.ts | 67 +++++++++++---- .../otlp-transformer/src/metrics/types.ts | 19 ++++- .../otlp-transformer/src/trace/index.ts | 82 ++++++++++--------- .../otlp-transformer/src/trace/types.ts | 30 +++++++ 12 files changed, 169 insertions(+), 71 deletions(-) diff --git a/examples/otlp-exporter-node/docker/docker-compose.yaml b/examples/otlp-exporter-node/docker/docker-compose.yaml index c699fb8b8d..6eaed872a6 100644 --- a/examples/otlp-exporter-node/docker/docker-compose.yaml +++ b/examples/otlp-exporter-node/docker/docker-compose.yaml @@ -2,7 +2,7 @@ version: "3" services: # Collector collector: - image: otel/opentelemetry-collector-contrib:0.49.0 + image: otel/opentelemetry-collector-contrib:0.50.0 # image: otel/opentelemetry-collector-contrib:latest command: ["--config=/conf/collector-config.yaml"] volumes: diff --git a/examples/otlp-exporter-node/metrics.js b/examples/otlp-exporter-node/metrics.js index 04accabd4e..049d8229ec 100644 --- a/examples/otlp-exporter-node/metrics.js +++ b/examples/otlp-exporter-node/metrics.js @@ -1,8 +1,8 @@ 'use strict'; const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api'); -// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http'); -const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc'); +const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http'); +// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc'); // const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto'); const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics-base'); const { Resource } = require('@opentelemetry/resources'); diff --git a/examples/otlp-exporter-node/tracing.js b/examples/otlp-exporter-node/tracing.js index d193034f87..d2a6414442 100644 --- a/examples/otlp-exporter-node/tracing.js +++ b/examples/otlp-exporter-node/tracing.js @@ -1,17 +1,20 @@ 'use strict'; -const opentelemetry = require('@opentelemetry/api'); const { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base'); const { Resource } = require('@opentelemetry/resources'); const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); -// const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http'); -const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc'); +const { + diag, + trace, + context, + DiagConsoleLogger, + DiagLogLevel, +} = require('@opentelemetry/api'); +const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http'); +// const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc'); // const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto'); -// opentelemetry.diag.setLogger( -// new opentelemetry.DiagConsoleLogger(), -// opentelemetry.DiagLogLevel.DEBUG, -// ); +diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); const exporter = new OTLPTraceExporter({ // headers: { @@ -28,7 +31,7 @@ provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); provider.register(); -const tracer = opentelemetry.trace.getTracer('example-otlp-exporter-node'); +const tracer = trace.getTracer('example-otlp-exporter-node'); // Create a span. A span must be closed. const parentSpan = tracer.startSpan('main'); @@ -47,7 +50,7 @@ setTimeout(() => { function doWork(parent) { // Start another span. In this example, the main method already started a // span, so that'll be the parent span, and this will be a child span. - const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), parent); + const ctx = trace.setSpan(context.active(), parent); const span = tracer.startSpan('doWork', undefined, ctx); // simulate some random work. diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts index 21c78e8368..b60071a752 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts @@ -42,7 +42,7 @@ export class OTLPTraceExporter ); } convert(spans: ReadableSpan[]): IExportTraceServiceRequest { - return createExportTraceServiceRequest(spans, true); + return createExportTraceServiceRequest(spans, true, true); } getDefaultUrl(config: OTLPExporterConfigBase): string { diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts index 517148717d..bc0807ea2f 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts @@ -44,7 +44,7 @@ export class OTLPTraceExporter } convert(spans: ReadableSpan[]): IExportTraceServiceRequest { - return createExportTraceServiceRequest(spans, true); + return createExportTraceServiceRequest(spans, true, true); } getDefaultUrl(config: OTLPExporterNodeConfigBase): string { diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts index 0189b6d9b3..def1f51a12 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts @@ -56,7 +56,8 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase toResourceMetrics(metrics, aggregationTemporality)) + resourceMetrics: resourceMetrics.map(metrics => toResourceMetrics(metrics, aggregationTemporality, json)) }; } diff --git a/experimental/packages/otlp-transformer/src/metrics/internal.ts b/experimental/packages/otlp-transformer/src/metrics/internal.ts index 3cae3ad222..4b5f3920f4 100644 --- a/experimental/packages/otlp-transformer/src/metrics/internal.ts +++ b/experimental/packages/otlp-transformer/src/metrics/internal.ts @@ -19,31 +19,68 @@ import { AggregationTemporality, DataPoint, DataPointType, - Histogram, + Histogram, InstrumentationLibraryMetrics, InstrumentType, MetricData, ResourceMetrics } from '@opentelemetry/sdk-metrics-base'; import { toAttributes } from '../common/internal'; -import { EAggregationTemporality, IHistogramDataPoint, IMetric, INumberDataPoint, IResourceMetrics } from './types'; - -export function toResourceMetrics(resourceMetrics: ResourceMetrics, aggregationTemporality: AggregationTemporality): IResourceMetrics { - return { +import { + EAggregationTemporality, + IHistogramDataPoint, IInstrumentationLibraryMetrics, + IMetric, + INumberDataPoint, + IResourceMetrics, + IScopeMetrics +} from './types'; + +export function toResourceMetrics(resourceMetrics: ResourceMetrics, + aggregationTemporality: AggregationTemporality, + json?: boolean): IResourceMetrics { + const transformedMetrics: IResourceMetrics = { resource: { attributes: toAttributes(resourceMetrics.resource.attributes), droppedAttributesCount: 0 }, schemaUrl: undefined, // TODO: Schema Url does not exist yet in the SDK. - scopeMetrics: Array.from(resourceMetrics.instrumentationLibraryMetrics.map(metrics => { - return { - scope: { - name: metrics.instrumentationLibrary.name, - version: metrics.instrumentationLibrary.version, - }, - metrics: metrics.metrics.map(metricData => toMetric(metricData, aggregationTemporality)), - schemaUrl: metrics.instrumentationLibrary.schemaUrl - }; - })) + scopeMetrics: toScopeMetrics(resourceMetrics.instrumentationLibraryMetrics, aggregationTemporality) }; + + if (json) { + transformedMetrics.instrumentationLibraryMetrics = + toInstrumentationLibraryMetrics(resourceMetrics.instrumentationLibraryMetrics, aggregationTemporality); + } + + return transformedMetrics; +} + +export function toScopeMetrics(instrumentationLibraryMetrics: InstrumentationLibraryMetrics[], + aggregationTemporality: AggregationTemporality): IScopeMetrics[]{ + return Array.from(instrumentationLibraryMetrics.map(metrics => { + const scopeMetrics : IScopeMetrics = { + scope: { + name: metrics.instrumentationLibrary.name, + version: metrics.instrumentationLibrary.version, + }, + metrics: metrics.metrics.map(metricData => toMetric(metricData, aggregationTemporality)), + schemaUrl: metrics.instrumentationLibrary.schemaUrl + }; + return scopeMetrics; + })); +} + +export function toInstrumentationLibraryMetrics(instrumentationLibraryMetrics: InstrumentationLibraryMetrics[], + aggregationTemporality: AggregationTemporality): IInstrumentationLibraryMetrics[]{ + return Array.from(instrumentationLibraryMetrics.map(metrics => { + const resultMetrics : IInstrumentationLibraryMetrics = { + instrumentationLibrary: { + name: metrics.instrumentationLibrary.name, + version: metrics.instrumentationLibrary.version, + }, + metrics: metrics.metrics.map(metricData => toMetric(metricData, aggregationTemporality)), + schemaUrl: metrics.instrumentationLibrary.schemaUrl + }; + return resultMetrics; + })); } export function toMetric(metricData: MetricData, metricTemporality: AggregationTemporality): IMetric { diff --git a/experimental/packages/otlp-transformer/src/metrics/types.ts b/experimental/packages/otlp-transformer/src/metrics/types.ts index d4767094e6..409d842ad9 100644 --- a/experimental/packages/otlp-transformer/src/metrics/types.ts +++ b/experimental/packages/otlp-transformer/src/metrics/types.ts @@ -15,6 +15,7 @@ */ import { IInstrumentationScope, IKeyValue } from '../common/types'; import { IResource } from '../resource/types'; +import { InstrumentationLibrary } from '@opentelemetry/core'; /** Properties of an ExportMetricsServiceRequest. */ export interface IExportMetricsServiceRequest { @@ -32,6 +33,9 @@ export interface IResourceMetrics { /** ResourceMetrics scopeMetrics */ scopeMetrics: IScopeMetrics[] + /** ResourceMetrics instrumentationLibraryMetrics */ + instrumentationLibraryMetrics?: IInstrumentationLibraryMetrics[] + /** ResourceMetrics schemaUrl */ schemaUrl?: string; } @@ -39,7 +43,6 @@ export interface IResourceMetrics { /** Properties of an IScopeMetrics. */ export interface IScopeMetrics { - /** ScopeMetrics scope */ scope?: IInstrumentationScope; @@ -50,6 +53,20 @@ export interface IScopeMetrics { schemaUrl?: string; } +/** Properties of an IInstrumentationLibraryMetrics. */ +export interface IInstrumentationLibraryMetrics { + /** + * InstrumentationLibraryMetrics instrumentationLibrary + */ + instrumentationLibrary?: InstrumentationLibrary + + /** InstrumentationLibraryMetrics metrics */ + metrics: IMetric[]; + + /** InstrumentationLibraryMetrics schemaUrl */ + schemaUrl?: string; +} + /** Properties of a Metric. */ export interface IMetric { diff --git a/experimental/packages/otlp-transformer/src/trace/index.ts b/experimental/packages/otlp-transformer/src/trace/index.ts index c49637be32..1464cbd92f 100644 --- a/experimental/packages/otlp-transformer/src/trace/index.ts +++ b/experimental/packages/otlp-transformer/src/trace/index.ts @@ -13,46 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import type { InstrumentationLibrary } from '@opentelemetry/core'; import type { Resource } from '@opentelemetry/resources'; import type { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import { toAttributes } from '../common/internal'; import { sdkSpanToOtlpSpan } from './internal'; -import { IExportTraceServiceRequest } from './types'; +import { IExportTraceServiceRequest, IInstrumentationLibrarySpans, IResourceSpans, IScopeSpans } from './types'; -export function createExportTraceServiceRequest(spans: ReadableSpan[], useHex?: boolean): IExportTraceServiceRequest { +export function createExportTraceServiceRequest(spans: ReadableSpan[], useHex?: boolean, json?: boolean): IExportTraceServiceRequest { return { - resourceSpans: spanRecordsToResourceSpans(spans).map(({ resource, resourceSpans, resourceSchemaUrl }) => ({ - resource: { - attributes: toAttributes(resource.attributes), - droppedAttributesCount: 0, - }, - scopeSpans: resourceSpans.map(({ instrumentationLibrary, instrumentationLibrarySpans, librarySchemaUrl }) => ({ - scope: instrumentationLibrary, - spans: instrumentationLibrarySpans.map(span => sdkSpanToOtlpSpan(span, useHex)), - schemaUrl: librarySchemaUrl, - })), - schemaUrl: resourceSchemaUrl, - })) + resourceSpans: spanRecordsToResourceSpans(spans, useHex, json) }; } -type ResourceSpans = { - resource: Resource, - resourceSpans: InstrumentationLibrarySpans[], - resourceSchemaUrl?: string, -}; - -type InstrumentationLibrarySpans = { - instrumentationLibrary: InstrumentationLibrary, - instrumentationLibrarySpans: ReadableSpan[], - librarySchemaUrl?: string, -}; - -function spanRecordsToResourceSpans(spans: ReadableSpan[]): ResourceSpans[] { +function createResourceMap(readableSpans: ReadableSpan[]) { const resourceMap: Map> = new Map(); - - for (const record of spans) { + for (const record of readableSpans) { let ilmMap = resourceMap.get(record.resource); if (!ilmMap) { @@ -72,25 +47,58 @@ function spanRecordsToResourceSpans(spans: ReadableSpan[]): ResourceSpans[] { records.push(record); } - const out: ResourceSpans[] = []; + return resourceMap; +} + +function spanRecordsToResourceSpans(readableSpans: ReadableSpan[], useHex?: boolean, json?: boolean): IResourceSpans[] { + const resourceMap = createResourceMap(readableSpans); + const out: IResourceSpans[] = []; const entryIterator = resourceMap.entries(); let entry = entryIterator.next(); while (!entry.done) { const [resource, ilmMap] = entry.value; - const resourceSpans: InstrumentationLibrarySpans[] = []; + const scopeResourceSpans: IScopeSpans[] = []; + const instrumentationLibraryResourceSpans: IInstrumentationLibrarySpans[] = []; const ilmIterator = ilmMap.values(); let ilmEntry = ilmIterator.next(); while (!ilmEntry.done) { - const instrumentationLibrarySpans = ilmEntry.value; - if (instrumentationLibrarySpans.length > 0) { - const { name, version, schemaUrl } = instrumentationLibrarySpans[0].instrumentationLibrary; - resourceSpans.push({ instrumentationLibrary: { name, version }, instrumentationLibrarySpans, librarySchemaUrl: schemaUrl }); + const scopeSpans = ilmEntry.value; + if (scopeSpans.length > 0) { + const { name, version, schemaUrl } = scopeSpans[0].instrumentationLibrary; + const spans = scopeSpans.map(readableSpan => sdkSpanToOtlpSpan(readableSpan, useHex)); + + scopeResourceSpans.push({ + scope: { name, version }, + spans: spans, + schemaUrl: schemaUrl + }); + + if (json) { + instrumentationLibraryResourceSpans.push({ + instrumentationLibrary: { name, version }, + spans: spans, + schemaUrl: schemaUrl + }); + } } ilmEntry = ilmIterator.next(); } // TODO SDK types don't provide resource schema URL at this time - out.push({ resource, resourceSpans }); + const transformedSpans: IResourceSpans = { + resource: { + attributes: toAttributes(resource.attributes), + droppedAttributesCount: 0, + }, + scopeSpans: scopeResourceSpans, + schemaUrl: undefined + }; + + if (json) { + transformedSpans.instrumentationLibrarySpans = instrumentationLibraryResourceSpans; + } + + out.push(transformedSpans); entry = entryIterator.next(); } diff --git a/experimental/packages/otlp-transformer/src/trace/types.ts b/experimental/packages/otlp-transformer/src/trace/types.ts index 61ad37b57b..c061367878 100644 --- a/experimental/packages/otlp-transformer/src/trace/types.ts +++ b/experimental/packages/otlp-transformer/src/trace/types.ts @@ -33,6 +33,9 @@ export interface IResourceSpans { /** ResourceSpans scopeSpans */ scopeSpans: IScopeSpans[]; + /** ResourceSpans instrumentationLibrarySpans */ + instrumentationLibrarySpans?: IScopeSpans[]; + /** ResourceSpans schemaUrl */ schemaUrl?: string; } @@ -50,6 +53,33 @@ export interface IScopeSpans { schemaUrl?: (string | null); } +/** Properties of an InstrumentationLibrarySpans. */ +export interface IInstrumentationLibrarySpans { + + /** InstrumentationLibrarySpans scope */ + instrumentationLibrary?: IInstrumentationScope; + + /** InstrumentationLibrarySpans spans */ + spans?: ISpan[] + + /** InstrumentationLibrarySpans schemaUrl */ + schemaUrl?: (string | null); +} + + +/** Properties of an InstrumentationLibrarySpans. */ +export interface IInstrumentationLibrarySpans { + + /** IInstrumentationLibrarySpans InstrumentationLibrary */ + instrumentationLibrary?: IInstrumentationScope; + + /** IInstrumentationLibrarySpans spans */ + spans?: ISpan[] + + /** IInstrumentationLibrarySpans schemaUrl */ + schemaUrl?: (string | null); +} + /** Properties of a Span. */ export interface ISpan { From 4507c5211573ed3e30228f5b73c8daa7ff512bf6 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Mon, 2 May 2022 15:23:28 +0200 Subject: [PATCH 18/29] fix(otlp-transformer): change publish config from 'restricted' to 'public' --- experimental/packages/otlp-transformer/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/packages/otlp-transformer/package.json b/experimental/packages/otlp-transformer/package.json index bdd9aced4d..540c5af84f 100644 --- a/experimental/packages/otlp-transformer/package.json +++ b/experimental/packages/otlp-transformer/package.json @@ -2,7 +2,7 @@ "name": "@opentelemetry/otlp-transformer", "private": false, "publishConfig": { - "access": "restricted" + "access": "public" }, "version": "0.28.0", "description": "Transform OpenTelemetry SDK data into OTLP", From 6c77da935598fc302c55f62a46f697931220eaad Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Mon, 2 May 2022 16:23:12 +0200 Subject: [PATCH 19/29] docs(readme): add upgrade guidelines entries. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 3a9a825440..9bfcce580d 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,13 @@ These instrumentations are hosted at Date: Mon, 2 May 2022 16:42:10 +0200 Subject: [PATCH 20/29] fix(otlp-transformer): cleanup. --- .../otlp-transformer/src/metrics/internal.ts | 6 ++++-- .../packages/otlp-transformer/src/trace/types.ts | 15 --------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/experimental/packages/otlp-transformer/src/metrics/internal.ts b/experimental/packages/otlp-transformer/src/metrics/internal.ts index 4b5f3920f4..c491033662 100644 --- a/experimental/packages/otlp-transformer/src/metrics/internal.ts +++ b/experimental/packages/otlp-transformer/src/metrics/internal.ts @@ -19,9 +19,11 @@ import { AggregationTemporality, DataPoint, DataPointType, - Histogram, InstrumentationLibraryMetrics, + Histogram, + InstrumentationLibraryMetrics, InstrumentType, - MetricData, ResourceMetrics + MetricData, + ResourceMetrics } from '@opentelemetry/sdk-metrics-base'; import { toAttributes } from '../common/internal'; import { diff --git a/experimental/packages/otlp-transformer/src/trace/types.ts b/experimental/packages/otlp-transformer/src/trace/types.ts index c061367878..466d6e1be5 100644 --- a/experimental/packages/otlp-transformer/src/trace/types.ts +++ b/experimental/packages/otlp-transformer/src/trace/types.ts @@ -66,21 +66,6 @@ export interface IInstrumentationLibrarySpans { schemaUrl?: (string | null); } - -/** Properties of an InstrumentationLibrarySpans. */ -export interface IInstrumentationLibrarySpans { - - /** IInstrumentationLibrarySpans InstrumentationLibrary */ - instrumentationLibrary?: IInstrumentationScope; - - /** IInstrumentationLibrarySpans spans */ - spans?: ISpan[] - - /** IInstrumentationLibrarySpans schemaUrl */ - schemaUrl?: (string | null); -} - - /** Properties of a Span. */ export interface ISpan { /** Span traceId */ From 3cd3a0ebf65121fcfe79d4331328b506aa30e44e Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Mon, 2 May 2022 16:56:46 +0200 Subject: [PATCH 21/29] docs(otlp-exporters): add compatible version range for opentelemetry-collector. --- experimental/packages/exporter-trace-otlp-grpc/README.md | 3 ++- experimental/packages/exporter-trace-otlp-http/README.md | 3 ++- experimental/packages/exporter-trace-otlp-proto/README.md | 3 ++- .../opentelemetry-exporter-metrics-otlp-grpc/README.md | 3 ++- .../opentelemetry-exporter-metrics-otlp-http/README.md | 4 +++- .../opentelemetry-exporter-metrics-otlp-proto/README.md | 3 ++- 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/README.md b/experimental/packages/exporter-trace-otlp-grpc/README.md index 6cc3ca7c2e..fa58300b9b 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/README.md +++ b/experimental/packages/exporter-trace-otlp-grpc/README.md @@ -3,7 +3,8 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. +This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url]. +Compatible with [opentelemetry-collector][opentelemetry-collector-url] versions `>=0.16 <=0.50`. ## Installation diff --git a/experimental/packages/exporter-trace-otlp-http/README.md b/experimental/packages/exporter-trace-otlp-http/README.md index 12099bc415..8621e73291 100644 --- a/experimental/packages/exporter-trace-otlp-http/README.md +++ b/experimental/packages/exporter-trace-otlp-http/README.md @@ -3,7 +3,8 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. +This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] +Compatible with [opentelemetry-collector][opentelemetry-collector-url] versions `>=0.48 <=0.50`. ## Installation diff --git a/experimental/packages/exporter-trace-otlp-proto/README.md b/experimental/packages/exporter-trace-otlp-proto/README.md index 6edba4c9b4..5e5d2ed6e8 100644 --- a/experimental/packages/exporter-trace-otlp-proto/README.md +++ b/experimental/packages/exporter-trace-otlp-proto/README.md @@ -3,7 +3,8 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. +This module provides exporter for node to be used with [opentelemetry-collector][opentelemetry-collector-url]. +Compatible with [opentelemetry-collector][opentelemetry-collector-url] versions `>=0.32 <=0.50`. ## Installation diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md index 33ad539140..8d514d0e82 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md @@ -3,7 +3,8 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. +This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url]. +Compatible with [opentelemetry-collector][opentelemetry-collector-url] versions `>=0.16 <=0.50`. ## Installation diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md index 752ca4fb90..fd9bb64b32 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md @@ -3,7 +3,9 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. +This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url]. +Compatible with [opentelemetry-collector][opentelemetry-collector-url] versions `>=0.48 <=0.50`. + ## Installation diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md index 65ef6dd883..c0e3dde68f 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/README.md @@ -3,7 +3,8 @@ [![NPM Published Version][npm-img]][npm-url] [![Apache License][license-image]][license-image] -This module provides exporter for node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.49.0**. +This module provides exporter for node to be used with [opentelemetry-collector][opentelemetry-collector-url]. +Compatible with [opentelemetry-collector][opentelemetry-collector-url] versions `>=0.32 <=0.50`. ## Installation From d7f400e0522a8295a68e4171ea243b07bd0d914c Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Mon, 2 May 2022 17:02:43 +0200 Subject: [PATCH 22/29] docs(exporter-metrics-otlp-http): remove double newline. --- .../packages/opentelemetry-exporter-metrics-otlp-http/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md index fd9bb64b32..5035110219 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md @@ -6,7 +6,6 @@ This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url]. Compatible with [opentelemetry-collector][opentelemetry-collector-url] versions `>=0.48 <=0.50`. - ## Installation ```bash From fbcf9c3596ce821b7a90799c16c860e305677433 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Tue, 3 May 2022 11:08:56 +0200 Subject: [PATCH 23/29] feat(otlp-transformer): revert to only publishing scopeMetrics. --- .../src/platform/browser/OTLPTraceExporter.ts | 2 +- .../src/platform/node/OTLPTraceExporter.ts | 2 +- .../platform/browser/OTLPMetricExporter.ts | 3 +- .../src/platform/node/OTLPMetricExporter.ts | 3 +- .../otlp-transformer/src/metrics/index.ts | 5 ++-- .../otlp-transformer/src/metrics/internal.ts | 29 ++----------------- .../otlp-transformer/src/metrics/types.ts | 25 ++-------------- .../otlp-transformer/src/trace/index.ts | 21 +++----------- .../otlp-transformer/src/trace/types.ts | 16 ---------- 9 files changed, 16 insertions(+), 90 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts index b60071a752..21c78e8368 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts @@ -42,7 +42,7 @@ export class OTLPTraceExporter ); } convert(spans: ReadableSpan[]): IExportTraceServiceRequest { - return createExportTraceServiceRequest(spans, true, true); + return createExportTraceServiceRequest(spans, true); } getDefaultUrl(config: OTLPExporterConfigBase): string { diff --git a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts index bc0807ea2f..517148717d 100644 --- a/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts +++ b/experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts @@ -44,7 +44,7 @@ export class OTLPTraceExporter } convert(spans: ReadableSpan[]): IExportTraceServiceRequest { - return createExportTraceServiceRequest(spans, true, true); + return createExportTraceServiceRequest(spans, true); } getDefaultUrl(config: OTLPExporterNodeConfigBase): string { diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts index def1f51a12..0189b6d9b3 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts @@ -56,8 +56,7 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase toResourceMetrics(metrics, aggregationTemporality, json)) + resourceMetrics: resourceMetrics.map(metrics => toResourceMetrics(metrics, aggregationTemporality)) }; } diff --git a/experimental/packages/otlp-transformer/src/metrics/internal.ts b/experimental/packages/otlp-transformer/src/metrics/internal.ts index c491033662..48e592a1a5 100644 --- a/experimental/packages/otlp-transformer/src/metrics/internal.ts +++ b/experimental/packages/otlp-transformer/src/metrics/internal.ts @@ -28,7 +28,7 @@ import { import { toAttributes } from '../common/internal'; import { EAggregationTemporality, - IHistogramDataPoint, IInstrumentationLibraryMetrics, + IHistogramDataPoint, IMetric, INumberDataPoint, IResourceMetrics, @@ -36,9 +36,8 @@ import { } from './types'; export function toResourceMetrics(resourceMetrics: ResourceMetrics, - aggregationTemporality: AggregationTemporality, - json?: boolean): IResourceMetrics { - const transformedMetrics: IResourceMetrics = { + aggregationTemporality: AggregationTemporality): IResourceMetrics { + return { resource: { attributes: toAttributes(resourceMetrics.resource.attributes), droppedAttributesCount: 0 @@ -46,13 +45,6 @@ export function toResourceMetrics(resourceMetrics: ResourceMetrics, schemaUrl: undefined, // TODO: Schema Url does not exist yet in the SDK. scopeMetrics: toScopeMetrics(resourceMetrics.instrumentationLibraryMetrics, aggregationTemporality) }; - - if (json) { - transformedMetrics.instrumentationLibraryMetrics = - toInstrumentationLibraryMetrics(resourceMetrics.instrumentationLibraryMetrics, aggregationTemporality); - } - - return transformedMetrics; } export function toScopeMetrics(instrumentationLibraryMetrics: InstrumentationLibraryMetrics[], @@ -70,21 +62,6 @@ export function toScopeMetrics(instrumentationLibraryMetrics: InstrumentationLib })); } -export function toInstrumentationLibraryMetrics(instrumentationLibraryMetrics: InstrumentationLibraryMetrics[], - aggregationTemporality: AggregationTemporality): IInstrumentationLibraryMetrics[]{ - return Array.from(instrumentationLibraryMetrics.map(metrics => { - const resultMetrics : IInstrumentationLibraryMetrics = { - instrumentationLibrary: { - name: metrics.instrumentationLibrary.name, - version: metrics.instrumentationLibrary.version, - }, - metrics: metrics.metrics.map(metricData => toMetric(metricData, aggregationTemporality)), - schemaUrl: metrics.instrumentationLibrary.schemaUrl - }; - return resultMetrics; - })); -} - export function toMetric(metricData: MetricData, metricTemporality: AggregationTemporality): IMetric { const out: IMetric = { name: metricData.descriptor.name, diff --git a/experimental/packages/otlp-transformer/src/metrics/types.ts b/experimental/packages/otlp-transformer/src/metrics/types.ts index 409d842ad9..061ce9f025 100644 --- a/experimental/packages/otlp-transformer/src/metrics/types.ts +++ b/experimental/packages/otlp-transformer/src/metrics/types.ts @@ -15,7 +15,6 @@ */ import { IInstrumentationScope, IKeyValue } from '../common/types'; import { IResource } from '../resource/types'; -import { InstrumentationLibrary } from '@opentelemetry/core'; /** Properties of an ExportMetricsServiceRequest. */ export interface IExportMetricsServiceRequest { @@ -24,25 +23,22 @@ export interface IExportMetricsServiceRequest { resourceMetrics: IResourceMetrics[] } - /** Properties of a ResourceMetrics. */ export interface IResourceMetrics { + /** ResourceMetrics resource */ resource?: IResource; /** ResourceMetrics scopeMetrics */ scopeMetrics: IScopeMetrics[] - /** ResourceMetrics instrumentationLibraryMetrics */ - instrumentationLibraryMetrics?: IInstrumentationLibraryMetrics[] - /** ResourceMetrics schemaUrl */ schemaUrl?: string; } - /** Properties of an IScopeMetrics. */ export interface IScopeMetrics { + /** ScopeMetrics scope */ scope?: IInstrumentationScope; @@ -53,20 +49,6 @@ export interface IScopeMetrics { schemaUrl?: string; } -/** Properties of an IInstrumentationLibraryMetrics. */ -export interface IInstrumentationLibraryMetrics { - /** - * InstrumentationLibraryMetrics instrumentationLibrary - */ - instrumentationLibrary?: InstrumentationLibrary - - /** InstrumentationLibraryMetrics metrics */ - metrics: IMetric[]; - - /** InstrumentationLibraryMetrics schemaUrl */ - schemaUrl?: string; -} - /** Properties of a Metric. */ export interface IMetric { @@ -299,8 +281,7 @@ export interface IExemplar { * AggregationTemporality defines how a metric aggregator reports aggregated * values. It describes how those values relate to the time interval over * which they are aggregated. - */ -export const enum EAggregationTemporality { + */export const enum EAggregationTemporality { /* UNSPECIFIED is the default AggregationTemporality, it MUST not be used. */ AGGREGATION_TEMPORALITY_UNSPECIFIED = 0, diff --git a/experimental/packages/otlp-transformer/src/trace/index.ts b/experimental/packages/otlp-transformer/src/trace/index.ts index 1464cbd92f..0601f017a9 100644 --- a/experimental/packages/otlp-transformer/src/trace/index.ts +++ b/experimental/packages/otlp-transformer/src/trace/index.ts @@ -17,11 +17,11 @@ import type { Resource } from '@opentelemetry/resources'; import type { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import { toAttributes } from '../common/internal'; import { sdkSpanToOtlpSpan } from './internal'; -import { IExportTraceServiceRequest, IInstrumentationLibrarySpans, IResourceSpans, IScopeSpans } from './types'; +import { IExportTraceServiceRequest, IResourceSpans, IScopeSpans } from './types'; -export function createExportTraceServiceRequest(spans: ReadableSpan[], useHex?: boolean, json?: boolean): IExportTraceServiceRequest { +export function createExportTraceServiceRequest(spans: ReadableSpan[], useHex?: boolean): IExportTraceServiceRequest { return { - resourceSpans: spanRecordsToResourceSpans(spans, useHex, json) + resourceSpans: spanRecordsToResourceSpans(spans, useHex) }; } @@ -50,7 +50,7 @@ function createResourceMap(readableSpans: ReadableSpan[]) { return resourceMap; } -function spanRecordsToResourceSpans(readableSpans: ReadableSpan[], useHex?: boolean, json?: boolean): IResourceSpans[] { +function spanRecordsToResourceSpans(readableSpans: ReadableSpan[], useHex?: boolean): IResourceSpans[] { const resourceMap = createResourceMap(readableSpans); const out: IResourceSpans[] = []; @@ -59,7 +59,6 @@ function spanRecordsToResourceSpans(readableSpans: ReadableSpan[], useHex?: bool while (!entry.done) { const [resource, ilmMap] = entry.value; const scopeResourceSpans: IScopeSpans[] = []; - const instrumentationLibraryResourceSpans: IInstrumentationLibrarySpans[] = []; const ilmIterator = ilmMap.values(); let ilmEntry = ilmIterator.next(); while (!ilmEntry.done) { @@ -73,14 +72,6 @@ function spanRecordsToResourceSpans(readableSpans: ReadableSpan[], useHex?: bool spans: spans, schemaUrl: schemaUrl }); - - if (json) { - instrumentationLibraryResourceSpans.push({ - instrumentationLibrary: { name, version }, - spans: spans, - schemaUrl: schemaUrl - }); - } } ilmEntry = ilmIterator.next(); } @@ -94,10 +85,6 @@ function spanRecordsToResourceSpans(readableSpans: ReadableSpan[], useHex?: bool schemaUrl: undefined }; - if (json) { - transformedSpans.instrumentationLibrarySpans = instrumentationLibraryResourceSpans; - } - out.push(transformedSpans); entry = entryIterator.next(); } diff --git a/experimental/packages/otlp-transformer/src/trace/types.ts b/experimental/packages/otlp-transformer/src/trace/types.ts index 466d6e1be5..a12260c7e4 100644 --- a/experimental/packages/otlp-transformer/src/trace/types.ts +++ b/experimental/packages/otlp-transformer/src/trace/types.ts @@ -33,9 +33,6 @@ export interface IResourceSpans { /** ResourceSpans scopeSpans */ scopeSpans: IScopeSpans[]; - /** ResourceSpans instrumentationLibrarySpans */ - instrumentationLibrarySpans?: IScopeSpans[]; - /** ResourceSpans schemaUrl */ schemaUrl?: string; } @@ -53,19 +50,6 @@ export interface IScopeSpans { schemaUrl?: (string | null); } -/** Properties of an InstrumentationLibrarySpans. */ -export interface IInstrumentationLibrarySpans { - - /** InstrumentationLibrarySpans scope */ - instrumentationLibrary?: IInstrumentationScope; - - /** InstrumentationLibrarySpans spans */ - spans?: ISpan[] - - /** InstrumentationLibrarySpans schemaUrl */ - schemaUrl?: (string | null); -} - /** Properties of a Span. */ export interface ISpan { /** Span traceId */ From 00e70155930c7bbe4ddb78a47f6969d71d862f1c Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Tue, 3 May 2022 17:13:08 +0200 Subject: [PATCH 24/29] docs(otlp-exporter-node-example): fix links. --- examples/otlp-exporter-node/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/otlp-exporter-node/README.md b/examples/otlp-exporter-node/README.md index 6fb61fe6ca..43b902b2a6 100644 --- a/examples/otlp-exporter-node/README.md +++ b/examples/otlp-exporter-node/README.md @@ -1,8 +1,8 @@ # Overview This example shows how to use -[@opentelemetry/exporter-trace-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.26.0/experimental/packages/opentelemetry-exporter-trace-otlp-http) -and [@opentelemetry/exporter-metrics-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.26.0/experimental/packages/opentelemetry-exporter-metrics-otlp-http) +[@opentelemetry/exporter-trace-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.28.0/experimental/packages/exporter-trace-otlp-http) +and [@opentelemetry/exporter-metrics-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.28.0/experimental/packages/opentelemetry-exporter-metrics-otlp-http) to instrument a simple Node.js application. ## Installation From c33fbe1c321c8e118894a7ccecd164baffab20be Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Tue, 3 May 2022 17:35:01 +0200 Subject: [PATCH 25/29] fix(otlp-exporters): cleanup test assertions. --- .../exporter-trace-otlp-http/test/traceHelper.ts | 16 ++++------------ .../test/traceHelper.ts | 12 ++++-------- .../test/metricsHelper.ts | 8 ++------ .../test/metricsHelper.ts | 8 ++------ 4 files changed, 12 insertions(+), 32 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts index 99d2abccdc..e5415fcf6a 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts @@ -398,27 +398,19 @@ export function ensureExportTraceServiceRequestIsSet( json: IExportTraceServiceRequest ) { const resourceSpans = json.resourceSpans; - assert.strictEqual( - resourceSpans && resourceSpans.length, - 1, - 'resourceSpans is missing' - ); + assert.strictEqual(resourceSpans?.length, 1, 'resourceSpans is missing'); const resource = resourceSpans?.[0].resource; - assert.strictEqual(!!resource, true, 'resource is missing'); + assert.ok(resource, 'resource is missing'); const scopeSpans = resourceSpans?.[0].scopeSpans; - assert.strictEqual( - scopeSpans && scopeSpans.length, - 1, - 'scopeSpans is missing' - ); + assert.strictEqual(scopeSpans?.length, 1, 'scopeSpans is missing'); const scope = scopeSpans?.[0].scope; assert.ok(scope, 'scope is missing'); const spans = scopeSpans?.[0].spans; - assert.strictEqual(spans && spans.length, 1, 'spans are missing'); + assert.strictEqual(spans?.length, 1, 'spans are missing'); } export function ensureHeadersContain( diff --git a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts index b8404de926..10a0765458 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/traceHelper.ts @@ -232,18 +232,14 @@ export function ensureExportTraceServiceRequestIsSet( json: IExportTraceServiceRequest ) { const resourceSpans = json.resourceSpans; - assert.strictEqual( - resourceSpans && resourceSpans.length, - 1, - 'resourceSpans is missing' - ); + assert.strictEqual(resourceSpans?.length, 1, 'resourceSpans is missing'); const resource = resourceSpans?.[0].resource; - assert.strictEqual(!!resource, true, 'resource is missing'); + assert.ok(resource, 'resource is missing'); const scopeSpans = resourceSpans?.[0].scopeSpans; assert.strictEqual( - scopeSpans && scopeSpans.length, + scopeSpans?.length, 1, 'scopeSpans is missing' ); @@ -252,7 +248,7 @@ export function ensureExportTraceServiceRequestIsSet( assert.ok(scope, 'scope is missing'); const spans = scopeSpans?.[0].spans; - assert.strictEqual(spans && spans.length, 1, 'spans are missing'); + assert.strictEqual(spans?.length, 1, 'spans are missing'); } export class MockedResponse extends Stream { diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts index b44d21104d..457b525f19 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/metricsHelper.ts @@ -378,14 +378,10 @@ export function ensureExportMetricsServiceRequestIsSet( ); const resource = resourceMetrics[0].resource; - assert.strictEqual(!!resource, true, 'resource is missing'); + assert.ok(resource, 'resource is missing'); const scopeMetrics = resourceMetrics[0].scopeMetrics; - assert.strictEqual( - scopeMetrics && scopeMetrics.length, - 1, - 'scopeMetrics is missing' - ); + assert.strictEqual(scopeMetrics?.length, 1, 'scopeMetrics is missing'); const scope = scopeMetrics[0].scope; assert.ok(scope, 'scope is missing'); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts index 99a8a4fe07..1006f34804 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-proto/test/metricsHelper.ts @@ -201,14 +201,10 @@ export function ensureExportMetricsServiceRequestIsSet( ); const resource = resourceMetrics[0].resource; - assert.strictEqual(!!resource, true, 'resource is missing'); + assert.ok(resource, 'resource is missing'); const scopeMetrics = resourceMetrics[0].scopeMetrics; - assert.strictEqual( - scopeMetrics && scopeMetrics.length, - 1, - 'scopeMetrics is missing' - ); + assert.strictEqual(scopeMetrics?.length, 1, 'scopeMetrics is missing'); const scope = scopeMetrics[0].scope; assert.ok(scope, 'scope is missing'); From 02c4c5555cd7d5996769bd759ad067666d1a2b0b Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Tue, 3 May 2022 17:48:30 +0200 Subject: [PATCH 26/29] fix(otlp-exporters): cleanup tests. --- .../test/OTLPTraceExporter.test.ts | 18 +++++++----------- .../browser/CollectorTraceExporter.test.ts | 4 +--- .../test/OTLPMetricExporter.test.ts | 15 ++++----------- .../browser/CollectorMetricExporter.test.ts | 10 +++------- .../otlp-transformer/src/metrics/types.ts | 3 ++- 5 files changed, 17 insertions(+), 33 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index 2ea938e8c5..207ac017d1 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -186,17 +186,15 @@ const testCollectorExporter = (params: TestParams) => typeof spans !== 'undefined', 'spans do not exist' ); - if(spans) { - ensureExportedSpanIsCorrect(spans[0]); - } + + ensureExportedSpanIsCorrect(spans[0]); assert.ok( typeof resource !== 'undefined', "resource doesn't exist" ); - if (resource) { - ensureResourceIsCorrect(resource); - } + + ensureResourceIsCorrect(resource); } if (params.metadata && reqMetadata) { ensureMetadataIsCorrect(reqMetadata, params.metadata); @@ -344,8 +342,6 @@ describe('when configuring via environment', () => { }); }); -describe('', () => { - testCollectorExporter({ useTLS: true }); - testCollectorExporter({ useTLS: false }); - testCollectorExporter({ metadata }); -}); +testCollectorExporter({ useTLS: true }); +testCollectorExporter({ useTLS: false }); +testCollectorExporter({ metadata }); diff --git a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts index 04858ad1b5..0cac9269a6 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts @@ -121,9 +121,7 @@ describe('OTLPTraceExporter - web', () => { const resource = json.resourceSpans?.[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); - if (resource) { - ensureWebResourceIsCorrect(resource); - } + ensureWebResourceIsCorrect(resource); assert.strictEqual(url, 'http://foo.bar.com'); assert.strictEqual(stubBeacon.callCount, 1); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts index c259dfe880..1fcdd28b68 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts @@ -239,9 +239,7 @@ const testOTLPMetricExporter = (params: TestParams) => typeof resource !== 'undefined', "resource doesn't exist" ); - if (resource) { - ensureResourceIsCorrect(resource); - } + ensureResourceIsCorrect(resource); } if (params.metadata && reqMetadata) { ensureMetadataIsCorrect(reqMetadata, params.metadata); @@ -319,11 +317,6 @@ describe('when configuring via environment', () => { }); }); -describe('', () => { - testOTLPMetricExporter({ useTLS: true }); - testOTLPMetricExporter({ useTLS: false }); - testOTLPMetricExporter({ metadata }); -}); - - - +testOTLPMetricExporter({ useTLS: true }); +testOTLPMetricExporter({ useTLS: false }); +testOTLPMetricExporter({ metadata }); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts index a17cd91a84..7bcb9a669a 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts @@ -150,13 +150,11 @@ describe('OTLPMetricExporter - web', () => { const resource = json.resourceMetrics[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); - if (resource) { - ensureWebResourceIsCorrect(resource); - } + ensureWebResourceIsCorrect(resource); assert.strictEqual(url, 'http://foo.bar.com'); - assert.strictEqual(stubBeacon.callCount, 1); + assert.strictEqual(stubBeacon.callCount, 1); assert.strictEqual(stubOpen.callCount, 0); ensureExportMetricsServiceRequestIsSet(json); @@ -261,9 +259,7 @@ describe('OTLPMetricExporter - web', () => { const resource = json.resourceMetrics[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); - if (resource) { - ensureWebResourceIsCorrect(resource); - } + ensureWebResourceIsCorrect(resource); assert.strictEqual(stubBeacon.callCount, 0); ensureExportMetricsServiceRequestIsSet(json); diff --git a/experimental/packages/otlp-transformer/src/metrics/types.ts b/experimental/packages/otlp-transformer/src/metrics/types.ts index 061ce9f025..0e98ff2a25 100644 --- a/experimental/packages/otlp-transformer/src/metrics/types.ts +++ b/experimental/packages/otlp-transformer/src/metrics/types.ts @@ -281,7 +281,8 @@ export interface IExemplar { * AggregationTemporality defines how a metric aggregator reports aggregated * values. It describes how those values relate to the time interval over * which they are aggregated. - */export const enum EAggregationTemporality { + */ +export const enum EAggregationTemporality { /* UNSPECIFIED is the default AggregationTemporality, it MUST not be used. */ AGGREGATION_TEMPORALITY_UNSPECIFIED = 0, From 35d976293aff242f9e0664e543f6a6b3dca7059e Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 4 May 2022 12:58:59 +0200 Subject: [PATCH 27/29] fix(exporter-trace-otlp-http): remove 'if' for undefined checks. --- .../test/browser/CollectorTraceExporter.test.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts index 0cac9269a6..b6835b0151 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/browser/CollectorTraceExporter.test.ts @@ -115,9 +115,7 @@ describe('OTLPTraceExporter - web', () => { json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureSpanIsCorrect(span1); - } + ensureSpanIsCorrect(span1); const resource = json.resourceSpans?.[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); @@ -206,15 +204,11 @@ describe('OTLPTraceExporter - web', () => { json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureSpanIsCorrect(span1); - } + ensureSpanIsCorrect(span1); const resource = json.resourceSpans?.[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); - if (resource) { - ensureWebResourceIsCorrect(resource); - } + ensureWebResourceIsCorrect(resource); assert.strictEqual(stubBeacon.callCount, 0); From 47aa7e468e2e6d3cf7e235869d9a50d16b37ee34 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 4 May 2022 13:28:36 +0200 Subject: [PATCH 28/29] fix(otlp-exporters): remove 'if' for undefined checks from tests. --- .../test/OTLPTraceExporter.test.ts | 47 +++++----- .../test/traceHelper.ts | 10 +- .../test/node/CollectorTraceExporter.test.ts | 8 +- .../test/OTLPTraceExporter.test.ts | 8 +- .../test/OTLPMetricExporter.test.ts | 72 +++++++-------- .../test/metricsHelper.ts | 10 +- .../browser/CollectorMetricExporter.test.ts | 92 +++++++++---------- 7 files changed, 114 insertions(+), 133 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index 207ac017d1..ebdb6bd682 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -176,29 +176,26 @@ const testCollectorExporter = (params: TestParams) => typeof exportedData !== 'undefined', 'resource' + " doesn't exist" ); - let spans; - let resource; - if (exportedData) { - spans = exportedData.scopeSpans[0].spans; - resource = exportedData.resource; - assert.ok( - typeof spans !== 'undefined', - 'spans do not exist' - ); + const spans = exportedData.scopeSpans[0].spans; + const resource = exportedData.resource; - ensureExportedSpanIsCorrect(spans[0]); + assert.ok( + typeof spans !== 'undefined', + 'spans do not exist' + ); - assert.ok( - typeof resource !== 'undefined', - "resource doesn't exist" - ); + ensureExportedSpanIsCorrect(spans[0]); + + assert.ok( + typeof resource !== 'undefined', + "resource doesn't exist" + ); + + ensureResourceIsCorrect(resource); + + ensureMetadataIsCorrect(reqMetadata, params?.metadata); - ensureResourceIsCorrect(resource); - } - if (params.metadata && reqMetadata) { - ensureMetadataIsCorrect(reqMetadata, params.metadata); - } done(); }, 200); }); @@ -247,13 +244,13 @@ const testCollectorExporter = (params: TestParams) => typeof resource !== 'undefined', "resource doesn't exist" ); - if (resource) { - ensureResourceIsCorrect(resource); - } - } - if (params.metadata && reqMetadata) { - ensureMetadataIsCorrect(reqMetadata, params.metadata); + + ensureResourceIsCorrect(resource); + } + + ensureMetadataIsCorrect(reqMetadata, params.metadata); + done(); }, 500); }); diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts b/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts index b7476f45f0..5b0d0444a1 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/traceHelper.ts @@ -320,11 +320,11 @@ export function ensureResourceIsCorrect( } export function ensureMetadataIsCorrect( - actual: grpc.Metadata, - expected: grpc.Metadata + actual?: grpc.Metadata, + expected?: grpc.Metadata ) { //ignore user agent - expected.remove('user-agent'); - actual.remove('user-agent'); - assert.deepStrictEqual(actual.getMap(), expected.getMap()); + expected?.remove('user-agent'); + actual?.remove('user-agent'); + assert.deepStrictEqual(actual?.getMap(), expected?.getMap() ?? {}); } diff --git a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts index 2c14e406a1..bfe9d4ccb4 100644 --- a/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-http/test/node/CollectorTraceExporter.test.ts @@ -230,9 +230,7 @@ describe('OTLPTraceExporter - node with json over http', () => { const json = JSON.parse(responseBody) as IExportTraceServiceRequest; const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureSpanIsCorrect(span1); - } + ensureSpanIsCorrect(span1); ensureExportTraceServiceRequestIsSet(json); @@ -323,9 +321,7 @@ describe('OTLPTraceExporter - node with json over http', () => { const json = JSON.parse(responseBody) as IExportTraceServiceRequest; const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureSpanIsCorrect(span1); - } + ensureSpanIsCorrect(span1); ensureExportTraceServiceRequestIsSet(json); assert.ok(spySetHeader.calledWith('Content-Encoding', 'gzip')); diff --git a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts index 87c4a091a4..56f6aba69c 100644 --- a/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-proto/test/OTLPTraceExporter.test.ts @@ -156,9 +156,7 @@ describe('OTLPTraceExporter - node with proto over http', () => { const json = data?.toJSON() as IExportTraceServiceRequest; const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureProtoSpanIsCorrect(span1); - } + ensureProtoSpanIsCorrect(span1); ensureExportTraceServiceRequestIsSet(json); @@ -242,9 +240,7 @@ describe('OTLPTraceExporter - node with proto over http', () => { const json = data?.toJSON() as IExportTraceServiceRequest; const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0]; assert.ok(typeof span1 !== 'undefined', "span doesn't exist"); - if (span1) { - ensureProtoSpanIsCorrect(span1); - } + ensureProtoSpanIsCorrect(span1); ensureExportTraceServiceRequestIsSet(json); assert.ok(spySetHeader.calledWith('Content-Encoding', 'gzip')); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts index 1fcdd28b68..76dd71708d 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/OTLPMetricExporter.test.ts @@ -207,43 +207,43 @@ const testOTLPMetricExporter = (params: TestParams) => setTimeout(() => { assert.ok( typeof exportedData !== 'undefined', - 'resource' + " doesn't exist" + 'resource does not exist' ); - let resource; - if (exportedData) { - resource = exportedData[0].resource; - const counter = - exportedData[0].scopeMetrics[0].metrics[0]; - const observableGauge = - exportedData[0].scopeMetrics[0].metrics[1]; - const histogram = - exportedData[0].scopeMetrics[0].metrics[2]; - ensureExportedCounterIsCorrect( - counter, - counter.sum?.dataPoints[0].timeUnixNano, - counter.sum?.dataPoints[0].startTimeUnixNano - ); - ensureExportedObservableGaugeIsCorrect( - observableGauge, - observableGauge.gauge?.dataPoints[0].timeUnixNano, - observableGauge.gauge?.dataPoints[0].startTimeUnixNano - ); - ensureExportedHistogramIsCorrect( - histogram, - histogram.histogram?.dataPoints[0].timeUnixNano, - histogram.histogram?.dataPoints[0].startTimeUnixNano, - [0, 100], - ['0', '2', '0'] - ); - assert.ok( - typeof resource !== 'undefined', - "resource doesn't exist" - ); - ensureResourceIsCorrect(resource); - } - if (params.metadata && reqMetadata) { - ensureMetadataIsCorrect(reqMetadata, params.metadata); - } + + assert.ok(exportedData, 'exportedData does not exist'); + + const resource = exportedData[0].resource; + const counter = + exportedData[0].scopeMetrics[0].metrics[0]; + const observableGauge = + exportedData[0].scopeMetrics[0].metrics[1]; + const histogram = + exportedData[0].scopeMetrics[0].metrics[2]; + ensureExportedCounterIsCorrect( + counter, + counter.sum?.dataPoints[0].timeUnixNano, + counter.sum?.dataPoints[0].startTimeUnixNano + ); + ensureExportedObservableGaugeIsCorrect( + observableGauge, + observableGauge.gauge?.dataPoints[0].timeUnixNano, + observableGauge.gauge?.dataPoints[0].startTimeUnixNano + ); + ensureExportedHistogramIsCorrect( + histogram, + histogram.histogram?.dataPoints[0].timeUnixNano, + histogram.histogram?.dataPoints[0].startTimeUnixNano, + [0, 100], + ['0', '2', '0'] + ); + assert.ok( + typeof resource !== 'undefined', + "resource doesn't exist" + ); + ensureResourceIsCorrect(resource); + + ensureMetadataIsCorrect(reqMetadata, params.metadata); + done(); }, 500); }); diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts index 7c73fc0105..7f33ceb9ef 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts @@ -258,11 +258,11 @@ export function ensureResourceIsCorrect( } export function ensureMetadataIsCorrect( - actual: grpc.Metadata, - expected: grpc.Metadata + actual?: grpc.Metadata, + expected?: grpc.Metadata ) { //ignore user agent - expected.remove('user-agent'); - actual.remove('user-agent'); - assert.deepStrictEqual(actual.getMap(), expected.getMap()); + expected?.remove('user-agent'); + actual?.remove('user-agent'); + assert.deepStrictEqual(actual?.getMap(), expected?.getMap() ?? {}); } diff --git a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts index 7bcb9a669a..21cec954f9 100644 --- a/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts +++ b/experimental/packages/opentelemetry-exporter-metrics-otlp-http/test/browser/CollectorMetricExporter.test.ts @@ -112,41 +112,37 @@ describe('OTLPMetricExporter - web', () => { const metric3 = json.resourceMetrics[0].scopeMetrics[0].metrics[2]; assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist"); - if (metric1) { - ensureCounterIsCorrect( - metric1, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].startTime) - ); - } + + ensureCounterIsCorrect( + metric1, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].startTime) + ); + assert.ok( typeof metric2 !== 'undefined', "second metric doesn't exist" ); - if (metric2) { - ensureObservableGaugeIsCorrect( - metric2, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].startTime), - 6, - 'double-observable-gauge2' - ); - } + ensureObservableGaugeIsCorrect( + metric2, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].startTime), + 6, + 'double-observable-gauge2' + ); assert.ok( typeof metric3 !== 'undefined', "third metric doesn't exist" ); - if (metric3) { - ensureHistogramIsCorrect( - metric3, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].startTime), - [0, 100], - [0, 2, 0] - ); - } + ensureHistogramIsCorrect( + metric3, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].startTime), + [0, 100], + [0, 2, 0] + ); const resource = json.resourceMetrics[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); @@ -221,41 +217,37 @@ describe('OTLPMetricExporter - web', () => { const metric1 = json.resourceMetrics[0].scopeMetrics[0].metrics[0]; const metric2 = json.resourceMetrics[0].scopeMetrics[0].metrics[1]; const metric3 = json.resourceMetrics[0].scopeMetrics[0].metrics[2]; + assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist"); - if (metric1) { - ensureCounterIsCorrect( - metric1, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].startTime) - ); - } + ensureCounterIsCorrect( + metric1, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[0].dataPoints[0].startTime) + ); + assert.ok( typeof metric2 !== 'undefined', "second metric doesn't exist" ); - if (metric2) { - ensureObservableGaugeIsCorrect( - metric2, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].startTime), - 6, - 'double-observable-gauge2' - ); - } + ensureObservableGaugeIsCorrect( + metric2, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[1].dataPoints[0].startTime), + 6, + 'double-observable-gauge2' + ); assert.ok( typeof metric3 !== 'undefined', "third metric doesn't exist" ); - if (metric3) { - ensureHistogramIsCorrect( - metric3, - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].endTime), - hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].startTime), - [0, 100], - [0, 2, 0] - ); - } + ensureHistogramIsCorrect( + metric3, + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].endTime), + hrTimeToNanoseconds(metrics.instrumentationLibraryMetrics[0].metrics[2].dataPoints[0].startTime), + [0, 100], + [0, 2, 0] + ); const resource = json.resourceMetrics[0].resource; assert.ok(typeof resource !== 'undefined', "resource doesn't exist"); From 2dcb4c19c69eb688efc762206b4c14172903a5e8 Mon Sep 17 00:00:00 2001 From: "marc.pichler" Date: Wed, 4 May 2022 15:17:28 +0200 Subject: [PATCH 29/29] fix(otlp-exporters): remove more 'if's for undefined checks from tests. --- .../test/OTLPTraceExporter.test.ts | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts index ebdb6bd682..5eb2fc585d 100644 --- a/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts +++ b/experimental/packages/exporter-trace-otlp-grpc/test/OTLPTraceExporter.test.ts @@ -228,26 +228,20 @@ const testCollectorExporter = (params: TestParams) => typeof exportedData !== 'undefined', 'resource' + " doesn't exist" ); - let spans; - let resource; - if (exportedData) { - spans = exportedData.scopeSpans[0].spans; - resource = exportedData.resource; - - assert.ok( - typeof spans !== 'undefined', - 'spans do not exist' - ); - ensureExportedSpanIsCorrect(spans[0]); - - assert.ok( - typeof resource !== 'undefined', - "resource doesn't exist" - ); + const spans = exportedData.scopeSpans[0].spans; + const resource = exportedData.resource; - ensureResourceIsCorrect(resource); + assert.ok( + typeof spans !== 'undefined', + 'spans do not exist' + ); + ensureExportedSpanIsCorrect(spans[0]); - } + assert.ok( + typeof resource !== 'undefined', + "resource doesn't exist" + ); + ensureResourceIsCorrect(resource); ensureMetadataIsCorrect(reqMetadata, params.metadata);