atp-sdk
Version:
Official TypeScript SDK for Agent Trust Protocolβ’ - Build secure, verifiable, and trustworthy applications with decentralized identity, verifiable credentials, payment protocols (AP2/ACP), and robust access control
143 lines (122 loc) β’ 5.43 kB
JavaScript
// Monitoring & Health Checks Example
// Run with: node packages/sdk/examples/10-monitoring-health.js
//
// This example demonstrates:
// 1. Testing connectivity to all ATP services
// 2. Getting gateway health status
// 3. Checking service-specific health endpoints
// 4. Monitoring connection statistics
import { ATPClient, createQuickConfig } from 'atp-sdk';
async function main() {
console.log('π₯ ATP SDK - Health & Monitoring Example\n');
// Create client with local services (default)
const config = createQuickConfig('http://localhost');
const client = new ATPClient(config);
try {
// 1. Test connectivity to all services
console.log('1οΈβ£ Testing connectivity to all ATP services...');
const connectivity = await client.testConnectivity();
console.log('\nπ Connectivity Results:');
console.log(` Identity Service: ${connectivity.identity ? 'β
' : 'β'}`);
console.log(` Credentials Service: ${connectivity.credentials ? 'β
' : 'β'}`);
console.log(` Permissions Service: ${connectivity.permissions ? 'β
' : 'β'}`);
console.log(` Audit Service: ${connectivity.audit ? 'β
' : 'β'}`);
console.log(` Gateway Service: ${connectivity.gateway ? 'β
' : 'β'}`);
console.log(` Overall: ${connectivity.overall ? 'β
All Healthy' : 'β Some Services Down'}`);
if (!connectivity.overall) {
console.log('\nβ οΈ Some services are not responding. Check docker-compose.yml or service URLs.');
process.exitCode = 1;
return;
}
// 2. Get detailed gateway health status
console.log('\n2οΈβ£ Getting gateway health status...');
try {
const health = await client.gateway.getHealth();
console.log('\nπ Gateway Health:');
console.log(` Status: ${health.data?.status || 'unknown'}`);
if (health.data?.services) {
console.log('\n Service Health:');
Object.entries(health.data.services).forEach(([service, status]) => {
const icon = status?.status === 'up' ? 'β
' : 'β';
console.log(` ${icon} ${service}: ${status?.status || 'unknown'}`);
});
}
} catch (error) {
console.log(` β οΈ Could not fetch gateway health: ${error.message}`);
}
// 3. Get gateway status with load information
console.log('\n3οΈβ£ Getting gateway status (with load info)...');
try {
const status = await client.gateway.getStatus();
if (status.data) {
console.log('\nπ Gateway Status:');
console.log(` Status: ${status.data.status}`);
console.log(` Version: ${status.data.version || 'N/A'}`);
if (status.data.load) {
console.log('\n Load Metrics:');
console.log(` CPU: ${status.data.load.cpu}%`);
console.log(` Memory: ${status.data.load.memory}%`);
console.log(` Connections: ${status.data.load.connections}`);
}
}
} catch (error) {
console.log(` β οΈ Could not fetch gateway status: ${error.message}`);
}
// 4. Get connection statistics
console.log('\n4οΈβ£ Getting connection statistics...');
try {
const stats = await client.gateway.getConnectionStats();
if (stats.data) {
console.log('\nπ Connection Stats:');
console.log(` Total Connections: ${stats.data.totalConnections}`);
console.log(` Active Connections: ${stats.data.activeConnections}`);
console.log(` HTTP: ${stats.data.httpConnections}`);
console.log(` WebSocket: ${stats.data.wsConnections}`);
console.log(` TLS: ${stats.data.tlsConnections}`);
if (stats.data.connectionsByService) {
console.log('\n By Service:');
Object.entries(stats.data.connectionsByService).forEach(([service, count]) => {
console.log(` ${service}: ${count}`);
});
}
}
} catch (error) {
console.log(` β οΈ Could not fetch connection stats: ${error.message}`);
}
// 5. Simple health check function for use in your app
console.log('\n5οΈβ£ Example: Simple health check function...\n');
console.log(`
// Add this to your Express/API health endpoint:
import { ATPClient, createQuickConfig } from 'atp-sdk';
export async function healthCheck(req, res) {
const client = new ATPClient(createQuickConfig('http://localhost'));
try {
const connectivity = await client.testConnectivity();
const isHealthy = connectivity.overall;
res.status(isHealthy ? 200 : 503).json({
status: isHealthy ? 'healthy' : 'degraded',
services: connectivity,
timestamp: new Date().toISOString()
});
} catch (error) {
res.status(503).json({
status: 'unhealthy',
error: error.message,
timestamp: new Date().toISOString()
});
}
}
`);
console.log('\nβ
Monitoring example completed!\n');
} catch (error) {
console.error('\nβ Monitoring example failed:', error.message);
console.error('\nHint: Ensure ATP services are running:');
console.error(' docker compose up -d');
console.error('\nOr set environment variables:');
console.error(' ATP_GATEWAY_URL=http://your-gateway:3000');
console.error(' ATP_IDENTITY_URL=http://your-identity:3001');
console.error(' ... (see docs for all service URLs)');
process.exitCode = 1;
}
}
main();