image:
  # there are some bugs in official image, so we need a custom build
  # see: https://github.com/lobshunter/fluent-bit/blob/py-fix/README-zh.md
  repository: hub.pingcap.net/qa/fluent-bit
  tag: "v1.9.3"

tolerations:
  - key: "dedicated"
    operator: "Equal"
    value: "test-infra"
    effect: "NoSchedule"
  - key: "node-role.kubernetes.io/master"
    operator: "Exists"
    effect: "NoSchedule"

nodeSelector: {}

config:
  service: |
    [SERVICE]
        Daemon Off
        Flush 1
        Log_Level info
        Parsers_File /fluent-bit/etc/custom_parsers.conf
        HTTP_Server On
        HTTP_Listen 0.0.0.0
        HTTP_Port 2020
        Health_Check On
  inputs: |
    # log file
    [INPUT]
        Name tail
        # NOTE: currently log of TiFlash is under "PVC/logs/*.log", others are in "PVC/log/*"
        # note that if TiFlash use fast-disk, it's log can't be collected now due to implementation
        Path /localpath/**/*.log,/localpath/**/logs/*.log
        Parser tidb
        Tag kube.*
        Mem_Buf_Limit 32MB
        Skip_Long_Lines On
        # keep watch file after rotation for 604800 seconds(7 days), then ignores log files whos modify time is older than 604000 seconds
        # ALL is to prevent rotated log file from being read again (becuase fluentbit requires "Path patterns cannot match the rotated files", so we need this work around, see: https://docs.fluentbit.io/manual/pipeline/inputs/tail#file-rotation)
        # (note that even the file is being watched, Ignore_Older still takes effect, result in file being removed from watching list, a long Ignore_Older interval prevents removing inactive log file from watching list)
        Rotate_Wait 604800
        Ignore_Older 604000

    # stdout log
    [INPUT]
        Name tail
        Path /var/log/containers/*.log
        Parser docker
        Tag kube.*
        Mem_Buf_Limit 4MB
        Skip_Long_Lines On

    # hosts' dmesg
    [INPUT]
        Name tail
        Path /var/log/messages*
        Parser syslog
        Tag syslog
        Mem_Buf_Limit 4MB
        Skip_Long_Lines On

  filters: |
    # log file
    [FILTER]
        Name kubernetes
        Match kube.localpath.*
        Merge_Log On
        Regex_Parser kube_custom_localpath
        Kube_Tag_Prefix  kube.localpath.
        Keep_Log Off
        Labels Off
        Kube_CA_File        /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        Kube_Token_File     /var/run/secrets/kubernetes.io/serviceaccount/token
        Kube_Meta_Cache_TTL 300

    # stdout log
    [FILTER]
        Name kubernetes
        Match kube.var.*
        Merge_Log On
        Kube_Tag_Prefix  kube.var.log.containers.
        Keep_Log Off
        Labels Off
        Kube_CA_File        /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        Kube_Token_File     /var/run/secrets/kubernetes.io/serviceaccount/token
        Kube_Meta_Cache_TTL 300

    # filters shared by log file & stdout log
    [FILTER]
        Name    grep
        Match   kube.*
        # NOTE: only scrape log of pod that managed by test-infra
        Regex $kubernetes['annotations']['app.kubernetes.io/managed-by'] test-infra
    [FILTER]
        Name lua
        Match kube.*
        call append_tag
        code function append_tag(tag, timestamp, record) new_record = record new_record["namespace"] = record["kubernetes"]["namespace_name"] new_record["pod"] = record["kubernetes"]["pod_name"] new_record["container"] = record["kubernetes"]["container_name"] return 1, timestamp, new_record end
        ##### code above is folded version of:
        # function append_tag(tag, timestamp, record)
        #   new_record = record
        #   new_record["namespace"] = record["kubernetes"]["namespace_name"]
        #   new_record["pod"] = record["kubernetes"]["pod_name"]
        #   new_record["container"] = record["kubernetes"]["container_name"]
        #   return 1, timestamp, new_record
        # end
    [FILTER]
        Name record_modifier
        Match   kube.*
        Remove_key kubernetes
  outputs: |
    # log file
    [OUTPUT]
        Name loki
        Match kube.localpath.*
        Host test-infra-loki
        Port 3100
        # NOTE: add more labels
        labels job=fluentbit, stream=logfile, node=${NODE_NAME}, testbed=$namespace, pod=$pod
        Auto_Kubernetes_Labels false
        workers 1

    # stdout log
    [OUTPUT]
        Name loki
        Match kube.var.*
        Host test-infra-loki
        Port 3100
        # NOTE: add more labels
        labels job=fluentbit, stream=stdout, node=${NODE_NAME}, testbed=$namespace, pod=$pod
        Auto_Kubernetes_Labels false
        workers 1

    # syslog
    [OUTPUT]
        Name loki
        Match syslog
        Host test-infra-loki
        Port 3100
        labels job=fluentbit, stream=syslog, node=${NODE_NAME}
        workers 1

  customParsers: |
    [PARSER]
        Name    kube_custom_localpath
        Format  regex
        Regex (pvc-[a-z0-9\-]+)_(?<namespace_name>[a-z0-9\-]+)_(?<container_name>dm-master|dm-worker|[a-z0-9]+|)-(log-)?(?<pod_name>[-a-z0-9]+).*.log$

    [PARSER]
        # NOTE: used in INPUT.tail, tail must specify parser
        # otherwise it can't extract timestamp from log, cause all logs belongs to a log file use the same timestamp.
        # then querying Loki from Grafana will be difficult because logs are huddled together.
        Name tidb
        Format regex
        Regex \[(?<time>.+?)\]\s\[(?<level>.+?)\]\s(?<log>.*)
        Time_Key time
        Time_Format %Y/%m/%d %H:%M:%S.%L %z

    [PARSER]
        Name         docker
        Format       json
        Time_Key     time
        Time_Format  %Y-%m-%dT%H:%M:%S.%L
        Time_Keep    On

    [PARSER]
        Name syslog
        Format regex
        Regex (?<time>\w{3}\s+\d+\s+\d{2}:\d{2}:\d{2})(?<log>.+)
        Time_Key time
        # be sure all hosts are in the same timezone and use same syslog format
        Time_Offset +0800
        Time_Format %b %e %H:%M:%S

env:
  - name: NODE_NAME # so log search can choose fluent-bit instance
    valueFrom:
      fieldRef:
        apiVersion: v1
        fieldPath: spec.nodeName

daemonSetVolumes:
  - hostPath:
      path: /test-infra/local-path-volumes
    name: localpath

  # for stdout log & /var/log/messages
  - hostPath:
      path: /var/log
    name: varlog
  # actual docker.data-root
  - hostPath:
      path: /data/sdb/docker
    name: datasdbdocker
  - hostPath:
      path: /data/nvme1n1/docker
    name: datanvme1n1docker

daemonSetVolumeMounts:
  - mountPath: /localpath
    name: localpath
    readOnly: true

  # for stdout log & /var/log/messages
  - mountPath: /var/log
    name: varlog
    readOnly: true
  # actual docker.data-root
  - mountPath: /data/sdb/docker
    name: datasdbdocker
    readOnly: true
  - mountPath: /data/nvme1n1/docker
    name: datanvme1n1docker
    readOnly: true
