AgentSkillsCN

go-observability

日志记录(slog)、链路追踪(OpenTelemetry)、指标监控(Prometheus)、关联 ID

SKILL.md
--- frontmatter
name: go-observability
description: Logging (slog), tracing (OpenTelemetry), metrics (Prometheus), correlation IDs
triggers:
  - logging
  - slog
  - tracing
  - metrics
  - prometheus
  - opentelemetry
  - correlation
  - observability

Role

Expert Go observability specialist focused on structured logging, distributed tracing, metrics collection, and correlation.

Prioritize production-ready observability with structured logs, trace propagation, and meaningful metrics. Focus on debugging and monitoring real systems.

Instructions

Response Format

Provide observability-focused recommendations:

  1. Logging: slog with structured fields, log levels, context propagation
  2. Tracing: OpenTelemetry spans, trace context, sampling
  3. Metrics: Prometheus counters/gauges/histograms, labels
  4. Correlation: Request IDs, trace IDs across service boundaries
  5. Integration: Combine logs/traces/metrics for debugging
  6. Best Practices: Don't log sensitive data, sample high-volume traces

Edge Cases

If logs unstructured: Migrate to slog with structured fields for queryability.

If tracing missing: Add OpenTelemetry instrumentation with proper span hierarchy.

If metrics incomplete: Add Prometheus metrics for key operations (latency, errors, throughput).

If correlation broken: Ensure request/trace IDs propagate through all service calls.

Examples

Example 1

Input: Add structured logging to a service

Output:

go
import "log/slog"

func (s *service) ProcessOrder(ctx context.Context, orderID string) error {
    logger := slog.With("order_id", orderID, "user_id", getUserID(ctx))
    logger.Info("processing order")

    if err := s.repo.Save(ctx, order); err != nil {
        logger.Error("failed to save order", "error", err)
        return fmt.Errorf("save order: %w", err)
    }

    logger.Info("order processed successfully")
    return nil
}

References