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

lib: add metrics support for http2 #256

Open
wants to merge 2 commits into
base: node-v22.x-nsolid-v5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
93 changes: 92 additions & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ const { UV_EOF } = internalBinding('uv');

const { StreamPipe } = internalBinding('stream_pipe');
const { _connectionListener: httpConnectionListener } = http;

const dc = require('diagnostics_channel');
const onClientStreamCreatedChannel = dc.channel('http2.client.stream.created');
const onClientStreamStartChannel = dc.channel('http2.client.stream.start');
const onClientStreamErrorChannel = dc.channel('http2.client.stream.error');
const onClientStreamFinishChannel = dc.channel('http2.client.stream.finish');
const onClientStreamCloseChannel = dc.channel('http2.client.stream.close');
const onServerStreamStartChannel = dc.channel('http2.server.stream.start');
const onServerStreamErrorChannel = dc.channel('http2.server.stream.error');
const onServerStreamFinishChannel = dc.channel('http2.server.stream.finish');
const onServerStreamCloseChannel = dc.channel('http2.server.stream.close');

let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
debug = fn;
});
Expand Down Expand Up @@ -375,9 +387,22 @@ function onSessionHeaders(handle, id, cat, flags, headers, sensitiveHeaders) {
stream.end();
stream[kState].flags |= STREAM_FLAGS_HEAD_REQUEST;
}

if (onServerStreamStartChannel.hasSubscribers) {
onServerStreamStartChannel.publish({
stream,
headers,
});
}
} else {
// eslint-disable-next-line no-use-before-define
stream = new ClientHttp2Stream(session, handle, id, {});
if (onClientStreamCreatedChannel.hasSubscribers) {
onClientStreamCreatedChannel.publish({
stream,
});
}

if (endOfStream) {
stream.push(null);
}
Expand Down Expand Up @@ -416,6 +441,16 @@ function onSessionHeaders(handle, id, cat, flags, headers, sensitiveHeaders) {
reqAsync.runInAsyncScope(process.nextTick, null, emit, stream, event, obj, flags, headers);
else
process.nextTick(emit, stream, event, obj, flags, headers);

if (event === 'response') {
if (onClientStreamFinishChannel.hasSubscribers) {
onClientStreamFinishChannel.publish({
stream,
headers,
flags,
});
}
}
}
if (endOfStream) {
stream.push(null);
Expand Down Expand Up @@ -766,7 +801,14 @@ function requestOnConnect(headers, options) {
}
return;
}

this[kInit](ret.id(), ret);
if (onClientStreamStartChannel.hasSubscribers) {
onClientStreamStartChannel.publish({
stream: this,
headers,
});
}
}

// Validates that priority options are correct, specifically:
Expand Down Expand Up @@ -1851,6 +1893,13 @@ class ClientHttp2Session extends Http2Session {
} else {
onConnect();
}

if (onClientStreamCreatedChannel.hasSubscribers) {
onClientStreamCreatedChannel.publish({
stream,
});
}

return stream;
}
}
Expand Down Expand Up @@ -1925,6 +1974,7 @@ const kSubmitRstStream = 1;
const kForceRstStream = 2;

function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {
const type = stream.session[kType];
const state = stream[kState];
state.flags |= STREAM_FLAGS_CLOSED;
state.rstCode = code;
Expand Down Expand Up @@ -1955,6 +2005,20 @@ function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {
else
stream.once('finish', finishFn);
}

if (type === NGHTTP2_SESSION_CLIENT) {
if (onClientStreamCloseChannel.hasSubscribers) {
onClientStreamCloseChannel.publish({
stream,
code,
});
}
} else if (onServerStreamCloseChannel.hasSubscribers) {
onServerStreamCloseChannel.publish({
stream,
code,
});
}
}

function finishCloseStream(code) {
Expand Down Expand Up @@ -2381,6 +2445,21 @@ class Http2Stream extends Duplex {
setImmediate(() => {
session[kMaybeDestroy]();
});
if (err) {
if (session[kType] === NGHTTP2_SESSION_CLIENT) {
if (onClientStreamErrorChannel.hasSubscribers) {
onClientStreamErrorChannel.publish({
stream: this,
error: err,
});
}
} else if (onServerStreamErrorChannel.hasSubscribers) {
onServerStreamErrorChannel.publish({
stream: this,
error: err,
});
}
}
callback(err);
}
// The Http2Stream can be destroyed if it has closed and if the readable
Expand Down Expand Up @@ -2766,6 +2845,13 @@ class ServerHttp2Stream extends Http2Stream {
stream[kState].flags |= STREAM_FLAGS_HEAD_REQUEST;

process.nextTick(callback, null, stream, headers, 0);

if (onServerStreamStartChannel.hasSubscribers) {
onServerStreamStartChannel.publish({
stream,
headers,
});
}
}

// Initiate a response on this Http2Stream
Expand Down Expand Up @@ -2813,8 +2899,13 @@ class ServerHttp2Stream extends Http2Stream {
}

const ret = this[kHandle].respond(headersList, streamOptions);
if (ret < 0)
if (ret < 0) {
this.destroy(new NghttpError(ret));
} else if (onServerStreamFinishChannel.hasSubscribers) {
onServerStreamFinishChannel.publish({
stream: this,
});
}
}

// Initiate a response using an open FD. Note that there are fewer
Expand Down
49 changes: 49 additions & 0 deletions lib/internal/nsolid_diag.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const dc = require('diagnostics_channel');
const {
kHttpClientAbortCount,
kHttpClientCount,
kHttpServerAbortCount,
kHttpServerCount,
kSpanHttpClient,
kSpanHttpMethod,
kSpanHttpReqUrl,
Expand All @@ -31,6 +33,9 @@ const {

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

// To lazy load the http2 constants
let http2Constants;

let tracingEnabled = false;

const fetchSubscribeListener = (message, name) => {};
Expand Down Expand Up @@ -119,3 +124,47 @@ dc.subscribe('undici:request:error', ({ request, error }) => {
}
}
});

dc.subscribe('http2.client.stream.created', ({ stream }) => {
stream[nsolid_tracer_s] = {
start: now(),
response: false,
};
});

dc.subscribe('http2.client.stream.finish', ({ stream, flags }) => {
stream[nsolid_tracer_s].response = true;
});

dc.subscribe('http2.client.stream.close', ({ stream, code }) => {
http2Constants ||= require('internal/http2/core').constants;
const tracingInfo = stream[nsolid_tracer_s];
if (code === http2Constants.NGHTTP2_NO_ERROR && tracingInfo.response) {
nsolid_counts[kHttpClientCount]++;
nsolidApi.pushClientBucket(now() - tracingInfo.start);
} else {
nsolid_counts[kHttpClientAbortCount]++;
}
});

dc.subscribe('http2.server.stream.start', ({ stream }) => {
stream[nsolid_tracer_s] = {
start: now(),
response: false,
};
});

dc.subscribe('http2.server.stream.finish', ({ stream, flags }) => {
stream[nsolid_tracer_s].response = true;
});

dc.subscribe('http2.server.stream.close', ({ stream, code }) => {
http2Constants ||= require('internal/http2/core').constants;
const tracingInfo = stream[nsolid_tracer_s];
if (code === http2Constants.NGHTTP2_NO_ERROR && tracingInfo.response) {
nsolid_counts[kHttpServerCount]++;
nsolidApi.pushServerBucket(now() - tracingInfo.start);
} else {
nsolid_counts[kHttpServerAbortCount]++;
}
});
Loading
Loading