> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 为 LangSmith 遥测配置您的收集器

LangSmith 部署中的各种服务以日志、指标和追踪的形式发出遥测数据。您可能已经在 Kubernetes 集群中设置了遥测收集器，或者希望部署一个来监控您的应用程序。

本页描述了如何配置一个 [OTel 收集器](https://opentelemetry.io/docs/collector/configuration/) 以从 LangSmith 收集遥测数据。请注意，下面讨论的所有概念都可以转换到其他收集器，例如 [Fluentd](https://www.fluentd.org/) 或 [FluentBit](https://fluentbit.io/)。

<Warning>
  **此部分仅适用于 Kubernetes 部署。**
</Warning>

# 接收器

## 日志

这是一个 ***Sidecar*** 收集器从其自身 Pod 读取日志的示例，排除了非领域特定容器的日志。Sidecar 配置在这里很有用，因为我们要求访问每个容器的文件系统。也可以使用 DaemonSet。

```yaml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
filelog:
  exclude:
    - "**/otc-container/*.log"
  include:
    - /var/log/pods/${POD_NAMESPACE}_${POD_NAME}_${POD_UID}/*/*.log
  include_file_name: false
  include_file_path: true
  operators:
    - id: container-parser
      type: container
  retry_on_failure:
    enabled: true
  start_at: end
env:
  - name: POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name
  - name: POD_NAMESPACE
    valueFrom:
      fieldRef:
        fieldPath: metadata.namespace
  - name: POD_UID
    valueFrom:
      fieldRef:
        fieldPath: metadata.uid
volumes:
  - name: varlogpods
    hostPath:
      path: /var/log/pods
volumeMounts:
  - name: varlogpods
    mountPath: /var/log/pods
    readOnly: true
```

<Info>
  **此配置需要对给定命名空间中的 Pod 具有 'get'、'list' 和 'watch' 权限。**
</Info>

## 指标

可以使用 Prometheus 端点来抓取指标。可以使用单个 ***Gateway*** 收集器实例来避免在获取指标时查询重复。以下配置抓取所有默认命名的 LangSmith 服务：

```yaml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
prometheus:
  config:
    scrape_configs:
      - job_name: langsmith-services
        metrics_path: /metrics
        scrape_interval: 15s
        # 仅抓取 LangSmith 命名空间中的端点
        kubernetes_sd_configs:
          - role: endpoints
            namespaces:
              names: [<langsmith-namespace>]
        relabel_configs:
          # 仅抓取名称为 langsmith-.* 的服务
          - source_labels: [__meta_kubernetes_service_name]
            regex: "langsmith-.*"
            action: keep
          # 仅抓取具有以下名称的端口
          - source_labels: [__meta_kubernetes_endpoint_port_name]
            regex: "(backend|platform|playground|redis-metrics|postgres-metrics|metrics)"
            action: keep
          # 将有用的元数据提升为常规标签
          - source_labels: [__meta_kubernetes_service_name]
            target_label: k8s_service
          - source_labels: [__meta_kubernetes_pod_name]
            target_label: k8s_pod
          # 替换默认的 "host:port" 作为 Prom 的实例标签
          - source_labels: [__address__]
            target_label: instance
```

<Info>
  **此配置需要对给定命名空间中的 Pod、服务和端点具有 'get'、'list' 和 'watch' 权限。**
</Info>

### 追踪

对于追踪，您需要启用 OTLP 接收器。以下配置可用于监听端口 4318 上的 HTTP 追踪和端口 4317 上的 GRPC 追踪：

```yaml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
otlp:
  protocols:
    grpc:
      endpoint: 0.0.0.0:4317
    http:
      endpoint: 0.0.0.0:4318
```

## 处理器

### 推荐的 OTEL 处理器

使用 OTel 收集器时，推荐以下处理器：

* [批处理器](https://github.com/open-telemetry/opentelemetry-collector/blob/main/processor/batchprocessor/README.md)：在将数据发送到导出器之前，将数据分组成批。
* [内存限制器](https://github.com/open-telemetry/opentelemetry-collector/blob/main/processor/memorylimiterprocessor/README.md)：防止收集器使用过多内存而崩溃。当超过软限制时，收集器将停止接受新数据。
* [Kubernetes 属性处理器](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/k8sattributesprocessor)：将 Kubernetes 元数据（如 Pod 名称）添加到遥测数据中。

## 导出器

导出器只需要指向您喜欢的外部端点。以下配置允许您为日志、指标和追踪配置单独的端点：

```yaml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
otlphttp/logs:
  endpoint: <your_logs_endpoint>
otlphttp/metrics:
  endpoint: <your_metrics_endpoint>
otlphttp/traces:
  endpoint: <your_traces_endpoint>
```

<Note>
  **OTel 收集器也支持直接导出到 [Datadog](https://docs.datadoghq.com/opentelemetry/setup/collector_exporter) 端点。**
</Note>

# 收集器配置示例：日志 Sidecar

```yaml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
mode: sidecar
image: otel/opentelemetry-collector-contrib
config:
  receivers:
    filelog:
      exclude:
        - "**/otc-container/*.log"
      include:
        - /var/log/pods/${POD_NAMESPACE}_${POD_NAME}_${POD_UID}/*/*.log
      include_file_name: false
      include_file_path: true
      operators:
        - id: container-parser
          type: container
      retry_on_failure:
        enabled: true
      start_at: end
  processors:
    batch:
      send_batch_size: 8192
      timeout: 10s
    memory_limiter:
      check_interval: 1m
      limit_percentage: 90
      spike_limit_percentage: 80
  exporters:
    otlphttp/logs:
      endpoint: <your-endpoint>
  service:
    pipelines:
      logs/langsmith:
        receivers: [filelog]
        processors: [batch, memory_limiter]
        exporters: [otlphttp/logs]
env:
  - name: POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name
  - name: POD_NAMESPACE
    valueFrom:
      fieldRef:
        fieldPath: metadata.namespace
  - name: POD_UID
    valueFrom:
      fieldRef:
        fieldPath: metadata.uid
volumes:
  - name: varlogpods
    hostPath:
      path: /var/log/pods
volumeMounts:
  - name: varlogpods
    mountPath: /var/log/pods
    readOnly: true
```

# 收集器配置示例：指标和追踪 Gateway

```yaml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
mode: deployment
image: otel/opentelemetry-collector-contrib
config:
  receivers:
    prometheus:
      config:
        scrape_configs:
          - job_name: langsmith-services
            metrics_path: /metrics
            scrape_interval: 15s
            # 仅抓取 LangSmith 命名空间中的端点
            kubernetes_sd_configs:
              - role: endpoints
                namespaces:
                  names: [<langsmith-namespace>]
            relabel_configs:
              # 仅抓取名称为 langsmith-.* 的服务
              - source_labels: [__meta_kubernetes_service_name]
                regex: "langsmith-.*"
                action: keep
              # 仅抓取具有以下名称的端口
              - source_labels: [__meta_kubernetes_endpoint_port_name]
                regex: "(backend|platform|playground|redis-metrics|postgres-metrics|metrics)"
                action: keep
              # 将有用的元数据提升为常规标签
              - source_labels: [__meta_kubernetes_service_name]
                target_label: k8s_service
              - source_labels: [__meta_kubernetes_pod_name]
                target_label: k8s_pod
              # 替换默认的 "host:port" 作为 Prom 的实例标签
              - source_labels: [__address__]
                target_label: instance
    otlp:
      protocols:
        grpc:
          endpoint: 0.0.0.0:4317
        http:
          endpoint: 0.0.0.0:4318
  processors:
    batch:
      send_batch_size: 8192
      timeout: 10s
    memory_limiter:
      check_interval: 1m
      limit_percentage: 90
      spike_limit_percentage: 80
  exporters:
    otlphttp/metrics:
      endpoint: <metrics_endpoint>
    otlphttp/traces:
      endpoint: <traces_endpoint>
  service:
    pipelines:
      metrics/langsmith:
        receivers: [prometheus]
        processors: [batch, memory_limiter]
        exporters: [otlphttp/metrics]
      traces/langsmith:
        receivers: [otlp]
        processors: [batch, memory_limiter]
        exporters: [otlphttp/traces]
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/langsmith-collector.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
