1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OpenTelemetry CollectorでログをSplunkに送信

Last updated at Posted at 2023-04-11

はじめに

OpenTelemetry Collector(以下Otel)でトレースやメトリクスをオブザーバビリティツールに送るのは一般的なケースになりつつあるかと思います。

しかし、ログの有効性はまだまだ残ります。
SplunkもAPMやRUMを使えるSplunk Observability Cloudという製品を提供していますが、Splunk Platform (Enterprise / Cloud)と連携して使うことができますし、もちろんSplunk Platform単体で高度なログ分析もできます。

そこで、Otelでログ収集、送信は可能でしょうか?
実はできます。

SplunkがOtelを拡張したSplunk OpenTelemetry Collectorにはログ収集のためfluentdがバンドルされていますし、最近Otelにはfilelog receiverというログ収集のためのreceiverが提供されています。
※Splunk Otelもゆくゆくはfilelog receiverに移行するという予定もあるそうです

送信はsplunkhec exporterで可能です。

本記事ではfilelog receiver、splunkhec exporterを使ってSplunk Enterpriseにログを送信してみます。

構成

Otelでは以下の構成を取ります。

  • receiver
    filelog

  • processor(任意)
    resourcedetection、resource など

  • exporter
    splunkhec

つまり、filelog receiverでログを収集、processorでattributeを追加し、exporterでSplunk EnterpriseにHECで送信します。

Linux (Ubuntu) で試したいと思います。

では早速やってみましょう。

設定

HEC準備

普通にSplunkでHECを準備します。

  1. ヘッダーメニューから設定 > データ入力
  2. HTTPイベントコレクターの「新規追加」をクリック
  3. 適当に設定
    名前:任意(「Otel」など)
    ソースタイプ:自動
    インデックス:いじらない ※indexもsourcetypeもOtel側で設定するため
  4. 払い出されたトークン値をメモしておく

Otelインストール

Otelのcontrib版をインストールします。

wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.75.0/otelcol-contrib_0.75.0_linux_amd64.deb
sudo apt install ./otelcol-contrib_0.75.0_linux_amd64.deb -y

※最新バージョンは下記ページを確認してください。

なお、Splunk Observability Cloudを利用の方はSplunk OpenTelemetry Collectorの方が良いです。
本家のOtelよりも機能が充実していたりコンフィグのデフォルト設定がリッチだったりします。

Otel設定

Otelの設定をしていきます。
デフォルトのコンフィグパスは/etc/otelcol-contrib/config.yamlです。

filelog receiverの設定

まずはfilelog receiverです。
以下のような例が載っています。

receivers:
  filelog:
    include: [ /var/log/myservice/*.json ]
    operators:
      - type: json_parser
        timestamp:
          parse_from: attributes.time
          layout: '%Y-%m-%d %H:%M:%S'

シンプルですね。includeで指定したファイルのリストに対してtailしてくれるようです。
operatorsでパースできるようですが、これはSplunkに任せた方がいいので無視しちゃいましょう。
ここでSplunkユーザーであれば疑問に思うのは「indexとsourcetypeの設定は???」でしょう。

親切なことにSplunkが設定例を公開しています。

otel-logs-with-sourcetypes-splunkの例をみてみましょう。

processors:
  batch:
  resource/one:
    attributes:
      # Set the com.splunk.sourcetype log attribute key to sourcetype1.
      # com.splunk.sourcetype is the default key the HEC exporter will use to extract the source type of the record.
      # See https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/splunkhecexporter
      # under the configuration key `hec_metadata_to_otel_attrs/sourcetype`
      - key: com.splunk.sourcetype
        value: "sourcetype1"
        action: upsert
  resource/two:
    attributes:
      - key: com.splunk.sourcetype
        value: "sourcetype2"
        action: upsert
  resource/three:
    attributes:
      - key: com.splunk.sourcetype
        value: "sourcetype3"
        action: upsert

この例だとresource processorでcom.splunk.sourcetypeというattributeに値を設定しているようです。
多分indexはcom.splunk.index、sourceはcom.splunk.sourceでしょう。

しかしファイルによってsourcetypeを変えたいので一律でsourcetypeを設定するのはあまりやりたくありません。
幸いにもfilelog receiverでattributeを指定できますのでやってみましょう。

Otelが入っているのがNginxサーバーと仮定してアクセスログを取得してみます。

  • HTTPアクセスログのsourcetypeと言えば、、、そう、access_combined_wcookieですね。
  • indexは適当にsandboxにでも送ってみます(indexは用意しておいてください)。
  • sourceもファイルパスをセットします。

ついでにerrorログも取得します。sourcetypeは適当に付けます。
※ログファイルの読み取り権限には注意ください。Otel contribはotelcol+ユーザーで実行されます。

receivers:
  filelog/nginx_access:
    include: [ /var/log/nginx/access.log ]
    attributes:
      com.splunk.sourcetype: "access_combined_wcookie"
      com.splunk.index: "sandbox"
      com.splunk.source: "/var/log/nginx/access.log"
  filelog/nginx_error:
    include: [ /var/log/nginx/error.log ]
    attributes:
      com.splunk.sourcetype: "nginx_error"
      com.splunk.index: "sandbox"
      com.splunk.source: "/var/log/nginx/error.log"

processorの設定

次にprocessorです。できることは色々あります。

ここではバッチ処理をするbatch、ホスト情報を自動付与してくれるresourcedetection、任意のattributeを追加するresourceを使ってみましょう。

processors:
  batch:
  resourcedetection:
    detectors: [ec2, system]
    override: true
  resource:
    attributes:
      - key: server-type
        value: "Web Server"
        action: upsert

splunkhec exporterの設定

最後にsplunkhec exporterです。

これも例を見ながら作ると簡単かと思います。

exporters:
  splunk_hec:
    token: "HECトークン"
    endpoint: "https://ホスト:8088/services/collector" #Splunk Cloudの場合はhttp-inputs-<スタック名>.splunkcloud.com:443
    source: "otel" #デフォルト値
    sourcetype: "otel" #デフォルト値
    index: "main" #デフォルト値
    tls:
      insecure_skip_verify: true #Splunk Enterpriseで証明書がオレオレな場合は必要

pipelineの設定

必要なコンポーネントの設定が整ったのでpipelineを組み立てます。

service:
  pipelines:
    logs/test:
      receivers: [filelog/nginx_access, filelog/nginx_error]
      processors: [batch, resourcedetection, resource]
      exporters: [splunk_hec]

設定を有効化するためにOtelを再起動します。

sudo systemctl restart otelcol-contrib

送信してみる

適当にNginxにアクセスしてログを出力させてみます。
その後、Splunkで確認すると

image.png

いいですね!Nginxのアクセスログが取得でき、index、sourcetype、sourceもセットできています。

フィールドを見てみると

image.png

想定通り、ホスト情報の自動付与やsourcetypeによるフィールド自動抽出がされています。

まとめ

Otelを使い、トレースやメトリクスをAPMツールなどに送るケースが増えていると思います。ログ分析についてはSplunkで行うとき同じOtelでログも送れたら便利な場合もあると思います。
そんなときでも可能であることが分かりました。

Otelなんでもできちゃいますねー

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?