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

enhance(logging): add flag to allow logging in ecs format, map custom fields to ecs fields #971

Merged
merged 13 commits into from
Sep 27, 2023

Conversation

KellyMerrick
Copy link
Contributor

@KellyMerrick KellyMerrick commented Sep 22, 2023

Add flag to allow logging in Elasticsearch Common Schema (ECS) format. Default will remain logrus.JSONFormatter.

Custom Vela fields will be nested under labels.vela, which allows separation from other non-Vela custom labels that may exist in one's logging stack.

&cli.StringFlag{
EnvVars: []string{"VELA_LOG_FORMATTER", "LOG_FORMATTER"},
Name: "log-formatter",
Usage: "set log formatter - options: (json|ecs)",
Value: "json",
},

// set log formatter
switch c.String("log-formatter") {
case "json":
// set logrus to log in JSON format
logrus.SetFormatter(&logrus.JSONFormatter{})
case "ecs":
// set logrus to log in Elasticsearch Common Schema (ecs) format
logrus.SetFormatter(&middleware.Formatter{
DataKey: "labels.vela",
})
}

As noted below, a custom formatter, borrowing from ecslogrus, was created to handle Vela custom fields.

// This file, in part, reproduces portions of
// https://github.com/elastic/ecs-logging-go-logrus/blob/v1.0.0/formatter.go
// to handle our custom fields in Format().

// Format formats e as ECS-compliant JSON,
// mapping our custom fields to ECS fields.
func (f *Formatter) Format(e *logrus.Entry) ([]byte, error) {
datahint := len(e.Data)
if f.DataKey != "" {
datahint = 2
}
data := make(logrus.Fields, datahint)
if len(e.Data) > 0 {
extraData := data
if f.DataKey != "" {
extraData = make(logrus.Fields, len(e.Data))
}
for k, v := range e.Data {
switch k {
case "ip":
data["client.ip"] = v
case "latency":
data["event.duration"] = v
case "method":
data["http.request.method"] = v
case "path":
data["url.path"] = v
case "status":
data["http.response.status_code"] = v
case "user-agent":
data["user_agent.name"] = v
case "version":
data["user_agent.version"] = v
default:
extraData[k] = v
}
}
if f.DataKey != "" && len(extraData) > 0 {
data[f.DataKey] = extraData
}
}
data["ecs.version"] = ecsVersion
ecopy := *e
ecopy.Data = data
e = &ecopy
jf := logrus.JSONFormatter{
TimestampFormat: "2006-01-02T15:04:05.000Z0700",
FieldMap: ecsFieldMap,
}
return jf.Format(e)
}

default log:

{"build":"a build","ip":"123.4.5.6","latency":2303458,"level":"info","method":"GET","msg":"200 2.303458ms 123.4.5.6 GET /some/path","path":"/some/path","status":200,"time":"2023-09-26T10:03:27-05:00","user":"a user","user-agent":"user-agent field","version":"version field"}

ecs formatted log:

{"@timestamp":"2023-09-26T10:04:07.218-0500","client.ip":"123.4.5.6","ecs.version":"1.6.0","event.duration":2928917,"http.request.method":"GET","http.response.status_code":200,"labels.vela":{"build":"a build","user":"a user"},"log.level":"info","message":"200 2.928917ms 123.4.5.6 GET /some/path","url.path":"/some/path","user_agent.name":"user-agent field","user_agent.version":"version field"}

@KellyMerrick KellyMerrick self-assigned this Sep 22, 2023
@codecov
Copy link

codecov bot commented Sep 26, 2023

Codecov Report

Merging #971 (9eacb1a) into main (dff9633) will increase coverage by 0.10%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #971      +/-   ##
==========================================
+ Coverage   70.98%   71.09%   +0.10%     
==========================================
  Files         312      312              
  Lines       12934    12982      +48     
==========================================
+ Hits         9181     9229      +48     
  Misses       3288     3288              
  Partials      465      465              
Files Coverage Δ
router/middleware/logger.go 97.50% <100.00%> (+1.66%) ⬆️

@KellyMerrick KellyMerrick marked this pull request as ready for review September 26, 2023 18:51
@KellyMerrick KellyMerrick requested a review from a team as a code owner September 26, 2023 18:51
docker-compose.yml Outdated Show resolved Hide resolved
router/middleware/logger.go Outdated Show resolved Hide resolved
router/middleware/logger.go Outdated Show resolved Hide resolved
router/middleware/logger.go Outdated Show resolved Hide resolved
@KellyMerrick KellyMerrick merged commit f3acd78 into main Sep 27, 2023
12 checks passed
@KellyMerrick KellyMerrick deleted the enh/ecs_custom_formatter branch September 27, 2023 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants