Skip to content

Commit c8c4ec6

Browse files
feat: record exceptions in http instrumentation (#3008)
1 parent 922e963 commit c8c4ec6

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ All notable changes to experimental packages in this project will be documented
1010

1111
### :rocket: (Enhancement)
1212

13+
* feat(http-instrumentation): record exceptions in http instrumentation #3008 @luismiramirez
14+
1315
### :bug: (Bug Fix)
1416

1517
* fix(otlp-transformer): remove type dependency on Long #3022 @legendecas

experimental/packages/opentelemetry-instrumentation-http/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export const setSpanWithError = (
150150
});
151151

152152
span.setStatus({ code: SpanStatusCode.ERROR, message });
153+
span.recordException(error);
153154
};
154155

155156
/**

experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ describe('HttpInstrumentation', () => {
570570
},
571571
component: 'http',
572572
noNetPeer: true,
573+
error: err,
573574
};
574575
assertSpan(spans[0], SpanKind.CLIENT, validations);
575576
return true;

experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ describe('Utility', () => {
259259
attributes[AttributeNames.HTTP_ERROR_MESSAGE],
260260
errorMessage
261261
);
262+
assert.strictEqual(span.events.length, 1);
263+
assert.strictEqual(span.events[0].name, 'exception');
262264
assert.ok(attributes[AttributeNames.HTTP_ERROR_NAME]);
263265
});
264266
});

experimental/packages/opentelemetry-instrumentation-http/test/utils/assertSpan.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { isValidSpanId, SpanKind, SpanStatus } from '@opentelemetry/api';
16+
import { isValidSpanId, SpanKind, SpanStatus, Exception } from '@opentelemetry/api';
1717
import { hrTimeToNanoseconds } from '@opentelemetry/core';
1818
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
1919
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
@@ -38,6 +38,7 @@ export const assertSpan = (
3838
serverName?: string;
3939
component: string;
4040
noNetPeer?: boolean; // we don't expect net peer info when request throw before being sent
41+
error?: Exception;
4142
}
4243
) => {
4344
assert.strictEqual(span.spanContext().traceId.length, 32);
@@ -65,7 +66,20 @@ export const assertSpan = (
6566
);
6667

6768
assert.strictEqual(span.links.length, 0);
68-
assert.strictEqual(span.events.length, 0);
69+
70+
if (validations.error) {
71+
assert.strictEqual(span.events.length, 1);
72+
assert.strictEqual(span.events[0].name, 'exception');
73+
74+
const eventAttributes = span.events[0].attributes;
75+
assert.ok(eventAttributes != null);
76+
assert.deepStrictEqual(
77+
Object.keys(eventAttributes),
78+
['exception.type', 'exception.message', 'exception.stacktrace']
79+
);
80+
} else {
81+
assert.strictEqual(span.events.length, 0);
82+
}
6983

7084
assert.deepStrictEqual(
7185
span.status,

0 commit comments

Comments
 (0)