/
githubmirror
/
nbs
Обзор
Документация
Войти
/
githubmirror
/
nbs
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
cloud/tasks/logging/fields.go
103 строки
3 KB
gy2411
issue-4 NBS-4829 [disk manager] add component and task id to dm logs (#647)
25 мар 2024, 17:57
Не верифицирован
25 мар 2024, 17:57
f91427f
Код
Авторство
О чём код?
package logging import ( "context" "time" "github.com/ydb-platform/nbs/cloud/tasks/headers" "github.com/ydb-platform/nbs/library/go/core/log" "github.com/ydb-platform/nbs/library/go/core/log/ctxlog" ) //////////////////////////////////////////////////////////////////////////////// type Field = log.Field //////////////////////////////////////////////////////////////////////////////// const ComponentYDB = "YDB" const ComponentS3 = "S3" const ComponentTaskRunner = "TASK_RUNNER" const ComponentTaskScheduler = "TASK_SCHEDULER" const ComponentTask = "TASK" //////////////////////////////////////////////////////////////////////////////// func String(key, value string) Field { return log.String(key, value) } func Strings(key string, value []string) Field { return log.Strings(key, value) } func Bool(key string, value bool) Field { return log.Bool(key, value) } func Int(key string, value int) Field { return log.Int(key, value) } func Int64(key string, value int64) Field { return log.Int64(key, value) } func Duration(key string, value time.Duration) Field { return log.Duration(key, value) } func ErrorValue(value error) Field { return log.Error(value) } func Any(key string, value interface{}) Field { return log.Any(key, value) } //////////////////////////////////////////////////////////////////////////////// func NewComponentField(value string) Field { return String("COMPONENT", value) } func NewTaskIDField(value string) Field { return String("TASK_ID", value) } //////////////////////////////////////////////////////////////////////////////// func WithComponent(ctx context.Context, value string) context.Context { return WithFields(ctx, NewComponentField(value)) } func WithTaskID(ctx context.Context, value string) context.Context { return WithFields(ctx, NewTaskIDField(value)) } func WithCommonFields(ctx context.Context) context.Context { fields := make([]log.Field, 0) idempotencyKey := headers.GetIdempotencyKey(ctx) if len(idempotencyKey) != 0 { fields = append(fields, log.String(idempotencyKeyKey, idempotencyKey)) } requestID := headers.GetRequestID(ctx) if len(requestID) != 0 { fields = append(fields, log.String(requestIDKey, requestID)) } operationID := headers.GetOperationID(ctx) if len(operationID) != 0 { fields = append(fields, log.String(operationIDKey, operationID)) } fields = append(fields, log.String(syslogIdentifierKey, "disk-manager")) return ctxlog.WithFields(ctx, fields...) } func WithFields(ctx context.Context, fields ...Field) context.Context { return ctxlog.WithFields(ctx, fields...) }