1. 引言:LLM开发中的监控挑战

在2025年的大模型(LLM)开发实践中,实时监控已成为确保模型训练效率和生产部署稳定性的关键环节。与传统软件开发不同,LLM项目面临着独特的监控挑战:

  1. 训练过程复杂:LLM训练周期长、资源消耗大,需要实时监控训练进度、损失函数变化、资源利用率等关键指标
  2. 实验迭代频繁:研究人员需要快速比较不同超参数组合、不同模型架构的实验效果
  3. 生产部署风险高:LLM服务在生产环境中可能面临流量波动、生成质量下降、安全漏洞等风险
  4. 性能指标多维:除了传统的延迟、吞吐量等指标外,还需关注回复质量、幻觉率、偏见程度等特定指标

一个完善的监控仪表盘系统能够帮助开发团队实时掌握模型训练和服务运行状态,及时发现和解决问题,优化资源使用效率。本文将深入探讨如何构建LLM开发环境的监控仪表盘,涵盖从训练监控到生产运维的全方位观测体系。

2. 监控仪表盘的核心组件

2.1 监控体系架构设计

在2025年,一个成熟的LLM开发监控体系通常采用分层架构设计:

+-------------------------------------+
|             可视化层                 |
|  (Grafana, MLflow UI, Wandb Dashboard) |
+-------------------------------------+
|             存储层                   |
|  (Prometheus TSDB, SQLite, S3, HDFS) |
+-------------------------------------+
|             采集层                   |
| (Exporters, Agents, SDKs, Log Collectors) |
+-------------------------------------+
|             目标层                   |
|  (GPU集群, 容器, LLM服务, API网关)    |
+-------------------------------------+

这种分层架构具有以下优势:

  1. 解耦设计:各层职责明确,便于独立升级和维护
  2. 可扩展性:支持横向扩展采集点和存储容量
  3. 灵活性:可根据不同监控需求选择合适的组件
  4. 统一视图:将不同来源的监控数据整合到统一仪表盘

2.2 关键监控工具介绍

在2025年的LLM开发实践中,以下工具成为监控仪表盘建设的主流选择:

  1. Prometheus + Grafana

    • Prometheus:开源监控系统,专为时间序列数据设计,具有强大的查询语言和告警功能
    • Grafana:领先的开源可视化平台,支持多种数据源,提供丰富的图表类型和自定义仪表盘功能
    • 应用场景:基础设施监控、容器监控、API性能监控等
  2. MLflow

    • 功能:实验跟踪、模型管理、模型部署、模型注册
    • 特点:与主流ML框架无缝集成,支持本地和分布式部署
    • 应用场景:实验对比、模型版本控制、模型性能监控等
  3. Weights & Biases (Wandb)

    • 功能:实验跟踪、超参数优化、数据集版本控制、模型可视化
    • 特点:云端服务,开箱即用,强大的可视化能力
    • 应用场景:研究实验监控、团队协作、模型性能分析等
  4. DeepEval

    • 功能:LLM评测指标支持、实时性能仪表盘、细粒度调用分析
    • 特点:专为LLM应用设计,开源工具驱动
    • 应用场景:生产环境监控、质量评估、持续优化等
  5. 自定义监控框架

    • 特点:根据特定需求定制,灵活度高
    • 应用场景:特定业务指标监控、自定义告警规则等

3. 构建基础设施监控仪表盘

3.1 Prometheus与Grafana部署

在LLM开发环境中,基础设施监控是确保训练和推理稳定性的基础。以下是2025年Prometheus与Grafana的标准部署流程:

  1. 使用Docker Compose部署
# docker-compose.yml
version: '3.8'

services:
  prometheus:
    image: prom/prometheus:v2.52.0
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle'
    ports:
      - "9090:9090"
    restart: unless-stopped
    networks:
      - monitoring

  grafana:
    image: grafana/grafana:11.0.0
    depends_on:
      - prometheus
    ports:
      - "3000:3000"
    volumes:
      - grafana_data:/var/lib/grafana
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=secure_password
      - GF_USERS_ALLOW_SIGN_UP=false
    restart: unless-stopped
    networks:
      - monitoring

  node_exporter:
    image: prom/node-exporter:v1.8.2
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    command:
      - '--path.procfs=/host/proc'
      - '--path.rootfs=/rootfs'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
    ports:
      - "9100:9100"
    restart: unless-stopped
    networks:
      - monitoring

  cadvisor:
    image: gcr.io/cadvisor/cadvisor:v0.50.0
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
      - /dev/disk/:/dev/disk:ro
    ports:
      - "8080:8080"
    restart: unless-stopped
    networks:
      - monitoring

networks:
  monitoring:
    driver: bridge

volumes:
  prometheus_data:
  grafana_data:
  1. Prometheus配置文件
# prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    static_configs:
      - targets: ['node_exporter:9100']

  - job_name: 'cadvisor'
    static_configs:
      - targets: ['cadvisor:8080']

  - job_name: 'gpu_exporter'
    static_configs:
      - targets: ['gpu_exporter:9400']

  - job_name: 'llm_service'
    metrics_path: '/metrics'
    static_configs:
      - targets: ['llm-service:8000']

3.2 GPU资源监控

对于LLM训练和推理,GPU资源监控至关重要。以下是2025年流行的GPU监控解决方案:

  1. NVIDIA DCGM Exporter
# 在docker-compose.yml中添加
gpu_exporter:
  image: nvidia/dcgm-exporter:3.4.0
  runtime: nvidia
  environment:
    - NVIDIA_VISIBLE_DEVICES=all
  ports:
    - "9400:9400"
  restart: unless-stopped
  networks:
    - monitoring
  1. 自定义GPU监控脚本
# gpu_monitor.py
import subprocess
import time
import requests
import json

def get_gpu_metrics():
    result = subprocess.run(['nvidia-smi', '--query-gpu=index,name,utilization.gpu,utilization.memory,memory.total,memory.used,temperature.gpu,power.draw', '--format=csv,noheader,nounits'], 
                           stdout=subprocess.PIPE, universal_newlines=True)
    metrics = []
    for line in result.stdout.strip().split('\n'):
        parts = line.split(', ')
        if len(parts) == 8:
            metrics.append({
                'gpu_index': int(parts[0]),
                'gpu_name': parts[1],
                'gpu_util': float(parts[2]),
                'mem_util': float(parts[3]),
                'mem_total': float(parts[4]),
                'mem_used': float(parts[5]),
                'temp': float(parts[6]),
                'power': float(parts[7])
            })
    return metrics

def push_to_prometheus_gateway(metrics):
    gateway_url = 'http://pushgateway:9091/metrics/job/gpu_metrics'
    payload = ''
    
    for gpu in metrics:
        payload += f'gpu_utilization{{gpu="{gpu["gpu_name"]}",index="{gpu["gpu_index"]}"}} {gpu["gpu_util"]}\n'
        payload += f'gpu_memory_utilization{{gpu="{gpu["gpu_name"]}",index="{gpu["gpu_index"]}"}} {gpu["mem_util"]}\n'
        payload += f'gpu_memory_used{{gpu="{gpu["gpu_name"]}",index="{gpu["gpu_index"]}"}} {gpu["mem_used"]}\n'
        payload += f'gpu_memory_total{{gpu="{gpu["gpu_name"]}",index="{gpu["gpu_index"]}"}} {gpu["mem_total"]}\n'
        payload += f'gpu_temperature{{gpu="{gpu["gpu_name"]}",index="{gpu["gpu_index"]}"}} {gpu["temp"]}\n'
        payload += f'gpu_power_draw{{gpu="{gpu["gpu_name"]}",index="{gpu["gpu_index"]}"}} {gpu["power"]}\n'
    
    response = requests.post(gateway_url, data=payload)
    print(f'Pushed metrics: {response.status_code}')

if __name__ == '__main__':
    while True:
        metrics = get_gpu_metrics()
        push_to_prometheus_gateway(metrics)
        time.sleep(15)

3.3 容器和服务监控

在容器化部署LLM服务时,需要监控容器状态和服务健康状况:

  1. Docker容器监控

    • 使用cAdvisor收集容器指标
    • 在Prometheus中配置容器监控规则
  2. 服务健康检查

# prometheus.yml中添加
service_monitor:
  job_name: 'llm-service-health'
  metrics_path: '/health'
  scrape_interval: 5s
  static_configs:
    - targets: ['llm-service:8000']
  relabel_configs:
    - source_labels: [__address__]
      target_label: instance
  1. API性能监控
# 在LLM服务中集成Prometheus客户端
from prometheus_client import Counter, Histogram, Summary, start_http_server

# 定义指标
REQUEST_COUNT = Counter('llm_requests_total', 'Total LLM API requests', ['endpoint', 'model', 'status'])
REQUEST_LATENCY = Histogram('llm_request_latency_seconds', 'LLM API request latency in seconds', ['endpoint', 'model'])
TOKEN_COUNT = Summary('llm_tokens_processed', 'Total tokens processed', ['direction', 'model'])

