RailsのログをFluent Bitを経由してAzure Blob Storageに格納する

先日、RailsのログをFluent Bitに送るということをやってみました。

miyohide.hatenablog.com

今日はそこから少し発展させて、Fluent BitからAzure Blob Storageに格納することをやってみます。

Fluent Bitの設定

Flulent BitからAzure Blob Storageに格納するときの設定項目と記述例は以下に記載があります。

docs.fluentbit.io

先日の私の実装例を元にすると、fluent-bit.confの記載は以下のようになりました。

[SERVICE]
  Log_Level  info

[INPUT]
  Name forward
  Listen 0.0.0.0
  Port   24224

[OUTPUT]
  Name                  azure_blob
  Match                 *
  account_name          ${ACCOUNT_NAME}
  shared_key            ${STORAGE_KEY}
  container_name        logs
  auto_create_container on
  tls                   on

ここで、アカウント名とキーは念の為環境変数経由で指定するようにしています。Fluent Bitの設定ファイルから環境変数の値を参照する方法については、以下の記載を参照しました。

docs.fluentbit.io

実行

実行すると、ログの出力と合わせて、Azure Blob Storageへの書き込みログが出力されます(下記の最後の行)。

rails_and_sidecar-web-1     | 07:08:25 web.1  | Processing by PostsController#show as TURBO_STREAM
rails_and_sidecar-web-1     | 07:08:25 web.1  |   Parameters: {"id"=>"3"}
rails_and_sidecar-web-1     | 07:08:25 web.1  |   Post Load (2.4ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
rails_and_sidecar-web-1     | 07:08:25 web.1  |   ↳ app/controllers/posts_controller.rb:64:in `set_post'
rails_and_sidecar-web-1     | 07:08:25 web.1  |   Rendering layout layouts/application.html.erb
rails_and_sidecar-web-1     | 07:08:25 web.1  |   Rendering posts/show.html.erb within layouts/application
rails_and_sidecar-web-1     | 07:08:25 web.1  |   Rendered posts/_post.html.erb (Duration: 0.0ms | Allocations: 19)
rails_and_sidecar-web-1     | 07:08:25 web.1  |   Rendered posts/show.html.erb within layouts/application (Duration: 0.8ms | Allocations: 650)
rails_and_sidecar-web-1     | 07:08:25 web.1  |   Rendered layout layouts/application.html.erb (Duration: 7.8ms | Allocations: 3045)
rails_and_sidecar-web-1     | 07:08:25 web.1  | Completed 200 OK in 11ms (Views: 8.1ms | ActiveRecord: 2.4ms | Allocations: 3813)
rails_and_sidecar-web-1     | 07:08:25 web.1  | 
rails_and_sidecar-web-1     | 07:08:25 web.1  | 
rails_and_sidecar-fluent-1  | [2023/03/19 07:08:26] [ info] [output:azure_blob:azure_blob.0] content appended to blob successfully

Azure Blob Storageを確認すると、私の場合developmentというものが作成されていました。

中身を見ると、Railsのログが出力されていました。

{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["Processing by PostsController#show as TURBO_STREAM"],"severity":"INFO"}
{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["  Parameters: {\"id\"=>\"3\"}"],"severity":"INFO"}
{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["  \u001b[1m\u001b[36mPost Load (2.4ms)\u001b[0m  \u001b[1m\u001b[34mSELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"id\" = ? LIMIT ?\u001b[0m  [[\"id\", 3], [\"LIMIT\", 1]]"],"severity":"DEBUG"}
{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["  \u21b3 app/controllers/posts_controller.rb:64:in `set_post'"],"severity":"DEBUG"}
{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["  Rendering layout layouts/application.html.erb"],"severity":"DEBUG"}
{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["  Rendering posts/show.html.erb within layouts/application"],"severity":"DEBUG"}
{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["  Rendered posts/_post.html.erb (Duration: 0.0ms | Allocations: 19)"],"severity":"DEBUG"}
{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["  Rendered posts/show.html.erb within layouts/application (Duration: 0.8ms | Allocations: 650)"],"severity":"INFO"}
{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["  Rendered layout layouts/application.html.erb (Duration: 7.8ms | Allocations: 3045)"],"severity":"INFO"}
{"@timestamp":"2023-03-19T07:08:25.000000Z","messages":["Completed 200 OK in 11ms (Views: 8.1ms | ActiveRecord: 2.4ms | Allocations: 3813)\n\n"],"severity":"INFO"}