apm-agent-nodejs icon indicating copy to clipboard operation
apm-agent-nodejs copied to clipboard

how to capture transactions with long duration? timeout ?

Open sibelius opened this issue 1 year ago • 4 comments

Describe the bug

Not sure if this is a bug or a wrong configuration that it is not enabling capture long duration transaction or without timeout

To Reproduce

Steps to reproduce the behavior:

  1. Use this config '...'
  2. Then call '....'
  3. Then do '....'
  4. See error

setup APM, and try to make a long duration request, more than 3 minutes

Expected behavior

it should capture and show this transaction in APM

Environment (please complete the following information)

  • OS: [e.g. Linux]
  • Node.js version:
  • APM Server version:
  • Agent version:

System: OS: macOS 15.1.1 CPU: (8) arm64 Apple M1 Pro Memory: 127.75 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 22.12.0 - ~/.nvm/versions/node/v22.12.0/bin/node npm: 10.9.0 - ~/.nvm/versions/node/v22.12.0/bin/npm pnpm: 9.15.0 - ~/Library/pnpm/pnpm bun: 1.1.38 - /opt/homebrew/bin/bun Browsers: Chrome: 131.0.6778.109 Safari: 18.1.1

apm server 7 elastic-apm-node: 4.7.3

How are you starting the agent? (please tick one of the boxes)

  • [ ] Calling agent.start() directly (e.g. require('elastic-apm-node').start(...))
  • [ ] Requiring elastic-apm-node/start from within the source code
  • [x] Starting node with -r elastic-apm-node/start

Additional context

Agent config options:

Click to expand
replace this line with your agent config options

package.json dependencies:

Click to expand
replace this line with your dependencies section from package.json

What is the right environment variable for this?

ELASTIC_APM_API_REQUEST_TIME ELASTIC_APM_SERVER_TIMEOUT

sibelius avatar Dec 09 '24 18:12 sibelius

this code do not capture

async function fetchWithTimeout() {
  const controller = new AbortController();
  const timeout = setTimeout(() => {
    controller.abort(); // Trigger abort after 3 seconds
  }, 3000);

  try {
    const response = await fetch('http://localhost:5001/api/timeout2', {
      signal: controller.signal,
    });

    clearTimeout(timeout); // Clear timeout if the request completes early

    console.log('Response status:', response.status);
    const data = await response.text();
    console.log('Response data:', data);
  } catch (error) {
    if (error.name === 'AbortError') {
      console.log('Request aborted due to timeout');
    } else {
      console.error('Unexpected error:', error);
    }
  }
}

sibelius avatar Dec 09 '24 19:12 sibelius

{"log.level":"debug","@timestamp":"2024-12-09T19:15:20.832Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"intercepted request event call to http.Server.prototype.emit for /api/timeout2"}
  <-- GET /api/timeout2
{"log.level":"debug","@timestamp":"2024-12-09T19:15:20.838Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"start trace {\"trans\":\"257c1f963f5c3ba6\",\"trace\":\"a509ba0a8aa90571619254c2adde9b88\",\"name\":\"unnamed\",\"type\":\"custom\"}"}
{"log.level":"debug","@timestamp":"2024-12-09T19:15:20.838Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","ctxmgr":"AsyncLocalStorageRunContextManager( RunContext(Transaction(257c1f963f5c3ba6, 'unnamed')) )","message":"supersedeWithTransRunContext(<Trans 257c1f963f5c3ba6>)"}
{"log.level":"debug","@timestamp":"2024-12-09T19:15:20.843Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"setting default transaction name: GET /api/timeout2 {\"trans\":\"257c1f963f5c3ba6\",\"trace\":\"a509ba0a8aa90571619254c2adde9b88\"}"}

sibelius avatar Dec 09 '24 19:12 sibelius

async function captureAbortMiddleware(ctx: Koa.Context, next: Koa.Next) {
  // Listen for 'close' events to detect aborts
  ctx.req.on('close', () => {
    if (!ctx.res.writableEnded) {
      console.log('Client aborted the request');
      if (apm.currentTransaction) {
        apm.currentTransaction.setOutcome('failure'); // Mark transaction as failure
        apm.currentTransaction.addLabels({
          aborted: true,
        });
        apm.currentTransaction.end(); // End the transaction
      }
    }
  });

  // Proceed with the request
  await next();
}

this middleware solves my problem

sibelius avatar Dec 09 '24 19:12 sibelius

this code is not working well

any other solution for this ?

sibelius avatar Dec 11 '24 13:12 sibelius