Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instrumentation-http): monitor error events with events.errorMonitor #3402

Merged
merged 2 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(instrumentation-http): monitor error events with events.errorMonitor [#3402](https://github.com/open-telemetry/opentelemetry-js/pull/3402) @legendecas

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
import { errorMonitor } from 'events';

/**
* Http instrumentation instrumentation for Opentelemetry
Expand Down Expand Up @@ -361,7 +362,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {

this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
});
response.on('error', (error: Err) => {
response.on(errorMonitor, (error: Err) => {
this._diag.debug('outgoingRequest on error()', error);
utils.setSpanWithError(span, error);
const code = utils.parseResponseStatus(SpanKind.CLIENT, response.statusCode);
Expand All @@ -376,7 +377,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
}
});
request.on('error', (error: Err) => {
request.on(errorMonitor, (error: Err) => {
this._diag.debug('outgoingRequest on request error()', error);
utils.setSpanWithError(span, error);
this._closeHttpSpan(span, SpanKind.CLIENT, startTime, metricAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,21 +670,21 @@ describe('HttpInstrumentation', () => {
);
req.setTimeout(10, () => {
req.abort();
reject('timeout');
});
// Instrumentation should not swallow error event.
assert.strictEqual(req.listeners('error').length, 0);
req.on('error', err => {
reject(err);
});
return req.end();
});

try {
await promiseRequest;
assert.fail();
} catch (error) {
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
assert.ok(Object.keys(span.attributes).length >= 6);
}
await assert.rejects(promiseRequest, /Error: socket hang up/);
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
assert.ok(Object.keys(span.attributes).length >= 6);
});

it('should have 1 ended span when request is aborted after receiving response', async () => {
Expand All @@ -701,28 +701,29 @@ describe('HttpInstrumentation', () => {
(resp: http.IncomingMessage) => {
let data = '';
resp.on('data', chunk => {
req.destroy(Error());
req.destroy(Error('request destroyed'));
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}
);
// Instrumentation should not swallow error event.
assert.strictEqual(req.listeners('error').length, 0);
req.on('error', err => {
reject(err);
});

return req.end();
});

try {
await promiseRequest;
assert.fail();
} catch (error) {
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
assert.ok(Object.keys(span.attributes).length > 7);
}
await assert.rejects(promiseRequest, /Error: request destroyed/);
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
assert.ok(Object.keys(span.attributes).length > 7);
});

it("should have 1 ended client span when request doesn't listening response", done => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,21 +639,21 @@ describe('HttpsInstrumentation', () => {
);
req.setTimeout(10, () => {
req.abort();
reject('timeout');
});
// Instrumentation should not swallow error event.
assert.strictEqual(req.listeners('error').length, 0);
req.on('error', err => {
reject(err);
});
return req.end();
});

try {
await promiseRequest;
assert.fail();
} catch (error) {
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
assert.ok(Object.keys(span.attributes).length >= 6);
}
await assert.rejects(promiseRequest, /Error: socket hang up/);
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
assert.ok(Object.keys(span.attributes).length >= 6);
});

it('should have 1 ended span when request is aborted after receiving response', async () => {
Expand All @@ -670,28 +670,29 @@ describe('HttpsInstrumentation', () => {
(resp: http.IncomingMessage) => {
let data = '';
resp.on('data', chunk => {
req.destroy(Error());
req.destroy(Error('request destroyed'));
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}
);
// Instrumentation should not swallow error event.
assert.strictEqual(req.listeners('error').length, 0);
req.on('error', err => {
reject(err);
});

return req.end();
});

try {
await promiseRequest;
assert.fail();
} catch (error) {
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
assert.ok(Object.keys(span.attributes).length > 7);
}
await assert.rejects(promiseRequest, /Error: request destroyed/);
const spans = memoryExporter.getFinishedSpans();
const [span] = spans;
assert.strictEqual(spans.length, 1);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
assert.ok(Object.keys(span.attributes).length > 7);
});

it("should have 1 ended span when response is listened by using req.on('response')", done => {
Expand Down