协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
observability-tracing-strategies【开发与工程】Implements best practices for distributed tracing in observability, improving application performance monitoring and debugging capabilities.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: observability-tracing-strategies description: Implements best practices for distributed tracing in observability, improving application performance monitoring and debugging capabilities. license: MIT compatibility: opencode metadata: version: "1.1.1" domain: devops triggers: observability tracing, distributed tracing, performance monitoring, debugging strategies, trace analysis archetypes: [implementation, reference] anti_triggers: [neglecting trace context, lack of visibility] response_profile: verbosity: medium directive_strength: high abstraction_level: operational role: implementation scope: infrastructure output-format: code
Distributed tracing plays a vital role in observability, allowing teams to monitor request paths across microservices and identify performance bottlenecks effectively. Here are key practices to improve your tracing strategies:
To implement tracing with OpenTelemetry in a Node.js application:
const { NodeTracerProvider } = require('@opentelemetry/node');
const provider = new NodeTracerProvider();
provider.register();
const tracer = provider.getTracer('my-service');
app.get('/', (req, res) => {
const span = tracer.startSpan('handler');
// Handle request
span.end();
res.send('Hello World');
});
Track key performance indicators like the average trace duration, the number of traces generated per request, and the rate of success for operations that rely on tracing for issue resolution.
By implementing solid distributed tracing strategies, organizations can significantly improve their application observability, leading to better performance monitoring and quicker debugging processes, ultimately enhancing user experience.
import logging
import time
from contextlib import contextmanager
from typing import Any
logger = logging.getLogger(__name__)
class TraceContext:
"""Manages distributed trace state."""
def __init__(self, trace_id=None, span_id=None):
self.trace_id = trace_id or _generate_id()
self.span_id = span_id or _generate_id()
self.parent_span_id = None
self.attributes = {}
def set_attribute(self, key: str, value: Any) -> None:
self.attributes[key] = value
class Tracer:
"""Simple distributed tracer with span management."""
def __init__(self, service_name: str):
self.service_name = service_name
@contextmanager
def start_span(self, operation: str, trace_context=None) -> TraceContext:
"""Start a new span within an optional parent context."""
if trace_context is not None:
ctx = TraceContext(trace_id=trace_context.trace_id, parent_span_id=trace_context.span_id)
else:
ctx = TraceContext()
ctx.attributes["service"] = self.service_name
start_time = time.monotonic()
try:
yield ctx
except Exception as e:
duration = time.monotonic() - start_time
logger.error("Span error: op=%s duration=%.3f", operation, duration)
raise
else:
duration = time.monotonic() - start_time
logger.info("Span completed: %s (%.3fs)", operation, duration)
def _generate_id():
import uuid
return uuid.uuid4().hex[:16]
# Usage in a microservice:
# tracer = Tracer("order-service")
# with tracer.start_span("process_order") as ctx:
# ctx.set_attribute("order_id", order_id)
Authoritative documentation links for this skill's domain. The model follows markdown links to resolve external references and inline content.