ingress nginx日志时间精确到毫秒

1 概述:

1.1 环境

版本信息如下:
a、操作系统:centos 7.6
b、kubernetes版本:v1.15.0
c、ingress nginx版本:0.47.0


1.2 nginx的access log的时间精度

nginx的访问日志的时间字段的精度默认为秒,但提供了内置变量$msec,它是一个精度为毫秒的时间戳(http://nginx.org/en/docs/http/ngx_http_core_module.html#var_msec)。有了这个基础,可通过一些方式来生成我们想要的精度为毫秒的时间,例如2022-05-18T13:32:37.027+00:00。


2 修改ingress nginx的configmap

1)使用http-snippet字段来注入nginx配置指令,nginx配置指令会生成自定义变量 $time_iso8601_ms 。
注入的nginx配置指令,来源于文章 https://rotadev.com/can-nginx-log-time-in-iso-8601-format-but-include-milliseconds-server-fault/。
2)使用log-format-upstream字段来设置访问日志字段的格式,时间字段的值使用 $time_iso8601_ms 。

apiVersion: v1
data:

  http-snippet: |
    map "$time_iso8601 # $msec" $time_iso8601_ms { "~(^[^+]+)(\+[0-9:]+) # \d+\.(\d+)$" $1.$3$2; }
    
  log-format-upstream: '{"time": "$time_iso8601_ms", "remote_addr": "$proxy_protocol_addr",
    "x_forward_for": "$proxy_add_x_forwarded_for", "request_id": "$req_id", "remote_user":
    "$remote_user", "bytes_sent": $bytes_sent, "request_time": $request_time, "status":
    $status, "vhost": "$host", "request_proto": "$server_protocol", "path": "$uri",
    "request_query": "$args", "request_length": $request_length, "duration": $request_time,"method":
    "$request_method", "http_referrer": "$http_referer", "http_user_agent": "$http_user_agent"
    }'
    
kind: ConfigMap
metadata:
  annotations:
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  name: nginx-configuration
  namespace: ingress-nginx


3 测试

nginx的日志如下图:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值