From b1f6992ba4a49ebbafa9e4a7410df8095a3c1a57 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Thu, 13 Oct 2022 15:31:58 -0700 Subject: [PATCH 01/15] Add tracing suppresing for Metrics Export --- .../export/PeriodicExportingMetricReader.ts | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts index e4f78d1cf0..3182a2374e 100644 --- a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts +++ b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts @@ -18,6 +18,7 @@ import * as api from '@opentelemetry/api'; import { ExportResultCode, globalErrorHandler, + suppressTracing, unrefTimer } from '@opentelemetry/core'; import { MetricReader } from './MetricReader'; @@ -86,17 +87,19 @@ export class PeriodicExportingMetricReader extends MetricReader { } return new Promise((resolve, reject) => { - this._exporter.export(resourceMetrics, result => { - if (result.code !== ExportResultCode.SUCCESS) { - reject( - result.error ?? - new Error( - `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` - ) - ); - } else { - resolve(); - } + api.context.with(suppressTracing(api.context.active()), () => { + this._exporter.export(resourceMetrics, result => { + if (result.code !== ExportResultCode.SUCCESS) { + reject( + result.error ?? + new Error( + `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` + ) + ); + } else { + resolve(); + } + }); }); }); } From c948fa979d1c793ee283fee5a79368ec6f6509ee Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Thu, 13 Oct 2022 15:37:31 -0700 Subject: [PATCH 02/15] Add changelog --- experimental/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index f14d274c65..333153782f 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* feat(metrics-sdk): Add tracing suppresing for Metrics Export [#3332](https://github.com/open-telemetry/opentelemetry-js/pull/3332) @hectorhdzg * feat(sdk-node): configure trace exporter with environment variables [#3143](https://github.com/open-telemetry/opentelemetry-js/pull/3143) @svetlanabrennan ### :bug: (Bug Fix) From 99c2c7807199d38194a7078525819191ea5e0dc8 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Fri, 14 Oct 2022 11:06:32 -0700 Subject: [PATCH 03/15] Addressing comments --- .../export/PeriodicExportingMetricReader.ts | 26 ++++++++-------- packages/opentelemetry-core/src/Exporter.ts | 30 +++++++++++++++++++ packages/opentelemetry-core/src/index.ts | 1 + .../src/export/SimpleSpanProcessor.ts | 27 ++++++++--------- 4 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 packages/opentelemetry-core/src/Exporter.ts diff --git a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts index 3182a2374e..79e56e35d8 100644 --- a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts +++ b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts @@ -17,8 +17,8 @@ import * as api from '@opentelemetry/api'; import { ExportResultCode, + coreExport, globalErrorHandler, - suppressTracing, unrefTimer } from '@opentelemetry/core'; import { MetricReader } from './MetricReader'; @@ -87,19 +87,17 @@ export class PeriodicExportingMetricReader extends MetricReader { } return new Promise((resolve, reject) => { - api.context.with(suppressTracing(api.context.active()), () => { - this._exporter.export(resourceMetrics, result => { - if (result.code !== ExportResultCode.SUCCESS) { - reject( - result.error ?? - new Error( - `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` - ) - ); - } else { - resolve(); - } - }); + coreExport(this._exporter, resourceMetrics, result => { + if (result.code !== ExportResultCode.SUCCESS) { + reject( + result.error ?? + new Error( + `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` + ) + ); + } else { + resolve(); + } }); }); } diff --git a/packages/opentelemetry-core/src/Exporter.ts b/packages/opentelemetry-core/src/Exporter.ts new file mode 100644 index 0000000000..b8d10c0c7d --- /dev/null +++ b/packages/opentelemetry-core/src/Exporter.ts @@ -0,0 +1,30 @@ +/* + * 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 { context } from "@opentelemetry/api"; +import { ExportResult } from "./ExportResult"; +import { suppressTracing } from "./trace/suppress-tracing"; + +export interface Exporter { + export(arg: T, resultCallback: (result: ExportResult) => void): void; +} + +export function coreExport(exporter: Exporter, arg: T, resultCallback: (result: ExportResult) => void): void { + // prevent downstream exporter calls from generating spans + context.with(suppressTracing(context.active()), () => { + exporter.export(arg, resultCallback); + }); +} diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index fd8d3c6778..2caddafe73 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -21,6 +21,7 @@ export * from './common/global-error-handler'; export * from './common/logging-error-handler'; export * from './common/time'; export * from './common/types'; +export * from './Exporter'; export * from './ExportResult'; export * from './version'; export * as baggageUtils from './baggage/utils'; diff --git a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts index c775bdf6d4..ab462ab544 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts @@ -14,12 +14,12 @@ * limitations under the License. */ -import { context, Context, TraceFlags } from '@opentelemetry/api'; +import { Context, TraceFlags } from '@opentelemetry/api'; import { ExportResultCode, globalErrorHandler, - suppressTracing, BindOnceFuture, + coreExport, } from '@opentelemetry/core'; import { Span } from '../Span'; import { SpanProcessor } from '../SpanProcessor'; @@ -45,7 +45,7 @@ export class SimpleSpanProcessor implements SpanProcessor { } // does nothing. - onStart(_span: Span, _parentContext: Context): void {} + onStart(_span: Span, _parentContext: Context): void { } onEnd(span: ReadableSpan): void { if (this._shutdownOnce.isCalled) { @@ -56,18 +56,15 @@ export class SimpleSpanProcessor implements SpanProcessor { return; } - // prevent downstream exporter calls from generating spans - context.with(suppressTracing(context.active()), () => { - this._exporter.export([span], result => { - if (result.code !== ExportResultCode.SUCCESS) { - globalErrorHandler( - result.error ?? - new Error( - `SimpleSpanProcessor: span export failed (status ${result})` - ) - ); - } - }); + coreExport(this._exporter, [span], result => { + if (result.code !== ExportResultCode.SUCCESS) { + globalErrorHandler( + result.error ?? + new Error( + `SimpleSpanProcessor: span export failed (status ${result})` + ) + ); + } }); } From c0a81d04d178af81485e4f0471e254673a6e626d Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Fri, 14 Oct 2022 11:13:02 -0700 Subject: [PATCH 04/15] Lint --- packages/opentelemetry-core/src/Exporter.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-core/src/Exporter.ts b/packages/opentelemetry-core/src/Exporter.ts index b8d10c0c7d..aa8096ee2f 100644 --- a/packages/opentelemetry-core/src/Exporter.ts +++ b/packages/opentelemetry-core/src/Exporter.ts @@ -14,9 +14,9 @@ * limitations under the License. */ -import { context } from "@opentelemetry/api"; -import { ExportResult } from "./ExportResult"; -import { suppressTracing } from "./trace/suppress-tracing"; +import { context } from '@opentelemetry/api'; +import { ExportResult } from './ExportResult'; +import { suppressTracing } from './trace/suppress-tracing'; export interface Exporter { export(arg: T, resultCallback: (result: ExportResult) => void): void; From 86a9c202a3b04fb6e2c01e1d74feeef9e6515f60 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Mon, 17 Oct 2022 09:07:43 -0700 Subject: [PATCH 05/15] Adding internal tsdoc --- packages/opentelemetry-core/src/Exporter.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-core/src/Exporter.ts b/packages/opentelemetry-core/src/Exporter.ts index aa8096ee2f..c7d42b8aa0 100644 --- a/packages/opentelemetry-core/src/Exporter.ts +++ b/packages/opentelemetry-core/src/Exporter.ts @@ -22,7 +22,11 @@ export interface Exporter { export(arg: T, resultCallback: (result: ExportResult) => void): void; } -export function coreExport(exporter: Exporter, arg: T, resultCallback: (result: ExportResult) => void): void { +/** +* @internal +* Shared functionality used by Exporters while exporting data, including suppresion of Traces. +*/ +export function _export(exporter: Exporter, arg: T, resultCallback: (result: ExportResult) => void): void { // prevent downstream exporter calls from generating spans context.with(suppressTracing(context.active()), () => { exporter.export(arg, resultCallback); From 3b9b20b1e11c8a761ec2c522e5b0805d32113c81 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Mon, 17 Oct 2022 09:19:02 -0700 Subject: [PATCH 06/15] Update --- .../src/export/PeriodicExportingMetricReader.ts | 4 ++-- .../src/export/SimpleSpanProcessor.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts index 79e56e35d8..db8ffde437 100644 --- a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts +++ b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts @@ -16,8 +16,8 @@ import * as api from '@opentelemetry/api'; import { + _export, ExportResultCode, - coreExport, globalErrorHandler, unrefTimer } from '@opentelemetry/core'; @@ -87,7 +87,7 @@ export class PeriodicExportingMetricReader extends MetricReader { } return new Promise((resolve, reject) => { - coreExport(this._exporter, resourceMetrics, result => { + _export(this._exporter, resourceMetrics, result => { if (result.code !== ExportResultCode.SUCCESS) { reject( result.error ?? diff --git a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts index ab462ab544..7ec10e9031 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts @@ -16,10 +16,10 @@ import { Context, TraceFlags } from '@opentelemetry/api'; import { + _export, ExportResultCode, globalErrorHandler, - BindOnceFuture, - coreExport, + BindOnceFuture } from '@opentelemetry/core'; import { Span } from '../Span'; import { SpanProcessor } from '../SpanProcessor'; @@ -56,7 +56,7 @@ export class SimpleSpanProcessor implements SpanProcessor { return; } - coreExport(this._exporter, [span], result => { + _export(this._exporter, [span], result => { if (result.code !== ExportResultCode.SUCCESS) { globalErrorHandler( result.error ?? From a5d1291bec987cc34bc81eebf34164980254692b Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Mon, 17 Oct 2022 10:53:57 -0700 Subject: [PATCH 07/15] Moved to internal folder --- packages/opentelemetry-core/src/index.ts | 2 +- .../src/{Exporter.ts => internal/exporter.ts} | 7 ++- .../test/internal/exporter.test.ts | 46 +++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) rename packages/opentelemetry-core/src/{Exporter.ts => internal/exporter.ts} (83%) create mode 100644 packages/opentelemetry-core/test/internal/exporter.test.ts diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index 2caddafe73..8ef339fcb1 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -21,10 +21,10 @@ export * from './common/global-error-handler'; export * from './common/logging-error-handler'; export * from './common/time'; export * from './common/types'; -export * from './Exporter'; export * from './ExportResult'; export * from './version'; export * as baggageUtils from './baggage/utils'; +export * from './internal/exporter'; export * from './platform'; export * from './propagation/composite'; export * from './trace/W3CTraceContextPropagator'; diff --git a/packages/opentelemetry-core/src/Exporter.ts b/packages/opentelemetry-core/src/internal/exporter.ts similarity index 83% rename from packages/opentelemetry-core/src/Exporter.ts rename to packages/opentelemetry-core/src/internal/exporter.ts index c7d42b8aa0..0841e19cf7 100644 --- a/packages/opentelemetry-core/src/Exporter.ts +++ b/packages/opentelemetry-core/src/internal/exporter.ts @@ -15,16 +15,15 @@ */ import { context } from '@opentelemetry/api'; -import { ExportResult } from './ExportResult'; -import { suppressTracing } from './trace/suppress-tracing'; +import { ExportResult } from '../ExportResult'; +import { suppressTracing } from '../trace/suppress-tracing'; export interface Exporter { export(arg: T, resultCallback: (result: ExportResult) => void): void; } /** -* @internal -* Shared functionality used by Exporters while exporting data, including suppresion of Traces. +* Internal shared functionality used by Exporters while exporting data, including suppresion of Traces. */ export function _export(exporter: Exporter, arg: T, resultCallback: (result: ExportResult) => void): void { // prevent downstream exporter calls from generating spans diff --git a/packages/opentelemetry-core/test/internal/exporter.test.ts b/packages/opentelemetry-core/test/internal/exporter.test.ts new file mode 100644 index 0000000000..39ceeb47d5 --- /dev/null +++ b/packages/opentelemetry-core/test/internal/exporter.test.ts @@ -0,0 +1,46 @@ +/* + * 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 * as assert from 'assert'; +import * as sinon from 'sinon'; +import { ExportResult, ExportResultCode } from '../../src'; +import * as suppress from '../../src/trace/suppress-tracing'; +import { _export } from '../../src/internal/exporter'; + +describe('exporter', () => { + const sandbox = sinon.createSandbox(); + + afterEach(() => { + sandbox.restore(); + }); + + class TestExporter { + export(arg: any, resultCallback: (result: ExportResult) => void) { + resultCallback({ code: ExportResultCode.SUCCESS }); + } + } + + it('_export should suppress tracing', done => { + const suppressSpy = sandbox.spy(suppress, "suppressTracing"); + const exporter = new TestExporter(); + let data = ["Test1", "Test2"]; + + _export(exporter, data, (result: ExportResult) => { + assert.ok(suppressSpy.calledOnce); + done(); + }); + }); +}); From 7cf89d52edeeddcfd4fe23e8265cded47822d749 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Mon, 17 Oct 2022 10:59:21 -0700 Subject: [PATCH 08/15] Add internal namespace --- .../src/export/PeriodicExportingMetricReader.ts | 4 ++-- packages/opentelemetry-core/src/index.ts | 5 ++++- packages/opentelemetry-core/test/internal/exporter.test.ts | 6 ++---- .../src/export/SimpleSpanProcessor.ts | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts index db8ffde437..089e083a76 100644 --- a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts +++ b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts @@ -16,7 +16,7 @@ import * as api from '@opentelemetry/api'; import { - _export, + internal, ExportResultCode, globalErrorHandler, unrefTimer @@ -87,7 +87,7 @@ export class PeriodicExportingMetricReader extends MetricReader { } return new Promise((resolve, reject) => { - _export(this._exporter, resourceMetrics, result => { + internal._export(this._exporter, resourceMetrics, result => { if (result.code !== ExportResultCode.SUCCESS) { reject( result.error ?? diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index 8ef339fcb1..a926353704 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -24,7 +24,6 @@ export * from './common/types'; export * from './ExportResult'; export * from './version'; export * as baggageUtils from './baggage/utils'; -export * from './internal/exporter'; export * from './platform'; export * from './propagation/composite'; export * from './trace/W3CTraceContextPropagator'; @@ -43,3 +42,7 @@ export * from './utils/url'; export * from './utils/wrap'; export * from './utils/callback'; export * from './version'; +import { _export } from './internal/exporter'; +export const internal = { + _export +}; diff --git a/packages/opentelemetry-core/test/internal/exporter.test.ts b/packages/opentelemetry-core/test/internal/exporter.test.ts index 39ceeb47d5..dadeed8ef3 100644 --- a/packages/opentelemetry-core/test/internal/exporter.test.ts +++ b/packages/opentelemetry-core/test/internal/exporter.test.ts @@ -34,11 +34,9 @@ describe('exporter', () => { } it('_export should suppress tracing', done => { - const suppressSpy = sandbox.spy(suppress, "suppressTracing"); + const suppressSpy = sandbox.spy(suppress, 'suppressTracing'); const exporter = new TestExporter(); - let data = ["Test1", "Test2"]; - - _export(exporter, data, (result: ExportResult) => { + _export(exporter, ['Test1'], (result: ExportResult) => { assert.ok(suppressSpy.calledOnce); done(); }); diff --git a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts index 7ec10e9031..6a53a43a71 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts @@ -16,7 +16,7 @@ import { Context, TraceFlags } from '@opentelemetry/api'; import { - _export, + internal, ExportResultCode, globalErrorHandler, BindOnceFuture @@ -56,7 +56,7 @@ export class SimpleSpanProcessor implements SpanProcessor { return; } - _export(this._exporter, [span], result => { + internal._export(this._exporter, [span], result => { if (result.code !== ExportResultCode.SUCCESS) { globalErrorHandler( result.error ?? From aeb33e23eb04f04485403d5cea3a1c8301b301e9 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Mon, 17 Oct 2022 11:05:21 -0700 Subject: [PATCH 09/15] Lint --- packages/opentelemetry-core/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index a926353704..6c0834fe0f 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -44,5 +44,5 @@ export * from './utils/callback'; export * from './version'; import { _export } from './internal/exporter'; export const internal = { - _export + _export }; From 799add6bb2e392a37770a11ffc48c0a8378260f4 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Tue, 18 Oct 2022 10:38:43 -0700 Subject: [PATCH 10/15] Return promise in _export --- .../export/PeriodicExportingMetricReader.ts | 25 +++++++++---------- .../src/internal/exporter.ts | 10 +++++--- .../test/internal/exporter.test.ts | 9 +++---- .../src/export/SimpleSpanProcessor.ts | 5 ++-- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts index 089e083a76..c6202fbc67 100644 --- a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts +++ b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts @@ -86,19 +86,18 @@ export class PeriodicExportingMetricReader extends MetricReader { api.diag.error('PeriodicExportingMetricReader: metrics collection errors', ...errors); } - return new Promise((resolve, reject) => { - internal._export(this._exporter, resourceMetrics, result => { - if (result.code !== ExportResultCode.SUCCESS) { - reject( - result.error ?? - new Error( - `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` - ) - ); - } else { - resolve(); - } - }); + return new Promise(async (resolve, reject) => { + const result = await internal._export(this._exporter, resourceMetrics); + if (result.code !== ExportResultCode.SUCCESS) { + reject( + result.error ?? + new Error( + `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` + ) + ); + } else { + resolve(); + } }); } diff --git a/packages/opentelemetry-core/src/internal/exporter.ts b/packages/opentelemetry-core/src/internal/exporter.ts index 0841e19cf7..8b81579e1b 100644 --- a/packages/opentelemetry-core/src/internal/exporter.ts +++ b/packages/opentelemetry-core/src/internal/exporter.ts @@ -25,9 +25,11 @@ export interface Exporter { /** * Internal shared functionality used by Exporters while exporting data, including suppresion of Traces. */ -export function _export(exporter: Exporter, arg: T, resultCallback: (result: ExportResult) => void): void { - // prevent downstream exporter calls from generating spans - context.with(suppressTracing(context.active()), () => { - exporter.export(arg, resultCallback); +export function _export(exporter: Exporter, arg: T): Promise { + return new Promise((resolve) => { + // prevent downstream exporter calls from generating spans + context.with(suppressTracing(context.active()), () => { + exporter.export(arg, (result: ExportResult) => { resolve(result) }); + }); }); } diff --git a/packages/opentelemetry-core/test/internal/exporter.test.ts b/packages/opentelemetry-core/test/internal/exporter.test.ts index dadeed8ef3..c1e0bc2842 100644 --- a/packages/opentelemetry-core/test/internal/exporter.test.ts +++ b/packages/opentelemetry-core/test/internal/exporter.test.ts @@ -33,12 +33,11 @@ describe('exporter', () => { } } - it('_export should suppress tracing', done => { + it('_export should suppress tracing', async () => { const suppressSpy = sandbox.spy(suppress, 'suppressTracing'); const exporter = new TestExporter(); - _export(exporter, ['Test1'], (result: ExportResult) => { - assert.ok(suppressSpy.calledOnce); - done(); - }); + let result = await _export(exporter, ['Test1']); + assert.strictEqual(result.code, ExportResultCode.SUCCESS); + assert.ok(suppressSpy.calledOnce); }); }); diff --git a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts index 6a53a43a71..0233b4b4d1 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts @@ -19,7 +19,8 @@ import { internal, ExportResultCode, globalErrorHandler, - BindOnceFuture + BindOnceFuture, + ExportResult } from '@opentelemetry/core'; import { Span } from '../Span'; import { SpanProcessor } from '../SpanProcessor'; @@ -56,7 +57,7 @@ export class SimpleSpanProcessor implements SpanProcessor { return; } - internal._export(this._exporter, [span], result => { + internal._export(this._exporter, [span]).then((result: ExportResult) => { if (result.code !== ExportResultCode.SUCCESS) { globalErrorHandler( result.error ?? From 9a55db54e53eeefe4d337f21a086b97a56a37425 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Tue, 18 Oct 2022 10:46:17 -0700 Subject: [PATCH 11/15] lint --- packages/opentelemetry-core/src/internal/exporter.ts | 6 ++++-- packages/opentelemetry-core/test/internal/exporter.test.ts | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-core/src/internal/exporter.ts b/packages/opentelemetry-core/src/internal/exporter.ts index 8b81579e1b..dec40eeca0 100644 --- a/packages/opentelemetry-core/src/internal/exporter.ts +++ b/packages/opentelemetry-core/src/internal/exporter.ts @@ -26,10 +26,12 @@ export interface Exporter { * Internal shared functionality used by Exporters while exporting data, including suppresion of Traces. */ export function _export(exporter: Exporter, arg: T): Promise { - return new Promise((resolve) => { + return new Promise(resolve => { // prevent downstream exporter calls from generating spans context.with(suppressTracing(context.active()), () => { - exporter.export(arg, (result: ExportResult) => { resolve(result) }); + exporter.export(arg, (result: ExportResult) => { + resolve(result); + }); }); }); } diff --git a/packages/opentelemetry-core/test/internal/exporter.test.ts b/packages/opentelemetry-core/test/internal/exporter.test.ts index c1e0bc2842..1ca256d659 100644 --- a/packages/opentelemetry-core/test/internal/exporter.test.ts +++ b/packages/opentelemetry-core/test/internal/exporter.test.ts @@ -36,7 +36,7 @@ describe('exporter', () => { it('_export should suppress tracing', async () => { const suppressSpy = sandbox.spy(suppress, 'suppressTracing'); const exporter = new TestExporter(); - let result = await _export(exporter, ['Test1']); + const result = await _export(exporter, ['Test1']); assert.strictEqual(result.code, ExportResultCode.SUCCESS); assert.ok(suppressSpy.calledOnce); }); From b5427cee398df2548042a995a00c4b366225e9b5 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Tue, 18 Oct 2022 10:57:29 -0700 Subject: [PATCH 12/15] Addressing comments --- .../export/PeriodicExportingMetricReader.ts | 18 ++++++++---------- .../src/export/SimpleSpanProcessor.ts | 2 ++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts index c6202fbc67..47e26dbb58 100644 --- a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts +++ b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts @@ -86,19 +86,17 @@ export class PeriodicExportingMetricReader extends MetricReader { api.diag.error('PeriodicExportingMetricReader: metrics collection errors', ...errors); } - return new Promise(async (resolve, reject) => { + try { const result = await internal._export(this._exporter, resourceMetrics); if (result.code !== ExportResultCode.SUCCESS) { - reject( - result.error ?? - new Error( - `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` - ) - ); - } else { - resolve(); + throw new Error( + `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` + ) } - }); + } + catch (err) { + throw err; + } } protected override onInitialized(): void { diff --git a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts index 0233b4b4d1..66a60d43b3 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts @@ -66,6 +66,8 @@ export class SimpleSpanProcessor implements SpanProcessor { ) ); } + }).catch((error) => { + globalErrorHandler(error); }); } From be123f15c6b3a361d0846f2c71a4440040353aa6 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Tue, 18 Oct 2022 11:00:05 -0700 Subject: [PATCH 13/15] Adding internal tag back --- packages/opentelemetry-core/src/internal/exporter.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-core/src/internal/exporter.ts b/packages/opentelemetry-core/src/internal/exporter.ts index dec40eeca0..a489b35eac 100644 --- a/packages/opentelemetry-core/src/internal/exporter.ts +++ b/packages/opentelemetry-core/src/internal/exporter.ts @@ -23,7 +23,8 @@ export interface Exporter { } /** -* Internal shared functionality used by Exporters while exporting data, including suppresion of Traces. +* @internal +* Shared functionality used by Exporters while exporting data, including suppresion of Traces. */ export function _export(exporter: Exporter, arg: T): Promise { return new Promise(resolve => { From 17948bad8f337092a8de3881596cafd327e67c19 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Tue, 18 Oct 2022 11:06:23 -0700 Subject: [PATCH 14/15] Lint --- .../src/export/SimpleSpanProcessor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts index 66a60d43b3..a510ad02a6 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/SimpleSpanProcessor.ts @@ -66,7 +66,7 @@ export class SimpleSpanProcessor implements SpanProcessor { ) ); } - }).catch((error) => { + }).catch(error => { globalErrorHandler(error); }); } From 9d92bb136ce604e71cd8006648ff613da8b22f6a Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Tue, 18 Oct 2022 11:12:29 -0700 Subject: [PATCH 15/15] lint --- .../src/export/PeriodicExportingMetricReader.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts index 47e26dbb58..2a686181cc 100644 --- a/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts +++ b/experimental/packages/opentelemetry-sdk-metrics/src/export/PeriodicExportingMetricReader.ts @@ -86,16 +86,11 @@ export class PeriodicExportingMetricReader extends MetricReader { api.diag.error('PeriodicExportingMetricReader: metrics collection errors', ...errors); } - try { - const result = await internal._export(this._exporter, resourceMetrics); - if (result.code !== ExportResultCode.SUCCESS) { - throw new Error( - `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` - ) - } - } - catch (err) { - throw err; + const result = await internal._export(this._exporter, resourceMetrics); + if (result.code !== ExportResultCode.SUCCESS) { + throw new Error( + `PeriodicExportingMetricReader: metrics export failed (error ${result.error})` + ); } }