Skip to content

Commit 52285f6

Browse files
committed
lib: add metrics support for http2
1 parent dba0f6a commit 52285f6

8 files changed

+605
-2
lines changed

lib/internal/nsolid_diag.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const dc = require('diagnostics_channel');
2323
const {
2424
kHttpClientAbortCount,
2525
kHttpClientCount,
26+
kHttpServerAbortCount,
27+
kHttpServerCount,
2628
kSpanHttpClient,
2729
kSpanHttpMethod,
2830
kSpanHttpReqUrl,
@@ -31,6 +33,9 @@ const {
3133

3234
const undiciFetch = dc.tracingChannel('undici:fetch');
3335

36+
// To lazy load the http2 constants
37+
let http2Constants;
38+
3439
let tracingEnabled = false;
3540

3641
const fetchSubscribeListener = (message, name) => {};
@@ -119,3 +124,47 @@ dc.subscribe('undici:request:error', ({ request, error }) => {
119124
}
120125
}
121126
});
127+
128+
dc.subscribe('http2.client.stream.created', ({ stream }) => {
129+
stream[nsolid_tracer_s] = {
130+
start: now(),
131+
response: false,
132+
};
133+
});
134+
135+
dc.subscribe('http2.client.stream.finish', ({ stream, flags }) => {
136+
stream[nsolid_tracer_s].response = true;
137+
});
138+
139+
dc.subscribe('http2.client.stream.close', ({ stream, code }) => {
140+
http2Constants ||= require('internal/http2/core').constants;
141+
const tracingInfo = stream[nsolid_tracer_s];
142+
if (code === http2Constants.NGHTTP2_NO_ERROR && tracingInfo.response) {
143+
nsolid_counts[kHttpClientCount]++;
144+
nsolidApi.pushClientBucket(now() - tracingInfo.start);
145+
} else {
146+
nsolid_counts[kHttpClientAbortCount]++;
147+
}
148+
});
149+
150+
dc.subscribe('http2.server.stream.start', ({ stream }) => {
151+
stream[nsolid_tracer_s] = {
152+
start: now(),
153+
response: false,
154+
};
155+
});
156+
157+
dc.subscribe('http2.server.stream.finish', ({ stream, flags }) => {
158+
stream[nsolid_tracer_s].response = true;
159+
});
160+
161+
dc.subscribe('http2.server.stream.close', ({ stream, code }) => {
162+
http2Constants ||= require('internal/http2/core').constants;
163+
const tracingInfo = stream[nsolid_tracer_s];
164+
if (code === http2Constants.NGHTTP2_NO_ERROR && tracingInfo.response) {
165+
nsolid_counts[kHttpServerCount]++;
166+
nsolidApi.pushServerBucket(now() - tracingInfo.start);
167+
} else {
168+
nsolid_counts[kHttpServerAbortCount]++;
169+
}
170+
});

test/parallel/test-diagnostics-channel-http2.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const isServerHttp2Stream = (stream) => {
2020
stream.session.type === http2.constants.NGHTTP2_SESSION_SERVER;
2121
};
2222

23+
const isHttp2Stream = (stream) => {
24+
return stream instanceof Http2Stream;
25+
};
26+
2327
const isError = (error) => error instanceof Error;
2428

2529
const isValidHeaders = (headers) => {
@@ -37,7 +41,7 @@ dc.subscribe('http2.client.stream.start', common.mustCall(({ stream, headers })
3741
}, 2));
3842

3943
dc.subscribe('http2.client.stream.error', common.mustCall(({ stream, error }) => {
40-
assert.strictEqual(isClientHttp2Stream(stream), true);
44+
assert.strictEqual(isHttp2Stream(stream), true);
4145
assert.strictEqual(isError(error), true);
4246
}));
4347

@@ -57,7 +61,7 @@ dc.subscribe('http2.server.stream.start', common.mustCall(({ stream, headers })
5761
}, 2));
5862

5963
dc.subscribe('http2.server.stream.error', common.mustCall(({ stream, error }) => {
60-
assert.strictEqual(isServerHttp2Stream(stream), true);
64+
assert.strictEqual(isHttp2Stream(stream), true);
6165
assert.strictEqual(isError(error), true);
6266
}));
6367

0 commit comments

Comments
 (0)