# Time spent by span type showing only app(not showing db, http, redis)

**URL:** <https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459>\
**Category:** APM\
**Tags:** nodejs\
**Created:** [August 6, 2024, 9:51am UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459 "2024-08-06T09:51:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amol\_Nagotkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amol_nagotkar/32/124188_2.png) [@Amol\_Nagotkar](https://discuss.elastic.co/u/Amol_Nagotkar)\
**Post date:** [August 6, 2024, 9:51am UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/1 "2024-08-06T09:51:42Z")

</div>

Hi all,

Using 7.15.1 version for all

1. I am able to see only app in "Time spent by span type" section. in my case it should show app, http, redis, mongo... but not showing
2. in "Trace sample" i am able to see my api's route.. but not its bifurcation(third party api's/ redis/ mongo) called inside it.

What agent are you using (type and version)? - "elastic-apm-node": "^4.7.1",

What client libraries are you using for DB, HTTP, Redis (type and version)?  
DB- "mongodb": "^4.4.1"  
HTTP - simple http call  
Redis - "ioredis": "^4.28.5"

Did you look in your APM agent's documentation to see that the clients you are using are supported? yes supported. Supported technologies | APM Node.js Agent Reference [4.x] | Elastic

What does your APM agent log says?

{"log.level":"info","@timestamp":"2024-08-08T06:48:48.449Z","log.logger":"request","log.origin":{"file.name":"middleware/log\_middleware.go","file.line":63},"message":"request accepted","service.name":"apm-server","url.original":"/intake/v2/events","http.request.method":"POST","user\_agent.original":"apm-agent-nodejs/4.7.2 (Template 1.0.0)","source.address":"172.21.0.1","http.request.id":"619cda47-0046-4268-94c9-050e29c20374","event.duration":10001330339,"http.response.status\_code":202,"ecs.version":"1.6.0"}

---------docker-compose.yml-------------

version: '3'

