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

how to capture transactions with long duration? timeout ? #4358

Open
1 of 3 tasks
sibelius opened this issue Dec 9, 2024 · 4 comments
Open
1 of 3 tasks

how to capture transactions with long duration? timeout ? #4358

sibelius opened this issue Dec 9, 2024 · 4 comments

Comments

@sibelius
Copy link
Contributor

sibelius commented Dec 9, 2024

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
  • 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
Copy link
Contributor Author

sibelius commented Dec 9, 2024

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
Copy link
Contributor Author

sibelius commented Dec 9, 2024

{"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
Copy link
Contributor Author

sibelius commented Dec 9, 2024

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 sibelius closed this as completed Dec 9, 2024
@sibelius sibelius reopened this Dec 9, 2024
@sibelius
Copy link
Contributor Author

this code is not working well

any other solution for this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant