Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse external request id from request headers, and print it in access log #22906

Merged
merged 24 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3ef5609
log reuqest id that parsed from request header
sillyguodong Feb 14, 2023
e23a9f6
fix
sillyguodong Feb 14, 2023
bca651f
update example and config cheat sheet
sillyguodong Feb 14, 2023
68ae309
put request id into access log
sillyguodong Feb 14, 2023
248ca3c
delete useless character
sillyguodong Feb 14, 2023
f15ce89
fix lint
sillyguodong Feb 14, 2023
e040fa8
set the maximum length of request id to 256 bits
sillyguodong Feb 15, 2023
55c2779
fix markdown
sillyguodong Feb 15, 2023
c5a5442
lint markdown
sillyguodong Feb 15, 2023
ec0d6c0
set maxRequestIDBtyeLength to 40
sillyguodong Feb 15, 2023
0c47e3f
fix comment misspelling
sillyguodong Feb 15, 2023
dbabb46
fix comment and the judgement of "needRequestID"
sillyguodong Feb 17, 2023
6f9d8da
Merge branch 'main' into feature/issue_22890
sillyguodong Feb 17, 2023
84a1a04
fix comment
sillyguodong Feb 17, 2023
20c36b3
Merge branch 'main' into feature/issue_22890
sillyguodong Feb 20, 2023
4a5bca7
Merge branch 'main' into feature/issue_22890
sillyguodong Feb 20, 2023
fc843f0
fix example
sillyguodong Feb 21, 2023
37d578c
Merge branch 'main' into feature/issue_22890
sillyguodong Feb 21, 2023
16d2d01
Merge branch 'main' into feature/issue_22890
lunny Mar 9, 2023
031f83e
Merge branch 'main' into feature/issue_22890
lunny Mar 9, 2023
29283b1
Merge branch 'main' into feature/issue_22890
jolheiser Mar 9, 2023
b387a1e
Update custom/conf/app.example.ini
zeripath Mar 9, 2023
f231c64
fix eample
sillyguodong Mar 10, 2023
a812d1d
Merge branch 'main' into feature/issue_22890
jolheiser Mar 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,22 @@ ROUTER = console
;; The routing level will default to that of the system but individual router level can be set in
;; [log.<mode>.router] LEVEL
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Print request id which parsed from request headers in router log
;; * e.g:
;; * In reuqest Header: X-Request-ID: test-id-123
zeripath marked this conversation as resolved.
Show resolved Hide resolved
;; * Configuration in app.ini: REQUEST_ID_HEADERS = X-Request-ID
;; * Print in log: 2023/02/14 13:18:50 [63eb19ba] [test-id-123] ...
;;
;; If you configure more than one in the .ini file, it will match in the order of configuration,
;; and the first match will be finally printed in the log.
;; * e.g:
;; * In reuqest Header: X-Trace-ID: test-trace-id-123
;; * Configuration in app.ini: REQUEST_ID_HEADERS = X-Request-ID, X-Trace-ID, X-Req-ID
;; * Print in log: 2023/02/14 13:18:50 [63eb19ba] [test-trace-id-123] ...
;;
;; REQUEST_ID_HEADERS =

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
Expand Down
1 change: 1 addition & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ Default templates for project boards:
- `DISABLE_ROUTER_LOG`: **false**: Mute printing of the router log.
- `ROUTER`: **console**: The mode or name of the log the router should log to. (If you set this to `,` it will log to default Gitea logger.)
NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take effect. Configure each mode in per mode log subsections `\[log.modename.router\]`.
- `REQUEST_ID_HEADERS`: **\<empty\>**: You can configure multiple values that are splited by comma here. It will match in the order of configuration, and the first match will be finally printed in the log. You can refer to the `app.example.ini` file for examples
sillyguodong marked this conversation as resolved.
Show resolved Hide resolved

### Access Log (`log`)

Expand Down
4 changes: 2 additions & 2 deletions docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ menu:
- `START_SSH_SERVER`: 是否启用内部SSH服务器。
- `SSH_PORT`: SSH端口,默认为 `22`。
- `OFFLINE_MODE`: 针对静态和头像文件禁用 CDN。
- `DISABLE_ROUTER_LOG`: 关闭日志中的路由日志。
sillyguodong marked this conversation as resolved.
Show resolved Hide resolved
- `CERT_FILE`: 启用HTTPS的证书文件。
- `KEY_FILE`: 启用HTTPS的密钥文件。
- `STATIC_ROOT_PATH`: 存放模板和静态文件的根目录,默认是 Gitea 的根目录。
- `STATIC_CACHE_TIME`: **6h**: 静态资源文件,包括 `custom/`, `public/` 和所有上传的头像的浏览器缓存时间。
- `ENABLE_GZIP`: 启用实时生成的数据启用 GZIP 压缩,不包括静态资源。
- `LANDING_PAGE`: 未登录用户的默认页面,可选 `home` 或 `explore`。

