correlation-tracing
from aj-geddes/useful-ai-prompts
A curated collection of useful AI prompts for various tasks and use cases
50 stars4 forksUpdated Dec 28, 2025
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill correlation-tracingSKILL.md
Correlation & Distributed Tracing
Overview
Implement correlation IDs and distributed tracing to track requests across multiple services and understand system behavior.
When to Use
- Microservices architectures
- Debugging distributed systems
- Performance monitoring
- Request flow visualization
- Error tracking across services
- Dependency analysis
- Latency optimization
Implementation Examples
1. Correlation ID Middleware (Express)
import express from 'express';
import { v4 as uuidv4 } from 'uuid';
// Async local storage for context
import { AsyncLocalStorage } from 'async_hooks';
const traceContext = new AsyncLocalStorage<Map<string, any>>();
interface TraceContext {
traceId: string;
spanId: string;
parentSpanId?: string;
serviceName: string;
}
function correlationMiddleware(serviceName: string) {
return (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
// Extract or generate trace ID
const traceId = req.headers['x-trace-id'] as string || uuidv4();
const parentSpanId = req.headers['x-span-id'] as string;
const spanId = uuidv4();
// Set context
const context = new Map<string, any>();
context.set('traceId', traceId);
context.set('spanId', spanId);
context.set('parentSpanId', parentSpanId);
context.set('serviceName', serviceName);
// Inject trace headers
res.setHeader('X-Trace-Id', traceId);
res.setHeader('X-Span-Id', spanId);
// Run in context
traceContext.run(context, () => {
next();
});
};
}
// Helper to get current context
function getTraceContext(): TraceContext | null {
const context = traceContext.getStore();
if (!context) return null;
return {
traceId: context.get('traceId'),
spanId: context.get('spanId'),
parentSpanId: context.get('parentSpanId'),
serviceName: context.get('serviceName')
};
}
// Enhanced logger with trace context
class TracedLogger {
log(level: string, message: string, data?: any): void {
const context = getTraceContext();
const logEntry = {
level,
message,
...data,
...context,
timestamp: new Date().toISOString()
};
console.log(JSON.stringify(logEntry));
}
info(message: string, data?: any): void {
this.log('info', message, data);
}
error(message: string, data?: any): void {
this.log('error', message, data);
}
warn(message: string, data?: any): void {
this.log('warn', message, data);
}
}
const logger = new TracedLogger();
// HTTP client with trace propagation
async function tracedFetch(
url: string,
options: RequestInit = {}
): Promise<Response> {
const context = getTraceContext();
const headers = new Headers(options.headers);
if (context) {
headers.set('X-Trace-Id', context.traceId);
headers.set('X-Span-Id', context.spanId);
headers.set('X-Parent-Span-Id', context.spanId);
}
const startTime = Date.now();
try {
const response = await fetch(url, {
...options,
headers
});
const duration = Date.now() - startTime;
logger.info('HTTP request completed', {
method: options.method || 'GET',
url,
statusCode: response.status,
duration
});
return response;
} catch (error) {
const duration = Date.now() - startTime;
logger.error('HTTP request failed', {
method: options.method || 'GET',
url,
error: (error as Error).message,
duration
});
throw error;
}
}
// Usage
const app = express();
app.use(correlationMiddleware('api-service'));
app.get('/api/users/:id', async (req, res) => {
logger.info('Fetching user', { userId: req.params.id });
// Call another service with trace propagation
const response = await tracedFetch(
`http://user-service/users/${req.params.id}`
);
const data = await response.json();
logger.info('User fetched successfully');
res.json(data);
});
2. OpenTelemetry Integration
import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
// Configure OpenTelemetry
const sdk = new NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'my-service',
[SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
}),
traceExporter: new JaegerExporter({
endpoint: 'http://localhost:14268/api/traces',
}),
instrumentations: [
getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-http': {
enabled: true,
},
'@opentelemetry/instrumentation-express': {
enabled: true,
},
'@opentelemetry/instrumentation-pg': {
enabled: true,
},
}),
],
});
sdk.start(
...
Repository Stats
Stars50
Forks4
LicenseMIT License