Skip to content

Commit e7ebfad

Browse files
committed
attempt to add a test
1 parent e8aef9c commit e7ebfad

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

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

+41
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
import {
1717
SpanStatusCode,
1818
context,
19+
diag,
1920
propagation,
2021
Span as ISpan,
2122
SpanKind,
2223
trace,
2324
SpanAttributes,
25+
DiagConsoleLogger,
2426
} from '@opentelemetry/api';
2527
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
2628
import {
@@ -269,6 +271,14 @@ describe('HttpInstrumentation', () => {
269271
// hang the request.
270272
return;
271273
}
274+
if (request.url?.includes('/destroy-request')) {
275+
// force flush http response header to trigger client response callback
276+
response.write('');
277+
setTimeout(() => {
278+
request.socket.destroy();
279+
}, 100);
280+
return;
281+
}
272282
if (request.url?.includes('/ignored')) {
273283
provider.getTracer('test').startSpan('some-span').end();
274284
}
@@ -920,6 +930,37 @@ describe('HttpInstrumentation', () => {
920930
assert.strictEqual(clientSpan.status.code, SpanStatusCode.ERROR);
921931
assert.ok(Object.keys(clientSpan.attributes).length >= 6);
922932
});
933+
934+
it('should not end span multiple times if request socket destroyed before response completes', async () => {
935+
const warnMessages: string[] = [];
936+
diag.setLogger({
937+
...new DiagConsoleLogger(),
938+
warn: message => {
939+
warnMessages.push(message);
940+
},
941+
});
942+
const promise = new Promise<void>(resolve => {
943+
const req = http.get(
944+
`${protocol}://${hostname}:${serverPort}/destroy-request`,
945+
res => {
946+
res.on('end', () => {});
947+
res.on('close', () => {});
948+
res.on('error', () => {
949+
resolve();
950+
});
951+
}
952+
);
953+
// force flush http request header to trigger client response callback
954+
req.write('');
955+
req.on('error', () => {});
956+
});
957+
958+
await promise;
959+
960+
diag.disable();
961+
962+
assert.strictEqual(warnMessages, []);
963+
});
923964
});
924965

925966
describe('with require parent span', () => {

0 commit comments

Comments
 (0)