vague memory

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

Datadog Logs Windows上の日本語ログ(SJIS)登録

Windows 上の Datadog Agent v6 でのログを tail で収集する場合、対象のログファイルは UTF8 である必要があるとのことです。

Note: If you are using the Windows 6 Agent and trailing files for logs - make sure that those files have a UTF8 encoding.

SJISのログを送信すると文字化けしてしまうので、UTF8へ変換しつつのログ送信を検証します。
Datadogのマニュアルに未サポートのログファイルに対しては、ログ転送ソフトを介して送信するよう案内があります。 td-agent 3 が Windows をサポートしたので、今回は td-agent(fluentd) を使用します。

f:id:htnosm:20180811161224p:plain



環境

  • Windows Server 2012 R2 Standard
    • Datadog Agent Version 6.3.3
    • td-agent Version v3.1.1 (Fluentd v1.0.2)

対象ログファイル

以下の様な Shift_JIS 日本語を含むログファイルを対象とします。

f:id:htnosm:20180811162717p:plain

設定

Datadog Agent

ログ収集機能を有効化します。

  • [Datadog Agent Manager] -> [Settings]
    • 実体は C:\ProgramData\Datadog\datadog.yaml
log_enabled: true

カスタムログ用ファイル格納

任意の名称で設定ファイルを格納します。

mkdir C:\ProgramData\Datadog\conf.d\customlog.d
echo. > C:\ProgramData\Datadog\conf.d\customlog.d\conf.yaml

ディレクトリとファイルを作成することで、 GUI にも表示されるようになります。

  • [Datadog Agent Manager] -> [Checks] -> [Manage Checks]

Datadog Agent (v6)

datadog-agent(v6) 標準機能でのログ収集設定です。

  • customlog.d\conf.yaml
logs:

  - type: file
    path: C:\log\test.log
    service: app
    source: customlog

結果例

  • 文字化けする

f:id:htnosm:20180811161216p:plain

fluentd(td-agent)

fluentd で変換して取り込む設定です。 2パターン検証しましたが、いずれでも日本語文字列の文字化けは回避できました。

td-agent3 から Windows をサポートしています。

現時点での最新版 version v3.1.1 からは beta の文字が取れていました。

f:id:htnosm:20180811163048p:plain

文字コード変換には fluent-plugin-record-modifier の char_encoding を使用します。

設定

プラグインインストール

td-agent をインストールすると td-agent command prompt が利用できますので、そこからプラグインをインストールします。

f:id:htnosm:20180811162934p:plain

C:\opt\td-agent>fluent-gem install fluent-plugin-record-modifier
WARN: Unresolved specs during Gem::Specification.reset:
      win32-api (>= 1.4.5)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
Fetching: fluent-plugin-record-modifier-1.1.0.gem (100%)
Successfully installed fluent-plugin-record-modifier-1.1.0
Parsing documentation for fluent-plugin-record-modifier-1.1.0
Installing ri documentation for fluent-plugin-record-modifier-1.1.0
Done installing documentation for fluent-plugin-record-modifier after 0 seconds
1 gem installed

ログディレクト

pos ファイル格納用のディレクトリを作成しておきます。

C:\opt\td-agent>mkdir var\log

td-agent.conf

td-agent.conf は以下に格納されています。

C:\opt\td-agent\etc\td-agent\td-agent.conf

tail Input と record-modifier の設定を行います。

<source>
  @type tail
  path C:\log\test.log
  pos_file C:\opt\td-agent\var\log\test.log.pos
  tag fluentd.raw
  format none
</source>

<match fluentd.raw>
  @type record_modifier
  tag fluentd.encode
  char_encoding Windows-31J:utf-8
</match>

a) fluent-plugin-datadog

Datadog 公式のプラグインです。

インストール

C:\opt\td-agent>fluent-gem install fluent-plugin-datadog
WARN: Unresolved specs during Gem::Specification.reset:
      win32-api (>= 1.4.5)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
Fetching: fluent-plugin-datadog-0.10.4.gem (100%)
Successfully installed fluent-plugin-datadog-0.10.4
Parsing documentation for fluent-plugin-datadog-0.10.4
Installing ri documentation for fluent-plugin-datadog-0.10.4
Done installing documentation for fluent-plugin-datadog after 0 seconds
1 gem installed

設定

以下の様に設定します。 (便宜的に store を使用していますが、出力先が一つであれば不要です)

<match fluentd.encode>
  @type copy
  <store>
    @type datadog
    @id awesome_agent
    api_key <your api key>

    # Optional
    include_tag_key true
    tag_key 'tag'

    # Optional parameters
    dd_source customlog
    dd_tags 'host:WIN-XXXXXXXXXXX,service: app,filename:test.log'
    dd_sourcecategory multibytes
  </store>
</match>

結果例

  • 文字化け解消
  • Host、Serviceが付かない
    • dd_tags で Tag を付与しているが、一覧上は抜けた状態

f:id:htnosm:20180811161355p:plain f:id:htnosm:20180811161351p:plain

b) file Output Plugin

fluentd 標準で何とかする案です。変換後のファイルを Datadog Agent に読ませます。

設定

以下の様に設定します。

<match fluentd.encode>
  @type copy
  <store>
    @type file
    path C:\log\test.utc8.log
    append true
  </store>
</match>
  • customlog.d\conf.yaml
logs:

  - type: file
    path: C:\log\test.utc8.log\*.log
    service: app
    source: customlog

結果例

  • 文字化け解消
  • Datadog Agent 側の設定に沿って Host,Servce,Source が付与
  • ログ部分は JSON 形式 {"message": ""}

f:id:htnosm:20180811161347p:plain

サービス化

公式の手順通りに fluentd を Windows サービス化します。

  • td-agent command prompt
fluentd --reg-winsvc i
fluentd --reg-winsvc-fluentdopt '-c C:/opt/td-agent/etc/td-agent/td-agent.conf -o C:/opt/td-agent/var/log/td-agent.log'

f:id:htnosm:20180811163137p:plain

折角なので fluentd の監視 ( FluentD ) も入れます。

  • td-agent.conf
<source>
  type monitor_agent
  bind 0.0.0.0
  port 24220
</source>
  • Datadog (conf.d/fluentd.yaml)
init_config:

instances:
    -  monitor_agent_url: http://example.com:24220/api/plugins.json

f:id:htnosm:20180811161336p:plain


まとめ

日本語ログは扱いに困ります。

  • Windows での Datadog Agent(v6) Logs は UTF8 でないと文字化けする
  • UTF8 へ変換することで文字化けは解消可能(今回は fluentd を利用)
    • a案 fluent-plugin-datadog は Host、Service が付与されない
    • b案 file Output で別ファイルに書き出す事場合は出力形式が変わる

公式プラグインがイマイチだったのでb案を試してみた感じですが、性能面は全く見てないので実際使えるかは要検証です。