# 在API端点中使用
@app.route('/api/generate', methods=['POST'])
def generate():
    model = request.json.get('model', 'default')
    
    # 记录请求开始
    start_time = time.time()
    
    try:
        # 处理请求...
        
        # 记录输入和输出token数
        TOKEN_COUNT.labels(direction='input', model=model).observe(input_tokens)
        TOKEN_COUNT.labels(direction='output', model=model).observe(output_tokens)
        
        # 记录成功请求
        REQUEST_COUNT.labels(endpoint='/api/generate', model=model, status='success').inc()
        
        return jsonify({'generated_text': result})
    except Exception as e:
        # 记录失败请求
        REQUEST_COUNT.labels(endpoint='/api/generate', model=model, status='error').inc()
        return jsonify({'error': str(e)}), 500
    finally:
        # 记录请求延迟
        REQUEST_LATENCY.labels(endpoint='/api/generate', model=model).observe(time.time() - start_time)

# 启动Prometheus指标服务器
start_http_server(8000)

3.4 自定义Grafana仪表盘

在2025年,Grafana提供了丰富的可视化选项,用于创建LLM开发环境的监控仪表盘:

  1. 基础设施概览仪表盘

    • 服务器CPU、内存、磁盘使用率
    • GPU利用率、显存使用、温度
    • 网络流量和延迟
  2. LLM服务监控仪表盘

    • API请求量和延迟分布
    • 模型响应时间趋势
    • Token处理速率
    • 错误率和失败类型分布
  3. 创建自定义仪表盘的步骤

    1. 登录Grafana (默认 http://localhost:3000)
    2. 添加Prometheus数据源 (配置 URL: http://prometheus:9090)
    3. 创建新仪表盘并添加面板
    4. 配置面板查询 (例如: sum(rate(llm_requests_total[5m])) by (endpoint))
    5. 设置告警规则和通知渠道
    6. 保存仪表盘并设置自动刷新间隔
    
  4. 仪表盘配置示例

{
  "panels": [
    {
      "title": "LLM API请求量",
      "type": "graph",
      "datasource": "Prometheus",
      "targets": [
        {
          "expr": "sum(rate(llm_requests_total[5m])) by (endpoint)",
          "legendFormat": "{{endpoint}}",
          "interval": ""
        }
      ],
      "options": {
        "tooltip": { "mode": "multi" },
        "legend": { "show": true }
      }
    },
    {
      "title": "GPU利用率",
      "type": "gauge",
      "datasource": "Prometheus",
      "targets": [
        {
          "expr": "avg(gpu_utilization) by (gpu)",
          "legendFormat": "{{gpu}}",
          "interval": ""
        }
      ],
      "options": {
        "minValue": 0,
        "maxValue": 100,
        "thresholds": [
          { "color": "green", "value": null },
          { "color": "orange", "value": 70 },
          { "color": "red", "value": 90 }
        ]
      }
    }
  ]
}

4. 实验跟踪与模型监控

4.1 MLflow部署与配置

MLflow是2025年LLM开发中最流行的实验跟踪和模型管理工具之一。以下是标准部署流程:

  1. 本地开发环境部署
# 安装MLflow
pip install mlflow==2.17.0

# 启动MLflow服务器
mlflow server \
  --backend-store-uri sqlite:///mlflow.db \
  --default-artifact-root ./mlruns \
  --host 0.0.0.0 \
  --port 5000
  1. 生产环境分布式部署
# docker-compose-mlflow.yml
version: '3.8'

services:
  mlflow-server:
    image: ghcr.io/mlflow/mlflow:v2.17.0
    ports:
      - "5000:5000"
    environment:
      - MLFLOW_S3_ENDPOINT_URL=http://minio:9000
      - AWS_ACCESS_KEY_ID=minioadmin
      - AWS_SECRET_ACCESS_KEY=minioadmin
    command:
      - server
      - --backend-store-uri
      - mysql+pymysql://root:password@mysql:3306/mlflow
      - --default-artifact-root
      - s3://mlflow-artifacts/
      - --host
      - 0.0.0.0
    depends_on:
      - mysql
      - minio

  mysql:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=mlflow
    volumes:
      - mysql_data:/var/lib/mysql

  minio:
    image: minio/minio:RELEASE.2025-01-01T00-00-00Z
    ports:
      - "9000:9000"
      - "9001:9001"
    environment:
      - MINIO_ROOT_USER=minioadmin
      - MINIO_ROOT_PASSWORD=minioadmin
    command:
      - server
      - /data
      - --console-address
      - :9001
    volumes:
      - minio_data:/data

volumes:
  mysql_data:
  minio_data:

4.2 在LLM训练中集成MLflow

以下是在LLM训练代码中集成MLflow的最佳实践:

import mlflow
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer
import datasets

# 设置MLflow跟踪URI
mlflow.set_tracking_uri("http://localhost:5000")

# 定义实验
mlflow.set_experiment("llm-fine-tuning-experiment")

# 超参数配置
params = {
    "model_name": "meta-llama/Llama-3-8B",
    "learning_rate": 2e-5,
    "batch_size": 16,
    "num_epochs": 3,
    "max_seq_length": 1024,
    "weight_decay": 0.01,
    "warmup_ratio": 0.05
}

# 启动MLflow运行
with mlflow.start_run(run_name=f"{params['model_name']}-finetune") as run:
    # 记录超参数
    mlflow.log_params(params)
    
    # 加载模型和分词器
    tokenizer = AutoTokenizer.from_pretrained(params["model_name"])
    model = AutoModelForCausalLM.from_pretrained(params["model_name"])
    
    # 加载数据集
    dataset = datasets.load_dataset("your_dataset")
    
    # 预处理函数
    def preprocess_function(examples):
        return tokenizer(
            examples["text"],
            truncation=True,
            max_length=params["max_seq_length"]
        )
    
    # 处理数据集
    tokenized_datasets = dataset.map(preprocess_function, batched=True)
    
    # 训练参数
    training_args = TrainingArguments(
        output_dir="./results",
        learning_rate=params["learning_rate"],
        per_device_train_batch_size=params["batch_size"],
        num_train_epochs=params["num_epochs"],
        weight_decay=params["weight_decay"],
        warmup_ratio=params["warmup_ratio"],
        logging_dir="./logs",
        logging_steps=10,
        eval_strategy="epoch",
        save_strategy="epoch",
    )
    
    # 自定义训练器以记录指标
    class MLflowTrainer(Trainer):
        def log(self, logs):
            super().log(logs)
            # 记录到MLflow
            for key, value in logs.items():
                if key != "epoch" and not key.startswith("_"):
                    mlflow.log_metric(key, value, step=self.state.global_step)
    
    # 创建训练器
    trainer = MLflowTrainer(
        model=model,
        args=training_args,
        train_dataset=tokenized_datasets["train"],
        eval_dataset=tokenized_datasets["validation"]
    )
    
    # 开始训练
    trainer.train()
    
    # 保存模型到MLflow
    mlflow.pytorch.log_model(model, "model")
    
    # 记录最终评估指标
    eval_results = trainer.evaluate()
    mlflow.log_metrics(eval_results)
    
    # 记录示例输出
    test_prompts = ["Explain quantum computing in simple terms", "Write a short poem about AI"]
    for i, prompt in enumerate(test_prompts):
        inputs = tokenizer(prompt, return_tensors="pt")
        with torch.no_grad():
            outputs = model.generate(**inputs, max_new_tokens=100)
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        mlflow.log_text(f"Prompt: {prompt}\nResponse: {response}", f"example_{i}.txt")

4.3 Weights & Biases (Wandb)集成

Wandb提供了强大的实验跟踪和可视化功能,特别适合LLM研究和开发:

  1. 安装与配置
# 安装wandb
pip install wandb==0.18.1

# 登录wandb
wandb login
  1. LLM训练集成示例
import wandb
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer
import datasets

# 初始化wandb运行
run = wandb.init(
    project="llm-fine-tuning",
    name="llama3-8b-finetune",
    config={
        "model_name": "meta-llama/Llama-3-8B",
        "learning_rate": 2e-5,
        "batch_size": 16,
        "num_epochs": 3,
        "max_seq_length": 1024,
        "weight_decay": 0.01,
        "warmup_ratio": 0.05
    }
)

# 加载模型和分词器
config = run.config
tokenizer = AutoTokenizer.from_pretrained(config.model_name)
model = AutoModelForCausalLM.from_pretrained(config.model_name)

# 加载和预处理数据集
dataset = datasets.load_dataset("your_dataset")

def preprocess_function(examples):
    return tokenizer(
        examples["text"],
        truncation=True,
        max_length=config.max_seq_length
    )

tokenized_datasets = dataset.map(preprocess_function, batched=True)

# 配置训练参数
training_args = TrainingArguments(
    output_dir="./results",
    learning_rate=config.learning_rate,
    per_device_train_batch_size=config.batch_size,
    num_train_epochs=config.num_epochs,
    weight_decay=config.weight_decay,
    warmup_ratio=config.warmup_ratio,
    logging_dir="./logs",
    logging_steps=10,
    eval_strategy="epoch",
    save_strategy="epoch",
)

# 自定义训练器集成wandb
class WandbTrainer(Trainer):
    def log(self, logs):
        super().log(logs)
        # 记录到wandb
        if "loss" in logs:
            wandb.log({"loss": logs["loss"]}, step=self.state.global_step)
        if "eval_loss" in logs:
            wandb.log({"eval_loss": logs["eval_loss"]}, step=self.state.global_step)
        if "learning_rate" in logs:
            wandb.log({"learning_rate": logs["learning_rate"]}, step=self.state.global_step)

# 创建训练器
trainer = WandbTrainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_datasets["train"],
    eval_dataset=tokenized_datasets["validation"]
)

# 记录模型图结构
if config.max_seq_length <= 64:  # 避免过大的计算图
    dummy_inputs = tokenizer("dummy input", return_tensors="pt")
    wandb.watch(model, log="all", log_freq=10)

# 开始训练
trainer.train()

# 记录最终模型性能
final_metrics = trainer.evaluate()
wandb.log(final_metrics)

# 记录示例输出和可视化
test_prompts = ["Explain quantum computing in simple terms", "Write a short poem about AI"]
generations = []

for prompt in test_prompts:
    inputs = tokenizer(prompt, return_tensors="pt")
    with torch.no_grad():
        outputs = model.generate(**inputs, max_new_tokens=100)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    generations.append({
        "prompt": prompt,
        "response": response
    })

wandb.log({"generations": generations})

# 记录计算资源使用情况
wandb.log({
    "gpu_memory_allocated": torch.cuda.max_memory_allocated() / 1e9,  # GB
    "gpu_count": torch.cuda.device_count()
})

# 完成运行
run.finish()

4.4 实验对比与分析

在2025年,LLM开发中的实验对比变得更加智能和直观:

  1. MLflow实验对比

    • 使用MLflow UI比较不同实验的指标
    • 使用Python API进行批量分析
    import mlflow
    from mlflow.tracking import MlflowClient
    
    client = MlflowClient()
    experiment_id = client.get_experiment_by_name("llm-fine-tuning-experiment").experiment_id
    
    # 获取所有实验运行
    runs = client.search_runs(experiment_id)
    
    # 比较不同学习率的效果
    learning_rates = []
    eval_losses = []
    
    for run in runs:
        learning_rates.append(run.data.params.get("learning_rate"))
        eval_losses.append(run.data.metrics.get("eval_loss"))
    
    # 绘制对比图表
    import matplotlib.pyplot as plt
    plt.scatter(learning_rates, eval_losses)
    plt.xlabel("Learning Rate")
    plt.ylabel("Eval Loss")
    plt.title("Learning Rate vs Eval Loss")
    plt.savefig("lr_vs_loss.png")
    
  2. Wandb实验对比

    • 使用Wandb Dashboard的平行坐标图比较多维度指标
    • 使用Weave进行交互式分析
    • 设置超参数重要性分析
  3. 关键实验指标可视化

    • 训练/验证损失曲线
    • 学习率调度可视化
    • 梯度范数监控
    • GPU利用率和内存使用情况
    • Token生成速度和质量指标

5. 生产环境LLM服务监控

5.1 实时性能监控

在2025年,LLM服务的实时性能监控已成为标准配置:

  1. 关键性能指标

    • 请求延迟(P50/P90/P99)
    • 吞吐量(每秒请求数)
    • GPU利用率和显存使用
    • Token处理速率(输入/输出)
    • 错误率和重试次数
  2. 监控实现方案

# 使用Prometheus客户端监控LLM服务
from prometheus_client import Counter, Histogram, Summary, Gauge, start_http_server
import time
import random
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

# 初始化FastAPI应用
app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 在生产环境中应该设置具体的域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 定义Prometheus指标
REQUEST_COUNT = Counter('llm_requests_total', 'Total LLM API requests', ['endpoint', 'model', 'status'])
REQUEST_LATENCY = Histogram('llm_request_latency_seconds', 'LLM API request latency in seconds', ['endpoint', 'model'])
ACTIVE_REQUESTS = Gauge('llm_active_requests', 'Number of active LLM requests', ['model'])
TOKEN_COUNT = Summary('llm_tokens_processed', 'Total tokens processed', ['direction', 'model'])
GPU_UTILIZATION = Gauge('llm_gpu_utilization_percent', 'GPU utilization for LLM inference')
MEMORY_USAGE = Gauge('llm_memory_usage_bytes', 'Memory usage for LLM service')

# 模拟GPU利用率监控
def update_gpu_metrics():
    # 在实际应用中,应该使用nvidia-smi或DCGM获取真实GPU指标
    GPU_UTILIZATION.set(random.uniform(30, 95))
    MEMORY_USAGE.set(random.uniform(5e9, 15e9))

# 中间件记录请求指标
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    model = request.query_params.get("model", "default")
    ACTIVE_REQUESTS.labels(model=model).inc()
    
    start_time = time.time()
    response = await call_next(request)
    
    # 计算请求延迟
    process_time = time.time() - start_time
    REQUEST_LATENCY.labels(endpoint=request.url.path, model=model).observe(process_time)
    
    # 更新GPU指标
    update_gpu_metrics()
    
    # 减少活跃请求计数
    ACTIVE_REQUESTS.labels(model=model).dec()
    
    return response

# LLM生成端点
@app.post("/api/generate")
async def generate(request: dict):
    model = request.get("model", "default")
    prompt = request.get("prompt", "")
    max_tokens = request.get("max_tokens", 100)
    
    try:
        # 模拟LLM处理时间
        processing_time = random.uniform(0.1, 2.0)
        time.sleep(processing_time)
        
        # 模拟token计数
        input_tokens = len(prompt.split())
        output_tokens = random.randint(10, max_tokens)
        
        # 记录token计数
        TOKEN_COUNT.labels(direction='input', model=model).observe(input_tokens)
        TOKEN_COUNT.labels(direction='output', model=model).observe(output_tokens)
        
        # 记录成功请求
        REQUEST_COUNT.labels(endpoint='/api/generate', model=model, status='success').inc()
        
        return {
            "model": model,
            "generated_text": f"Response to: {prompt}...",
            "input_tokens": input_tokens,
            "output_tokens": output_tokens
        }
    except Exception as e:
        # 记录失败请求
        REQUEST_COUNT.labels(endpoint='/api/generate', model=model, status='error').inc()
        return {"error": str(e)}, 500

# 健康检查端点
@app.get("/health")
async def health_check():
    return {"status": "healthy"}

# 启动Prometheus指标服务器
start_http_server(8000)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8080)

