分享

Fluentd配置:缓存(Buffer)配置项

 RealPython 2021-03-14

Fluentd的output插件支持<buffer>配置项,用以缓存日志事件,提高系统性能。可在此配置项中设置buffer插件的相关参数。

  1. Buffer配置项概览
    由于是output缓存,<buffer>需要在<match>中进行配置。
    <match tag.*> @type file
    # ... parameters for output plugin
    <buffer> # buffer section parameters ... </buffer>
    # <buffer> section can be configured just once</match>

  2. buffer插件类型
    使用@type参数在<buffer>中指定缓存插件类型:
    <buffer> @type file</buffer>
    Fluentd内核绑定了两种缓存插件:memory和file。也可以根据需要安装其他插件。
    @type可以省略。省略则使用output插件默认的缓存插件(如果有的话),否则就使用memory。
    对于大多数场景,建议使用file buffer插件,可以更多的持久化事件,避免memory的易失性。

  3. Chunk键值配置
    output插件将收集到的事件组织为buffer chunk进行管理。可在<buffer>中配置chunk的键值,来决定如何收集日志事件。
    <buffer ARGUMENT_CHUNK_KEYS> # ...</buffer>
  4. chunk的键值形式为逗号分隔的字符串,或者留空。

    1. 默认chunk键
      若不设置<buffer>的chunk键,并且所在output插件也没有指定默认的chunk键,output插件将会把所有收匹配到的事件写入同一个chunk,直到写满才进行flush。
      <match tag.**> # ... <buffer> # ... </buffer></match>
      # No chunk keys: All events will be appended into the same chunk.
      11:59:30 web.access {"key1":"yay","key2":100} --| |12:00:01 web.access {"key1":"foo","key2":200} --|---> CHUNK_A |12:00:25 ssh.login {"key1":"yay","key2":100} --|
    2. chunk为tag
      若使用tag作为chunk的键值,output插件将会为每类tag维护一个chunk,相同tag的事件被写入同一个chunk。
      <match tag.**> # ... <buffer tag> # ... </buffer></match>
      # Tag chunk key: events will be separated per tags
      11:59:30 web.access {"key1":"yay","key2":100} --| |---> CHUNK_A12:00:01 web.access {"key1":"foo","key2":200} --|
      12:00:25 ssh.login {"key1":"yay","key2":100} ------> CHUNK_B
    3. chunk为time
      若time作为chunk的键值(此时必须设置buffer的time_key参数),output插件将按照time_key的设定将事件写入每个time_key_chunk。
      Time key的计算公式为:time(unix time) / timekey(seconds)。例如:
      timekey 60: ["12:00:00", ..., "12:00:59"], ["12:01:00", ..., "12:01:59"], ...timekey 180: ["12:00:00", ..., "12:02:59"], ["12:03:00", ..., "12:05:59"], ...timekey 3600: ["12:00:00", ..., "12:59:59"], ["13:00:00", ..., "13:59:59"], ...

      事件会按照计算出的时间范围写入不同的chunk,并将在chunk过期后进行flush。

      <match tag.**> # ... <buffer time> timekey 1h # chunks per hours ("3600" also available) timekey_wait 5m # 5mins delay for flush ("300" also available) </buffer></match>
      # Time chunk key: events will be separated for hours (by timekey 3600)
      11:59:30 web.access {"key1":"yay","key2":100} ------> CHUNK_A
      12:00:01 web.access {"key1":"foo","key2":200} --| |---> CHUNK_B12:00:25 ssh.login {"key1":"yay","key2":100} --|

      timekey_wait参数用来设定chunk的输出延迟时间,事件在chunk中停留“延迟时间”后被flush。默认值为600(10分钟)。

      timekey: 3600 ------------------------------------------------------- time range for chunk | timekey_wait | actual flush time 12:00:00 - 12:59:59 | 0s | 13:00:00 12:00:00 - 12:59:59 | 60s (1m) | 13:01:00 12:00:00 - 12:59:59 | 600s (10m) | 13:10:00

      上边表格展示了每小时的缓存事件在不同的timekey_wait设置情况下实际的flush时间。

    4. 使用事件record的字段作为chunk键
      除去tag和time,chunk的键值还可以设置为事件record的字段。output插件将会根据这个字段的值来将事件写入不同的chunk。
      <match tag.**> # ... <buffer key1> # ... </buffer></match>
      # Chunk keys: events will be separated by values of "key1"
      11:59:30 web.access {"key1":"yay","key2":100} --|---> CHUNK_A |12:00:01 web.access {"key1":"foo","key2":200} -)|(--> CHUNK_B |12:00:25 ssh.login {"key1":"yay","key2":100} --|

      这里,事件将会按照key1的不同取值写入相应的chunk中。

      chunk键值支持嵌套的record字段。可以参照插件的record_accessor语法来访问嵌套字段。例如:

      <match tag.**> # ... <buffer $.nest.field> # access record['nest']['field'] # ... </buffer></match>
    5. 键值组合
      可以使用多个chunk键来配置缓存方式。
      # <buffer tag,time>
      11:58:01 ssh.login {"key1":"yay","key2":100} ------> CHUNK_A
      11:59:13 web.access {"key1":"yay","key2":100} --| |---> CHUNK_B11:59:30 web.access {"key1":"yay","key2":100} --|
      12:00:01 web.access {"key1":"foo","key2":200} ------> CHUNK_C
      12:00:25 ssh.login {"key1":"yay","key2":100} ------> CHUNK_D

      这里,将事件按照tag+time的方式缓存到不同的chunk中。

      当然,chunk键值组合不宜过多,否则会降低I/O性能,也会消耗系统资源。

    6. 空键

      chunk的键值可以设置为[],用以禁用output插件默认的chunk键配置。

      <match tag.**> # ... <buffer []> # ... </buffer></match>
  5. 占位符

    Fluentd配置文件支持占位符变量,这些变量会在运行中被替换为实际的值。比如out_file插件支持在path参数中设置占位符。

    # chunk_key: tag# ${tag} will be replaced with actual tag string<match log.*> @type file path /data/${tag}/access.log #=> "/data/log.map/access.log" <buffer tag> # ... </buffer></match>
  6. buffer参数
    除了上边chunk键值的配置,buffer还支持以下参数。
    1. 部分参数
      1. chunk_limit_size:每个chunk的最大空间。memory默认为8MB,file默认为256MB。
      2. chunk_limit_records:chunk可存储的最大事件数
      3. total_limit_size:buffer插件可用的最大空间。memory默认为512MB,file默认为64GB。超出此值,后续操作会失败,数据会丢失!
      4. chunk_full_threshold:chunk写满阈值,默认为0.95。当chunk实际占用存储超过此百分比后,事件会被flush。
      5. compress:数据压缩方式(text或gzip),默认text表示不压缩。若设为gzip,Fluentd会将事件压缩后才写入chunk,在flush到output之前会自动解压。
    2. flush参数
      用以配置flush方式,以优化性能(包括时延和吞吐量)
      1. flush_at_shutdown:程序退出时进行flush
      2. flush_mode:flush模式。lazy,每timekey一次;interval,根据flush_interval的设置进行间隔flush;immediate,事件写入chunk后就flush。default,chunk键为time时等同lazy,其他等同interval。
      3. flush_interval:默认每60s flush一次。
      4. flush_thread_count:执行flush的线程数,默认1.
      5. flush_thread_interval:flush线程等待间隔。
      6. overflow_action:buffer队列满时执行的操作。throw_exception,抛异常;block,阻塞input插件;drop_oldest_chunk,丢弃oldestchunk。
    3. 重试参数
      1. retry_timeout:flush失败后最大重试时长,默认72h。
      2. retry_forever:是否一直重试
      3. retry_max_times:最大重试次数
      4. retry_wait:重试等待时长

【码字不易,若对您有帮助,希望可以收藏转发】

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多