Skip to main content

OpenTelemetry Tracing

Panduan lengkap implementasi OpenTelemetry (OTEL) untuk observability di MStore Backend.

🎯 Overview

MStore Backend menggunakan OpenTelemetry untuk:
  • Distributed Tracing - Track request flow across services
  • Structured Logging - Contextual logs dengan correlation ID
  • Metrics Collection - Performance metrics & monitoring
Package: pkg/utils/logger/otel.go

📊 Architecture

OTEL Stack


🚀 Quick Start

1. Initialize OTEL


🎨 Trace Layers

Layer Types

MStore menggunakan 4 layer standar untuk tracing:

📝 Usage Examples

Handler Layer


Service Layer


Repository Layer


External API Layer


🔧 Helper Functions

StartLayerSpan

Memulai span baru dengan layer context.
Example:

StartLayerSpanFromFiber

Memulai span dari Fiber context (untuk handlers).
Example:

TraceStep

Mencatat step dalam span (tanpa error).
Example:

TraceStepErr

Mencatat step dengan error.
Example:

AddStepEvent

Menambahkan event dengan attributes ke span.
Example:

EndLayerSpan

Mengakhiri span dengan error handling.
Example:

🎯 Best Practices

1. Always Defer EndLayerSpan


2. Use Descriptive Operation Names


3. Add Meaningful Attributes


4. Trace Important Steps


5. Always Log Errors


📊 Viewing Traces

Grafana Tempo

Access: http://localhost:3000/explore Query Examples:

Trace Structure


🔍 Troubleshooting

Traces Not Appearing

Check:
  1. OTLP endpoint accessible
  2. Service name configured
  3. Spans properly closed
  4. No network issues

High Cardinality Attributes

Problem: Too many unique attribute values Solution: Use bounded values

Missing Context

Problem: Context not propagated Solution: Always pass context

🎨 Configuration

Environment Variables


Sampling Strategy

Development: 100% sampling
Production: Adaptive sampling

System Design

Overall system architecture

Observability (LGTM)

LGTM stack overview

Grafana Tempo

Tempo traces documentation

Error Handling

Error handling best practices

OpenTelemetry: Tracing diaktifkan secara default di development & production. Gunakan EnableTraceSteps = false untuk disable step events.