5.2 质量监控与评估

在2025年,LLM服务的质量监控已从简单的性能指标扩展到生成质量评估:

  1. 生成质量指标

    • 准确性(与事实一致性)
    • 相关性(与输入提示的相关程度)
    • 连贯性(逻辑连贯和上下文一致)
    • 安全性(避免有害输出)
    • 多样性(输出的多样性和创造性)
  2. DeepEval集成示例

# 安装DeepEval
# pip install deepeval

from deepeval import evaluate
from deepeval.metrics import AnswerRelevancy, Faithfulness
from deepeval.dataset import EvaluationDataset
import pandas as pd
from datetime import datetime
import mlflow

# 设置MLflow跟踪
mlflow.set_tracking_uri("http://localhost:5000")
mlflow.set_experiment("llm-quality-monitoring")

# 创建评估数据集
dataset = EvaluationDataset()

# 加载测试样本(实际应用中应该从数据库或文件中加载)
test_samples = [
    {
        "query": "Explain quantum computing in simple terms",
        "expected_output": "Quantum computing uses quantum bits or qubits that can exist in multiple states simultaneously, allowing it to solve certain problems much faster than classical computers."
    },
    {
        "query": "What are the main benefits of machine learning?",
        "expected_output": "Machine learning enables systems to learn from data, improve over time, automate complex tasks, and make predictions or decisions without explicit programming."
    }
]

# 添加到评估数据集
for sample in test_samples:
    dataset.add_sample(
        input=sample["query"],
        expected_output=sample["expected_output"]
    )

# 定义LLM生成函数
def generate_response(prompt):
    # 在实际应用中,这里应该调用你的LLM服务
    import requests
    response = requests.post(
        "http://localhost:8080/api/generate",
        json={"prompt": prompt, "model": "llama3-8b"}
    )
    return response.json()["generated_text"]

# 启动评估
with mlflow.start_run(run_name=f"quality-eval-{datetime.now().strftime('%Y%m%d-%H%M%S')}") as run:
    # 记录配置
    mlflow.log_params({
        "model": "llama3-8b",
        "eval_samples": len(test_samples)
    })
    
    # 收集实际输出
    actual_outputs = []
    for sample in test_samples:
        actual_output = generate_response(sample["query"])
        actual_outputs.append(actual_output)
        # 记录样本和输出
        mlflow.log_text(
            f"Query: {sample['query']}\nExpected: {sample['expected_output']}\nActual: {actual_output}",
            f"sample_{test_samples.index(sample)}.txt"
        )
    
    # 定义评估指标
    answer_relevancy = AnswerRelevancy()
    faithfulness = Faithfulness()
    
    # 运行评估
    results = evaluate(
        dataset=dataset,
        metrics=[answer_relevancy, faithfulness],
        model=generate_response  # 也可以直接提供实际输出
    )
    
    # 记录评估结果
    for i, result in enumerate(results):
        mlflow.log_metrics({
            f"sample_{i}_relevancy": result["answer_relevancy"]["score"],
            f"sample_{i}_faithfulness": result["faithfulness"]["score"]
        })
    
    # 计算平均指标
    avg_relevancy = sum(r["answer_relevancy"]["score"] for r in results) / len(results)
    avg_faithfulness = sum(r["faithfulness"]["score"] for r in results) / len(results)
    
    # 记录平均指标
    mlflow.log_metrics({
        "avg_relevancy": avg_relevancy,
        "avg_faithfulness": avg_faithfulness
    })
    
    print(f"Average Relevancy: {avg_relevancy:.2f}")
    print(f"Average Faithfulness: {avg_faithfulness:.2f}")

5.3 告警系统配置

在2025年,LLM服务监控的告警系统变得更加智能和主动:

  1. Prometheus告警规则
# alert_rules.yml
groups:
- name: llm_service_alerts
  rules:
  - alert: LLMHighErrorRate
    expr: sum(rate(llm_requests_total{status="error"}[5m])) / sum(rate(llm_requests_total[5m])) * 100 > 5
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "LLM服务错误率过高"
      description: "错误率超过5%,当前值: {{ $value }}%"

  - alert: LLMHighLatency
    expr: histogram_quantile(0.95, sum(rate(llm_request_latency_seconds_bucket[5m])) by (le, model)) > 1
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "LLM服务延迟过高"
      description: "P95延迟超过1秒,模型: {{ $labels.model }}, 当前值: {{ $value }}秒"

  - alert: LLMHighGPUUtilization
    expr: llm_gpu_utilization_percent > 90
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "GPU利用率过高"
      description: "GPU利用率超过90%,当前值: {{ $value }}%"

  - alert: LLMServiceDown
    expr: up{job="llm-service"} == 0
    for: 30s
    labels:
      severity: critical
    annotations:
      summary: "LLM服务不可用"
      description: "LLM服务检测失败"
  1. Alertmanager配置
# alertmanager.yml
global:
  resolve_timeout: 5m

route:
  group_by: ['alertname', 'severity']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 4h
  receiver: 'email-notifications'
  routes:
  - match:
      severity: critical
    receiver: 'critical-notifications'
    repeat_interval: 1h

receivers:
- name: 'email-notifications'
  email_configs:
  - to: 'alerts@example.com'
    from: 'prometheus@example.com'
    smarthost: 'smtp.example.com:587'
    auth_username: 'prometheus'
    auth_password: 'password'

- name: 'critical-notifications'
  email_configs:
  - to: 'critical-alerts@example.com'
    from: 'prometheus@example.com'
    smarthost: 'smtp.example.com:587'
    auth_username: 'prometheus'
    auth_password: 'password'
  webhook_configs:
  - url: 'http://dingtalk-webhook:8060/dingtalk/critical/send'
    send_resolved: true
  1. 智能告警聚合与降噪
    • 基于机器学习的异常检测
    • 告警关联分析和根因定位
    • 基于业务时间的动态阈值
    • 告警抑制和静默规则

5.4 用户体验监控

除了技术指标外,2025年的LLM服务监控还包括用户体验指标:

  1. 关键用户体验指标

    • 服务等级协议(SLA)达标率
    • 用户满意度(NPS评分)
    • 会话持续时间
    • 交互次数和模式
    • 功能使用频率
  2. 实现方案

# 用户体验指标收集
from prometheus_client import Counter, Gauge, Histogram

# 定义用户体验指标
USER_SESSIONS = Counter('llm_user_sessions_total', 'Total user sessions')
SESSION_DURATION = Histogram('llm_session_duration_seconds', 'User session duration in seconds')
USER_SATISFACTION = Gauge('llm_user_satisfaction_score', 'User satisfaction score (1-5)')
FEATURE_USAGE = Counter('llm_feature_usage_total', 'Feature usage count', ['feature'])
RESPONSE_RELEVANCE = Gauge('llm_response_relevance_score', 'User-rated response relevance (1-5)')

# 记录用户会话
@app.post("/api/session/start")
async def start_session():
    USER_SESSIONS.inc()
    session_id = generate_session_id()
    # 在实际应用中,应该将会话ID存储在数据库或缓存中
    return {"session_id": session_id}

# 记录会话结束
@app.post("/api/session/end")
async def end_session(request: dict):
    session_id = request.get("session_id")
    duration = request.get("duration", 0)
    satisfaction = request.get("satisfaction", 0)
    
    if duration > 0:
        SESSION_DURATION.observe(duration)
    
    if satisfaction > 0:
        USER_SATISFACTION.set(satisfaction)
    
    return {"status": "success"}

# 记录功能使用
@app.post("/api/feature/usage")
async def record_feature_usage(request: dict):
    feature = request.get("feature", "unknown")
    FEATURE_USAGE.labels(feature=feature).inc()
    return {"status": "success"}

# 记录响应相关性评分
@app.post("/api/response/rate")
async def rate_response(request: dict):
    response_id = request.get("response_id")
    relevance = request.get("relevance", 0)
    
    if relevance > 0:
        RESPONSE_RELEVANCE.set(relevance)
    
    return {"status": "success"}

6. 高级监控策略与最佳实践

6.1 多维度指标关联分析

在2025年,LLM监控已从单一指标监控发展到多维度关联分析:

  1. 指标关联策略

    • 技术指标与业务指标关联
    • 模型性能与用户体验关联
    • 系统资源与请求负载关联
    • 故障事件与历史模式关联
  2. 实现方案

# 使用Pandas进行指标关联分析
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from prometheus_api_client import PrometheusConnect

# 连接Prometheus
prom = PrometheusConnect(url="http://localhost:9090", disable_ssl=True)

# 查询关键指标
start_time = "2025-01-01T00:00:00Z"
end_time = "2025-01-02T00:00:00Z"

# 查询GPU利用率
gpu_query = 'avg_over_time(llm_gpu_utilization_percent[5m])'
gpu_data = prom.custom_query_range(query=gpu_query, start_time=start_time, end_time=end_time, step="5m")

# 查询请求延迟
latency_query = 'histogram_quantile(0.95, sum(rate(llm_request_latency_seconds_bucket[5m])) by (le))'
latency_data = prom.custom_query_range(query=latency_query, start_time=start_time, end_time=end_time, step="5m")

# 查询用户满意度
satisfaction_query = 'llm_user_satisfaction_score'
satisfaction_data = prom.custom_query_range(query=satisfaction_query, start_time=start_time, end_time=end_time, step="5m")

# 转换为DataFrame
def convert_to_dataframe(prom_data, metric_name):
    timestamps = []
    values = []
    for point in prom_data[0]['values']:
        timestamps.append(pd.Timestamp(float(point[0]), unit='s'))
        values.append(float(point[1]))
    return pd.DataFrame({metric_name: values}, index=timestamps)

gpu_df = convert_to_dataframe(gpu_data, 'gpu_utilization')
latency_df = convert_to_dataframe(latency_data, 'p95_latency')
satisfaction_df = convert_to_dataframe(satisfaction_data, 'user_satisfaction')

# 合并数据框
df = pd.concat([gpu_df, latency_df, satisfaction_df], axis=1)
df = df.resample('5T').mean()  # 重采样到5分钟

# 计算相关性
corr_matrix = df.corr()

# 绘制热力图
plt.figure(figsize=(10, 8))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', vmin=-1, vmax=1)
plt.title('LLM服务指标相关性分析')
plt.savefig('correlation_analysis.png')

# 绘制时间序列对比图
plt.figure(figsize=(12, 6))

# 左侧Y轴:GPU利用率和延迟
ax1 = plt.subplot(111)
ax1.plot(df.index, df['gpu_utilization'], 'b-', label='GPU Utilization (%)')
ax1.set_ylabel('GPU Utilization (%)', color='b')
ax1.tick_params(axis='y', labelcolor='b')

# 右侧Y轴:用户满意度
ax2 = ax1.twinx()
ax2.plot(df.index, df['user_satisfaction'], 'r-', label='User Satisfaction')
ax2.set_ylabel('User Satisfaction', color='r')
ax2.tick_params(axis='y', labelcolor='r')

# 添加第二个Y轴:延迟
ax3 = ax1.twinx()
ax3.spines['right'].set_position(('outward', 60))
ax3.plot(df.index, df['p95_latency'], 'g-', label='P95 Latency (s)')
ax3.set_ylabel('P95 Latency (s)', color='g')
ax3.tick_params(axis='y', labelcolor='g')

