gitbeaker icon indicating copy to clipboard operation
gitbeaker copied to clipboard

GitBeaker Request Logging / Tracing?

Open vallamost opened this issue 2 years ago โ€ข 29 comments

Description

It's unclear how I can track the number of requests made to GitLab from the gitbeaker NodeJS client or how I can see the HTTP requests & responses GitBeaker is getting. Is there some documentation somewhere that would let me enable and see request tracing and logging?

How can I enable HTTP request tracing to see all of the requests GitBeaker is making? E.g. If I make a request to get all successful pipelines for a projectID, I want to see how many HTTP requests are made to complete that request and what the details of those HTTP requests are, latency, response codes, size, etc.

Checklist

  • [X] I have checked that this is not a duplicate issue.
  • [X] I have read the documentation.

vallamost avatar Jul 15 '22 20:07 vallamost

The block of code below was something I found that helps trace outgoing requests made by GitBeaker however it doesn't show the raw responses. You can put this in your main entry point file, e.g. main.js or app.js and you should see gitlab requests in your console log when you make them.

function enableRequestLogger() {
    const https = require('https');
    const original = https.request
    https.request = function(options, callback){
      console.log(options);
      console.log(`${new Date().toISOString()} ${options.method} ${options.protocol}//${options.hostname}${options.path}`);
      return original(options, callback);
    }
}

enableRequestLogger();

vallamost avatar Jul 15 '22 20:07 vallamost

Related to #764

jdalrymple avatar Aug 05 '22 12:08 jdalrymple

Full response object is include in the error when it occurs

jdalrymple avatar Apr 26 '23 18:04 jdalrymple

Full response object is include in the error when it occurs

That's not too helpful in many cases. We really need to see request including all headers and full response.

In case of e.g. HTTP 404 the response just says "not found" - with no information about where it was looking.

marcelstoer avatar Jun 15 '23 09:06 marcelstoer

So the full response is included, but the full request isnt. easy fix. Ill through up a PR in a bit

jdalrymple avatar Jun 15 '23 15:06 jdalrymple

I just realized I can actually do NODE_DEBUG=http,http2 node index.js. Dumps loads of information but it's not very concise and hard to read.

marcelstoer avatar Jun 15 '23 15:06 marcelstoer

Just had a case where curl works but @gitbeaker doesn't. This would definitely be super helpful!

trevor-vaughan avatar Jun 23 '23 17:06 trevor-vaughan

Hoping to merge the latest stuff over the weekend! Keep an eye out.

jdalrymple avatar Jun 23 '23 17:06 jdalrymple

Just had a case where curl works but @gitbeaker doesn't. This would definitely be super helpful!

Also, just out of curiosity, did it have to do with a headers timeout?

jdalrymple avatar Jun 23 '23 17:06 jdalrymple

It didn't. I'm having issues with the ProtectedBranches.edit function kicking back a 400 Bad Request.

I patched in the branch and inspected the request and it honestly looks fine from what I can tell. But, when I do the exact same request in curl it works just fine :-/

trevor-vaughan avatar Jun 23 '23 17:06 trevor-vaughan

Hmm, thats odd, any chance you could recreate the problem in a public repo so i could debug later?

jdalrymple avatar Jun 23 '23 17:06 jdalrymple

I'll see what I can do. I'm going to try rolling back a few versions and see if it works (it certainly used to).

trevor-vaughan avatar Jun 23 '23 17:06 trevor-vaughan

Def share if you find anything!

jdalrymple avatar Jun 23 '23 17:06 jdalrymple

Looks like the last time it worked was back in @gitbeaker/node 35.8.0

trevor-vaughan avatar Jun 23 '23 17:06 trevor-vaughan

haha oh damn. big changes since then. Very possible its related to native fetch in some way, but i wouldn't know without digging deeper

jdalrymple avatar Jun 23 '23 17:06 jdalrymple

I tried passing everything into the raw requester with the same result.

๐Ÿค” Could it be related to patch? The other functions on that endpoint work just fine.

trevor-vaughan avatar Jun 23 '23 17:06 trevor-vaughan

@trevor-vaughan I had cases with GitBeaker(Rest) on Node 18 failing due to fetch instabilities. Using Node 20 instead helped massively.

marcelstoer avatar Jun 23 '23 17:06 marcelstoer

I hacked around it using straight got.patch and that worked but :-(. ๐Ÿค” I recently moved off of node-fetch for 16 => 18 compat. I'll try using node-fetch again.

trevor-vaughan avatar Jun 23 '23 18:06 trevor-vaughan

๐Ÿคฆ Welp, @marcelstoer You hit the nail on the head. Overriding the inbuilt fetch with node-fetch worked. ๐Ÿ’€

trevor-vaughan avatar Jun 23 '23 18:06 trevor-vaughan

Ugh, of course :upside_down_face:

jdalrymple avatar Jun 23 '23 18:06 jdalrymple

For anyone else that hits this, here's a horrible hack-around for the entry-point to your app:

const semver = require('semver')
if ( semver.lt(process.version, '20.0.0') ) {
  global.fetch = require('node-fetch')
}

trevor-vaughan avatar Jun 23 '23 18:06 trevor-vaughan

Hopefully those hiccups get sorted as time goes on. Id like to know the particular reason why its getting a bad request though. There must be a difference in the request itself thats causing that

jdalrymple avatar Jun 23 '23 18:06 jdalrymple

FWIW, it was only on patch. Everything else was fine. Weird that it worked via got though :-/

trevor-vaughan avatar Jun 23 '23 19:06 trevor-vaughan

I hate to divert even further from the OT (which is wire logging) but for the Node 18 fetch() discussion this might be indeed relevant: https://github.com/nodejs/undici/issues/583#issuecomment-855379400

marcelstoer avatar Jul 07 '23 13:07 marcelstoer

The current logs seem quite useful, a built-in tracing feature would be great for cases like https://github.com/un-ts/changesets-gitlab/issues/122.

The documentation https://www.npmjs.com/package/@gitbeaker/rest#error-handling is still unclear to me. What the error interface would be?

Is the following I can expect?

export interface GitLabAPIError extends Error {
  cause: {
    text: string;
    response: GitlabAPIResponse;
  }
}

JounQin avatar Dec 18 '23 13:12 JounQin

Yea, I'll update the docs to show the proper type, that would be more useful.

The cause is an object with the description as a string, and the response as the original Response object from fetch (https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#response)

jdalrymple avatar Dec 18 '23 14:12 jdalrymple

@JounQin were you able to sort out the source of the issue?

jdalrymple avatar Dec 18 '23 21:12 jdalrymple

@jdalrymple Sorry but I'm not quite sure to understand what is your meaning. I'm just trying get more useful error details.

See also https://github.com/un-ts/changesets-gitlab/issues/122

JounQin avatar Dec 18 '23 23:12 JounQin

@jdalrymple Sorry but I'm not quite sure to understand what is your meaning. I'm just trying get more useful error details.

See also un-ts/changesets-gitlab#122

I responded in your pr ๐Ÿ˜Œ

jdalrymple avatar Dec 18 '23 23:12 jdalrymple