可观测性:日志、指标与追踪
集成全面的日志记录、指标收集和分布式追踪,深入了解 LLM 应用的运行行为。
可观测性:日志、指标与追踪 是 CoddyKit 上的免费 LLM Apps in Production (RAG + Vector DB + Caching) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LLM Apps in Production (RAG + Vector DB + Caching) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Observability?
In this lesson, we'll explore observability, a crucial concept for managing complex software systems, especially LLM applications.
Observability means understanding the internal state of a system by examining the data it produces. Think of it as having X-ray vision into your application's behavior.
For LLM apps, this helps us answer critical questions like:
- Why is a request slow?
- Is the RAG retrieval working as expected?
- Are we incurring unexpected costs?
Logs: Recording Events
Logs are timestamped records of events that happen within your application. They are like a diary of your system's activities.
For LLM applications, logs are essential for:
- Tracking incoming user prompts.
- Storing responses from the LLM.
- Recording intermediate steps in a RAG pipeline (e.g., documents retrieved).
- Capturing errors or warnings.
They provide detailed contextual information for debugging and post-mortem analysis.
Logging LLM Interactions
Here's a simple Python example demonstrating how to log an LLM interaction. We're using Python's built-in logging module.
This helps you see exactly what prompts were sent and what responses were received, which is vital for debugging and improving your application.
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def call_llm(prompt):
logging.info(f"LLM Request: '{prompt[:40]}...' ")
# Simulate LLM processing
response = f"Simulated response to: {prompt}"
logging.info(f"LLM Response: '{response[:40]}...' ")
return response
if __name__ == "__main__":
user_prompt = "Explain observability simply."
result = call_llm(user_prompt)
print(f"Application output: {result}")Metrics: Measuring Performance
Metrics are numerical measurements collected over time, providing aggregated insights into your system's health and performance.
Unlike logs, which are individual events, metrics are typically quantitative values that can be visualized as graphs and dashboards. Key metrics for LLM apps include:
- Latency: How long it takes for the LLM to respond.
- Token Usage: Input/output tokens consumed per request.
- Error Rate: Percentage of failed LLM calls or RAG retrievals.
- Cache Hit Rate: How often cached responses are used.
Collecting Custom Metrics
You can collect custom metrics to understand specific aspects of your LLM application. This example shows how to track the number of LLM calls and their average latency.
In a real-world scenario, you'd send these metrics to a monitoring system like Prometheus or Datadog.
import time
class LLMMetrics:
def __init__(self):
self.total_calls = 0
self.total_latency = 0.0
def record_call(self, duration):
self.total_calls += 1
self.total_latency += duration
def get_avg_latency(self):
if self.total_calls == 0:
return 0.0
return self.total_latency / self.total_calls
metrics_store = LLMMetrics()
def call_llm_with_metrics(prompt):
start_time = time.time()
# Simulate LLM processing
time.sleep(0.05) # simulate 50ms work
response = f"Simulated reply to: {prompt}"
end_time = time.time()
metrics_store.record_call(end_time - start_time)
return response
if __name__ == "__main__":
print("Collecting LLM call metrics...")
call_llm_with_metrics("Hi")
call_llm_with_metrics("How are you?")
print(f"Total calls: {metrics_store.total_calls}")
print(f"Avg latency: {metrics_store.get_avg_latency():.3f}s")Tracing: Following Request Paths
Tracing is about following a single request as it flows through multiple services and components in a distributed system. This is especially vital for RAG applications that involve many steps: user input, embedding generation, vector DB lookup, LLM call, etc.
A trace visualizes the entire journey of a request, showing the exact path it took and the time spent in each operation.
Traces, Spans, and Context
A trace is a complete end-to-end journey of a request. It's composed of multiple spans.
- A span represents a single operation or unit of work within a trace (e.g., 'retrieve documents', 'call embedding model', 'invoke LLM').
- Spans have a parent-child relationship, forming a tree structure that shows dependencies.
- Context propagation ensures that a unique trace ID follows the request across different services, linking all related spans together.
This helps pinpoint bottlenecks or failures across microservices.
OpenTelemetry for Tracing
While implementing tracing from scratch is complex, tools like OpenTelemetry (an open-source observability framework) provide standardized ways to instrument your code.
You'd use OpenTelemetry SDKs to:
- Start a new trace when a request comes in.
- Create new spans for each significant operation (e.g., a function call to a vector database or an LLM API).
- Propagate the trace context to downstream services.
This allows you to visualize the full request flow in a tracing UI.
The Observability Triangle
Logs, metrics, and traces are often called the "observability triangle" because they offer complementary views of your system:
- Logs: The granular details and events.
- Metrics: The aggregated numbers and trends.
- Traces: The end-to-end journey of a request.
Together, they provide a comprehensive understanding of your LLM application's behavior, making it easier to diagnose issues, optimize performance, and ensure reliability in production.
Quick Check: Observability
You've learned about the three pillars of observability. Let's see if you can distinguish their primary uses.
Recap: Deep Insights
Congratulations! You've explored the world of observability for LLM applications.
- We defined observability as understanding internal system state from external data.
- We learned about logs for detailed event recording.
- We covered metrics for aggregated performance measurements.
- We understood traces for visualizing end-to-end request flows.
By integrating these three pillars, you gain powerful insights, enabling you to build more reliable, performant, and cost-efficient LLM systems.
常见问题解答
「可观测性:日志、指标与追踪」课时是免费的吗?
是的 — 「可观测性:日志、指标与追踪」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LLM Apps in Production (RAG + Vector DB + Caching) 课程的其余内容,请升级到 CoddyKit PRO。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。
「可观测性:日志、指标与追踪」这节课中我会学到什么?
集成全面的日志记录、指标收集和分布式追踪,深入了解 LLM 应用的运行行为。 你通过在浏览器中直接运行的动手代码来练习 LLM Apps in Production (RAG + Vector DB + Caching),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 LLM Apps in Production (RAG + Vector DB + Caching) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 LLM Apps in Production (RAG + Vector DB + Caching) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「可观测性:日志、指标与追踪」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 LLM Apps in Production (RAG + Vector DB + Caching) 课中编写并运行代码吗?
能。每节 LLM Apps in Production (RAG + Vector DB + Caching) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- RAG 组件的水平扩展
- 可观测性:日志、指标与追踪
- LLM 运维的告警与事件响应
- 负载测试与容量规划