plt.title('LLM服务多维度指标时间序列分析')
plt.tight_layout()
plt.savefig('multi_metric_analysis.png')

6.2 预测性监控与异常检测

2025年的LLM监控已经从被动响应转向主动预测:

  1. 异常检测方法

    • 统计方法(Z-score, IQR)
    • 机器学习方法(孤立森林、自编码器)
    • 深度学习方法(LSTM-Autoencoder)
    • 时序分解和季节性分析
  2. 预测性监控实现

# 使用Prophet进行时间序列预测
from prophet import Prophet
import pandas as pd
import matplotlib.pyplot as plt
from prometheus_api_client import PrometheusConnect

# 连接Prometheus
prom = PrometheusConnect(url="http://localhost:9090", disable_ssl=True)

# 查询历史请求量数据
start_time = "2025-01-01T00:00:00Z"
end_time = "2025-01-15T00:00:00Z"
query = 'sum(rate(llm_requests_total[5m]))'
data = prom.custom_query_range(query=query, start_time=start_time, end_time=end_time, step="1h")

# 转换为Prophet所需格式
timestamps = []
values = []
for point in data[0]['values']:
    timestamps.append(pd.Timestamp(float(point[0]), unit='s'))
    values.append(float(point[1]))

df = pd.DataFrame({'ds': timestamps, 'y': values})

# 训练Prophet模型
model = Prophet(
    yearly_seasonality=False,
    weekly_seasonality=True,
    daily_seasonality=True,
    seasonality_mode='multiplicative'
)
model.fit(df)

# 预测未来7天
future = model.make_future_dataframe(periods=7*24, freq='H')
forecast = model.predict(future)

# 可视化预测结果
fig = model.plot(forecast)
plt.title('LLM服务请求量预测')
plt.savefig('request_forecast.png')

# 可视化组件分解
fig = model.plot_components(forecast)
plt.savefig('forecast_components.png')

# 使用ADTK进行异常检测
# pip install adtk
from adtk.detector import SeasonalAD, ThresholdAD, InterQuartileRangeAD
from adtk.visualization import plot

# 准备数据
ts = pd.Series(values, index=timestamps)
ts = ts.resample('H').mean()

# 使用季节性异常检测器
seasonal_ad = SeasonalAD()
anomalies_seasonal = seasonal_ad.fit_detect(ts)

# 使用阈值异常检测器
threshold_ad = ThresholdAD(high=ts.quantile(0.95), low=ts.quantile(0.05))
anomalies_threshold = threshold_ad.detect(ts)

# 使用IQR异常检测器
iqr_ad = InterQuartileRangeAD()
anomalies_iqr = iqr_ad.fit_detect(ts)

# 可视化异常检测结果
fig, axes = plt.subplots(3, 1, figsize=(15, 10), sharex=True)

plot(ts, anomaly=anomalies_seasonal, ax=axes[0])
axes[0].set_title('季节性异常检测')

plot(ts, anomaly=anomalies_threshold, ax=axes[1])
axes[1].set_title('阈值异常检测')

plot(ts, anomaly=anomalies_iqr, ax=axes[2])
axes[2].set_title('IQR异常检测')

plt.tight_layout()
plt.savefig('anomaly_detection.png')

6.3 监控系统的可扩展性设计

为了支持大规模LLM部署,监控系统需要具备良好的可扩展性:

  1. 水平扩展策略

    • Prometheus联邦集群
    • 时序数据库分片和复制
    • 高可用部署架构
    • 数据压缩和降采样
  2. 联邦集群配置示例

# 全局Prometheus配置 (prometheus-global.yml)
global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'federate'
    scrape_interval: 15s
    honor_labels: true
    metrics_path: '/federate'
    params:
      'match[]':
        - '{job=~"llm-.*"}'
        - '{__name__=~"^job:.*"}'
    static_configs:
      - targets:
        - 'prometheus-1:9090'
        - 'prometheus-2:9090'
        - 'prometheus-3:9090'
  1. 数据保留和归档策略
# prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

storage:
  tsdb:
    path: /prometheus
    retention.time: 15d
    retention.size: 100GB

remote_write:
  - url: "http://thanos-receive:9090/api/v1/receive"
    queue_config:
      capacity: 10000
      max_shards: 200
      min_shards: 1
      max_samples_per_send: 500
      batch_send_deadline: 5s

6.4 安全与合规监控

在2025年,LLM监控还包括安全与合规方面:

  1. 关键安全指标

    • 访问控制和认证失败
    • 异常请求模式检测
    • 敏感内容生成监控
    • 数据泄露风险评估
  2. 合规监控实现

# 安全指标监控
from prometheus_client import Counter, Gauge, Histogram
import re
from fastapi import Request, HTTPException

# 定义安全指标
AUTH_FAILURES = Counter('llm_auth_failures_total', 'Authentication failures', ['method', 'ip_address'])
RATE_LIMIT_VIOLATIONS = Counter('llm_rate_limit_violations_total', 'Rate limit violations', ['endpoint', 'ip_address'])
SENSITIVE_CONTENT_DETECTED = Counter('llm_sensitive_content_detected_total', 'Sensitive content detected', ['category'])
ANOMALOUS_REQUESTS = Counter('llm_anomalous_requests_total', 'Anomalous requests detected', ['type'])

# 敏感内容检测中间件
@app.middleware("http")
async def sensitive_content_detection(request: Request, call_next):
    # 检查请求内容
    if request.method in ["POST", "PUT"]:
        try:
            body = await request.body()
            # 在实际应用中,这里应该使用更复杂的敏感内容检测算法
            sensitive_patterns = {
                "profanity": re.compile(r"bad_word1|bad_word2", re.IGNORECASE),
                "violence": re.compile(r"violent_term1|violent_term2", re.IGNORECASE),
                "privacy": re.compile(r"credit_card|social_security", re.IGNORECASE)
            }
            
            for category, pattern in sensitive_patterns.items():
                if pattern.search(body.decode('utf-8', errors='ignore')):
                    SENSITIVE_CONTENT_DETECTED.labels(category=category).inc()
                    # 在生产环境中,这里可以记录详细日志或触发告警
        except Exception as e:
            # 出错时不应阻止请求处理
            pass
    
    response = await call_next(request)
    return response

# 速率限制中间件
rate_limits = {}

@app.middleware("http")
async def rate_limiting(request: Request, call_next):
    client_ip = request.client.host
    endpoint = request.url.path
    
    # 简单的内存速率限制实现
    # 在生产环境中,应该使用Redis等分布式解决方案
    key = f"{client_ip}:{endpoint}"
    current_time = time.time()
    
    if key not in rate_limits:
        rate_limits[key] = []
    
    # 清理过期请求
    rate_limits[key] = [t for t in rate_limits[key] if current_time - t < 60]
    
    # 检查是否超过限制
    if len(rate_limits[key]) >= 100:  # 每分钟100个请求
        RATE_LIMIT_VIOLATIONS.labels(endpoint=endpoint, ip_address=client_ip).inc()
        raise HTTPException(status_code=429, detail="Rate limit exceeded")
    
    # 记录请求时间
    rate_limits[key].append(current_time)
    
    response = await call_next(request)
    return response

7. 案例研究:企业级LLM监控系统

7.1 案例一:研究机构的实验跟踪平台

背景:某研究机构专注于LLM研究,需要一个统一的实验跟踪平台来管理大量实验。

架构设计

+----------------------------------+
|       统一监控门户               |
|  (Grafana + MLflow UI + Wandb)   |
+----------------------------------+
        |            |            |
+----------------+----------------+----------------+
|  Prometheus    |   MLflow      |   Wandb        |
|  (基础设施监控) | (实验跟踪)   | (高级可视化)   |
+----------------+----------------+----------------+
        |            |            |
+----------------------------------+
|      数据集成层                  |
+----------------------------------+
        |            |            |
+----------------+----------------+----------------+
| GPU集群监控    |  LLM训练监控   | 模型评估系统   |
+----------------+----------------+----------------+

实施细节

  1. 数据采集层

    • 使用NVIDIA DCGM Exporter监控GPU集群
    • 自定义Prometheus Exporter监控LLM训练指标
    • MLflow和Wandb客户端集成到训练代码
  2. 数据存储层

    • Prometheus存储实时监控数据(保留15天)
    • Thanos存储长期历史数据
    • MySQL存储MLflow实验元数据
    • MinIO存储模型和大文件
  3. 可视化层

    • Grafana提供基础设施和服务监控仪表盘
    • MLflow UI提供实验跟踪和比较功能
    • Wandb提供高级模型可视化和团队协作功能

成果

  • 实验跟踪效率提升40%
  • 研究人员可以专注于算法创新而非基础设施管理
  • 实验复现率从65%提升至95%
  • 计算资源利用率提高25%

7.2 案例二:金融科技公司的生产LLM服务监控

背景:某金融科技公司部署了客户服务LLM,需要确保高可用性、数据安全和合规性。

架构设计