- `LFS_START_SERVER`: 是否启用 git-lfs 支持. 可以为 `true` 或 `false`, 默认是 `false`。
- `LFS_JWT_SECRET`: LFS 认证密钥,改成自己的。
- `LFS_CONTENT_PATH`: **已废弃**, 存放 lfs 命令上传的文件的地方,默认是 `data/lfs`。**废弃** 请使用 `[lfs]` 的设置。
Expand Down Expand Up @@ -263,6 +261,8 @@ test01.xls: application/vnd.ms-excel; charset=binary
- `ROOT_PATH`: 日志文件根目录。
- `MODE`: 日志记录模式,默认是为 `console`。如果要写到多个通道,用逗号分隔
- `LEVEL`: 日志级别,默认为`Trace`。
- `DISABLE_ROUTER_LOG`: 关闭日志中的路由日志。
- `REQUEST_ID_HEADERS`: 从Request Header中匹配指定Key,并将匹配到的值输出到router log中。如果在该参数中配置多个Key, 请用逗号分割,程序将按照配置的顺序进行匹配,示例可以参考`app.example.ini`文件。

## Cron (`cron`)

Expand Down
1 change: 1 addition & 0 deletions modules/setting/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func newRouterLogService() {
Cfg.Section("log").Key("ROUTER").MustString("console")
// Allow [log] DISABLE_ROUTER_LOG to override [server] DISABLE_ROUTER_LOG
DisableRouterLog = Cfg.Section("log").Key("DISABLE_ROUTER_LOG").MustBool(DisableRouterLog)
RequestIDHeaders = Cfg.Section("log").Key("REQUEST_ID_HEADERS").Strings(",")

if !DisableRouterLog {
options := newDefaultLogOptions()
Expand Down
9 changes: 4 additions & 5 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,10 @@ var (
LogRootPath string
EnableSSHLog bool
EnableXORMLog bool

DisableRouterLog bool

EnableAccessLog bool
AccessLogTemplate string
DisableRouterLog bool
EnableAccessLog bool
AccessLogTemplate string
RequestIDHeaders []string

// Time settings
TimeFormat string
Expand Down
58 changes: 38 additions & 20 deletions modules/web/routing/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)

// NewLoggerHandler is a handler that will log routing to the router log taking account of
Expand All @@ -35,15 +36,34 @@ var (

func logPrinter(logger log.Logger) func(trigger Event, record *requestRecord) {
return func(trigger Event, record *requestRecord) {
if trigger == StartEvent {
if !logger.IsTrace() {
// for performance, if the "started" message shouldn't be logged, we just return as early as possible
// developers can set the router log level to TRACE to get the "started" request messages.
return
if trigger == StartEvent && !logger.IsTrace() {
// for performance, if the "started" message shouldn't be logged, we just return as early as possible
// developers can set the router log level to TRACE to get the "started" request messages.
return
}

var requestID string
if len(setting.RequestIDHeaders) > 0 {
for _, key := range setting.RequestIDHeaders {
headerVal := record.request.Header.Get(key)
if record.request.Header.Get(key) != "" {
requestID = headerVal
break
}
}
}
var format string
var v []interface{}
if requestID != "" {
format = "[%s] "
v = append(v, requestID)
}
if trigger == StartEvent {
// when a request starts, we have no information about the handler function information, we only have the request path
req := record.request
logger.Trace("router: %s %v %s for %s", startMessage, log.ColoredMethod(req.Method), req.RequestURI, req.RemoteAddr)
format += "router: %s %v %s for %s"
v = append(v, startMessage, log.ColoredMethod(req.Method), req.RequestURI, req.RemoteAddr)
logger.Trace(format, v...)
return
}

Expand All @@ -64,23 +84,21 @@ func logPrinter(logger log.Logger) func(trigger Event, record *requestRecord) {
level = log.INFO
message = pollingMessage
}
_ = logger.Log(0, level, "router: %s %v %s for %s, elapsed %v @ %s",
message,
log.ColoredMethod(req.Method), req.RequestURI, req.RemoteAddr,
log.ColoredTime(time.Since(record.startTime)),
handlerFuncInfo,
)
format += "router: %s %v %s for %s, elapsed %v @ %s"
v = append(v, message, log.ColoredMethod(req.Method), req.RequestURI,
req.RemoteAddr, log.ColoredTime(time.Since(record.startTime)), handlerFuncInfo)
_ = logger.Log(0, level, format, v...)
return
}

if panicErr != nil {
_ = logger.Log(0, log.WARN, "router: %s %v %s for %s, panic in %v @ %s, err=%v",
failedMessage,
format += "router: %s %v %s for %s, panic in %v @ %s, err=%v"
v = append(v, failedMessage,
log.ColoredMethod(req.Method), req.RequestURI, req.RemoteAddr,
log.ColoredTime(time.Since(record.startTime)),
handlerFuncInfo,
panicErr,
)
panicErr)
_ = logger.Log(0, log.WARN, format, v...)
return
}

Expand All @@ -95,11 +113,11 @@ func logPrinter(logger log.Logger) func(trigger Event, record *requestRecord) {
message = unknownHandlerMessage
}

_ = logger.Log(0, level, "router: %s %v %s for %s, %v %v in %v @ %s",
message,
format += "router: %s %v %s for %s, %v %v in %v @ %s"
v = append(v, message,
log.ColoredMethod(req.Method), req.RequestURI, req.RemoteAddr,
log.ColoredStatus(status), log.ColoredStatus(status, http.StatusText(status)), log.ColoredTime(time.Since(record.startTime)),
handlerFuncInfo,
)
handlerFuncInfo)
_ = logger.Log(0, level, format, v...)
}
}