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

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
ports:
- "9200:9200"
environment:
- discovery.type=single-node

kibana:
image: 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
environment:
- apm-server.host=0.0.0.0:8200
- output.elasticsearch.hosts=["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)

serverUrl: 'http://localhost:8200',

// Set the service {environment

environment: "development"

})

Please help.


Can anyone help me out with this ? as i see this doc Trace sample timeline | Elastic Observability [8.14] | Elastic

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"}

From Elasticsearch to APM

Added nodejs and removed docker, elastic-stack-monitoring

@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:

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)?

If you are using ESM, then you will need to read ECMAScript module support | APM Node.js Agent Reference [4.x] | Elastic
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.

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:

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.


Please take a look at the " Hoisted ES module imports" section of Starting the agent | APM Node.js Agent Reference [4.x] | Elastic
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

tried above solution, but it didnt change.

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

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)"}

`

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

Thank you