新年1発目
アプリケーションのログを標準エラーに出力する
zap がパフォーマンスが良いという意見だけで zap にした。
結果、こんなのが標準エラーに出力されるようにした。
{ "level":"error", "ts":1577969526.688373, "caller":"lib/logger.go:29", "msg":"checkCSRFToken returns false", "func":"PreSigned", "error":"checkCSRFToken returns false", "stacktrace":"github.com/rluisr/easyuploader_api/lib.ErrorWithResponseBadRequest\n\t/go/src/easyuploader_api/lib/logger.go:29\ngithub.com/rluisr/easyuploader_api/resource.PreSigned\n\t/go/src/easyuploader_api/resource/pre-signed.go:19\nmain.main.func7\n\t/go/src/easyuploader_api/main.go:63\ngithub.com/gin-gonic/gin.(*Context).Next\n\t/go/pkg/mod/github.com/gin-gonic/gin@v1.4.0/context.go:124\ngithub.com/gin-gonic/gin.RecoveryWithWriter.func1\n\t/go/pkg/mod/github.com/gin-gonic/gin@v1.4.0/recovery.go:83\ngithub.com/gin-gonic/gin.(*Context).Next\n\t/go/pkg/mod/github.com/gin-gonic/gin@v1.4.0/context.go:124\ngo.elastic.co/apm/module/apmgin.(*middleware).handle\n\t/go/pkg/mod/go.elastic.co/apm/module/apmgin@v1.5.0/middleware.go:132\ngithub.com/gin-gonic/gin.(*Context).Next\n\t/go/pkg/mod/github.com/gin-gonic/gin@v1.4.0/context.go:124\ngithub.com/gin-gonic/gin.(*Engine).handleHTTPRequest\n\t/go/pkg/mod/github.com/gin-gonic/gin@v1.4.0/gin.go:389\ngithub.com/gin-gonic/gin.(*Engine).ServeHTTP\n\t/go/pkg/mod/github.com/gin-gonic/gin@v1.4.0/gin.go:351\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2802\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1890" }
fluend を用意する
イメージと設定ファイル
github.com
ジョブファイル
job "fluentd" {
datacenters = ["luis"]
type = "service"
update {
stagger = "10s"
max_parallel = 1
}
group "fluentd" {
count = 1
restart {
attempts = 10
interval = "5m"
delay = "25s"
mode = "delay"
}
ephemeral_disk {
size = 3000
}
task "fluentd" {
driver = "docker"
config {
image = "registry.gitlab.com/rluisr/docker-registry_fluentd:20200102-6"
auth {
username = "r"
password = ""
}
port_map {
fluentd = 24224
}
}
resources {
cpu = 100
memory = 256
network {
mbits = 10
port "fluentd" {}
}
}
service {
name = "fluentd"
port = "fluentd"
tags = [
"traefik.enable=true",
"traefik.tag=service-v2.1.1",
"traefik.tcp.routers.fluentd.entrypoints=fluentd",
"traefik.tcp.routers.fluentd.rule=HostSNI(`*`)"
]
check {
type = "tcp"
port = "fluentd"
interval = "1s"
timeout = "1s"
}
}
}
}
}
見て分かるとおり、traefik で TCP ロードバランシングをする。
これを nginx で1つにまとめている。
server {
listen 24224;
proxy_pass traefik-fluentd;
proxy_timeout 3s;
proxy_connect_timeout 1s;
}
アプリケーションのロギングを変える
logging {
type = "fluentd"
config {
fluentd-address = "fluentd.luis.local:24224"
tag = "eu-stg-api"
}
}
確認

log に全部まとめられているのをどうにかしたい。