vague memory

うろ覚えを無くしていこうともがき苦しむ人の備忘録

New Relic Nginx integration のログ転送

Datadog でログの日時がずれる問題(Datadog Nginx error.log の timestamp を考慮する)があった際に、 New Relic ではどうなるのだろうかとふと思いました。



結論

  • Nginx integration の標準インストールで特に問題無し

これで終わってしまうのでインストールの際に気付いた点を連連とメモ。

前提

# /etc/newrelic-infra/logging.d/nginx-log.yml.example
logs:
  - name: nginx-access
    file: /var/log/nginx/access.log
    attributes:
      logtype: nginx
  - name: nginx-error
    file: /var/log/nginx/error.log
    attributes:
      logtype: nginx-error

ログ取得結果

access.log、error.log の timestamp がいずれも JST のログ時間と合致します。

留意点

公式ドキュメントのあちらこちらに書かれていますが、二重にログが送信されることがあります。

既存のログ転送ソリューションがあり、コンテキストで自動ログを使用するようにエージェントを更新している場合は、手動ログ転送を必ず無効にしてください。それ以外の場合、アプリは二重のログ行を送信します。アカウントによっては、これにより二重請求が発生する可能性があります。

設定ファイル内記述を重複記述できるため、 Infrastructure agent のみ使用している場合でもこの事象は発生しえます。

既に Nginx が導入済みの環境に Infrastructure Agent をインストールすると、自動検出して Nginx Integration を併せてインストールすることができます。 この際に Log 設定も追加されます。

We've detected additional monitoring that can be configured by installing the following:
  NGINX Integration

? Continue installing?  Yes

==> Installing NGINX Integration
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[OK] All checks passed. Installing Nginx Integration...

✔ Installing NGINX Integration
   Installed

  New Relic installation complete

  --------------------
  Installation Summary

  ✔  Infrastructure Agent  (installed)
  ✔  Logs Integration  (installed)
  ✔  NGINX Integration  (installed)

  View your data at the link below:
  ⮕  https://onenr.io/XXXXXXXXXX

  View your logs at the link below:
  ⮕  https://onenr.io/XXXXXXXXXX

  --------------------

その後公式の Nginx Integration インストール手順に沿って のインストールを進めると、logging.d/nginx-log.yml を作成する記載があります。

6. 自動NGINXアクセス、エラーログ解析および転送を有効にするには、nginx-log.yml.exampleファイルをnginx-log.ymlにコピー(または名前変更)します。エージェントを再起動する必要はありません。

ここで作成を行ってしまうと、Nginx のログに対する設定が二箇所に記載されることになり、ログが二重で送信されます。

なお、自動検出によるログの設定は /etc/newrelic-infra/logging.d/discovered.yml に記述されています。

ログ解析ルール

Built-in の解析ルールの詳細は公式ドキュメントに記載されています。

標準フォーマットに沿った内容となっているため、 属性の不足がある場合や、ログフォーマットの変更を行っている場合など、 合致しない場合は独自の解析ルールを作成する必要があります。

例として標準の Nginx の解析ルールだと http_x_forwarded_for が含まれていません。

  • Nginx 標準の log_format
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
  • logtype = 'nginx' parsing rule
%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes}|-) %{QS:referrer} %{QS:agent}

ログ解析し属性化することでグラフ作成なども容易に行えるようになります。

SELECT count(*) FROM Log WHERE `logtype` = 'nginx' FACET response SINCE 1 hour ago TIMESERIES