पाठ 22 / 32

Logs, Metrics and Traces

यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।

Emit structured logs with correlation IDs, watch the right platform metrics, and trace requests across functions and queues.

लेखक: · 15 साल से वेब डेवलपर

Seeing inside many short-lived functions

With dozens of functions and no server to log into, telemetry is the only way to understand production. Logs: everything written to stdout or stderr is shipped to the platform's log service (CloudWatch Logs on AWS, Workers Logs on Cloudflare). Emit structured logs, one JSON object per line with fields such as level, message, request ID and business IDs, so you can filter and aggregate instead of searching free text. Metrics: the platform provides invocations, errors, duration, throttles and concurrency; add business metrics such as orders placed. Traces: one user action often flows through an API, a function, a queue and another function. Pass a correlation ID along every hop and use distributed tracing (AWS X-Ray, OpenTelemetry) to see where time goes. Watch p95 and p99 latency, not just the average, because cold starts hide in the tail.

Structured logs with a correlation ID

Every line is valid JSON, and the correlation ID from the incoming header ties this function's logs to the upstream request.

// One JSON object per log line: easy to search and filter in any log tool.
function log(level, msg, fields = {}) {
  console.log(JSON.stringify({ level, msg, ...fields }));
}

export const handler = async (event, context) => {
  const correlationId = event.headers?.['x-correlation-id'] ?? context.awsRequestId;
  log('info', 'order received', { correlationId, orderId: event.orderId });
  try {
    if (!event.items?.length) throw new Error('empty cart');
    log('info', 'order stored', { correlationId, orderId: event.orderId });
    return { statusCode: 201 };
  } catch (err) {
    log('error', 'order rejected', { correlationId, orderId: event.orderId, error: err.message });
    return { statusCode: 400 };
  }
};

await handler({ orderId: 'o-881', items: [], headers: { 'x-correlation-id': 'c-5f2a' } }, { awsRequestId: 'r-1' });

Output:

{"level":"info","msg":"order received","correlationId":"c-5f2a","orderId":"o-881"}
{"level":"error","msg":"order rejected","correlationId":"c-5f2a","orderId":"o-881","error":"empty cart"}

Logs cost money and can leak data

Never log passwords, tokens, full card numbers or Aadhaar numbers. Set a retention period on log groups (on CloudWatch Logs the default is to keep logs forever), and sample noisy debug logs, because log ingestion is often one of the bigger lines on a serverless bill.

त्वरित जाँच: Why should a latency dashboard show p99 and not only the average duration?

  • The average cannot be computed for serverless functions
  • p99 is always lower and therefore looks better in reports
  • Rare slow requests, such as cold starts, are hidden by the average but visible in the tail
  • Providers bill only for p99 duration
Answer

Rare slow requests, such as cold starts, are hidden by the average but visible in the tail — A small share of slow requests barely moves the average yet hurts real users. Tail percentiles expose them.