简体   繁体   English

json 日志记录中的 Fluentd 时间字段处理

[英]Fluentd time field handling in json log records

I have standard EFK stack setup in my GKE cluster.我的 GKE 集群中有标准的 EFK 堆栈设置。 I did switch my test gunicorn service to log in json format.我确实将我的测试 gunicorn 服务切换为以 json 格式登录。 To be able to search service logs by json field values in kibana.能够通过 kibana 中的 json 字段值搜索服务日志。 I see all logs properly output in pods console using command:我使用命令在 pods 控制台中看到所有日志正确输出:

kubectl logs pod/dev-django-rest-service-678964c7f5-kh4k4 -n django-rest-service

{"bla": "bla-value", "REFERRER": null, "HOST": "api.myhost.com", "req_id": "4e845383a6ddbc2781919420b1ecb4fe", "message": "Test message", "time": "2019-12-06T11:25:21.635692"}

But kibana does not show these messages.但是 kibana 不显示这些消息。 I do not see any parsing error reporting by fluentd pods.我没有看到 fluentd pod 报告任何解析错误。 There was all messages showing up in kibana, before I switched service message format to json.在我将服务消息格式切换为 json 之前,kibana 中显示了所有消息。 I also did add some handler to my service just to output some text (not JSON) to pods console.我也确实向我的服务添加了一些处理程序,只是为了将一些文本(不是 JSON)输出到 pods 控制台。 And it is showing correctly in my kibana.它在我的 kibana 中正确显示。

SAY: Hello Word!
{"bla": "bla-value", "REFERRER": null, "HOST": "api.myhost.com", "req_id": "c7123f4419656c9effd40410aac43e51", "message": "Test message", "time": "2019-12-06T19:09:09.378297"}

SAY: Hello Word!说:你好词! - is properly shown in kibana, but the json message is not. - 在 kibana 中正确显示,但 json 消息不是。 Please help to find why json messages ignored.请帮助找出 json 消息被忽略的原因。

------edit------ - - - 编辑 - - -

I did create filter我确实创建了过滤器

    <filter kubernetes.var.log.containers.dev-django-rest-service**.log>
      @type stdout
    </filter>

and found my json log records in fluentd pods console, but with epoch time.并在 fluentd pods 控制台中找到了我的 json 日志记录,但有纪元时间。

1970-01-01 00:33:39.993485000 +0000 kubernetes.var.log.containers.dev-django-rest-service-5cf648b874-qj264_django-rest-service_kc2-micros-36e63553f54ecd1ee4cbd6503e19c56a4ff62b98152b7f0e20983384f4b0cf52.log: {"stream":"stdout","docker":{"container_id":"36e63553f54ecd1ee4cbd6503e19c56a4ff62b98152b7f0e20983384f4b0cf52"},"kubernetes":{"container_name":"kc2-micros","namespace_name":"django-rest-service","pod_name":"dev-django-rest-service-5cf648b874-qj264","container_image":"registry.gitlab.com/xxxx/project-templates/django_rest_service/dev:56048ac6eb463ed132a103a052274fb050748f1b","container_image_id":"docker-pullable://registry.gitlab.com/xxxx/project-templates/django_rest_service/dev@sha256:61776222330dacb9ed6b6175e87b6e6626945aa1e7eec87344638be530a2230b","pod_id":"9bdf8bc6-1866-11ea-994f-42010a800002","host":"gke-kc2-small-default-pool-b37c22e6-x5hv","labels":{"app":"dev-django-rest-service","pod-template-hash":"5cf648b874","release":"dev-django-rest-service","tier":"web","track":"stable"},"master_url":"https://10.0.0.1:443/api","namespace_id":"6a6b1eaf-ea8b-11e9-9ec8-42010a80020e"},"bla":"bla-value","REFERRER":null,"HOST":"api.my-host.com","req_id":"bc642dd8c8ae0ed59fa02632dc9567dd","message":"Test message"}

Actually, when the json log record contains time field and it has incorrect format, fluentd does replace the log record time with the epoch start time 1970-01-01.实际上,当json日志记录包含时间字段并且格式不正确时,fluentd确实将日志记录时间替换为纪元开始时间1970-01-01。 So when you press "Last 15 minutes" log filter in kibana you will never see these records.因此,当您在 kibana 中按“最后 15 分钟”日志过滤器时,您将永远不会看到这些记录。 I have not found any mentions about this in fluentd docs.我在 fluentd 文档中没有发现任何关于此的提及。 It was discovered experimental way using stdout filter.它是使用标准输出过滤器发现的实验方法。 I use this fluentd helm package https://github.com/kiwigrid/helm-charts/blob/master/charts/fluentd-elasticsearch/ .我使用这个 fluentd helm 包https://github.com/kiwigrid/helm-charts/blob/master/charts/fluentd-elasticsearch/ When I changed time format here https://github.com/kiwigrid/helm-charts/blob/master/charts/fluentd-elasticsearch/templates/configmaps.yaml#L175 so section look:当我在这里更改时间格式时https://github.com/kiwigrid/helm-charts/blob/master/charts/fluentd-elasticsearch/templates/configmaps.yaml#L175所以部分看:

      <parse>
        @type multi_format
        <pattern>
          format json
          time_key time
          time_format %Y-%m-%dT%H:%M:%S.%N %Z
        </pattern>
        <pattern>
          format none
        </pattern>
      </parse>

And my corresponding gunicorn time format was:而我对应的 gunicorn 时间格式是:

record['time']=timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%f000 %Z')

It starts to show correct time from my gunicorn log records.它开始从我的 gunicorn 日志记录中显示正确的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM