From 18be6b9863202be417b9ec63b0b69f67e719c91d Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Fri, 31 Jul 2020 17:19:40 +0200 Subject: [PATCH 1/9] feat: adding possibility of recording exception --- .../opentelemetry-api/src/common/Exception.ts | 22 +++++ packages/opentelemetry-api/src/index.ts | 1 + .../opentelemetry-api/src/trace/NoopSpan.ts | 4 + packages/opentelemetry-api/src/trace/span.ts | 9 ++ .../opentelemetry-plugin-http/package.json | 3 +- .../package.json | 3 +- .../src/trace/exception.ts | 23 +++++ .../src/trace/index.ts | 5 +- packages/opentelemetry-tracing/package.json | 3 +- packages/opentelemetry-tracing/src/Span.ts | 32 +++++++ .../opentelemetry-tracing/test/Span.test.ts | 84 +++++++++++++++++++ 11 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 packages/opentelemetry-api/src/common/Exception.ts create mode 100644 packages/opentelemetry-semantic-conventions/src/trace/exception.ts diff --git a/packages/opentelemetry-api/src/common/Exception.ts b/packages/opentelemetry-api/src/common/Exception.ts new file mode 100644 index 0000000000..d6ebaa57fe --- /dev/null +++ b/packages/opentelemetry-api/src/common/Exception.ts @@ -0,0 +1,22 @@ +/* + * 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. + */ + +/** + * Defines Exception. + * + * Error or string + */ +export type Exception = Partial | string; diff --git a/packages/opentelemetry-api/src/index.ts b/packages/opentelemetry-api/src/index.ts index f9bcf40664..68dcb00029 100644 --- a/packages/opentelemetry-api/src/index.ts +++ b/packages/opentelemetry-api/src/index.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +export * from './common/Exception'; export * from './common/Logger'; export * from './common/Time'; export * from './context/propagation/getter'; diff --git a/packages/opentelemetry-api/src/trace/NoopSpan.ts b/packages/opentelemetry-api/src/trace/NoopSpan.ts index a2a3bcef9f..a2b304a73d 100644 --- a/packages/opentelemetry-api/src/trace/NoopSpan.ts +++ b/packages/opentelemetry-api/src/trace/NoopSpan.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { Exception } from '../common/Exception'; import { TimeInput } from '../common/Time'; import { Attributes } from './attributes'; import { Span } from './span'; @@ -76,6 +77,9 @@ export class NoopSpan implements Span { isRecording(): boolean { return false; } + + // By default does nothing + recordException(exception: Exception, endTime?: TimeInput): void {} } export const NOOP_SPAN = new NoopSpan(); diff --git a/packages/opentelemetry-api/src/trace/span.ts b/packages/opentelemetry-api/src/trace/span.ts index 13124fff3c..966cd1a95d 100644 --- a/packages/opentelemetry-api/src/trace/span.ts +++ b/packages/opentelemetry-api/src/trace/span.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { Exception } from '../common/Exception'; import { Attributes } from './attributes'; import { SpanContext } from './span_context'; import { Status } from './status'; @@ -114,4 +115,12 @@ export interface Span { * with the `AddEvent` operation and attributes using `setAttributes`. */ isRecording(): boolean; + + /** + * Sets exception as a span event + * @param exception the exception the only accepted values are string or Error + * @param [time] the time to set as Span's event time. If not provided, + * use the current time. + */ + recordException(exception: Exception, time?: TimeInput): void; } diff --git a/packages/opentelemetry-plugin-http/package.json b/packages/opentelemetry-plugin-http/package.json index 1d60be118c..dc96f6430a 100644 --- a/packages/opentelemetry-plugin-http/package.json +++ b/packages/opentelemetry-plugin-http/package.json @@ -15,7 +15,8 @@ "precompile": "tsc --version", "version:update": "node ../../scripts/version-update.js", "compile": "npm run version:update && tsc -p .", - "prepare": "npm run compile" + "prepare": "npm run compile", + "watch": "tsc -w" }, "keywords": [ "opentelemetry", diff --git a/packages/opentelemetry-semantic-conventions/package.json b/packages/opentelemetry-semantic-conventions/package.json index b4c5934f6d..56eedd4b4d 100644 --- a/packages/opentelemetry-semantic-conventions/package.json +++ b/packages/opentelemetry-semantic-conventions/package.json @@ -15,7 +15,8 @@ "precompile": "tsc --version", "version:update": "node ../../scripts/version-update.js", "compile": "npm run version:update && tsc -p .", - "prepare": "npm run compile" + "prepare": "npm run compile", + "watch": "tsc -w" }, "keywords": [ "opentelemetry", diff --git a/packages/opentelemetry-semantic-conventions/src/trace/exception.ts b/packages/opentelemetry-semantic-conventions/src/trace/exception.ts new file mode 100644 index 0000000000..f0f5214268 --- /dev/null +++ b/packages/opentelemetry-semantic-conventions/src/trace/exception.ts @@ -0,0 +1,23 @@ +/* + * 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. + */ + +export const ExceptionAttribute = { + MESSAGE: 'exception.message', + STACKTRACE: 'exception.stacktrace', + TYPE: 'exception.type', +} + +export const ExceptionEventName = 'exception'; diff --git a/packages/opentelemetry-semantic-conventions/src/trace/index.ts b/packages/opentelemetry-semantic-conventions/src/trace/index.ts index a923ac0a36..9d4087963b 100644 --- a/packages/opentelemetry-semantic-conventions/src/trace/index.ts +++ b/packages/opentelemetry-semantic-conventions/src/trace/index.ts @@ -14,7 +14,8 @@ * limitations under the License. */ +export * from './database'; +export * from './exception'; export * from './general'; -export * from './rpc'; export * from './http'; -export * from './database'; +export * from './rpc'; diff --git a/packages/opentelemetry-tracing/package.json b/packages/opentelemetry-tracing/package.json index 2684d860d2..1b5e40176c 100644 --- a/packages/opentelemetry-tracing/package.json +++ b/packages/opentelemetry-tracing/package.json @@ -77,6 +77,7 @@ "@opentelemetry/api": "^0.10.1", "@opentelemetry/context-base": "^0.10.1", "@opentelemetry/core": "^0.10.1", - "@opentelemetry/resources": "^0.10.1" + "@opentelemetry/resources": "^0.10.1", + "@opentelemetry/semantic-conventions": "^0.10.1" } } diff --git a/packages/opentelemetry-tracing/src/Span.ts b/packages/opentelemetry-tracing/src/Span.ts index 6272729d2d..c13b75085e 100644 --- a/packages/opentelemetry-tracing/src/Span.ts +++ b/packages/opentelemetry-tracing/src/Span.ts @@ -23,6 +23,10 @@ import { timeInputToHrTime, } from '@opentelemetry/core'; import { Resource } from '@opentelemetry/resources'; +import { + ExceptionAttribute, + ExceptionEventName, +} from '@opentelemetry/semantic-conventions'; import { ReadableSpan } from './export/ReadableSpan'; import { Tracer } from './Tracer'; import { SpanProcessor } from './SpanProcessor'; @@ -178,6 +182,34 @@ export class Span implements api.Span, ReadableSpan { return true; } + recordException(exception: api.Exception, time: api.TimeInput = hrTime()) { + const attributes: api.Attributes = {}; + if (!exception) { + return; + } + if (typeof exception === 'string') { + attributes[ExceptionAttribute.MESSAGE] = exception; + } else { + if (exception.name) { + attributes[ExceptionAttribute.TYPE] = exception.name; + } + if (exception.message) { + attributes[ExceptionAttribute.MESSAGE] = exception.message; + } + if (exception.stack) { + attributes[ExceptionAttribute.STACKTRACE] = exception.stack; + } + } + + // these are minimum requirements from spec + if ( + attributes[ExceptionAttribute.TYPE] || + attributes[ExceptionAttribute.MESSAGE] + ) { + this.addEvent(ExceptionEventName, attributes as api.Attributes, time); + } + } + get duration(): api.HrTime { return this._duration; } diff --git a/packages/opentelemetry-tracing/test/Span.test.ts b/packages/opentelemetry-tracing/test/Span.test.ts index 6f1494518f..95a561ebe0 100644 --- a/packages/opentelemetry-tracing/test/Span.test.ts +++ b/packages/opentelemetry-tracing/test/Span.test.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { ExceptionAttribute } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; import { SpanKind, @@ -21,6 +22,7 @@ import { TraceFlags, SpanContext, LinkContext, + Exception, } from '@opentelemetry/api'; import { BasicTracerProvider, Span } from '../src'; import { @@ -357,4 +359,86 @@ describe('Span', () => { span.end(); assert.strictEqual(span.ended, true); }); + + describe('recordException', () => { + const invalidExceptions: any[] = [ + 1, + null, + undefined, + { foo: 'bar' }, + { stack: 'bar' }, + ['a', 'b', 'c'], + ]; + + invalidExceptions.forEach(key => { + describe(`when exception is (${JSON.stringify(key)})`, () => { + it('should NOT record an exception', () => { + const span = new Span(tracer, name, spanContext, SpanKind.CLIENT); + assert.strictEqual(span.events.length, 0); + span.recordException(key); + assert.strictEqual(span.events.length, 0); + }); + }); + }); + + describe('when exception type is "string"', () => { + let error: Exception; + beforeEach(() => { + error = 'boom'; + }); + it('should record an exception', () => { + const span = new Span(tracer, name, spanContext, SpanKind.CLIENT); + assert.strictEqual(span.events.length, 0); + span.recordException(error); + + const event = span.events[0]; + assert.strictEqual(event.name, 'exception'); + assert.deepStrictEqual(event.attributes, { + 'exception.message': 'boom', + }); + assert.ok(event.time[0] > 0); + }); + }); + + describe('when exception type is "Error"', () => { + let error: Exception; + beforeEach(() => { + try { + throw new Error('boom'); + } catch (e) { + error = e; + } + }); + it('should record an exception when type is "Error"', () => { + const span = new Span(tracer, name, spanContext, SpanKind.CLIENT); + assert.strictEqual(span.events.length, 0); + span.recordException(error); + + const event = span.events[0]; + assert.ok(event.time[0] > 0); + assert.strictEqual(event.name, 'exception'); + + assert.ok(event.attributes); + + const type = event.attributes[ExceptionAttribute.TYPE]; + const message = event.attributes[ExceptionAttribute.MESSAGE]; + const stacktrace = String( + event.attributes[ExceptionAttribute.STACKTRACE] + ); + assert.strictEqual(type, 'Error'); + assert.strictEqual(message, 'boom'); + assert.ok(stacktrace.indexOf('Error: boom') >= 0); + }); + }); + + describe('when time is provided', () => { + it('should record an error with provided time', () => { + const span = new Span(tracer, name, spanContext, SpanKind.CLIENT); + assert.strictEqual(span.events.length, 0); + span.recordException('boom', [0, 123]); + const event = span.events[0]; + assert.deepStrictEqual(event.time, [0, 123]); + }); + }); + }); }); From f792853dc945248263c04b82a5124a97cc9c6509 Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Fri, 31 Jul 2020 17:29:08 +0200 Subject: [PATCH 2/9] chore: copy --- packages/opentelemetry-tracing/test/Span.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-tracing/test/Span.test.ts b/packages/opentelemetry-tracing/test/Span.test.ts index 95a561ebe0..ecb302b426 100644 --- a/packages/opentelemetry-tracing/test/Span.test.ts +++ b/packages/opentelemetry-tracing/test/Span.test.ts @@ -409,7 +409,7 @@ describe('Span', () => { error = e; } }); - it('should record an exception when type is "Error"', () => { + it('should record an exception', () => { const span = new Span(tracer, name, spanContext, SpanKind.CLIENT); assert.strictEqual(span.events.length, 0); span.recordException(error); From fe7d5d65ed17918936109962e4ff45e64918a27d Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Fri, 31 Jul 2020 17:30:26 +0200 Subject: [PATCH 3/9] chore: copy --- packages/opentelemetry-tracing/test/Span.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-tracing/test/Span.test.ts b/packages/opentelemetry-tracing/test/Span.test.ts index ecb302b426..69fd6a6a23 100644 --- a/packages/opentelemetry-tracing/test/Span.test.ts +++ b/packages/opentelemetry-tracing/test/Span.test.ts @@ -432,7 +432,7 @@ describe('Span', () => { }); describe('when time is provided', () => { - it('should record an error with provided time', () => { + it('should record an exception with provided time', () => { const span = new Span(tracer, name, spanContext, SpanKind.CLIENT); assert.strictEqual(span.events.length, 0); span.recordException('boom', [0, 123]); From cdfd8d79938f6837eca432e838e40d3d4adea993 Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Fri, 31 Jul 2020 17:31:46 +0200 Subject: [PATCH 4/9] chore: linting --- .../opentelemetry-semantic-conventions/src/trace/exception.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-semantic-conventions/src/trace/exception.ts b/packages/opentelemetry-semantic-conventions/src/trace/exception.ts index f0f5214268..cf7dc596be 100644 --- a/packages/opentelemetry-semantic-conventions/src/trace/exception.ts +++ b/packages/opentelemetry-semantic-conventions/src/trace/exception.ts @@ -18,6 +18,6 @@ export const ExceptionAttribute = { MESSAGE: 'exception.message', STACKTRACE: 'exception.stacktrace', TYPE: 'exception.type', -} +}; export const ExceptionEventName = 'exception'; From b6f9a4ddf3ef7727d5f2317ff1516f2c11b4dbac Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Fri, 31 Jul 2020 18:08:34 +0200 Subject: [PATCH 5/9] chore: reviews --- packages/opentelemetry-api/src/trace/NoopSpan.ts | 2 +- packages/opentelemetry-tracing/src/Span.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry-api/src/trace/NoopSpan.ts b/packages/opentelemetry-api/src/trace/NoopSpan.ts index a2b304a73d..12fd8fb973 100644 --- a/packages/opentelemetry-api/src/trace/NoopSpan.ts +++ b/packages/opentelemetry-api/src/trace/NoopSpan.ts @@ -79,7 +79,7 @@ export class NoopSpan implements Span { } // By default does nothing - recordException(exception: Exception, endTime?: TimeInput): void {} + recordException(exception: Exception, time?: TimeInput): void {} } export const NOOP_SPAN = new NoopSpan(); diff --git a/packages/opentelemetry-tracing/src/Span.ts b/packages/opentelemetry-tracing/src/Span.ts index c13b75085e..f4510abdab 100644 --- a/packages/opentelemetry-tracing/src/Span.ts +++ b/packages/opentelemetry-tracing/src/Span.ts @@ -207,6 +207,8 @@ export class Span implements api.Span, ReadableSpan { attributes[ExceptionAttribute.MESSAGE] ) { this.addEvent(ExceptionEventName, attributes as api.Attributes, time); + } else { + this._logger.error(`Failed to record an exception ${exception}`); } } From 3ed3c40cb3acdd75a0bea8c651dd28dc0b3c9410 Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Tue, 4 Aug 2020 00:51:28 +0200 Subject: [PATCH 6/9] chore: updating exception type --- .../opentelemetry-api/src/common/Exception.ts | 16 ++++++++++++++-- packages/opentelemetry-tracing/src/Span.ts | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-api/src/common/Exception.ts b/packages/opentelemetry-api/src/common/Exception.ts index d6ebaa57fe..4e301e05f5 100644 --- a/packages/opentelemetry-api/src/common/Exception.ts +++ b/packages/opentelemetry-api/src/common/Exception.ts @@ -14,9 +14,21 @@ * limitations under the License. */ +interface ExceptionWithType { + message?: string; + name: string; + stack?: string; +} + +interface ExceptionWithMessage { + message: string; + name?: string; + stack?: string; +} + /** * Defines Exception. * - * Error or string + * string or an object with one of (message or name) and optional stack */ -export type Exception = Partial | string; +export type Exception = ExceptionWithType | ExceptionWithMessage | string; diff --git a/packages/opentelemetry-tracing/src/Span.ts b/packages/opentelemetry-tracing/src/Span.ts index f4510abdab..71f9072d9d 100644 --- a/packages/opentelemetry-tracing/src/Span.ts +++ b/packages/opentelemetry-tracing/src/Span.ts @@ -208,7 +208,7 @@ export class Span implements api.Span, ReadableSpan { ) { this.addEvent(ExceptionEventName, attributes as api.Attributes, time); } else { - this._logger.error(`Failed to record an exception ${exception}`); + this._logger.warn(`Failed to record an exception ${exception}`); } } From 391ae35f58f5b2aeb6f2fae904f9e02206870f7f Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Tue, 4 Aug 2020 02:01:14 +0200 Subject: [PATCH 7/9] chore: reviews --- .../opentelemetry-api/src/common/Exception.ts | 21 +++++-- packages/opentelemetry-tracing/src/Span.ts | 4 +- .../opentelemetry-tracing/test/Span.test.ts | 61 ++++++++++--------- 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/packages/opentelemetry-api/src/common/Exception.ts b/packages/opentelemetry-api/src/common/Exception.ts index 4e301e05f5..0fa98e741a 100644 --- a/packages/opentelemetry-api/src/common/Exception.ts +++ b/packages/opentelemetry-api/src/common/Exception.ts @@ -14,21 +14,34 @@ * limitations under the License. */ -interface ExceptionWithType { +interface ExceptionWithCode { + code: string; + name?: string; message?: string; - name: string; stack?: string; } interface ExceptionWithMessage { + code?: string; message: string; name?: string; stack?: string; } +interface ExceptionWithName { + code?: string; + message?: string; + name: string; + stack?: string; +} + /** * Defines Exception. * - * string or an object with one of (message or name) and optional stack + * string or an object with one of (message or name or code) and optional stack */ -export type Exception = ExceptionWithType | ExceptionWithMessage | string; +export type Exception = + | ExceptionWithCode + | ExceptionWithMessage + | ExceptionWithName + | string; diff --git a/packages/opentelemetry-tracing/src/Span.ts b/packages/opentelemetry-tracing/src/Span.ts index 71f9072d9d..e35e84556d 100644 --- a/packages/opentelemetry-tracing/src/Span.ts +++ b/packages/opentelemetry-tracing/src/Span.ts @@ -190,7 +190,9 @@ export class Span implements api.Span, ReadableSpan { if (typeof exception === 'string') { attributes[ExceptionAttribute.MESSAGE] = exception; } else { - if (exception.name) { + if (exception.code) { + attributes[ExceptionAttribute.TYPE] = exception.code; + } else if (exception.name) { attributes[ExceptionAttribute.TYPE] = exception.name; } if (exception.message) { diff --git a/packages/opentelemetry-tracing/test/Span.test.ts b/packages/opentelemetry-tracing/test/Span.test.ts index 69fd6a6a23..a86222e63b 100644 --- a/packages/opentelemetry-tracing/test/Span.test.ts +++ b/packages/opentelemetry-tracing/test/Span.test.ts @@ -400,34 +400,39 @@ describe('Span', () => { }); }); - describe('when exception type is "Error"', () => { - let error: Exception; - beforeEach(() => { - try { - throw new Error('boom'); - } catch (e) { - error = e; - } - }); - it('should record an exception', () => { - const span = new Span(tracer, name, spanContext, SpanKind.CLIENT); - assert.strictEqual(span.events.length, 0); - span.recordException(error); - - const event = span.events[0]; - assert.ok(event.time[0] > 0); - assert.strictEqual(event.name, 'exception'); - - assert.ok(event.attributes); - - const type = event.attributes[ExceptionAttribute.TYPE]; - const message = event.attributes[ExceptionAttribute.MESSAGE]; - const stacktrace = String( - event.attributes[ExceptionAttribute.STACKTRACE] - ); - assert.strictEqual(type, 'Error'); - assert.strictEqual(message, 'boom'); - assert.ok(stacktrace.indexOf('Error: boom') >= 0); + const errorsObj = [ + { + description: 'code', + obj: { code: 'Error', message: 'boom', stack: 'bar' }, + }, + { + description: 'name', + obj: { name: 'Error', message: 'boom', stack: 'bar' }, + }, + ]; + errorsObj.forEach(errorObj => { + describe(`when exception type is an object with ${errorObj.description}`, () => { + const error: Exception = errorObj.obj; + it('should record an exception', () => { + const span = new Span(tracer, name, spanContext, SpanKind.CLIENT); + assert.strictEqual(span.events.length, 0); + span.recordException(error); + + const event = span.events[0]; + assert.ok(event.time[0] > 0); + assert.strictEqual(event.name, 'exception'); + + assert.ok(event.attributes); + + const type = event.attributes[ExceptionAttribute.TYPE]; + const message = event.attributes[ExceptionAttribute.MESSAGE]; + const stacktrace = String( + event.attributes[ExceptionAttribute.STACKTRACE] + ); + assert.strictEqual(type, 'Error'); + assert.strictEqual(message, 'boom'); + assert.strictEqual(stacktrace, 'bar'); + }); }); }); From 7b39ec2e3c0f0bd1948ce7cb20d923aa0aee9f5c Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Tue, 4 Aug 2020 02:02:42 +0200 Subject: [PATCH 8/9] chore: reviews --- packages/opentelemetry-tracing/src/Span.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/opentelemetry-tracing/src/Span.ts b/packages/opentelemetry-tracing/src/Span.ts index e35e84556d..b8117b0195 100644 --- a/packages/opentelemetry-tracing/src/Span.ts +++ b/packages/opentelemetry-tracing/src/Span.ts @@ -184,12 +184,9 @@ export class Span implements api.Span, ReadableSpan { recordException(exception: api.Exception, time: api.TimeInput = hrTime()) { const attributes: api.Attributes = {}; - if (!exception) { - return; - } if (typeof exception === 'string') { attributes[ExceptionAttribute.MESSAGE] = exception; - } else { + } else if (exception) { if (exception.code) { attributes[ExceptionAttribute.TYPE] = exception.code; } else if (exception.name) { From 6d7daa106d5d504bc9cf4bd5cda3b591b57a24cb Mon Sep 17 00:00:00 2001 From: Bartlomiej Obecny Date: Mon, 24 Aug 2020 20:34:22 +0200 Subject: [PATCH 9/9] chore: fixing test when waiting for files to be loaded --- .../test/CollectorMetricExporter.test.ts | 20 +++++++++---------- .../test/CollectorTraceExporter.test.ts | 20 +++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts b/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts index c6c8cf958b..83f3f7a101 100644 --- a/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts +++ b/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts @@ -50,6 +50,9 @@ const mockResError = { statusCode: 400, }; +// send is lazy loading file so need to wait a bit +const waitTimeMS = 20; + describe('CollectorMetricExporter - node with proto over http', () => { let collectorExporter: CollectorMetricExporter; let collectorExporterConfig: collectorTypes.CollectorExporterConfigBase; @@ -57,7 +60,7 @@ describe('CollectorMetricExporter - node with proto over http', () => { let spyWrite: sinon.SinonSpy; let metrics: MetricRecord[]; describe('export', () => { - beforeEach(done => { + beforeEach(() => { spyRequest = sinon.stub(http, 'request').returns(fakeRequest as any); spyWrite = sinon.stub(fakeRequest, 'write'); collectorExporterConfig = { @@ -86,11 +89,6 @@ describe('CollectorMetricExporter - node with proto over http', () => { metrics[2].aggregator.update(7); metrics[2].aggregator.update(14); metrics[3].aggregator.update(5); - - // due to lazy loading ensure to wait to next tick - setImmediate(() => { - done(); - }); }); afterEach(() => { spyRequest.restore(); @@ -108,7 +106,7 @@ describe('CollectorMetricExporter - node with proto over http', () => { assert.strictEqual(options.method, 'POST'); assert.strictEqual(options.path, '/'); done(); - }); + }, waitTimeMS); }); it('should set custom headers', done => { @@ -119,7 +117,7 @@ describe('CollectorMetricExporter - node with proto over http', () => { const options = args[0]; assert.strictEqual(options.headers['foo'], 'bar'); done(); - }); + }, waitTimeMS); }); it('should successfully send metrics', done => { @@ -154,7 +152,7 @@ describe('CollectorMetricExporter - node with proto over http', () => { ensureExportMetricsServiceRequestIsSet(json); done(); - }); + }, waitTimeMS); }); it('should log the successful message', done => { @@ -175,7 +173,7 @@ describe('CollectorMetricExporter - node with proto over http', () => { assert.strictEqual(responseSpy.args[0][0], 0); done(); }); - }); + }, waitTimeMS); }); it('should log the error message', done => { @@ -195,7 +193,7 @@ describe('CollectorMetricExporter - node with proto over http', () => { assert.strictEqual(responseSpy.args[0][0], 1); done(); }); - }); + }, waitTimeMS); }); }); }); diff --git a/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts index 27803d9c16..c0eccf6df0 100644 --- a/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts @@ -44,6 +44,9 @@ const mockResError = { statusCode: 400, }; +// send is lazy loading file so need to wait a bit +const waitTimeMS = 20; + describe('CollectorExporter - node with proto over http', () => { let collectorExporter: CollectorTraceExporter; let collectorExporterConfig: collectorTypes.CollectorExporterConfigBase; @@ -51,7 +54,7 @@ describe('CollectorExporter - node with proto over http', () => { let spyWrite: sinon.SinonSpy; let spans: ReadableSpan[]; describe('export', () => { - beforeEach(done => { + beforeEach(() => { spyRequest = sinon.stub(http, 'request').returns(fakeRequest as any); spyWrite = sinon.stub(fakeRequest, 'write'); collectorExporterConfig = { @@ -67,11 +70,6 @@ describe('CollectorExporter - node with proto over http', () => { collectorExporter = new CollectorTraceExporter(collectorExporterConfig); spans = []; spans.push(Object.assign({}, mockedReadableSpan)); - - // due to lazy loading ensure to wait to next tick - setImmediate(() => { - done(); - }); }); afterEach(() => { spyRequest.restore(); @@ -89,7 +87,7 @@ describe('CollectorExporter - node with proto over http', () => { assert.strictEqual(options.method, 'POST'); assert.strictEqual(options.path, '/'); done(); - }); + }, waitTimeMS); }); it('should set custom headers', done => { @@ -100,7 +98,7 @@ describe('CollectorExporter - node with proto over http', () => { const options = args[0]; assert.strictEqual(options.headers['foo'], 'bar'); done(); - }); + }, waitTimeMS); }); it('should successfully send the spans', done => { @@ -121,7 +119,7 @@ describe('CollectorExporter - node with proto over http', () => { ensureExportTraceServiceRequestIsSet(json); done(); - }); + }, waitTimeMS); }); it('should log the successful message', done => { @@ -142,7 +140,7 @@ describe('CollectorExporter - node with proto over http', () => { assert.strictEqual(responseSpy.args[0][0], 0); done(); }); - }); + }, waitTimeMS); }); it('should log the error message', done => { @@ -162,7 +160,7 @@ describe('CollectorExporter - node with proto over http', () => { assert.strictEqual(responseSpy.args[0][0], 1); done(); }); - }); + }, waitTimeMS); }); }); });