[#44] add tracing support refactoring
Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
This commit is contained in:
parent
8a22991326
commit
cdaab4feab
9 changed files with 78 additions and 137 deletions
62
app.go
62
app.go
|
@ -17,6 +17,7 @@ import (
|
|||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/metrics"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/resolver"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/response"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/tokens"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/tree"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/uploader"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/utils"
|
||||
|
@ -37,6 +38,7 @@ import (
|
|||
|
||||
type (
|
||||
app struct {
|
||||
ctx context.Context
|
||||
log *zap.Logger
|
||||
logLevel zap.AtomicLevel
|
||||
pool *pool.Pool
|
||||
|
@ -60,7 +62,7 @@ type (
|
|||
// App is an interface for the main gateway function.
|
||||
App interface {
|
||||
Wait()
|
||||
Serve(context.Context)
|
||||
Serve()
|
||||
}
|
||||
|
||||
// Option is an application option.
|
||||
|
@ -101,6 +103,7 @@ func newApp(ctx context.Context, opt ...Option) App {
|
|||
)
|
||||
|
||||
a := &app{
|
||||
ctx: ctx,
|
||||
log: zap.L(),
|
||||
cfg: viper.GetViper(),
|
||||
webServer: new(fasthttp.Server),
|
||||
|
@ -353,16 +356,16 @@ func (a *app) setHealthStatus() {
|
|||
a.metrics.SetHealth(metrics.HealthStatusReady)
|
||||
}
|
||||
|
||||
func (a *app) Serve(ctx context.Context) {
|
||||
treeClient := a.initTree(ctx)
|
||||
uploadRoutes := uploader.New(ctx, a.AppParams(), a.settings.Uploader)
|
||||
downloadRoutes := downloader.New(ctx, a.AppParams(), a.settings.Downloader, treeClient)
|
||||
func (a *app) Serve() {
|
||||
treeClient := a.initTree(a.ctx)
|
||||
uploadRoutes := uploader.New(a.AppParams(), a.settings.Uploader)
|
||||
downloadRoutes := downloader.New(a.AppParams(), a.settings.Downloader, treeClient)
|
||||
|
||||
// Configure router.
|
||||
a.configureRouter(uploadRoutes, downloadRoutes)
|
||||
|
||||
a.startServices()
|
||||
a.initServers(ctx)
|
||||
a.initServers(a.ctx)
|
||||
|
||||
for i := range a.servers {
|
||||
go func(i int) {
|
||||
|
@ -379,10 +382,10 @@ func (a *app) Serve(ctx context.Context) {
|
|||
LOOP:
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-a.ctx.Done():
|
||||
break LOOP
|
||||
case <-sigs:
|
||||
a.configReload(ctx)
|
||||
a.configReload(a.ctx)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -477,28 +480,55 @@ func (a *app) configureRouter(uploadRoutes *uploader.Uploader, downloadRoutes *d
|
|||
r.MethodNotAllowed = func(r *fasthttp.RequestCtx) {
|
||||
response.Error(r, "Method Not Allowed", fasthttp.StatusMethodNotAllowed)
|
||||
}
|
||||
r.POST("/upload/{cid}", a.logger(uploadRoutes.Upload))
|
||||
r.POST("/upload/{cid}", a.logger(a.tokenizer(a.tracer(uploadRoutes.Upload))))
|
||||
a.log.Info("added path /upload/{cid}")
|
||||
r.GET("/get/{cid}/{oid:*}", a.logger(downloadRoutes.DownloadByAddressOrBucketName))
|
||||
r.HEAD("/get/{cid}/{oid:*}", a.logger(downloadRoutes.HeadByAddressOrBucketName))
|
||||
r.GET("/get/{cid}/{oid:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.DownloadByAddressOrBucketName))))
|
||||
r.HEAD("/get/{cid}/{oid:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.HeadByAddressOrBucketName))))
|
||||
a.log.Info("added path /get/{cid}/{oid}")
|
||||
r.GET("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.logger(downloadRoutes.DownloadByAttribute))
|
||||
r.HEAD("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.logger(downloadRoutes.HeadByAttribute))
|
||||
r.GET("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.DownloadByAttribute))))
|
||||
r.HEAD("/get_by_attribute/{cid}/{attr_key}/{attr_val:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.HeadByAttribute))))
|
||||
a.log.Info("added path /get_by_attribute/{cid}/{attr_key}/{attr_val:*}")
|
||||
r.GET("/zip/{cid}/{prefix:*}", a.logger(downloadRoutes.DownloadZipped))
|
||||
r.GET("/zip/{cid}/{prefix:*}", a.logger(a.tokenizer(a.tracer(downloadRoutes.DownloadZipped))))
|
||||
a.log.Info("added path /zip/{cid}/{prefix}")
|
||||
|
||||
a.webServer.Handler = r.Handler
|
||||
}
|
||||
|
||||
func (a *app) logger(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||
func (a *app) logger(req fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
a.log.Info("request", zap.String("remote", ctx.RemoteAddr().String()),
|
||||
zap.ByteString("method", ctx.Method()),
|
||||
zap.ByteString("path", ctx.Path()),
|
||||
zap.ByteString("query", ctx.QueryArgs().QueryString()),
|
||||
zap.Uint64("id", ctx.ID()))
|
||||
h(ctx)
|
||||
req(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *app) tokenizer(req fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
appCtx, err := tokens.StoreBearerTokenAppCtx(ctx, a.ctx)
|
||||
if err != nil {
|
||||
a.log.Error("could not fetch and store bearer token", zap.Error(err))
|
||||
response.Error(ctx, "could not fetch and store bearer token: "+err.Error(), fasthttp.StatusBadRequest)
|
||||
}
|
||||
utils.SetContextToRequest(appCtx, ctx)
|
||||
req(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *app) tracer(req fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
appCtx := utils.GetContextFromRequest(ctx)
|
||||
|
||||
appCtx, span := utils.StartHTTPServerSpan(appCtx, ctx, "REQUEST")
|
||||
defer func() {
|
||||
utils.SetHTTPTraceInfo(appCtx, span, ctx)
|
||||
span.End()
|
||||
}()
|
||||
|
||||
utils.SetContextToRequest(appCtx, ctx)
|
||||
req(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue