[Node.js] Setting HTTP Method in startTransaction

I’m integrating Sentry into our Node.js Hapi backend.

At the start of each incoming request, I call Sentry.startTransaction(). I would like to attach the HTTP method to this transaction, but I can’t find any documentation on how that is done.

I’ve tried just Sentry.startTransaction({ method }) or Sentry.startTransaction({ data: { method } }) but neither works.

Hi Shane,

Typically in our docs we initialize a Transaction with the op and name tags, like so:

const transaction = Sentry.startTransaction({
  op: "transaction",
  name: "My Transaction",
});

You can read more about it here.

If you feel like sharing a code snippet I can have a look at that too.

Hi @NickMeis,

I’m using that snippet as well, like so:

const transaction = Sentry.startTransaction({
  op: "http",
  name: path, // The API endpoint path (e.g. /users)
});

This works, as I get the transaction in my Sentry dashboard. However the HTTP Method column is empty. See below:

How can I set that value so it appears in my dashboard?

Hi Shane,

I have some hapi news.

I had some luck in playing around with this (which is by no means an official solution) - we don’t explicitly support the hapi framework but this might be good place to start for some custom instrumentation. I recommend reading more about scopes, and hubs as well.

I was able to get the method to appear in the UI consistently by configuring the scope through adding the method to the event (not the transaction), then setting the transaction on the scope:

const currentHub = Sentry.getCurrentHub();

currentHub.configureScope(scope => {
  scope.addEventProcessor(event => {
    event.request = {
      method: request.method,
      ...event.request,
    };

    return event;
  });
});

const transaction = Sentry.startTransaction(
  {
    name: 'GET /',
    op: 'http.server',
  }
);
                
currentHub.configureScope(scope => {
  scope.setSpan(transaction);
});

You will need to finish the transaction of course, also you might consider setting the status with transaction.setHttpStatus().

Thanks Nick, that looks like it does the trick. :+1:

I did have some questions about the Hubs/Scopes. Our backend processes multiple concurrent requests, which seem to get jumbled up in Sentry.

For example, we may have these endpoints:

Endpoint A: Queries the DB, then hits an external API and returns some value.
Endpoint B: Queries the DB and returns some value.

If Endpoint B’s Db query is executed between the DB query and external API call of Endpoint A, what seems to happen is that once the Endpoint A transaction finishes, the scope had been changed by Endpoint B and therefore the event in Sentry is incorrect.

The pseudocode flow is:

  1. On request, extract request ID, path, method and store in a map (with ID as the key). Attach the transaction to the current scope.
  2. Before response, set the status code of the transaction and finish it.

Is there a way for us to generate and destroy isolated scopes/hubs dynamically? Or is there some other way for us to handle this use case

Without looking at your code I can’t say for sure. Are you using Hapi’s server extensions with Sentry at all?

If you want to privately message me your organization slug I could also have a peak at some of those “jumbled up” transactions.

My gut reaction here is that we are in uncharted territory as we don’t necessarily support this framework, however looking at the SDK source and how we apply it other similar frameworks might give us a good course of action.

@NickMeis, can you share with me how to send a private message :slightly_smiling_face:

I tried following the instructions in the menu on the top right:

Message them by selecting their avatar and using the :email: message button

but I don’t see that option…