+--------------------------------------+
|            监控与告警中心             |
|  (Grafana + Alertmanager + PagerDuty) |
+--------------------------------------+
                  |
+--------------------------------------+
|              数据收集层               |
| (Prometheus + ELK + OpenTelemetry)    |
+--------------------------------------+
        |               |              |
+----------------+----------------+----------------+
| LLM服务监控    | 安全合规监控   | 用户体验监控   |
+----------------+----------------+----------------+
        |               |              |
+--------------------------------------+
|            LLM生产服务层             |
+--------------------------------------+

实施细节

  1. 多维度监控

    • 技术指标:延迟、吞吐量、错误率、资源使用
    • 业务指标:会话数、功能使用频率、用户满意度
    • 合规指标:敏感内容检测、异常访问模式
  2. 智能告警系统

    • 基于机器学习的异常检测
    • 动态阈值调整(考虑业务高峰期)
    • 告警聚合和降噪
    • 自动升级流程
  3. 持续质量评估

    • 定期抽样评估生成质量
    • A/B测试框架
    • 用户反馈整合到监控系统

成果

  • 服务可用性达到99.99%
  • 问题平均检测时间从45分钟减少到5分钟
  • 用户满意度提升18%
  • 合规审计通过率100%

8. 未来发展趋势与创新方向

8.1 2025年监控技术发展趋势

在2025年,LLM监控技术呈现以下发展趋势:

  1. AI驱动的智能监控

    • 自动发现监控目标和关键指标
    • 智能根因分析和故障预测
    • 自适应阈值和告警优化
    • 异常模式识别和分类
  2. 端到端可观测性

    • 整合监控、日志和追踪
    • 分布式追踪与LLM调用链分析
    • 统一的可观测性平台
    • 业务影响可视化
  3. 实时分析与边缘计算

    • 边缘设备上的轻量级监控
    • 流处理技术用于实时分析
    • 边缘计算减少延迟
    • 分层存储策略
  4. 安全与隐私保护

    • 隐私保护监控技术
    • 联邦学习在监控中的应用
    • 安全多方计算用于跨组织监控
    • 符合法规要求的监控实践

8.2 创新监控技术与工具

2025年出现了一些创新的监控技术和工具:

  1. Weights & Biases Mission Control

    • 与CoreWeave合作,实现硬件故障与训练进程的实时关联诊断
    • 提供硬件资源到应用层的全链路智能化监控
    • 支持大规模训练任务的可视化管理
  2. DeepEval生产环境监控

    • 细粒度调用分析和对话流程追踪
    • 基于实际用户请求的质量评估
    • 实时性能仪表盘和异常预警
  3. 自适应监控框架

    • 根据LLM类型和使用场景自动调整监控策略
    • 智能识别关键指标和异常模式
    • 自动化报告生成和见解提取
  4. 可解释性监控工具

    • 监控LLM内部状态和决策过程
    • 可视化注意力权重和激活模式
    • 提供生成结果的可信度评估

8.3 监控与优化的融合趋势

在2025年,监控系统与自动优化系统的融合成为新趋势:

  1. 闭环优化系统

    • 监控系统检测性能瓶颈
    • 自动触发优化动作
    • 评估优化效果并持续调整
  2. 资源动态调度

    • 基于实时负载自动调整计算资源
    • 预测性资源扩缩容
    • 多租户资源隔离和优先级管理
  3. 模型自动调优

    • 基于监控指标自动调整模型参数
    • 自适应的提示工程
    • 模型压缩和量化优化
  4. 成本优化与效率提升

    • 资源使用效率监控和优化
    • 成本分析和预测
    • ROI评估和优化建议

9. 结论与最佳实践总结

9.1 核心实践要点

通过本文的讨论,我们总结出构建LLM开发环境监控仪表盘的核心实践要点:

  1. 分层监控架构

    • 基础设施监控(Prometheus + Grafana)
    • 实验跟踪(MLflow, Wandb)
    • 服务性能监控(自定义指标 + 告警)
    • 用户体验监控(业务指标 + 反馈)
  2. 多维度指标采集

    • 技术指标:延迟、吞吐量、资源使用
    • 模型指标:损失函数、准确率、困惑度
    • 业务指标:用户满意度、功能使用频率
    • 安全合规指标:异常访问、敏感内容检测
  3. 智能告警与预测

    • 基于机器学习的异常检测
    • 多级别告警策略
    • 预测性分析和趋势预警
    • 自动根因分析
  4. 持续优化与改进

    • 定期审查监控覆盖范围
    • 根据业务变化调整指标和告警
    • 优化数据存储和查询性能
    • 整合新的监控技术和工具

9.2 常见问题解决方案

在实际应用中,可能会遇到以下常见问题,我们提供相应的解决方案:

  1. 监控数据过多导致的存储和查询压力

    • 实施数据采样和降采样策略
    • 使用分层存储架构(热数据、温数据、冷数据)
    • 合理设置保留期限
    • 优化查询性能
  2. 告警风暴和误报

    • 实施告警聚合和抑制
    • 基于业务时间调整动态阈值
    • 优化告警规则减少误报
    • 建立告警分级和升级机制
  3. 监控系统本身的可靠性

    • 实施监控系统的高可用部署
    • 监控监控系统自身
    • 建立备用监控通道
    • 定期测试和演练
  4. 跨团队协作和权限管理

    • 实施基于角色的访问控制
    • 提供团队特定的自定义视图
    • 建立监控数据共享机制
    • 统一的告警响应流程

9.3 未来发展建议

为了构建适应未来发展的LLM监控系统,我们提出以下建议:

  1. 持续学习和技术更新

    • 关注监控技术的最新发展
    • 参与开源社区贡献
    • 定期评估和更新监控工具
  2. 自动化和智能化

    • 增加自动化程度,减少人工干预
    • 引入机器学习提高监控效率
    • 构建闭环优化系统
  3. 可观测性文化建设

    • 培养团队的可观测性意识
    • 建立统一的监控标准和最佳实践
    • 鼓励数据驱动的决策
  4. 与业务目标对齐

    • 确保监控系统支持业务目标
    • 关注业务影响而非仅技术指标
    • 将监控指标与关键业务指标关联
    • 通过监控数据优化业务流程

9.4 监控仪表盘的ROI评估

构建和维护监控仪表盘需要投入资源,因此进行ROI评估非常重要:

  1. 成本因素

    • 基础设施和工具成本
    • 开发和维护人力成本
    • 培训和知识转移成本
    • 数据存储和处理成本
  2. 收益因素

    • 减少故障检测和恢复时间
    • 提高系统可用性和可靠性
    • 优化资源使用效率
    • 提升用户体验和满意度
    • 加速问题诊断和解决
  3. ROI计算方法

    • 直接收益:故障成本节约、资源优化收益
    • 间接收益:品牌声誉提升、用户留存增加
    • 长期收益:技术债务减少、创新能力提升

10. 附录:监控工具配置参考

10.1 Prometheus配置示例

# 完整的Prometheus配置文件示例
global:
  scrape_interval: 15s
  evaluation_interval: 15s
  scrape_timeout: 10s

# Alertmanager配置
alerting:
  alertmanagers:
  - static_configs:
    - targets: ['alertmanager:9093']

# 告警规则文件
rule_files:
  - "alert_rules/*.yml"

# 远程读写配置
remote_write:
  - url: "http://thanos-receive:9090/api/v1/receive"
    queue_config:
      capacity: 10000
      max_shards: 200
      min_shards: 1
      max_samples_per_send: 500
      batch_send_deadline: 5s

remote_read:
  - url: "http://thanos-query:9090/api/v1/read"

# 数据存储配置
storage:
  tsdb:
    path: /prometheus
    retention.time: 15d
    retention.size: 100GB

# 抓取配置
scrape_configs:
  # 自身监控
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  # 节点监控
  - job_name: 'node_exporter'
    static_configs:
    - targets: ['node_exporter:9100']

  # GPU监控
  - job_name: 'gpu_exporter'
    static_configs:
    - targets: ['gpu_exporter:9400']

  # 容器监控
  - job_name: 'cadvisor'
    static_configs:
    - targets: ['cadvisor:8080']

  # LLM服务监控
  - job_name: 'llm_service'
    metrics_path: '/metrics'
    scrape_interval: 5s
    static_configs:
    - targets: ['llm-service:8000']
    relabel_configs:
    - source_labels: [__address__]
      target_label: instance

  # 应用健康检查
  - job_name: 'llm_service_health'
    metrics_path: '/health'
    scrape_interval: 10s
    static_configs:
    - targets: ['llm-service:8000']

10.2 Grafana仪表盘JSON示例

