Skip to content

Commit 6c83f42

Browse files
committed
Fix timing event data on client-error events
Previously HTTP/2 events didn't get any abortedTimestamp at all (so had no duration information whatsoever) and both HTTP/1 & HTTP/2 did not use the raw socket timing data to capture the full connection duration info.
1 parent 598cb0b commit 6c83f42

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/server/mockttp-server.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ import {
7070
LastTunnelAddress,
7171
TlsSetupCompleted,
7272
SocketMetadata,
73-
TlsMetadata
73+
TlsMetadata,
74+
SocketTimingInfo
7475
} from '../util/socket-extensions';
7576
import { getSocketMetadataTags, getSocketMetadataFromProxyAuth } from '../util/socket-metadata'
7677
import {
@@ -1089,7 +1090,13 @@ ${await this.suggestRule(request)}`
10891090
`client-error:${error.code || 'UNKNOWN'}`,
10901091
...getSocketMetadataTags(socket[SocketMetadata])
10911092
],
1092-
timingEvents: { startTime: Date.now(), startTimestamp: now() } as TimingEvents
1093+
timingEvents: (socket?.[SocketTimingInfo]
1094+
? {
1095+
startTime: socket[SocketTimingInfo].initialSocket,
1096+
startTimestamp: socket[SocketTimingInfo].initialSocketTimestamp
1097+
}
1098+
: { startTime: Date.now(), startTimestamp: now() }
1099+
) as TimingEvents
10931100
};
10941101

10951102
const rawPacket = socket[ClientErrorInProgress]?.rawPacket
@@ -1183,6 +1190,15 @@ ${await this.suggestRule(request)}`
11831190
? pairFlatRawHeaders(error.badRequest?.rawHeaders as string[])
11841191
: error.badRequest?.rawHeaders as RawHeaders | undefined;
11851192

1193+
const timingEvents: TimingEvents = socket?.[SocketTimingInfo]
1194+
? {
1195+
startTime: socket[SocketTimingInfo].initialSocket,
1196+
startTimestamp: socket[SocketTimingInfo].initialSocketTimestamp
1197+
}
1198+
: { startTime: Date.now(), startTimestamp: now() };
1199+
1200+
timingEvents.abortedTimestamp = now();
1201+
11861202
this.announceClientErrorAsync(session.initialSocket, {
11871203
errorCode: error.code,
11881204
request: {
@@ -1193,9 +1209,9 @@ ${await this.suggestRule(request)}`
11931209
...getSocketMetadataTags(socket?.[SocketMetadata])
11941210
],
11951211
httpVersion: error.badRequest?.httpVersion ?? '2',
1212+
timingEvents,
11961213

11971214
// Best guesses:
1198-
timingEvents: { startTime: Date.now(), startTimestamp: now() },
11991215
protocol: error.badRequest?.protocol || (isTLS ? "https" : "http"),
12001216
url: error.badRequest?.url ||
12011217
(isTLS ? `https://${(socket as tls.TLSSocket).servername}/` : undefined),

test/integration/subscriptions/client-error-events.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ describe("Client error subscription", () => {
7373
'client-error:HPE_HEADER_OVERFLOW',
7474
'header-overflow'
7575
]);
76+
77+
expect(clientError.request.timingEvents.startTime).to.be.greaterThan(0);
78+
expect(clientError.request.timingEvents.startTimestamp).to.be.greaterThan(0);
79+
expect(clientError.request.timingEvents.responseSentTimestamp)
80+
.to.be.greaterThan(clientError.request.timingEvents.startTimestamp);
7681
});
7782

7883
nodeOnly(() => {
@@ -150,6 +155,11 @@ describe("Client error subscription", () => {
150155

151156
expect(response.statusCode).to.equal(undefined);
152157
expect(response.statusMessage).to.equal(undefined);
158+
159+
expect(clientError.request.timingEvents.startTime).to.be.greaterThan(0);
160+
expect(clientError.request.timingEvents.startTimestamp).to.be.greaterThan(0);
161+
expect(clientError.request.timingEvents.abortedTimestamp)
162+
.to.be.greaterThan(clientError.request.timingEvents.startTimestamp);
153163
});
154164
});
155165
});
@@ -290,6 +300,11 @@ describe("Client error subscription", () => {
290300
]);
291301
expect(error.request.url).to.equal(server.url + '/');
292302
expect(error.response).to.equal('aborted');
303+
304+
expect(error.request.timingEvents.startTime).to.be.greaterThan(0);
305+
expect(error.request.timingEvents.startTimestamp).to.be.greaterThan(0);
306+
expect(error.request.timingEvents.abortedTimestamp)
307+
.to.be.greaterThan(error.request.timingEvents.startTimestamp);
293308
});
294309

295310
it("should report HTTP/2 requests that fail after the preface", async () => {

0 commit comments

Comments
 (0)