将Nginx的日志数据进一步深入挖掘:通过New Relic的上下文日志功能将跟踪和日志关联起来的方法

请点击此处查看最新的更新详细信息:New Relic 更新列表。

首先

在这篇文章中,我们介绍了如何使用OpenTelemetry来收集nginx的遥测数据,并在New Relic上对这些数据进行可视化的方法。

 

只是,要充分利用New Relic的分布式追踪,仍然缺少一些设置。在这篇文章中,我们将介绍如何使用New Relic的Logs in Context来将nginx日志和追踪信息关联起来,以便可以查看每个事务的日志。关于Logs in Context的详细信息,请参考这篇博客。

 

Logs.png

将nginx的日志与New Relic进行集成。

本次,我们将使用Fluent Bit将nginx的日志传输到New Relic。首先,我们需要通过使用Fluentd Forward协议来配置OpenTelemetry Collector以接收日志。

receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318
+ fluentforward:
+ endpoint: 0.0.0.0:8006
processors:
  batch:

exporters:
  otlp:
    endpoint: https://otlp.nr-data.net:4317
    headers:
      api-key: ${NEW_RELIC_LICENSE_KEY} # New Relicのライセンスキーを環境変数に設定

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlp]
    metrics:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlp]
    logs:
-     receivers: [otlp]
+     receivers: [otlp, fluentforward]
      processors: [batch]
      exporters: [otlp]

接下来,我们将更改 Docker Compose 的定义文件,以便将日志从 nginx 输出到 OpenTelemetry Collector。

  # nginx
  web:
      container_name: nginx_web
      platform: 'linux/amd64'
      build:
        context: .
        dockerfile: ./containers/nginx.dockerfile
      ports:
          - "8080:80"
      depends_on:
          - php
+     logging:
+       driver: "fluentd"
+       options:
+         # OpenTelemetry Collector のポート 8006 を宛先に設定
+         fluentd-address: localhost:8006
+         # 起動時に接続を確立的無くても container は起動する設定
+         fluentd-async-connect: "true"
  # OpenTelemetry Collector
  otel-collector:
    container_name: otel-collector
    platform: 'linux/amd64'
    image: otel/opentelemetry-collector-contrib:0.83.0
    restart: always
    command: ["--config=/etc/otel-config.yml", ""]
    volumes:
      - ./otel-config.yml:/etc/otel-config.yml
    environment:
        # 環境変数でNew Relicのライセンスキーを設定
        - NEW_RELIC_LICENSE_KEY
+   # fluentforward で受信するためのポートを公開
+   ports:
+       - "8006:8006"

在这个设置中,nginx的日志将会与New Relic进行连接。现在,让我们在New Relic Logs中检查已连接的日志。

apm-logs-without-servicename.png

将nginx的日志与跟踪信息相关联,并在New Relic中显示。

要将日志与应用程序关联起来,需要在日志的元数据中设置service.name。请参考此官方文档以获取更多详细信息。

 

另外,如果要将应用程序的追踪信息和日志关联起来,在日志的元数据中需要设置追踪ID和跨度ID。请参考官方文档获取更多详细信息。

 

根据此内容,我们会逐步更改nginx的日志输出设置。

    log_format json escape=json '{"time": "$time_iso8601",'
                                '"host": "$remote_addr",'
                                '"vhost": "$host",'
                                '"user": "$remote_user",'
                                '"status": "$status",'
                                '"protocol": "$server_protocol",'
                                '"method": "$request_method",'
                                '"path": "$request_uri",'
                                '"req": "$request",'
                                '"size": "$body_bytes_sent",'
                                '"reqtime": "$request_time",'
                                '"apptime": "$upstream_response_time",'
                                '"user_agent": "$http_user_agent",'
                                '"forwardedfor": "$http_x_forwarded_for",'
                                '"forwardedproto": "$http_x_forwarded_proto",'
-                               '"referrer": "$http_referer"}';
+                               '"referrer": "$http_referer",'
+                               '"service.name": "nginx-php-proxy",'
+                               '"trace.id": "$opentelemetry_trace_id",'
+                               '"span.id": "$opentelemetry_span_id"}';

让我们通过此更改,在 New Relic 的 UI 上将 nginx 的日志与 nginx-php-proxy 的实体和跟踪信息关联起来。我们将重新构建 Docker 并确认结果。

apm-dt-logs-with-servicename.png

请提供相关信息

スクリーンショット 2023-09-03 22.25.26.png

总结

本文介绍了如何使用OpenTelemetry来收集nginx的追踪信息和日志,并在New Relic的Logs in Context中关联并可视化这些数据。

这样一来,可以有效地收集nginx的日志数据,并在New Relic中进行可视化。特别是通过启用分布式追踪,可以清楚地了解事务级别的日志。nginx的日志对于发现和解决系统问题至关重要。通过结合OpenTelemetry和New Relic,可以高效地完成这些工作。

希望尝试的朋友请注意,本次介绍的OpenTelemetry和New Relic的协作可以在永久免费许可下使用。请务必从此处尝试永久免费许可。

 

请点击此处查看最新更新的详细内容。新的Relic更新概览。
bannerAds