以下是一个简化的Grafana仪表盘JSON配置示例:

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "gnetId": null,
  "graphTooltip": 0,
  "id": 1,
  "links": [],
  "panels": [
    {
      "aliasColors": {},
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": "Prometheus",
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 10,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          },
          "unit": "reqps"
        },
        "overrides": []
      },
      "fill": 1,
      "fillGradient": 0,
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 0
      },
      "hiddenSeries": false,
      "id": 1,
      "legend": {
        "avg": false,
        "current": true,
        "max": false,
        "min": false,
        "show": true,
        "total": false,
        "values": true
      },
      "lines": true,
      "linewidth": 1,
      "nullPointMode": "null",
      "options": {
        "alertThreshold": true
      },
      "percentage": false,
      "pluginVersion": "10.0.0",
      "pointradius": 2,
      "points": false,
      "renderer": "flot",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "expr": "sum(rate(llm_requests_total[5m])) by (endpoint)",
          "interval": "",
          "legendFormat": "{{endpoint}}",
          "refId": "A"
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeRegions": [],
      "timeShift": null,
      "title": "LLM API请求量",
      "tooltip": {
        "shared": true,
        "sort": 0,
        "value_type": "individual"
      },
      "type": "graph",
      "xaxis": {
        "buckets": null,
        "mode": "time",
        "name": null,
        "show": true,
        "values": []
      },
      "yaxes": [
        {
          "format": "reqps",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        },
        {
          "format": "short",
          "label": null,
          "logBase": 1,
          "max": null,
          "min": null,
          "show": true
        }
      ],
      "yaxis": {
        "align": false,
        "alignLevel": null
      }
    }
  ],
  "refresh": "10s",
  "schemaVersion": 37,
  "style": "dark",
  "tags": ["llm", "monitoring"],
  "templating": {
    "list": []
  },
  "time": {
    "from": "now-1h",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "",
  "title": "LLM服务监控仪表盘",
  "uid": "llm-dashboard",
  "version": 1
}

10.3 MLflow与PyTorch集成完整示例

以下是MLflow与PyTorch集成的完整示例,用于LLM微调监控:

import os
import torch
import mlflow
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer
from datasets import load_dataset
from torch.utils.data import DataLoader
import evaluate
import numpy as np

# 设置环境变量
os.environ["MLFLOW_TRACKING_URI"] = "http://localhost:5000"
os.environ["MLFLOW_EXPERIMENT_NAME"] = "llm-fine-tuning"

# 超参数配置
config = {
    "model_name": "meta-llama/Llama-3-8B",
    "dataset_name": "pile-of-law/pile-of-law",
    "learning_rate": 2e-5,
    "batch_size": 16,
    "gradient_accumulation_steps": 4,
    "num_epochs": 3,
    "max_seq_length": 1024,
    "weight_decay": 0.01,
    "warmup_ratio": 0.05,
    "fp16": True,
    "gradient_checkpointing": True
}

# 启动MLflow运行
with mlflow.start_run(run_name=f"{config['model_name']}-finetune") as run:
    # 记录超参数
    mlflow.log_params(config)
    
    # 检查GPU
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(f"Using device: {device}")
    mlflow.log_param("device", str(device))
    mlflow.log_param("num_gpus", torch.cuda.device_count())
    
    # 加载模型和分词器
    print(f"Loading model: {config['model_name']}")
    tokenizer = AutoTokenizer.from_pretrained(config['model_name'])
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token
    
    # 记录模型信息
    mlflow.log_param("tokenizer_vocab_size", len(tokenizer))
    
    # 加载数据集
    print(f"Loading dataset: {config['dataset_name']}")
    dataset = load_dataset(config['dataset_name'], streaming=True, split="train")
    
    # 预处理函数
    def preprocess_function(examples):
        # 提取文本列(根据实际数据集调整)
        text = examples.get("text", [])
        if isinstance(text, str):
            text = [text]
        
        # 分词
        tokenized = tokenizer(
            text,
            truncation=True,
            max_length=config["max_seq_length"],
            padding="max_length"
        )
        
        # 对于因果语言模型,labels与input_ids相同
        tokenized["labels"] = tokenized["input_ids"].copy()
        
        return tokenized
    
    # 应用预处理
    print("Processing dataset...")
    # 对于流式数据集,我们使用小样本进行训练演示
    small_dataset = dataset.take(1000)
    tokenized_dataset = small_dataset.map(preprocess_function, batched=True)
    
    # 分割数据集
    tokenized_dataset = tokenized_dataset.train_test_split(test_size=0.1)
    train_dataset = tokenized_dataset["train"]
    eval_dataset = tokenized_dataset["test"]
    
    # 记录数据集信息
    mlflow.log_param("train_size", len(train_dataset))
    mlflow.log_param("eval_size", len(eval_dataset))
    
    # 加载评估指标
    print("Loading evaluation metrics...")
    perplexity = evaluate.load("perplexity")
    
    # 评估函数
    def compute_metrics(eval_pred):
        logits, labels = eval_pred
        # 将logits转换为概率分布
        probabilities = torch.softmax(torch.tensor(logits), dim=-1)
        
        # 计算困惑度
        results = perplexity.compute(
            model_id=None,
            add_start_token=False,
            predictions=probabilities.tolist()
        )
        
        return {
            "perplexity": results["mean_perplexity"],
            "perplexity_std": np.std(results["perplexities"])
        }
    
    # 配置训练参数
    training_args = TrainingArguments(
        output_dir="./results",
        learning_rate=config["learning_rate"],
        per_device_train_batch_size=config["batch_size"],
        per_device_eval_batch_size=config["batch_size"] * 2,
        gradient_accumulation_steps=config["gradient_accumulation_steps"],
        num_train_epochs=config["num_epochs"],
        weight_decay=config["weight_decay"],
        warmup_ratio=config["warmup_ratio"],
        fp16=config["fp16"],
        gradient_checkpointing=config["gradient_checkpointing"],
        logging_dir="./logs",
        logging_steps=10,
        eval_strategy="steps",
        eval_steps=50,
        save_strategy="steps",
        save_steps=100,
        save_total_limit=3,
        load_best_model_at_end=True,
        metric_for_best_model="eval_loss",
        greater_is_better=False,
    )
    
    # 自定义训练器
    class MLflowTrainer(Trainer):
        def log(self, logs):
            super().log(logs)
            # 记录到MLflow
            step = self.state.global_step
            for key, value in logs.items():
                if key != "epoch" and not key.startswith("_"):
                    mlflow.log_metric(key, value, step=step)
        
        def evaluate(self, eval_dataset=None, ignore_keys=None, metric_key_prefix="eval"):
            # 评估前记录GPU状态
            if torch.cuda.is_available():
                mlflow.log_metric("gpu_memory_used", torch.cuda.max_memory_allocated() / 1e9, step=self.state.global_step)
                mlflow.log_metric("gpu_utilization", torch.cuda.utilization(), step=self.state.global_step)
            
            return super().evaluate(eval_dataset, ignore_keys, metric_key_prefix)
    
    # 加载模型
    print("Loading model for training...")
    model = AutoModelForCausalLM.from_pretrained(
        config["model_name"],
        torch_dtype=torch.float16 if config["fp16"] else torch.float32,
        device_map="auto"
    )
    
    # 记录模型架构信息
    mlflow.log_param("model_num_parameters", sum(p.numel() for p in model.parameters()) / 1e9)
    
    # 创建训练器
    print("Creating trainer...")
    trainer = MLflowTrainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset,
        eval_dataset=eval_dataset,
        tokenizer=tokenizer,
        compute_metrics=compute_metrics
    )
    
    # 记录模型图(对于大模型可能需要禁用)
    # mlflow.pytorch.log_model(model, "model")
    
    # 开始训练
    print("Starting training...")
    trainer.train()
    
    # 最终评估
    print("Final evaluation...")
    final_metrics = trainer.evaluate()
    print(f"Final metrics: {final_metrics}")
    
    # 记录最终指标
    for key, value in final_metrics.items():
        mlflow.log_metric(f"final_{key}", value)
    
    # 保存最终模型
    print("Saving final model...")
    trainer.save_model("./final_model")
    
    # 记录模型到MLflow
    mlflow.pytorch.log_model(model, "final_model")
    
    # 生成示例输出
    print("Generating example outputs...")
    test_prompts = [
        "Explain quantum computing in simple terms",
        "Write a short poem about AI and humanity",
        "Summarize the key principles of machine learning"
    ]
    
    # 设置生成参数
    generation_config = transformers.GenerationConfig(
        max_new_tokens=100,
        temperature=0.7,
        top_p=0.95,
        do_sample=True
    )
    
    # 生成响应
    examples = []
    for i, prompt in enumerate(test_prompts):
        inputs = tokenizer(prompt, return_tensors="pt").to(device)
        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                generation_config=generation_config
            )
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        example = f"Prompt: {prompt}\nResponse: {response}"
        examples.append(example)
        mlflow.log_text(example, f"example_{i}.txt")
    
    # 记录所有示例
    mlflow.log_text("\n\n---\n\n".join(examples), "all_examples.txt")
    
    print("Training completed successfully!")

10.4 自定义监控脚本示例

以下是一个用于监控LLM服务的自定义Python脚本示例:

#!/usr/bin/env python3
"""
LLM服务监控脚本
用于监控LLM服务的各种指标并推送到Prometheus
"""

