pyroscope
pyroscope copied to clipboard
memory profiling not happening for node js server version 20.
Describe the bug
I have enable pyroscope profiling, only the cpu profiling is coming, not memory
Expected behavior
Both cpu and memory should be coming atleast
Environment
nodejs (v20.19.0) @pyroscope/nodejs ("^0.4.3")
Additional Context
docker file for pyrsoscope
pyroscope:
image: pyroscope/pyroscope:latest
ports:
- "4040:4040"
command:
- "server"
- "--no-self-profiling=true"
volumes:
- pyroscope_data:/var/lib/pyroscope
environment:
- PYROSCOPE_SERVER_HTTP_LISTEN_PORT=4040
- PYROSCOPE_LOG_LEVEL=debug
- PYROSCOPE_RETENTION_PERIOD=12h
- PYROSCOPE_PROFILES_ENABLED=cpu,mem,alloc,inuse_space,inuse_objects
server code.
// Initialize Pyroscope with debug logging
const initializePyroscope = () => {
console.log("Initializing Pyroscope...");
try {
Pyroscope.init({
serverAddress: "http://localhost:4040",
appName: "rental-backend",
tags: {
environment: process.env.NODE_ENV || "development",
},
// Enhanced profiling configuration
profilingTypes: ['cpu', 'heap', 'heap-alloc'],
// heapSamplingIntervalBytes: 524288, // Default value
// heapStackDepth: 64, // Default value
enableApiProfiling: true,
// Add debug logging
logger: {
level: "debug",
},
});
console.log("Pyroscope initialized successfully");
return true;
} catch (error) {
console.error("Failed to initialize Pyroscope:", error);
return false;
}
};
// Update the setupRouteProfiling function to use a different approach
const setupRouteProfiling = () => {
return (req, res, next) => {
const route = req.path || req.originalUrl;
const method = req.method;
// Skip profiling for metrics endpoint
if (route === '/metrics') return next();
try {
// Create profiling tags
const tags = {
route: route,
method: method,
environment: process.env.NODE_ENV || 'development'
};
// Start profiling with tags
Pyroscope.start({
tags: tags
});
// Continue request processing
next();
// Clean up on response finish
res.on('finish', () => {
try {
Pyroscope.stop();
} catch (error) {
console.error('Error stopping Pyroscope profiling:', error);
}
});
} catch (error) {
console.error('Error in route profiling:', error);
next(); // Continue even if profiling fails
}
};
};
// Initialize Pyroscope before setting up any middleware
metricsService.initializePyroscope();
// Initiate express
const app = expressService();
// Add Pyroscope route profiling middleware
app.use(metricsService.setupRouteProfiling());
checked the logs, in that only the cpu data is going
The code you shared starts and stops the profiler on every request.
// ref: setupRouteProfiling
// Start profiling with tags
Pyroscope.start({
tags: tags
});
// ...
// Clean up on response finish
res.on('finish', () => {
try {
Pyroscope.stop();
} catch (error) {
console.error('Error stopping Pyroscope profiling:', error);
}
});
Make sure you only start and stop the profiler once per application run. I recommend using wrapWithLabels in the middleware if you're interested in labeling profiles for specific routes. Keep in mind this will only work for CPU profiles, as memory profiles can only be statically labeled.