本文档介绍了如何修改使用现有导出器(在本例中为
googlecloud日志记录导出器)的 OpenTelemetry 收集器日志记录配置
,以使用 OTLP 导出器和
Telemetry (OTLP) API
telemetry.googleapis.com。
启用 Telemetry API
OTLP 导出器会写入 Telemetry API,因此必须在您的项目中启用该 API。运行以下命令来启用 Telemetry API:
gcloud services enable telemetry.googleapis.com
找到要替换的导出器
在配置文件中,找到日志记录的 exporters 条目。您的配置可能如下所示:
exporters:
googlecloud/logging:
替换 googlecloud/logging 导出器并添加 googleauthclient 扩展程序:
exporters:
otlp_grpc/otlp_logs:
auth:
authenticator: googleclientauth
balancer_name: pick_first
endpoint: telemetry.googleapis.com:443
extensions:
googleclientauth: {}
添加处理器
在配置文件中,找到 processors 条目。您的配置可能如下所示:
processors:
resourcedetection/_global_0: [...details omitted...]
transform/mylog__source_0: [...details omitted...]
将以下每个处理器的配置复制并粘贴到您的配置文件中:
一个
resource处理器,用于捕获有关您的 Google Cloud 项目的信息。resource/gcp_project_id: attributes: - action: insert value: PROJECT_ID key: gcp.project_id一个
transform处理器,用于保留有关插桩来源的信息。transform/otlp_grpc/preserve_instrumentation_source_version: error_mode: ignore log_statements: - context: log statements: - set(attributes["instrumentation_source"], instrumentation_scope.name) where instrumentation_scope.name != "" - set(attributes["instrumentation_version"], instrumentation_scope.version) where instrumentation_scope.version != ""一个
transform处理器,用于保留有关资源属性的信息。transform/otlp_grpc/preserve_service_resource_attributes: error_mode: ignore log_statements: - context: log statements: - set(attributes["service.name"], resource.attributes["service.name"]) where resource.attributes["service.name"] != nil - set(attributes["service.namespace"], resource.attributes["service.namespace"]) where resource.attributes["service.namespace"] != nil - set(attributes["service.instance.id"], resource.attributes["service.instance.id"]) where resource.attributes["service.instance.id"] != nil
修改后的配置如下所示:
processors:
resourcedetection/_global_0: [...details omitted...]
transform/mylog__source_0: [...details omitted...]
resource/gcp_project_id: [...details omitted...]
transform/otlp_grpc/preserve_instrumentation_source_version: [...details omitted...]
transform/otlp_grpc/preserve_service_resource_attributes: [...details omitted...]
修改日志记录服务和流水线
在配置中,找到用于日志记录的 service,其中包括使用 googlecloud/logging 导出器的 pipeline。您的配置可能如下所示:
service:
pipelines:
logs/logs_my__pipeline_mylog__source:
exporters:
- googlecloud/logging
processors:
- transform/mylog__source_0
- resourcedetection/_global_0
receivers:
- tcplog
对此配置进行以下更改:
- 将
googleclientauth扩展程序添加到service条目。 - 将流水线的导出器更改为
otlp_grpc/otlp_logs。 - 将新处理器添加到流水线的处理器列表中:
resource/gcp_project_idtransform/otlp_grpc/preserve_instrumentation_source_versiontransform/otlp_grpc/preserve_service_resource_attributes
修改后的配置如下所示:
service:
extensions:
- googleclientauth
pipelines:
logs/logs_my__pipeline_mylog__source:
exporters:
- otlp_grpc/otlp_logs
processors:
- transform/mylog__source_0
- resourcedetection/_global_0
- resource/gcp_project_id
- transform/otlp_grpc/preserve_instrumentation_source_version
- transform/otlp_grpc/preserve_service_resource_attributes
receivers:
- tcplog
根据需要迁移信息中心和提醒政策
如果您有跟踪 Cloud Logging API 使用情况的图表或提醒政策,请将其更新为监控 Telemetry API 的使用情况。OTLP 导出器会使用 Telemetry API 而不是 Cloud Logging API 将日志发送到您的 Google Cloud 项目。