import time
import subprocess
import json
import requests
import psutil
import re
import logging
from prometheus_client import start_http_server, Gauge, Counter, Histogram, Summary

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler("llm_monitor.log"),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger("llm_monitor")

# 定义Prometheus指标
# 系统资源指标
CPU_USAGE = Gauge('llm_system_cpu_usage_percent', 'System CPU usage percentage')
MEMORY_USAGE = Gauge('llm_system_memory_usage_bytes', 'System memory usage in bytes')
DISK_USAGE = Gauge('llm_system_disk_usage_percent', 'Disk usage percentage', ['mount_point'])

# GPU指标
GPU_UTILIZATION = Gauge('llm_gpu_utilization_percent', 'GPU utilization percentage', ['gpu_id', 'gpu_name'])
GPU_MEMORY_USED = Gauge('llm_gpu_memory_used_bytes', 'GPU memory used in bytes', ['gpu_id', 'gpu_name'])
GPU_MEMORY_TOTAL = Gauge('llm_gpu_memory_total_bytes', 'GPU memory total in bytes', ['gpu_id', 'gpu_name'])
GPU_TEMPERATURE = Gauge('llm_gpu_temperature_celsius', 'GPU temperature in Celsius', ['gpu_id', 'gpu_name'])
GPU_POWER_DRAW = Gauge('llm_gpu_power_draw_watts', 'GPU power draw in watts', ['gpu_id', 'gpu_name'])

# LLM服务指标
SERVICE_UP = Gauge('llm_service_up', 'LLM service status (1=up, 0=down)')
ACTIVE_REQUESTS = Gauge('llm_service_active_requests', 'Number of active LLM requests')
REQUEST_LATENCY = Histogram('llm_service_request_latency_seconds', 'LLM service request latency in seconds')
REQUEST_COUNT = Counter('llm_service_requests_total', 'Total LLM service requests', ['status'])
TOKEN_PROCESSED = Counter('llm_service_tokens_processed_total', 'Total tokens processed', ['direction'])

# 配置
class Config:
    PROMETHEUS_PORT = 9090
    CHECK_INTERVAL = 5  # 秒
    LLM_SERVICE_URL = "http://localhost:8080"
    LLM_HEALTH_ENDPOINT = "/health"
    LLM_METRICS_ENDPOINT = "/metrics"
    ENABLE_GPU_MONITORING = True
    ENABLE_SYSTEM_MONITORING = True
    ENABLE_SERVICE_MONITORING = True


def collect_system_metrics():
    """收集系统级指标"""
    try:
        # CPU使用率
        cpu_usage = psutil.cpu_percent(interval=1)
        CPU_USAGE.set(cpu_usage)
        logger.debug(f"CPU Usage: {cpu_usage}%")
        
        # 内存使用率
        memory = psutil.virtual_memory()
        MEMORY_USAGE.set(memory.used)
        logger.debug(f"Memory Used: {memory.used / 1e9:.2f} GB")
        
        # 磁盘使用率
        for part in psutil.disk_partitions():
            try:
                partition_usage = psutil.disk_usage(part.mountpoint)
                DISK_USAGE.labels(mount_point=part.mountpoint).set(partition_usage.percent)
                logger.debug(f"Disk Usage ({part.mountpoint}): {partition_usage.percent}%")
            except (PermissionError, FileNotFoundError):
                # 某些挂载点可能无法访问
                pass
    except Exception as e:
        logger.error(f"Error collecting system metrics: {e}")


def collect_gpu_metrics():
    """收集GPU指标"""
    try:
        # 使用nvidia-smi命令获取GPU信息
        result = subprocess.run(
            ['nvidia-smi', '--query-gpu=index,name,utilization.gpu,memory.total,memory.used,temperature.gpu,power.draw',
             '--format=csv,noheader,nounits'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True
        )
        
        if result.returncode != 0:
            logger.warning(f"nvidia-smi command failed: {result.stderr}")
            return
        
        # 解析输出
        for line in result.stdout.strip().split('\n'):
            parts = line.split(', ')
            if len(parts) < 7:
                continue
                
            gpu_id = parts[0]
            gpu_name = parts[1]
            gpu_util = float(parts[2])
            mem_total = float(parts[3]) * 1024 * 1024  # 转换为字节
            mem_used = float(parts[4]) * 1024 * 1024  # 转换为字节
            temp = float(parts[5])
            power = float(parts[6])
            
            # 更新Prometheus指标
            GPU_UTILIZATION.labels(gpu_id=gpu_id, gpu_name=gpu_name).set(gpu_util)
            GPU_MEMORY_TOTAL.labels(gpu_id=gpu_id, gpu_name=gpu_name).set(mem_total)
            GPU_MEMORY_USED.labels(gpu_id=gpu_id, gpu_name=gpu_name).set(mem_used)
            GPU_TEMPERATURE.labels(gpu_id=gpu_id, gpu_name=gpu_name).set(temp)
            GPU_POWER_DRAW.labels(gpu_id=gpu_id, gpu_name=gpu_name).set(power)
            
            logger.debug(f"GPU {gpu_id} ({gpu_name}): Util={gpu_util}%, Mem={mem_used/1e9:.2f}/{mem_total/1e9:.2f} GB, Temp={temp}C, Power={power}W")
    except FileNotFoundError:
        logger.warning("nvidia-smi not found, GPU monitoring disabled")
    except Exception as e:
        logger.error(f"Error collecting GPU metrics: {e}")


def collect_service_metrics():
    """收集LLM服务指标"""
    try:
        # 检查服务健康状态
        health_url = f"{Config.LLM_SERVICE_URL}{Config.LLM_HEALTH_ENDPOINT}"
        start_time = time.time()
        response = requests.get(health_url, timeout=10)
        request_time = time.time() - start_time
        
        if response.status_code == 200:
            SERVICE_UP.set(1)
            REQUEST_COUNT.labels(status="success").inc()
            REQUEST_LATENCY.observe(request_time)
            logger.debug(f"Service health check: UP (latency: {request_time:.3f}s)")
            
            # 尝试获取更详细的指标(如果服务提供)
            try:
                metrics_url = f"{Config.LLM_SERVICE_URL}{Config.LLM_METRICS_ENDPOINT}"
                metrics_response = requests.get(metrics_url, timeout=10)
                if metrics_response.status_code == 200:
                    # 解析简单的指标格式(示例)
                    metrics_text = metrics_response.text
                    
                    # 使用正则表达式提取指标
                    active_requests_match = re.search(r'llm_active_requests\s+(\d+)', metrics_text)
                    if active_requests_match:
                        active_requests = int(active_requests_match.group(1))
                        ACTIVE_REQUESTS.set(active_requests)
                        logger.debug(f"Active requests: {active_requests}")
                    
                    # 提取输入token计数
                    input_tokens_match = re.search(r'llm_tokens_processed\{direction="input"\}\s+(\d+)', metrics_text)
                    if input_tokens_match:
                        # 这里我们只是记录增量,实际应用中可能需要更复杂的逻辑
                        pass
                    
                    # 提取输出token计数
                    output_tokens_match = re.search(r'llm_tokens_processed\{direction="output"\}\s+(\d+)', metrics_text)
                    if output_tokens_match:
                        # 这里我们只是记录增量,实际应用中可能需要更复杂的逻辑
                        pass
            except Exception as e:
                logger.warning(f"Error collecting detailed service metrics: {e}")
        else:
            SERVICE_UP.set(0)
            REQUEST_COUNT.labels(status="error").inc()
            logger.warning(f"Service health check: DOWN (status code: {response.status_code})")
    except requests.exceptions.RequestException as e:
        SERVICE_UP.set(0)
        REQUEST_COUNT.labels(status="error").inc()
        logger.error(f"Error checking service health: {e}")


def main():
    """主函数"""
    logger.info("Starting LLM monitoring service")
    
    # 启动Prometheus HTTP服务器
    start_http_server(Config.PROMETHEUS_PORT)
    logger.info(f"Prometheus metrics available on port {Config.PROMETHEUS_PORT}")
    
    # 主循环
    while True:
        try:
            # 收集系统指标
            if Config.ENABLE_SYSTEM_MONITORING:
                collect_system_metrics()
            
            # 收集GPU指标
            if Config.ENABLE_GPU_MONITORING:
                collect_gpu_metrics()
            
            # 收集服务指标
            if Config.ENABLE_SERVICE_MONITORING:
                collect_service_metrics()
            
            # 等待下一个检查周期
            time.sleep(Config.CHECK_INTERVAL)
        except KeyboardInterrupt:
            logger.info("Monitoring service stopped by user")
            break
        except Exception as e:
            logger.error(f"Unexpected error in main loop: {e}")
            time.sleep(Config.CHECK_INTERVAL)


if __name__ == "__main__":
    main()

通过本文提供的详细指南和代码示例,您可以构建一个全面、高效的LLM开发环境监控仪表盘系统,实时掌握模型训练和服务运行状态,及时发现和解决问题,优化资源使用效率,确保LLM服务的稳定运行和高质量交付。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