services:  
elasticsearch:  
image: [docker.elastic.co/elasticsearch/elasticsearch:7.15.1](http://docker.elastic.co/elasticsearch/elasticsearch:7.15.1)  
ports:  
- "9200:9200"  
environment:  
- discovery.type=single-node

kibana:  
image: [docker.elastic.co/kibana/kibana:7.15.1](http://docker.elastic.co/kibana/kibana:7.15.1)  
ports:  
- "5601:5601"  
depends\_on:  
- elasticsearch

filebeat:  
build:  
context: ./filebeat  
container\_name: filebeat  
command: filebeat -e -strict.perms=false  
volumes:  
- path\_to\_log:/app/logs/template

```
depends_on:
    - elasticsearch
    - kibana

```

apm-server:  
image: [docker.elastic.co/apm/apm-server:7.15.1](http://docker.elastic.co/apm/apm-server:7.15.1)  
environment:  
- apm-server.host=0.0.0.0:8200  
- output.elasticsearch.hosts=["[http://elasticsearch:9200](http://elasticsearch:9200)"]  
ports:  
- "8200:8200"  
depends\_on:  
- elasticsearch  
template:  
network\_mode: "host"  
build:  
context: .  
dockerfile: Dockerfile  
container\_name: template  
depends\_on:  
- elasticsearch  
env\_file:  
- .env  
volumes:  
- /etc/hosts:/etc/hosts  
- ./logs/template:/app/logs

volumes:  
elasticsearch\_data:  
driver: local

-----apm code in route------  
app.use((req, res, next) =\> {  
const transaction = APM.startTransaction(req.method + ' ' + req.url, 'request')  
req.setTransactionName = (name) =\> {  
if (transaction) {  
transaction.setName(name)  
}  
}  
res.on('finish', () =\> {  
if (transaction) {  
if (res.statusCode \>= 400 && res.statusCode \< 600) {  
// Capture error for non-200 status codes  
APM.captureError(new Error(`HTTP Error: ${res.statusCode}`), {  
custom: {  
statusCode: res.statusCode,  
responseTime: transaction.duration()  
}  
})  
transaction.result = 'HTTP ' + res.statusCode  
} else {  
transaction.result = 'success'  
}  
transaction.end()  
}  
})

})`

----------apm ----------------  
import apm from 'elastic-apm-node'

apm.start({

serviceName: 'Template',

// Use if APM Server requires a secret token

secretToken: '',

// Set the custom APM Server URL (default: [http://localhost:8200](http://localhost:8200))

serverUrl: '[http://localhost:8200](http://localhost:8200)',

// Set the service {environment

environment: "development"

## })

Please help.

 ![Screenshot from 2024-08-06 15-15-56](https://us1.discourse-cdn.com/elastic/original/3X/5/7/5719bb5f942ca8cff418ae5d5c4776fd82e82792.png)  
 ![test123](https://us1.discourse-cdn.com/elastic/original/3X/b/0/b053650b68b84b6ff1791eb438b4063f9a2a21c8.png)

---

<div class="post-metadata">

**Author:** ![Amol\_Nagotkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amol_nagotkar/32/124188_2.png) [@Amol\_Nagotkar](https://discuss.elastic.co/u/Amol_Nagotkar)\
**Post date:** [August 7, 2024, 1:43pm UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/2 "2024-08-07T13:43:01Z")

</div>

Can anyone help me out with this ? as i see this doc [Trace sample timeline | Elastic Observability [8.14] | Elastic](https://www.elastic.co/guide/en/kibana/current/spans.html)

Spans are automatically captured by APM agents. but in my case that's not working  
node package- - "elastic-apm-node": "^4.7.1",  
DB- "mongodb": "^4.4.1"  
HTTP - simple http call  
Redis - "ioredis": "^4.28.5"

APM agent log ----\>  
{"log.level":"info","@timestamp":"2024-08-08T06:48:48.449Z","log.logger":"request","log.origin":{"file.name":"middleware/log\_middleware.go","file.line":63},"message":"request accepted","service.name":"apm-server","url.original":"/intake/v2/events","http.request.method":"POST","user\_agent.original":"apm-agent-nodejs/4.7.2 (Template 1.0.0)","source.address":"172.21.0.1","http.request.id":"619cda47-0046-4268-94c9-050e29c20374","event.duration":10001330339,"http.response.status\_code":202,"ecs.version":"1.6.0"}

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [August 8, 2024, 6:55am UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/3 "2024-08-08T06:55:43Z")

</div>

From #Elasticsearch to #APM

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [August 8, 2024, 6:55am UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/4 "2024-08-08T06:55:43Z")

</div>

Added #nodejs and removed #docker, #elastic-stack-monitoring

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [August 8, 2024, 9:27pm UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/5 "2024-08-08T21:27:14Z")

</div>

@Amol_Nagotkar Hi. I'm trying to understand your app a little better.

You showed some code using `app.use(...)`. Are you using `express` or some other web framework?

If it is a web framework that the Node.js APM agent (`elastic-apm-node`) supports, then you shouldn't need to be manually creating transactions via `APM.startTransaction()`, etc.

* * *

You showed code using:

```js
import apm from 'elastic-apm-node'
apm.start({ ... })

```

Is that TypeScript code that is compiled to JavaScript? Or is that JavaScript code using EcmaScript Modules (ESM, see [Modules: Packages | Node.js v22.6.0 Documentation](https://nodejs.org/api/packages.html#determining-module-system))?

If you are using ESM, then you will need to read [ECMAScript module support | APM Node.js Agent Reference [4.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/esm.html)  
At a minimum you would need to start your app with the `node --experimental-loader=elastic-apm-node/loader.mjs ...` argument.  
Note that instrumentation of ESM code is very limited.

---

<div class="post-metadata">

**Author:** ![Amol\_Nagotkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amol_nagotkar/32/124188_2.png) [@Amol\_Nagotkar](https://discuss.elastic.co/u/Amol_Nagotkar)\
**Post date:** [August 9, 2024, 7:14am UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/6 "2024-08-09T07:14:24Z")

</div>

> [@trentm](#):
>
> node --experimental-loader=elastic-apm-node/loader.mjs

Thank to so much for reply

1. Yes using express framework
2. i have added manually creating transactions via APM.startTransaction() to get request and to log error.. without this code my \>400 & 600\< status code were not getting logged properly
3. import apm from 'elastic-apm-node'  
apm.start({ ... })

neither we are using TypeScript code that is compiled to JavaScript nor that JavaScript code using EcmaScript Modules completly  
we are using some of feature of ESM and using babel .. it converts to plain JavaScript form

i used this command in my package.json file -\> node --experimental-loader=elastic-apm-node/loader.mjs  
still it is not wokring.  
i tried editing apm-server.yml file also  
-----------------------------apm-server.yml---------------  
apm-server:  
host: "0.0.0.0:8200"  
output.elasticsearch:  
hosts: ["elasticsearch:9200"]

instrumentation:  
enabled: true  
environment: "local"  
hosts:

- [http://localhost:8200](http://localhost:8200)

Now kept instrumentation enabled true  
attching screenshots. Please check.. its still not working.. i have mongo, http call, redis. but still Trace sample and Time spent by span type  
not showing all clients.

 ![test12](https://us1.discourse-cdn.com/elastic/original/3X/0/0/0057235a722471e96b06d1767cadfa3dcf45ee76.png)  
 ![test123](https://us1.discourse-cdn.com/elastic/original/3X/b/0/b053650b68b84b6ff1791eb438b4063f9a2a21c8.png)

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [August 9, 2024, 6:34pm UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/7 "2024-08-09T18:34:34Z")

</div>

> [@Amol\_Nagotkar](#):
>
> 1. import apm from 'elastic-apm-node'  
> apm.start({ ... })
> 
> neither we are using TypeScript code that is compiled to JavaScript nor that JavaScript code using EcmaScript Modules completly  
> we are using some of feature of ESM and using babel .. it converts to plain JavaScript form

Please take a look at the " Hoisted ES module imports" section of [Starting the agent | APM Node.js Agent Reference [4.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/starting-the-agent.html#start-gotchas)  
It is possible you are hitting that issue: where **babel re-orders your imports** so that `apm.start(...)` comes too late to instrument your dependencies.

Are you able to show some of the babel-generated JavaScript code that is being executed by node? We would need to see the top section of the file that shows the `import ...`s and the `apm.start(...)` call. That would help clarify if you may have hit this issue.

If this is the issue, then I would suggest moving your APM start code to a separate file and either importing that file first, or preloading that file via `node --require ./apmsetup.js app.js`. That is documented at [Starting the agent | APM Node.js Agent Reference [4.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/starting-the-agent.html#start-option-separate-init-module)

---

<div class="post-metadata">

**Author:** ![Amol\_Nagotkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amol_nagotkar/32/124188_2.png) [@Amol\_Nagotkar](https://discuss.elastic.co/u/Amol_Nagotkar)\
**Post date:** [August 14, 2024, 7:29am UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/8 "2024-08-14T07:29:46Z")

</div>

tried above solution, but it didnt change.

---

<div class="post-metadata">

**Author:** ![Amol\_Nagotkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amol_nagotkar/32/124188_2.png) [@Amol\_Nagotkar](https://discuss.elastic.co/u/Amol_Nagotkar)\
**Post date:** [September 5, 2024, 12:18pm UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/9 "2024-09-05T12:18:44Z")

</div>

i have added apm before mongo initialisation. now i am able to see third party http calls. but still not mongo.  
mongo version 4.4.25  
my mongo node package version is "^4.4.1".  
which is suitable as mentioned here -\> [Supported technologies | APM Node.js Agent Reference [4.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/current/supported-technologies.html#compatibility-frameworks)

below are the logs getting printed for mongo-\>  
`  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.754Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"instrumenting mongodb ConnectionPool#checkOut"}  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.174Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"shimming http.Server.prototype.emit function"}  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.175Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"shimming http.ServerResponse.prototype.writeHead function"}  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.175Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"shimming http.request function"}  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.175Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"shimming http.get function"}  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.175Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"instrumenting https@16.5.0 module (enabled=true, isImportMod=false)"}  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.176Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"shimming https.Server.prototype.emit function"}  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.176Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"shimming https.request function"}  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.176Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"shimming https.get function"}  
{"log.level":"debug","@timestamp":"2024-09-05T12:17:06.812Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","message":"instrumenting mongodb/lib/cmap/connection\_pool.js@4.17.2 module (enabled=true, isImportMod=false)"}

`

 ![Screenshot from 2024-09-05 17-48-31](https://us1.discourse-cdn.com/elastic/original/3X/1/4/1458932bc5de6f23e5b34553960765b021f09ee4.png)

---

<div class="post-metadata">

**Author:** ![Amol\_Nagotkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amol_nagotkar/32/124188_2.png) [@Amol\_Nagotkar](https://discuss.elastic.co/u/Amol_Nagotkar)\
**Post date:** [September 9, 2024, 9:11am UTC](https://discuss.elastic.co/t/time-spent-by-span-type-showing-only-app-not-showing-db-http-redis/364459/10 "2024-09-09T09:11:24Z")

</div>

Solved this issue.. just use require instead of import for redis, mongo, etc.  
no need to specify anything in package.json file

Thank you
