๐Ÿ”ง DevOps

Tracing

Tracing

Tracks request paths in distributed systems. Jaeger, Zipkin.

Detailed Explanation

Distributed Tracing is a technique for tracking the path a single request takes through multiple services in a microservices environment. It began with Google's Dapper paper in 2010, and today OpenTelemetry has established itself as the standard.

The core concepts are Trace (the entire request path), Span (an individual unit of work), and Context (identifying information passed between services). When a user request comes in, a unique Trace ID is generated, and this ID is propagated to every service call to link the entire path.

The core of tracing is Context Propagation. The Trace ID and Span ID are passed between services via HTTP headers (traceparent, b3, etc.) or gRPC metadata. Each service creates its own Span and links it to the parent Span ID to build the call tree.

In practice, it is essential when analyzing slow API responses, intermittent timeouts, and error propagation paths. Traces are collected in backends such as Jaeger, Zipkin, and Grafana Tempo, and bottlenecks are found using visualized waterfall charts. On average, the time to identify the cause of an incident is reduced by more than 80%.

Code Example

// OpenTelemetry๋ฅผ ์‚ฌ์šฉํ•œ Node.js ํŠธ๋ ˆ์ด์‹ฑ ์„ค์ •
// tracing.js
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');

const sdk = new NodeSDK({
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: 'user-service',
    [SemanticResourceAttributes.SERVICE_VERSION]: '1.0.0',
    [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: process.env.NODE_ENV,
  }),
  traceExporter: new OTLPTraceExporter({
    url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://tempo:4317',
  }),
  instrumentations: [
    getNodeAutoInstrumentations({
      '@opentelemetry/instrumentation-fs': { enabled: false },
      '@opentelemetry/instrumentation-http': { enabled: true },
      '@opentelemetry/instrumentation-express': { enabled: true },
    }),
  ],
});

sdk.start();

// ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ์—์„œ ์ปค์Šคํ…€ span ์ƒ์„ฑ
// userController.js
const { trace, SpanStatusCode } = require('@opentelemetry/api');

const tracer = trace.getTracer('user-service');

async function createUser(req, res) {
  // ์ปค์Šคํ…€ span ์‹œ์ž‘
  const span = tracer.startSpan('createUser', {
    attributes: {
      'user.email': req.body.email,
    },
  });

  try {
    // ์ค‘์ฒฉ span์œผ๋กœ ์„ธ๋ถ€ ์ž‘์—… ์ถ”์ 
    const validationSpan = tracer.startSpan('validateInput');
    await validateUserInput(req.body);
    validationSpan.end();

    const dbSpan = tracer.startSpan('saveToDatabase');
    const user = await db.users.create(req.body);
    dbSpan.setAttribute('db.rows_affected', 1);
    dbSpan.end();

    // ์™ธ๋ถ€ ์„œ๋น„์Šค ํ˜ธ์ถœ (์ž๋™์œผ๋กœ context ์ „ํŒŒ๋จ)
    const notificationSpan = tracer.startSpan('sendWelcomeEmail');
    await notificationService.sendWelcomeEmail(user.email);
    notificationSpan.end();

    span.setStatus({ code: SpanStatusCode.OK });
    res.json({ success: true, user });

  } catch (error) {
    span.setStatus({
      code: SpanStatusCode.ERROR,
      message: error.message,
    });
    span.recordException(error);
    res.status(500).json({ error: error.message });

  } finally {
    span.end();
  }
}

// package.json ์‹คํ–‰ ์Šคํฌ๋ฆฝํŠธ
// "start": "node --require ./tracing.js app.js"

How It's Said in Practice

Senior: "The order API intermittently takes over 5 seconds. I can't tell where the problem is just from the logs."

Junior: "I'll look it up in Jaeger by Trace ID. Oh, the payment service takes 3.2 seconds and the DB query takes 1.5 seconds."

Senior: "Expand the span on the payment service side. It looks like the external PG provider API call is the bottleneck."

Interviewer: "How did you debug performance issues in a microservices environment?"

Candidate: "I implemented distributed tracing with OpenTelemetry. I applied auto-instrumentation to all services and added custom spans to the key business logic. By linking traces and logs in Grafana, I was able to see the full path of a specific request and the time spent in each segment at a glance."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "You're calling an external API, but there's no span. Without it, we can't measure the time for this segment."

Developer: "I'll add tracer.startSpan to the axios request. Oh, doesn't the HTTP instrumentation do that automatically?"

Reviewer: "Auto-instrumentation exists, but adding custom attributes that carry business meaning makes searching easier."

Cautions

Learn More