# APM integration with ElysiaJS and Bun

**URL:** https://discuss.elastic.co/t/apm-integration-with-elysiajs-and-bun/345530
**Category:** Elastic Observability
**Created:** [October 22, 2023, 11:13pm UTC](https://discuss.elastic.co/t/apm-integration-with-elysiajs-and-bun/345530 "2023-10-22T23:13:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Thai\_Huynh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thai_huynh/32/122336_2.png) [@Thai\_Huynh](https://discuss.elastic.co/u/Thai_Huynh)
#### Post date: [October 22, 2023, 11:13pm UTC](https://discuss.elastic.co/t/apm-integration-with-elysiajs-and-bun/345530/1 "2023-10-22T23:13:00Z")

</div>

Hi,

Has anyone successfully integrated the APM with ElysiaJS and Bun runtime? We kept getting the following message in the console:

APM Server transport error: premature apm-server response with statusCode=202

We tried to manually set the transaction name on our onRequest hook but nothing shows up in the dashboard:

const url = new URL(request.url);  
apm.setTransactionName(`${request.method} ${url.pathname}`);

---

<div class="post-metadata">

### Author: ![zx8086](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zx8086/32/94917_2.png) [@zx8086](https://discuss.elastic.co/u/zx8086)
#### Post date: [October 23, 2023, 8:34pm UTC](https://discuss.elastic.co/t/apm-integration-with-elysiajs-and-bun/345530/2 "2023-10-23T20:34:16Z")

</div>

I am also getting this, will post f I find a solution

---

<div class="post-metadata">

### Author: ![Thai\_Huynh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thai_huynh/32/122336_2.png) [@Thai\_Huynh](https://discuss.elastic.co/u/Thai_Huynh)
#### Post date: [October 23, 2023, 9:10pm UTC](https://discuss.elastic.co/t/apm-integration-with-elysiajs-and-bun/345530/3 "2023-10-23T21:10:44Z")

</div>

apm.ts

```auto
import apm from 'elastic-apm-node';
export default apm.start({
  serviceName: 'search-service',
  secretToken: process.env.ELASTIC_APM_SECRET_TOKEN!!,
  serverUrl: process.env.ELASTIC_APM_SERVER_URL!!,
  environment: process.env.ELASTIC_APM_ENVIRONMENT!!,
  captureBody: process.env.ELASTIC_APM_CAPTURE_BODY!! as any,
  ignoreUrls: ['/ping'],
  logLevel: process.env.NODE_ENV === 'development' ? 'debug' : 'error'
});

```

server.ts

```auto
import cors from '@elysiajs/cors';
import Elysia from 'elysia';
import apm from './lib/apm';

new Elysia()
  .use(
    cors({
      origin: true,
      methods: ['OPTIONS', 'GET', 'DELETE'],
      allowedHeaders: ['Content-Type'],
      exposedHeaders: '*',
      credentials: false,
      preflight: true,
      maxAge: 86400
    })
  )
  .onRequest(({ request }) => {
    const url = new URL(request.url),
      pathname = url.pathname;

    if (pathname !== '/ping') {
      apm.startTransaction();
      apm.setTransactionName(`${request.method} ${pathname}`);
    }
  })
  .onAfterHandle(() => {
    apm.endTransaction();
  })

  .get('/ping', ({ set }) => {
    set.status = 200;
    return 'pong';
  })
  .listen(process.env.SERVICE_PORT ?? 5001);

```

@zx8086 I finally was able to capture stuff. It's more of a manual effort since the apm doesn't seem to be able to hook into Elysia's framework. If you want to capture requests downstream, you're going to have to instrument it yourself:

```auto
async function downStreamCall() {
const span: Span | null = apm.startSpan(`GET ${url.pathname}`);
// fetch()
span.end();
}

```

---

<div class="post-metadata">

### Author: ![zx8086](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zx8086/32/94917_2.png) [@zx8086](https://discuss.elastic.co/u/zx8086)
#### Post date: [October 23, 2023, 10:12pm UTC](https://discuss.elastic.co/t/apm-integration-with-elysiajs-and-bun/345530/4 "2023-10-23T22:12:15Z")

</div>

I was getting there since using Opentelemetry Bridge

Also work-around for the error is to set this in your apm.ts-\>

contextPropagationOnly: true,

```auto
import * as https from 'https';
import * as otel from '@opentelemetry/api';

const tracer = otel.trace.getTracer('trace-https-request');

tracer.startActiveSpan('makeRequest', span => {
  https.get('https://httpstat.us/200', (response) => {
    console.log('STATUS:', response.statusCode);
    const body: Buffer[] = [];
    response.on('data', (chunk: Buffer) => body.push(chunk));
    response.on('end', () => {
      console.log('BODY:', Buffer.concat(body).toString());
      span.end();
    });
  });
});

```

---

<div class="post-metadata">

### Author: ![Thai\_Huynh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thai_huynh/32/122336_2.png) [@Thai\_Huynh](https://discuss.elastic.co/u/Thai_Huynh)
#### Post date: [October 23, 2023, 10:56pm UTC](https://discuss.elastic.co/t/apm-integration-with-elysiajs-and-bun/345530/5 "2023-10-23T22:56:47Z")

</div>

Ahh make sense. Not too familiar but looks very similar.

---

<div class="post-metadata">

### Author: ![zx8086](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zx8086/32/94917_2.png) [@zx8086](https://discuss.elastic.co/u/zx8086)
#### Post date: [October 24, 2023, 12:10am UTC](https://discuss.elastic.co/t/apm-integration-with-elysiajs-and-bun/345530/6 "2023-10-24T00:10:23Z")

</div>

Are you sending the APM logs / specifying a logger ?

---

<div class="post-metadata">

### Author: ![Thai\_Huynh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thai_huynh/32/122336_2.png) [@Thai\_Huynh](https://discuss.elastic.co/u/Thai_Huynh)
#### Post date: [October 24, 2023, 12:44am UTC](https://discuss.elastic.co/t/apm-integration-with-elysiajs-and-bun/345530/7 "2023-10-24T00:44:34Z")

</div>

nope just the console log. I’m using the Bun runtime and the node apm doesn’t auto send transactions so I had to manually instrument them.
