Skip to content

Commit

Permalink
Implement LOG_FORMAT=json (#173)
Browse files Browse the repository at this point in the history
Centralized log collection system works better with logs in json format.
E.g. DataDog strongly encourage setting up your logging library to produce your logs in JSON format to avoid the need for custom parsing rules.
So, the next small fix is all we need to get json logs.

Signed-off-by: Sergey Belyaev <sbelyaev@setronica.com>
  • Loading branch information
Sergey Belyaev authored Sep 10, 2020
1 parent 9b42196 commit c897c8e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [Example 3](#example-3)
- [Example 4](#example-4)
- [Loading Configuration](#loading-configuration)
- [Log Format](#log-format)
- [Request Fields](#request-fields)
- [Statistics](#statistics)
- [HTTP Port](#http-port)
Expand Down Expand Up @@ -347,6 +348,40 @@ The former is the default behavior. To use the latter method, set the `RUNTIME_W

For more information on how runtime works you can read its [README](https://github.com/lyft/goruntime).

## Log Format

A centralized log collection system works better with logs in json format. JSON format avoids the need for custom parsing rules.
The Ratelimit service produces logs in a text format by default. For Example:

```
time="2020-09-10T17:22:35Z" level=debug msg="loading domain: messaging"
time="2020-09-10T17:22:35Z" level=debug msg="loading descriptor: key=messaging.message_type_marketing"
time="2020-09-10T17:22:35Z" level=debug msg="loading descriptor: key=messaging.message_type_marketing.to_number ratelimit={requests_per_unit=5, unit=DAY}"
time="2020-09-10T17:22:35Z" level=debug msg="loading descriptor: key=messaging.to_number ratelimit={requests_per_unit=100, unit=DAY}"
time="2020-09-10T17:21:55Z" level=warning msg="Listening for debug on ':6070'"
time="2020-09-10T17:21:55Z" level=warning msg="Listening for HTTP on ':8080'"
time="2020-09-10T17:21:55Z" level=debug msg="waiting for runtime update"
time="2020-09-10T17:21:55Z" level=warning msg="Listening for gRPC on ':8081'"
```

JSON Log format can be configured using the following environment variables:

```
LOG_FORMAT=json
```

Output example:
```
{"@message":"loading domain: messaging","@timestamp":"2020-09-10T17:22:44.926010192Z","level":"debug"}
{"@message":"loading descriptor: key=messaging.message_type_marketing","@timestamp":"2020-09-10T17:22:44.926019315Z","level":"debug"}
{"@message":"loading descriptor: key=messaging.message_type_marketing.to_number ratelimit={requests_per_unit=5, unit=DAY}","@timestamp":"2020-09-10T17:22:44.926037174Z","level":"debug"}
{"@message":"loading descriptor: key=messaging.to_number ratelimit={requests_per_unit=100, unit=DAY}","@timestamp":"2020-09-10T17:22:44.926048993Z","level":"debug"}
{"@message":"Listening for debug on ':6070'","@timestamp":"2020-09-10T17:22:44.926113905Z","level":"warning"}
{"@message":"Listening for gRPC on ':8081'","@timestamp":"2020-09-10T17:22:44.926182006Z","level":"warning"}
{"@message":"Listening for HTTP on ':8080'","@timestamp":"2020-09-10T17:22:44.926227031Z","level":"warning"}
{"@message":"waiting for runtime update","@timestamp":"2020-09-10T17:22:44.926267808Z","level":"debug"}
```

# Request Fields

For information on the fields of a Ratelimit gRPC request please read the information
Expand Down
11 changes: 11 additions & 0 deletions src/service_cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"math/rand"
"net/http"
"strings"
"time"

stats "github.com/lyft/gostats"
Expand Down Expand Up @@ -43,6 +44,16 @@ func (runner *Runner) Run() {
} else {
logger.SetLevel(logLevel)
}
if strings.ToLower(s.LogFormat) == "json" {
logger.SetFormatter(&logger.JSONFormatter{
TimestampFormat: time.RFC3339Nano,
FieldMap: logger.FieldMap{
logger.FieldKeyTime: "@timestamp",
logger.FieldKeyMsg: "@message",
},
})
}

var localCache *freecache.Cache
if s.LocalCacheSizeInBytes != 0 {
localCache = freecache.NewCache(s.LocalCacheSizeInBytes)
Expand Down
1 change: 1 addition & 0 deletions src/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Settings struct {
RuntimeIgnoreDotFiles bool `envconfig:"RUNTIME_IGNOREDOTFILES" default:"false"`
RuntimeWatchRoot bool `envconfig:"RUNTIME_WATCH_ROOT" default:"true"`
LogLevel string `envconfig:"LOG_LEVEL" default:"WARN"`
LogFormat string `envconfig:"LOG_FORMAT" default:"text"`
RedisSocketType string `envconfig:"REDIS_SOCKET_TYPE" default:"unix"`
RedisUrl string `envconfig:"REDIS_URL" default:"/var/run/nutcracker/ratelimit.sock"`
RedisPoolSize int `envconfig:"REDIS_POOL_SIZE" default:"10"`
Expand Down

0 comments on commit c897c8e

Please sign in to comment.