首页 > 解决方案 > 在 docker swarm + fluentd 上记录 java 异常

问题描述

我在我的 swarm 集群中进行了配置,以使用 fluentd 将日志发送到 elasticsearch。这部分工作正常,但是我的 java 图像的异常日志出现在记录中的每个堆栈行。我已经尝试过使用 detect_exceptions 和多行插件,但在我看来,它们仅在源为“tail”类型(在我的情况下为“forward”)时才有效。

我的 stack.yml

version: '3.6'

....

services:

  myjavaservice:
    image: myjavaservice
    logging:
      driver: "fluentd"
      options:
        tag: myjavaservice
    deploy:
      placement:
        constraints: [node.role==worker]
      replicas: 1

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.4.2
    ports:
      - "9200:9200"
    logging:
      driver: "json-file"
      options:
        max-size: 10M
        max-file: 1
    volumes:
      - esdata:/usr/share/elasticsearch/data
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.hostname == manager

  fluentd:
    image: my.repo/fluentd
    volumes:
      - ./Logs:/fluentd/log
    ports:
      - "24224:24224"
      - "24224:24224/udp"
    deploy:
      replicas: 1
      placement:
        constraints: [node.role == manager]
      update_config:
        delay: 2s
.....

还有我的 fluentd.conf

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>

<filter *>
  @type record_transformer
  <record>
    hostname "#{Socket.gethostname}"
    tag ${tag}
  </record>
</filter>

<label @raw>
  <match myapp.*>
    @type detect_exceptions
    remove_tag_prefix myapp
    message log
    languages java
    multiline_flush_interval 0.5
  </match>

  <match *>
    @type copy
    <store>
      @type elasticsearch
      host elasticsearch
      port 9200
      logstash_format true
      logstash_prefix logstash
      logstash_dateformat %Y%m%d
      include_tag_key true
      tag_key @log_name
      flush_interval 1s
    </store>
  </match>
</label>

你能告诉我是否有可能在 swarm 上使用日志驱动程序流利地执行此操作(将整个异常堆栈放在记录中)?

标签: javadockerelasticsearchdocker-swarmfluentd

解决方案


谢谢okkez。我能够使用 concat 插件解决问题,但我也要测试你通过的这个解决方案。这是我实施的解决方案:

<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>

<filter **>
  @type concat
  key log
  stream_identity_key container_id
  multiline_start_regexp /^\S+/
  flush_interval 1s
  timeout_label @processdata
</filter>

<label @ERROR>
  <match **>
    @type stdout
  </match>
</label>

<label @processdata>
  <match **>
    @type stdout
  </match>
</label>

<match **>
  @type elasticsearch
  logstash_format true
  host elasticsearch
  port 9200
  index_name fluentd
  type_name fluentd
  flush_interval 5s
</match>

推荐阅读