Fluentd/Bindplane 用于收集 HAProxy 日志的错误时间戳

时间:2021-06-29 08:06:55

标签: fluentd stackdriver

当我将 HAProxy 生成的日志发送到 Google Blindplane 时遇到问题。 日志时间格式为“%b %d %H:%M:%S”,GCP stackdriver 日志接受的格式为UTC,例如“2020-10-12T07:20:50.52Z”。 当我尝试转换日志时间时,它没有显示正确的日志时间,它只显示我运行 fluentd 服务的时间,而忽略了实时日志。

我尝试了几种使用 fluentd time_format、Ruby time.Parse() 和 Ruby time.strptime() 的方法,但都没有奏效。

这里是收集 HAProxy 日志的配置

<source>
  @type tail
  tag varnish.haproxy
  path e:/haproxy/log/haproxy.log*
  pos_file 'C:\Program Files (x86)\Stackdriver\LoggingAgent\Main\pos\varnish_haproxy.pos'
  <parse>
      @type multiline
      format_firstline /\w{3} \d{2} \d{2}:\d{2}:\d{2}/
      format1 /(?<message>.*)/
  </parse>
  read_from_head true
  refresh_interval 2
</source>

<filter varnish.haproxy>
  @type parser
  key_name message
  remove_key_name_field false
  reserve_data true
  reserve_time true
  <parse>
      @type multiline
      format_firstline /\w{3} \d{2} \d{2}:\d{2}:\d{2}/
      format1 /(?<timestamp>\w{3} \d{2} \d{2}:\d{2}:\d{2}) (?<servername>[^\s]) (?<ps>[^\[]+)(?<pid>\[[^\]]+\]): (?<client_ip>[\w\.]+):(?<client_port>\d+) \[(?<request_date>.+)\] (?<frontend_name>[\w\.-]+)~ (?<backend_name>[\w\.-]+)\/(?<server_name>[\w\.-]+) (?<TR>\d+)\/(?<Tw>\d+)\/(?<Tc>\d+)\/(?<Tr>\d+)\/(?<Ta>\d+) (?<status_code>\d+) (?<bytes_read>\d+) (?<captured_request_cookie>.+) (?<captured_response_cookie>.+) (?<termination_state>.+) (?<actconn>\d+)\/(?<feconn>\d+)\/(?<beconn>\d+)\/(?<srv_conn>\d+)\/(?<retries>\d+) (?<srv_queue>\d+)\/(?<backend_queue>\d+) \"(?<message>.*)\"/
  </parse>
</filter>

<filter varnish.haproxy>
  @type record_transformer
  enable_ruby
  <record>
    timestamp ${t = Time.parse(record['timestamp']).utc; {'seconds' => t.tv_sec, 'nanos' => t.tv_nsec}}
  </record>
</filter>

日志文件示例:

Jun 23 14:00:00 localhost haproxy[26781]: xx.xx.xx.xxx:xxxxx [23/Jun/2021:14:00:00.561] https-in~ nodes-http/xxxxxxxx 0/0/0/314/314 200 278 - - --NI 274/274/100/23/0 0/0 "GET /api/customer/xxxxxxxx HTTP/1.1"
Jun 23 14:00:00 localhost haproxy[26781]: xx.xx.xx.xxx:xxxxxx [23/Jun/2021:13:59:59.901] https-in~ nodes-http/xxxxxxxxx 0/0/1/994/995 200 1485 - - --NI 274/274/100/22/0 0/0 "GET /api/customer/view HTTP/1.1"

这是 GCP 日志记录时间戳的屏幕截图 GCP logging timestamp

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用 Time.strftime?

timestamp ${Time.parse(record['timestamp']).strftime("%Y-%m-%dT%k:%M:%S:00Z")}

我认为纳秒并不重要,因为 fluentd 似乎不提供它们。

相关问题