Observability is essential for enterprise applications. SAP CAP provides mechanisms for logging, monitoring, and tracing, enabling developers and operators to understand application behavior, detect issues, and optimize performance.
This guide explains CAP observability features, integration with SAP BTP tools, and best practices.
Why Observability Matters in CAP
- Detect errors and exceptions early
- Monitor service performance and database usage
- Analyze business-critical transactions
- Improve reliability and user experience
- Support compliance and auditing
Logging in CAP
CAP uses @sap/cds-logging and standard Node.js logging. Logs can include:
- Application events
- Service requests and responses
- Database queries
- Errors and exceptions
Example: Basic Logging
const cds = require('@sap/cds');
cds.on('bootstrap', app => {
app.use((req, res, next) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
next();
});
});
Structured Logging with @sap/cds-logging
const { logger } = require('@sap/cds-logging');
logger.info('CAP service started');
logger.error('Failed to process order', { orderId: 12345 });
Best Practices
- Use structured logs with context
- Log only necessary information to avoid sensitive data leaks
- Include timestamps, request IDs, and user IDs
- Use different log levels:
info,warn,error,debug
Monitoring CAP Applications
Monitoring ensures CAP apps perform optimally in production.
CAP Metrics
- Request counts and response times
- Error rates and exceptions
- Database query performance
- Memory and CPU usage
Integration with SAP BTP Monitoring
- Enable SAP Application Logging
- Use SAP Alert Notification for threshold breaches
- Use SAP Application Logging Viewer for log aggregation
- Export logs to Log Analytics services for dashboards
Example: Configure monitoring in mta.yaml for Cloud Foundry:
resources:
- name: cap-app-logs
type: com.sap.application-logging
Tracing in CAP
Tracing helps analyze request flows, especially in microservices architectures. CAP supports OpenTelemetry for distributed tracing.
Example: Enable Tracing
const { initTracing } = require('@sap/cds-telemetry');
initTracing();
Trace requests across services, capturing:
- HTTP calls
- Database queries
- Messaging events
- External API calls
Best Practices
- Correlate trace IDs across services
- Enable sampling for production to reduce overhead
- Integrate traces with SAP BTP Application Logging
Handling Errors and Exceptions
- Use
req.reject()for consistent error responses
srv.before('CREATE', 'Orders', req => {
if (!req.data.amount) req.reject(400, 'Amount is required');
});
- Log errors with context for easier troubleshooting
- Monitor error rates in dashboards to detect anomalies
Real-World Example
- CAP application exposes
OrdersandProductsservices - Logs every request and database query with user context
- Monitoring alerts on high error rates or slow queries
- Tracing shows the flow of an order creation across services
- Developers quickly identify bottlenecks and optimize queries
Best Practices for CAP Observability
- Combine logging, monitoring, and tracing for full observability
- Use structured logs and consistent log levels
- Integrate with SAP BTP tools for centralized monitoring
- Enable tracing for distributed microservices
- Protect sensitive information in logs
- Automate alerts for errors, performance, and thresholds
- Include user and tenant context for multi-tenant apps
Conclusion
Implementing logging, monitoring, and tracing in SAP CAP applications ensures robust, reliable, and observable enterprise systems. Leveraging CAP features with SAP BTP tools provides real-time insights, faster troubleshooting, and enhanced application performance. Observability is key to delivering high-quality CAP applications in production environments.

WhatsApp us