# Unable to see transactions with APM when used with apollo-server-express

**URL:** <https://discuss.elastic.co/t/unable-to-see-transactions-with-apm-when-used-with-apollo-server-express/263821>\
**Category:** APM\
**Tags:** nodejs\
**Created:** [February 10, 2021, 2:52am UTC](https://discuss.elastic.co/t/unable-to-see-transactions-with-apm-when-used-with-apollo-server-express/263821 "2021-02-10T02:52:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sim\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sim_singh/32/83684_2.png) [@Sim\_Singh](https://discuss.elastic.co/u/Sim_Singh)\
**Post date:** [February 10, 2021, 2:52am UTC](https://discuss.elastic.co/t/unable-to-see-transactions-with-apm-when-used-with-apollo-server-express/263821/1 "2021-02-10T02:52:22Z")

</div>

**APM Agent language and version** :  
`"elastic-apm-node": "^3.10.0",`

Using with:  
`"apollo-server-express": "^2.18.2",`  
`"express": "^4.17.1",`

This is a first time setup for this particular type of app. As suggested by Apollo team ([Integrating with Node.js middleware - Apollo Server - Apollo GraphQL Docs](https://www.apollographql.com/docs/apollo-server/integrations/middleware/#applying-middleware)), we are using the express app applied as a middleware to the ApolloServer.

**The problem is - APM is only shimming graphql and not the nodejs http modules, hence any other rest api calls are not getting tracked - only graphql endpoint requests are getting tracked** :

Running in trace log mode I see:

```auto
  pid: 76893,
  ppid: 76892,
  arch: 'x64',
  platform: 'darwin',
  node: 'v12.18.3',
  agent: '3.10.0',
  ancestors: [
    '/Users/ **** /node_modules/elastic-apm-node/index.js',
    '/Users/ **** /src/middlewares/verifyToken.ts',
    '/Users/ **** /src/index.ts',
    [length]: 3
  ],
  startTrace: [
    'at Agent.start (/Users/ **** /node_modules/elastic-apm-node/lib/agent.js:154:11)',
    'at Object.apmSetup [as default] (/Users/ **** /src/apmSetup.ts:5:21)',
    'at Object.<anonymous> (/Users/ **** /src/index.ts:30:27)',
    'at Module._compile (internal/modules/cjs/loader.js:1137:30)',
    'at Module.m._compile (/Users/ **** /node_modules/ts-node/src/index.ts:1056:23)',
    'at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)',
    'at Object.require.extensions.<computed> [as .ts] (/Users/ **** /node_modules/ts-node/src/index.ts:1059:12)',
    'at Module.load (internal/modules/cjs/loader.js:985:32)',
    'at Function.Module._load (internal/modules/cjs/loader.js:878:14)',
    'at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)',
    'at main (/Users/ **** /node_modules/ts-node/src/bin.ts:198:14)',
    'at Object.<anonymous> (/Users/ **** /node_modules/ts-node/src/bin.ts:288:3)',
    'at Module._compile (internal/modules/cjs/loader.js:1137:30)',
    'at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)',
    'at Module.load (internal/modules/cjs/loader.js:985:32)',
    'at Function.Module._load (internal/modules/cjs/loader.js:878:14)',
    [length]: 16
  ],
  main: undefined,
  dependencies: undefined,
  conf: {
    ignoreUrlStr: [[length]: 0 ],
    ignoreUrlRegExp: [[length]: 0 ],
    ignoreUserAgentStr: [[length]: 0 ],
    ignoreUserAgentRegExp: [[length]: 0 ],
    transactionIgnoreUrlRegExp: [[length]: 0 ],
    sanitizeFieldNamesRegExp: [
      /^password$/i { [lastIndex]: 0 },
      /^passwd$/i { [lastIndex]: 0 },
      /^pwd$/i { [lastIndex]: 0 },
      /^secret$/i { [lastIndex]: 0 },
      /^.*key$/i { [lastIndex]: 0 },
      /^.*token.*$/i { [lastIndex]: 0 },
      /^.*session.*$/i { [lastIndex]: 0 },
      /^.*credit.*$/i { [lastIndex]: 0 },
      /^.*card.*$/i { [lastIndex]: 0 },
      /^authorization$/i { [lastIndex]: 0 },
      /^set\x2dcookie$/i { [lastIndex]: 0 },
      /^pw$/i { [lastIndex]: 0 },
      /^pass$/i { [lastIndex]: 0 },
      /^connect\.sid$/i { [lastIndex]: 0 },
      [length]: 14
    ],
    abortedErrorThreshold: 25,
    active: true,
    apiRequestSize: 786432,
    apiRequestTime: 10,
    asyncHooks: true,
    breakdownMetrics: true,
    captureBody: 'off',
    captureErrorLogStackTraces: 'messages',
    captureExceptions: true,
    captureHeaders: true,
    captureSpanStackTraces: true,
    centralConfig: true,
    disableInstrumentations: [[length]: 0 ],
    environment: 'dev',
    errorMessageMaxLength: 2048,
    errorOnAbortedRequests: false,
    filterHttpHeaders: true,
    instrument: true,
    instrumentIncomingHTTPRequests: true,
    logLevel: 'trace',
    logUncaughtExceptions: false,
    metricsInterval: 30,
    metricsLimit: 1000,
    sanitizeFieldNames: [
      'password', 'passwd',
      'pwd', 'secret',
      '*key', '*token*',
      '*session*', '*credit*',
      '*card*', 'authorization',
      'set-cookie', 'pw',
      'pass', 'connect.sid',
      [length]: 14
    ],
    serverTimeout: 30,
    sourceLinesErrorAppFrames: 5,
    sourceLinesErrorLibraryFrames: 5,
    sourceLinesSpanAppFrames: 0,
    sourceLinesSpanLibraryFrames: 0,
    stackTraceLimit: 50,
    transactionIgnoreUrls: [[length]: 0 ],
    transactionMaxSpans: 500,
    transactionSampleRate: 1,
    useElasticTraceparentHeader: true,
    usePathAsTransactionName: false,
    verifyServerCert: true,
    serviceName: ' ******',
    secretToken: '',
    serverUrl: ' *********',
    serviceVersion: '0.0.1',
    serverHost: ' ********',
    serverPort: 443
  }
}
adding hook to Node.js module loader
shimming graphql@15.4.0 module

```

I was expecting APM to shim the following as well.

```auto
shimming http@12.18.3 module
shimming http.Server.prototype.emit function
shimming http.ServerResponse.prototype.writeHead function
shimming http.request function
shimming http.get function

```

Can somebody help on identifying why this could be happening and what can I do different for it to work?

Thanks,  
Sim

---

<div class="post-metadata">

**Author:** ![alanstorm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alanstorm/32/77134_2.png) [@alanstorm](https://discuss.elastic.co/u/alanstorm)\
**Post date:** [February 10, 2021, 3:02am UTC](https://discuss.elastic.co/t/unable-to-see-transactions-with-apm-when-used-with-apollo-server-express/263821/2 "2021-02-10T03:02:31Z")

</div>

> [@Sim\_Singh](#):
>
> **The problem is - APM is only shimming graphql and not the nodejs http modules, hence any other rest api calls are not getting tracked - only graphql endpoint requests are getting tracked** :

Sim\_Singh, thanks for dropping by.

Reading over your post -- great job at identifying the root cause -- if the modules you mention aren't shimmed that will definitely block transaction creation.

The most common reason for these modules not to be shimmed is the Node.js Agent isn't the first module loaded by your application. If something else loads `http` before the agent loads, the agent won't be able to shim that module. Can you talk a little bit about how you're invoking node and what your application bootstrap file looks like? With that information someone might be better able to diagnose your issue.

---

<div class="post-metadata">

**Author:** ![Sim\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sim_singh/32/83684_2.png) [@Sim\_Singh](https://discuss.elastic.co/u/Sim_Singh)\
**Post date:** [February 10, 2021, 3:55am UTC](https://discuss.elastic.co/t/unable-to-see-transactions-with-apm-when-used-with-apollo-server-express/263821/3 "2021-02-10T03:55:40Z")

</div>

hey Alan,

Thanks for looking into this.

As mentioned in the post I am bootstrapping my app something like this:

```auto
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { typeDefs, resolvers } = require('./schema');
import { start } from 'elastic-apm-node';

const apmObject = start({...});
const app = express();
const server = new ApolloServer({
  typeDefs,
  resolvers,
});

server.applyMiddleware({ app });

app.use((req, res) => {
  res.status(200);
  res.send('Hello!');
  res.end();
});

app.listen({ port: 4000 }, () =>
  console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
)

```

---

<div class="post-metadata">

**Author:** ![alanstorm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alanstorm/32/77134_2.png) [@alanstorm](https://discuss.elastic.co/u/alanstorm)\
**Post date:** [February 10, 2021, 7:58am UTC](https://discuss.elastic.co/t/unable-to-see-transactions-with-apm-when-used-with-apollo-server-express/263821/4 "2021-02-10T07:58:58Z")

</div>

Thanks @Sim_Singh, this makes the problem clear.

The `elastic-apm-node` needs to be the first thing that's loaded in your application's bootstrap file. So instead of this

```auto
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { typeDefs, resolvers } = require('./schema');
import { start } from 'elastic-apm-node';

```

try something like this

```auto
const apm = require('elastic-apm-node')
apm.start({...})
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { typeDefs, resolvers } = require('./schema');

```

Get `elastic-apm-node` loaded first and you should be good to go.

---

<div class="post-metadata">

**Author:** ![Sim\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sim_singh/32/83684_2.png) [@Sim\_Singh](https://discuss.elastic.co/u/Sim_Singh)\
**Post date:** [February 10, 2021, 9:31am UTC](https://discuss.elastic.co/t/unable-to-see-transactions-with-apm-when-used-with-apollo-server-express/263821/5 "2021-02-10T09:31:34Z")

</div>

Ah, that solves it! Order of imports, I forgot that.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 3, 2021, 5:32am UTC](https://discuss.elastic.co/t/unable-to-see-transactions-with-apm-when-used-with-apollo-server-express/263821/6 "2021-03-03T05:32:03Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
