MSP-8299 vendor update

This commit is contained in:
ziminis committed 2026-11-27 20:10:32 +07:00
1 parent c8e4a014a0
commit 59d487646c
607 files changed
+48706 -13176

No files matched your search

+3 -2
View File
@@ -2,6 +2,7 @@ package handler
import (
"context"
"slices"
"github.com/txix-open/bgjob"
)
@@ -38,8 +39,8 @@ type Sync struct {
// will be the outermost wrapper.
func NewSync(adapter SyncHandlerAdapter, middlewares ...Middleware) Sync {
s := Sync{}
for i := len(middlewares) - 1; i >= 0; i-- {
adapter = middlewares[i](adapter)
for _, middleware := range slices.Backward(middlewares) {
adapter = middleware(adapter)
}
s.handler = adapter
return s
+17
View File
@@ -34,6 +34,9 @@ const (
defaultEnableLogSampling = false
defaultMaxLogSamplingPerSecond = 1000
defaulLogSamplingPassEvery = 100
defaultLogSplitEnabled = false
defaultLogSplitMaxLineKb = log.DefaultMaxLineKb
)
// BaseBootstrap provides the core initialization context for all application types.
@@ -265,11 +268,25 @@ func appConfig(isDev bool) (*app.Config, error) {
}
}
var split *log.SplitConfig
isSplitEnabled := cfg.Optional().Bool("LOGS.SPLIT.ENABLE", defaultLogSplitEnabled)
if !isDev && isSplitEnabled {
maxLineKb := cfg.Optional().Int("LOGS.SPLIT.MAXLINEKB", defaultLogSplitMaxLineKb)
if maxLineKb <= 0 {
maxLineKb = defaultLogSplitMaxLineKb
}
split = &log.SplitConfig{
Enabled: true,
MaxLineKb: maxLineKb,
}
}
return log.Config{
IsInDevMode: isDev,
OutputPaths: outputPaths,
Sampling: sampling,
InitialLevel: initialLevel,
Split: split,
Hooks: []func(entry zapcore.Entry) error{
logCounter.SampledLogCounter(),
},
+1
View File
@@ -308,6 +308,7 @@ func initClusterClient(
},
EndpointsResolver: endpointsResolver,
MetricsAutodiscovery: metricsServiceDiscovery(localConfig, broadcastHost),
ModuleLeaderMode: localConfig.ModuleLeaderMode,
}
schema := rc.GenerateConfigSchema(remoteConfig)
+10 -2
View File
@@ -11,7 +11,7 @@ import "time"
// - MigrationsDirPath: Path to database migrations directory (optional)
// - RemoteConfigOverride: Override content for remote configuration (optional)
// - LogFile: File-based logging configuration
// - Logs: Log sampling and rate limiting configuration
// - Logs: Log sampling and splitting configuration
// - Observability: Sentry and tracing configuration
// - InfraServerPort: Custom port for infrastructure server (optional, defaults to GrpcInnerAddress.Port + 1)
// - HealthcheckHandlerTimeout: Timeout for health check requests
@@ -41,6 +41,7 @@ type LocalConfig struct {
// - DefaultRemoteConfigPath: Custom path for default remote configuration file (optional)
// - RemoteConfigReceiverTimeout: Timeout for receiving remote configuration updates
// - MetricsAutodiscovery: Configuration for Prometheus metrics auto-discovery
// - ModuleLeaderMode: Module leader election mode announced in MODULE:READY (optional)
type ClusteredLocalConfig struct {
LocalConfig
@@ -48,20 +49,27 @@ type ClusteredLocalConfig struct {
DefaultRemoteConfigPath string
RemoteConfigReceiverTimeout time.Duration
MetricsAutodiscovery MetricsAutodiscovery
ModuleLeaderMode string
}
// Logs configures log sampling and rate limiting.
// Logs configures log sampling and splitting.
//
// Fields:
// - Sampling.Enable: Enable log sampling to prevent log flooding
// - Sampling.MaxPerSecond: Maximum log entries per second when sampling is enabled
// - Sampling.PassEvery: Number of log entries to pass before sampling again
// - Split.Enable: Split JSON log records bigger than the limit into parts (default: false, disabled)
// - Split.MaxLineKb: Size of one part in kilobytes (default: 218)
type Logs struct {
Sampling struct {
Enable bool
MaxPerSecond int
PassEvery int
}
Split struct {
Enable bool
MaxLineKb int
}
}
// LogFile configures file-based logging output.
+1
View File
@@ -348,6 +348,7 @@ func (c *Client) notifyModuleReady(ctx context.Context, cli *clientWrapper, requ
RequiredModules: moduleDependencies,
Address: c.moduleInfo.GrpcOuterAddress,
MetricsAutodiscovery: c.moduleInfo.MetricsAutodiscovery,
ModuleLeaderMode: c.moduleInfo.ModuleLeaderMode,
}
_, err := cli.EmitJsonWithAck(ctx, ModuleReady, declaration)
if err != nil {
+12 -3
View File
@@ -10,12 +10,19 @@ const (
GrpcTransport = "grpc"
HttpTransport = "http"
EmptyTransport = "empty"
ModuleLeaderModeNone = ""
ModuleLeaderModeRequestElection = "REQUEST_ELECTION"
ModuleLeaderModeImLeader = "IM_LEADER"
ModuleLeaderModeImFollower = "IM_FOLLOWER"
)
// AddressConfiguration represents the IP address and port for a service endpoint.
// AddressConfiguration represents the IP address and port for a service endpoint,
// and whether this instance is the module leader.
type AddressConfiguration struct {
IP string `json:"ip"`
Port string `json:"port"`
IP string `json:"ip"`
Port string `json:"port"`
IsModuleLeader bool
}
// ConfigData contains configuration data for a module, including version information
@@ -36,6 +43,7 @@ type ModuleInfo struct {
GrpcOuterAddress AddressConfiguration
EndpointsResolver EndpointsResolver
MetricsAutodiscovery *MetricsAutodiscovery
ModuleLeaderMode string
}
// RoutingConfig represents a list of backend declarations for routing configuration.
@@ -52,6 +60,7 @@ type BackendDeclaration struct {
RequiredModules []ModuleDependency
Address AddressConfiguration
MetricsAutodiscovery *MetricsAutodiscovery
ModuleLeaderMode string
}
// EndpointDescriptor describes an HTTP endpoint, including its path, method, authentication
+3 -2
View File
@@ -7,6 +7,7 @@ package client
import (
"context"
"slices"
"strings"
"sync/atomic"
@@ -96,8 +97,8 @@ func New(initialHosts []string, opts ...Option) (*Client, error) {
cli.grpcCli = grpcCli
roundTripper := cli.do
for i := len(cli.middlewares) - 1; i >= 0; i-- {
roundTripper = cli.middlewares[i](roundTripper)
for _, v := range slices.Backward(cli.middlewares) {
roundTripper = v(roundTripper)
}
cli.roundTripper = roundTripper
+3 -2
View File
@@ -8,6 +8,7 @@ import (
"net"
"net/http"
"net/url"
"slices"
"strings"
"time"
@@ -289,8 +290,8 @@ func defaultTransportDialContext(dialer *net.Dialer) func(context.Context, strin
// nolint:ireturn
func joinMiddlewares(root RoundTripper, mws ...Middleware) RoundTripper {
roundTripper := root
for i := len(mws) - 1; i >= 0; i-- {
roundTripper = mws[i](roundTripper)
for _, mw := range slices.Backward(mws) {
roundTripper = mw(roundTripper)
}
return roundTripper
}
+105 -1
View File
@@ -18,6 +18,7 @@
- `WithDevelopmentMode() Option` – включить логирование в режиме разработки
- `WithFileOutput(fileOutput file.Output) Option` – добавить запись логов в файл
- `WithLevel(level Level) Option` – изменить уровень логирования
- `WithSplit(maxLineKb int) Option` – включить разбиение больших JSON-записей на части
#### `NewFromConfig(config Config) (*Adapter, error)`
@@ -143,4 +144,107 @@ func main() {
}
}
```
```
## Разбиение больших записей на части
Если одна JSON-запись не проходит в лимит лог-коллектора,
её можно писать несколькими обычными записями. Разбиение включается полем `Split` в
`log.Config` или опцией `log.WithSplit(maxLineKb)`, в `bootstrap` — секцией `logs.split`
локального конфига:
```yaml
logs:
split:
enable: true # по умолчанию false
maxLineKb: 218 # размер одной части в килобайтах, 218 × 1024 = 223 232 байта
```
Ключи конфига: `LOGS.SPLIT.ENABLE` (по умолчанию `false`) и `LOGS.SPLIT.MAXLINEKB`
(по умолчанию `218`). Одного `maxLineKb` без `enabled: true` недостаточно. В dev-режиме
разбиение не работает: вывод не JSON.
### Контракт части
Запись, которая не влезает в `maxLineKb × 1024` байт, превращается в несколько **обычных**
лог-записей: у каждой свой `level`, `ts` и исходные имена полей. Ничего не
переименовывается и не заворачивается в `payload`. В корень каждой части добавляются:
* `logPackageId` — UUID v4 (RFC 9562), общий для всех частей одной исходной записи;
* `logPackageIndex` — порядковый номер части, начиная с `1`;
* `logPackageTotal` — общее количество частей.
`requestId`, если он был в записи, попадает в корень **каждой** части. `level`, `ts`, `msg` и
`requestId` одинаковы во всех частях: по ним можно фильтровать в VL, и сообщение не теряется
ни в одной части.
Мелкие поля упаковываются в части жадно в исходном порядке. Поле, которое само не влезает в
часть, остаётся под своим именем, а его значение режется на куски: части идут в порядке
`logPackageIndex`, после сборки значение восстанавливается склейкой.
Пример: `responseBody` не влезает в лимит.
```json
{"level":"debug","ts":"...","msg":"http handler: response","requestId":"3365548a...",
"logPackageId":"9f2c...","logPackageIndex":1,"logPackageTotal":3,
"statusCode":200,"elapsedTimeMs":302}
{"level":"debug","ts":"...","msg":"http handler: response","requestId":"3365548a...",
"logPackageId":"9f2c...","logPackageIndex":2,"logPackageTotal":3,
"responseBody":"{\"added\":[],\"changed\":[],\"removed\":[{\"block\":\"ADDRESS\"..."}
{"level":"debug","ts":"...","msg":"http handler: response","requestId":"3365548a...",
"logPackageId":"9f2c...","logPackageIndex":3,"logPackageTotal":3,
"responseBody":"...\"errorType\":\"Уникальность связки\",...}]}"}
```
### Сборка на приёмнике
```
parts := group by logPackageId, sort by logPackageIndex
if len(parts) != logPackageTotal: пачка битая — отбросить
result := {}
for key in {level, ts, msg, requestId}:
if key in parts[0]:
result[key] = parts[0][key] // одинаковые во всех частях, берём из любой
values := map[string][]any // значения полей по частям, в порядке logPackageIndex
for part in parts:
for key, value in part:
if key in {level, ts, msg, logPackageId, logPackageIndex, logPackageTotal, requestId}:
continue
values[key] = append(values[key], value)
for key, list in values:
if len(list) == 1:
result[key] = list[0] // обычное поле (число, строка, bool)
else:
result[key] = concat(list) // куски строкового поля, склеиваем по порядку
```
Правило простое: поле, которое встречается больше одного раза, — это куски одного значения;
встречается один раз — обычное поле как есть. `requestId` и `msg` повторяются в каждой части,
но значения одни и те же, поэтому берутся из любой части.
### Ограничения
* Разбиваются только строковые поля (`String`, `ByteString`, `Error`, `fmt.Stringer`). Если в лимит не влезает
нестроковое поле (объект, массив, число), разбиение не применяется и запись пишется как есть.
* `Any` разбивается по типу значения: `string`, `[]byte`, `error` и `fmt.Stringer` — как соответствующее строковое поле
(zap резолвит их в конкретный тип при создании поля). Значения `struct`, `map`, `slice` (в том числе `[]string`) кодируются как JSON-объект/массив и не разбиваются:
куски склеиваются конкатенацией, для объектов и массивов восстановление структуры невозможно. Если такой объект не влезает в лимит, запись пишется как есть.
Большое тело (например, JSON-ответ) логируйте строкой — `log.String` или `log.ByteString`.
* Если в лимит не влезает сам `msg`, запись пишется как есть.
* Невалидный UTF-8 в большом поле не режется: ширину экранирования нельзя посчитать точно,
запись пишется как есть.
* Поля с пространствами имён (`zap.Namespace`) не разбиваются: после открытия namespace
последующие поля вложены в него, поэтому запись пишется как есть.
* Поля, добавленные к логгеру через `With` (в том числе в `StdLoggerWithLevel`), попадают в
каждую часть; они учитываются при расчёте бюджета, но не режутся.
* Нарезка идёт по байтам с учётом экранирования и откатом до начала UTF-8-символа, поэтому
кириллица и эмодзи не рвутся, а каждая часть укладывается в `maxLineKb × 1024` байт.
* Сэмплирование принимает решение по исходной записи один раз, поэтому пачка пишется целиком
или не пишется вовсе; хуки вызываются один раз на запись.
* `level`, `ts`, `msg` и `requestId` дублируются в каждой части (~200 байт накладных на часть):
это плата за то, что часть остаётся полноценной лог-записью и по ней можно фильтровать.
* Если запись не удалось разложить, она пишется как есть — сбой разбиения не теряет лог.
+3
View File
@@ -21,6 +21,9 @@ type Config struct {
Hooks []func(entry zapcore.Entry) error
// InitialLevel sets the minimum log level.
InitialLevel Level
// Split enables splitting of oversized JSON log records into several parts.
// It is ignored in development mode because the output is not JSON there.
Split *SplitConfig
}
// SamplingConfig is an alias for zap.SamplingConfig.
+83 -6
View File
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"time"
"github.com/txix-open/isp-kit/errors"
"go.uber.org/zap"
@@ -35,7 +36,12 @@ func NewFromConfig(config Config) (*Adapter, error) {
cfg.DisableStacktrace = true
cfg.DisableCaller = true
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.Sampling = config.Sampling
// Splitting is useless for the development encoder, which is not JSON.
var split *SplitConfig
if config.Split != nil && config.Split.enabled() && !config.IsInDevMode {
split = config.Split
}
if config.DisableDefaultOutput {
cfg.OutputPaths = nil
@@ -48,12 +54,22 @@ func NewFromConfig(config Config) (*Adapter, error) {
level := zap.NewAtomicLevelAt(config.InitialLevel)
cfg.Level = level
var opts []zap.Option
if len(config.Hooks) > 0 {
opts = append(opts, zap.Hooks(config.Hooks...))
var (
logger *zap.Logger
err error
)
if split != nil {
logger, err = newSplitLogger(cfg, *split, config.Sampling, config.Hooks)
} else {
cfg.Sampling = config.Sampling
// Hooks are attached to the logger, so they observe the original record
// once instead of every part.
var opts []zap.Option
if len(config.Hooks) > 0 {
opts = append(opts, zap.Hooks(config.Hooks...))
}
logger, err = cfg.Build(opts...)
}
logger, err := cfg.Build(opts...)
if err != nil {
return nil, errors.WithMessage(err, "build logger")
}
@@ -65,6 +81,67 @@ func NewFromConfig(config Config) (*Adapter, error) {
}, nil
}
// newSplitLogger assembles a logger with the splitting core manually: the core
// writes an encoded line to the sink itself to avoid a second encoding, and
// [zap.Config.Build] does not expose the encoder and the sink. The sampler and
// hooks are attached above the core, so a sampling decision and the hooks are
// applied to the original record once.
func newSplitLogger(
cfg zap.Config,
split SplitConfig,
sampling *SamplingConfig,
hooks []func(entry zapcore.Entry) error,
) (*zap.Logger, error) {
sink, _, err := zap.Open(cfg.OutputPaths...)
if err != nil {
return nil, errors.WithMessage(err, "open log output")
}
errSink, _, err := zap.Open(cfg.ErrorOutputPaths...)
if err != nil {
return nil, errors.WithMessage(err, "open log error output")
}
// zap.Open returns a locked sink, so lines from different goroutines do not
// interleave byte by byte.
encoder := zapcore.NewJSONEncoder(cfg.EncoderConfig)
core := newSplittingCore(encoder, sink, cfg.Level, cfg.EncoderConfig, split)
loggerOpts := []zap.Option{zap.ErrorOutput(errSink)}
if !cfg.DisableCaller {
loggerOpts = append(loggerOpts, zap.AddCaller())
}
if !cfg.DisableStacktrace {
loggerOpts = append(loggerOpts, zap.AddStacktrace(zapcore.ErrorLevel))
}
logger := zap.New(core, loggerOpts...)
if sampling != nil {
logger = logger.WithOptions(samplerOption(sampling))
}
if len(hooks) > 0 {
logger = logger.WithOptions(zap.Hooks(hooks...))
}
return logger, nil
}
// samplerOption returns a zap option that samples records the same way
// [zap.Config] does, but can be attached above the splitting core.
func samplerOption(sampling *SamplingConfig) zap.Option {
return zap.WrapCore(func(core zapcore.Core) zapcore.Core {
samplerOpts := make([]zapcore.SamplerOption, 0, 1)
if sampling.Hook != nil {
samplerOpts = append(samplerOpts, zapcore.SamplerHook(sampling.Hook))
}
return zapcore.NewSamplerWithOptions(
core,
time.Second,
sampling.Initial,
sampling.Thereafter,
samplerOpts...,
)
})
}
// Fatal logs a fatal-level message.
func (a *Adapter) Fatal(ctx context.Context, message any, fields ...Field) {
a.Log(ctx, FatalLevel, message, fields...)
+1 -1
View File
@@ -27,7 +27,7 @@ func LogLevelForError(err error) log.Level {
// It uses LogLevelForError to determine the log level and returns the corresponding Logger method.
func LogLevelFuncForError(err error, logger log.Logger) func(ctx context.Context, message any, fields ...log.Field) {
logLevel := LogLevelForError(err)
switch logLevel {
switch logLevel { //nolint:exhaustive
case log.ErrorLevel:
return logger.Error
case log.WarnLevel:
+11
View File
@@ -34,3 +34,14 @@ func WithLevel(level Level) Option {
a.InitialLevel = level
}
}
// WithSplit enables splitting of oversized JSON log records into parts of at
// most maxLineKb kilobytes each. It is ignored in development mode.
func WithSplit(maxLineKb int) Option {
return func(a *Config) {
a.Split = &SplitConfig{
Enabled: true,
MaxLineKb: maxLineKb,
}
}
}
+886
View File
@@ -0,0 +1,886 @@
package log
import (
"fmt"
"strconv"
"unicode/utf8"
"uuid"
"github.com/txix-open/isp-kit/errors"
"github.com/txix-open/isp-kit/requestid"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
// Field names added to the root of every part of a split JSON log record. A
// receiver groups parts by [SplitFieldPackageId], sorts them by
// [SplitFieldPackageIndex] and checks that their number is
// [SplitFieldPackageTotal].
const (
// SplitFieldPackageId is the id shared by all parts of one original record.
SplitFieldPackageId = "logPackageId"
// SplitFieldPackageIndex is the 1-based number of a part in a package.
SplitFieldPackageIndex = "logPackageIndex"
// SplitFieldPackageTotal is the total number of parts in a package.
SplitFieldPackageTotal = "logPackageTotal"
)
const (
// DefaultMaxLineKb is the default limit of a part in kilobytes. It is
// 218 KiB and leaves a margin up to the 256 KB limit of a log collector.
DefaultMaxLineKb = 218
// DefaultMaxLineBytes is [DefaultMaxLineKb] in bytes.
DefaultMaxLineBytes = DefaultMaxLineKb * 1024
// probePartNumber is the maximum part number reserved when the overhead of
// a part is measured. It has 9 digits.
probePartNumber = 999999999
// metaFieldsCapacity is the maximum number of package meta fields of a part.
metaFieldsCapacity = 4
// escapedSpecialWidth is the size of a character escaped with a backslash.
escapedSpecialWidth = 2
// escapedControlWidth is the size of a control character escaped as \u00XX.
escapedControlWidth = 6
// controlByteBoundary is the first printable ASCII byte.
controlByteBoundary = 0x20
// jsonBraces is the number of braces around an encoded field.
jsonBraces = 2
// fieldKeySyntaxWidth is the size of the comma, quotes and colon around a
// field key: ,"key": without the key itself.
fieldKeySyntaxWidth = 4
// fieldValueQuotesWidth is the size of the quotes around a string value.
fieldValueQuotesWidth = 2
// bytesPerKb is the number of bytes in a kilobyte.
bytesPerKb = 1024
// minSplitParts is the minimum number of parts of a split record: a record
// that fits into one part is written as is.
minSplitParts = 2
// utf8ContinuationMask and utf8ContinuationPrefix identify a continuation
// byte of a UTF-8 sequence: 10xxxxxx.
utf8ContinuationMask = 0xC0
utf8ContinuationPrefix = 0x80
// decimalBase is the base of the decimal representation of an integer.
decimalBase = 10
)
// errCannotSplit reports that a record cannot be split without losing data.
// Such a record is written as is, see [splittingCore.Write].
var errCannotSplit = errors.New("cannot split log record")
// SplitConfig configures splitting of oversized JSON log records into parts.
//
// When enabled, a record whose JSON representation is longer than
// [SplitConfig.MaxLineKb] kilobytes is written as several ordinary records.
// Every part keeps the original level, time and field names, and additionally
// carries [SplitFieldPackageId], [SplitFieldPackageIndex] and
// [SplitFieldPackageTotal]. The request id, if it was in the record, and the
// message are written to every part as well.
//
// Fields that fit into a part are packed into it unchanged. A field that does
// not fit keeps its name and its value is cut into pieces: concatenation of the
// pieces in the part order restores the value. Only string fields are cut.
//
// Splitting is ignored in development mode, where the output is not JSON.
type SplitConfig struct {
// Enabled turns splitting on. Splitting is off by default.
Enabled bool
// MaxLineKb is the maximum size of one part in kilobytes. A value less than
// or equal to zero makes splitting inactive.
MaxLineKb int
}
// enabled reports whether the configuration requires splitting.
func (c SplitConfig) enabled() bool {
return c.Enabled && c.MaxLineKb > 0
}
// maxLineBytes returns the byte limit of one part.
func (c SplitConfig) maxLineBytes() int {
return c.MaxLineKb * bytesPerKb
}
// splittingCore writes JSON records to a sink and splits oversized records into
// several records.
//
// The core encodes a record once. A record that fits is written to the sink as
// is, without a second encoding. An oversized record is planned and every part
// is encoded and written separately. Every part carries the same level, time,
// message and logger fields, so the parts can be filtered by them.
//
// The core is assembled manually, see [newSplitLogger]: a regular zap core
// encodes and writes a record itself, so a splitting wrapper would have to
// encode every record twice.
//
// The core is safe for concurrent use: zap encoders encode an entry into a new
// buffer and keep no state between calls.
type splittingCore struct {
encoder zapcore.Encoder
sink zapcore.WriteSyncer
level zapcore.LevelEnabler
// bareEncConfig is the encoder config without the record envelope: it is
// used to measure a single field.
bareEncConfig zapcore.EncoderConfig
maxLineBytes int
// withRequestId reports whether the request id is attached to the logger by
// With. The encoders write it into every record, so a meta request id field
// is not added.
withRequestId bool
// withNamespace reports whether a namespace is attached to the logger by
// With. The meta fields of a part would be nested into that namespace, so
// such records are written as is.
withNamespace bool
}
// newSplittingCore creates a core that writes ready lines to sink.
func newSplittingCore(
encoder zapcore.Encoder,
sink zapcore.WriteSyncer,
level zapcore.LevelEnabler,
encConfig zapcore.EncoderConfig,
cfg SplitConfig,
) *splittingCore {
return &splittingCore{
encoder: encoder,
sink: sink,
level: level,
bareEncConfig: fieldOnlyEncoderConfig(encConfig),
maxLineBytes: cfg.maxLineBytes(),
}
}
// fieldOnlyEncoderConfig returns a copy of cfg without the record envelope: a
// field is measured on its own, so no level, time or message is written.
func fieldOnlyEncoderConfig(cfg zapcore.EncoderConfig) zapcore.EncoderConfig {
cfg.LevelKey = ""
cfg.TimeKey = ""
cfg.MessageKey = ""
cfg.NameKey = ""
cfg.CallerKey = ""
cfg.FunctionKey = ""
cfg.StacktraceKey = ""
return cfg
}
// Enabled reports whether the given level is enabled.
func (c *splittingCore) Enabled(level zapcore.Level) bool {
return c.level.Enabled(level)
}
// With returns a new core with the given fields attached to the encoder. The
// request id and namespaces are tracked: they change how the parts are built.
func (c *splittingCore) With(fields []zapcore.Field) zapcore.Core {
if len(fields) == 0 {
return c
}
encoder := c.encoder.Clone()
for i := range fields {
fields[i].AddTo(encoder)
}
clone := *c
clone.encoder = encoder
clone.withRequestId = c.withRequestId || hasField(fields, requestid.LogKey)
clone.withNamespace = c.withNamespace || hasNamespaceField(fields)
return &clone
}
// Check registers the core itself.
func (c *splittingCore) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if !c.Enabled(entry.Level) {
return checkedEntry
}
return checkedEntry.AddCore(entry, c)
}
// Sync flushes the sink.
func (c *splittingCore) Sync() error {
return c.sink.Sync()
}
// Write encodes a record once and writes it to the sink. An oversized record is
// split into parts: every part is encoded and written separately. A record that
// cannot be split is written as is, so a logging call never loses data because
// of splitting.
func (c *splittingCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
// Escaping only makes a record longer, so the raw content is a lower bound
// of the encoded record. A record that is already longer than the limit does
// not need to be encoded before splitting.
if c.definitelyOversized(entry, fields) {
return c.writeSplit(entry, fields, nil)
}
line, err := c.encoder.EncodeEntry(entry, fields)
if err != nil {
return err
}
if recordLen(line) <= c.maxLineBytes {
return c.writeBuffer(line)
}
return c.writeSplit(entry, fields, line)
}
// writeSplit plans the parts of an oversized record and writes them. The
// encoded whole record is passed when it is already available: it is written as
// is when the record cannot be split.
func (c *splittingCore) writeSplit(
entry zapcore.Entry,
fields []zapcore.Field,
oversizedLine *buffer.Buffer,
) error {
if c.withNamespace {
return c.writeWhole(entry, fields, oversizedLine)
}
sizes := make([]int, len(fields))
if !c.measureFields(fields, sizes) {
return c.writeWhole(entry, fields, oversizedLine)
}
logPackageId := uuid.New().String()
packable, packableSizes, requestIdField, hasRequestId := c.extractRequestId(fields, sizes)
parts := c.planParts(entry, packable, packableSizes, logPackageId, requestIdField, hasRequestId)
if len(parts) < minSplitParts {
return c.writeWhole(entry, fields, oversizedLine)
}
if oversizedLine != nil {
oversizedLine.Free()
}
return c.writeParts(entry, parts, logPackageId, requestIdField, hasRequestId)
}
// writeParts encodes and writes every part with the same encoder as a whole
// record. It stops at the first error: the remaining parts are not written
// when the encoder or the sink has failed.
func (c *splittingCore) writeParts(
entry zapcore.Entry,
parts [][]zapcore.Field,
logPackageId string,
requestIdField zapcore.Field,
hasRequestId bool,
) error {
for i, part := range parts {
partFields := make([]zapcore.Field, 0, len(part)+metaFieldsCapacity)
partFields = append(partFields,
metaFields(logPackageId, i+1, len(parts), requestIdField, hasRequestId)...)
partFields = append(partFields, part...)
line, err := c.encoder.EncodeEntry(entry, partFields)
if err != nil {
return errors.WithMessage(err, "cannot encode log part")
}
err = c.writeBuffer(line)
if err != nil {
return errors.WithMessage(err, "cannot write log part")
}
}
return nil
}
// writeWhole writes the record as is. The encoded line is reused when it is
// already available.
func (c *splittingCore) writeWhole(
entry zapcore.Entry,
fields []zapcore.Field,
oversizedLine *buffer.Buffer,
) error {
if oversizedLine != nil {
return c.writeBuffer(oversizedLine)
}
line, err := c.encoder.EncodeEntry(entry, fields)
if err != nil {
return err
}
return c.writeBuffer(line)
}
// writeBuffer writes the encoded line to the sink and releases the buffer.
func (c *splittingCore) writeBuffer(line *buffer.Buffer) error {
defer line.Free()
_, err := c.sink.Write(line.Bytes())
return err
}
// definitelyOversized reports whether the raw content of the record is already
// longer than the limit. Every byte takes at least one byte in the JSON output,
// so such a record is definitely oversized.
func (c *splittingCore) definitelyOversized(entry zapcore.Entry, fields []zapcore.Field) bool {
total := len(entry.Message)
if total > c.maxLineBytes {
return true
}
for i := range fields {
field := fields[i]
if field.Type == zapcore.StringType {
total += len(field.Key) + len(field.String)
} else if field.Type == zapcore.ByteStringType {
value, ok := field.Interface.([]byte)
if ok {
total += len(field.Key) + len(value)
}
}
if total > c.maxLineBytes {
return true
}
}
return false
}
// measureFields fills sizes with the exact size of every field in a record. It
// reports false when a field cannot be measured field by field.
func (c *splittingCore) measureFields(fields []zapcore.Field, sizes []int) bool {
sizer := &fieldSizer{encConfig: c.bareEncConfig, limit: c.maxLineBytes}
for i := range fields {
size, err := sizer.size(fields[i])
if err != nil {
return false
}
sizes[i] = size
}
return true
}
// extractRequestId removes the request id from the packable fields: it is
// written to every part by [metaFields]. When the request id was attached to
// the logger by With, the encoders already write it into every part, so no meta
// field is added and the duplicate is removed. sizes stays aligned with the
// returned fields.
func (c *splittingCore) extractRequestId(
fields []zapcore.Field,
sizes []int,
) ([]zapcore.Field, []int, zapcore.Field, bool) {
requestIdField, hasRequestId := findRequestId(fields)
if !hasRequestId {
return fields, sizes, zapcore.Field{}, false
}
packable, packableSizes := withoutField(fields, sizes, requestid.LogKey)
if c.withRequestId {
return packable, packableSizes, zapcore.Field{}, false
}
return packable, packableSizes, requestIdField, true
}
// planParts packs the fields into parts. Small fields are packed greedily in the
// original order, a field that does not fit into a part is cut into pieces and
// goes to its own parts. sizes contains the exact size of every field. Every
// part carries the whole envelope (level, time, message and logger fields), so
// all parts share the same budget. It returns nil when the record cannot be
// split: the limit is smaller than the envelope (for example, an oversized
// message), a field cannot be packed or a field cannot be cut.
func (c *splittingCore) planParts(
entry zapcore.Entry,
fields []zapcore.Field,
sizes []int,
logPackageId string,
requestIdField zapcore.Field,
hasRequestId bool,
) [][]zapcore.Field {
budget, ok := c.partBudget(entry, logPackageId, requestIdField, hasRequestId)
if !ok {
return nil
}
parts := make([][]zapcore.Field, 0, len(fields)+1)
current := make([]zapcore.Field, 0, len(fields))
currentSize := 0
for i, field := range fields {
if field.Type == zapcore.SkipType {
continue // the field writes nothing, it must not create a part
}
size := sizes[i]
if size > budget {
if len(current) > 0 {
parts = append(parts, current)
current, currentSize = nil, 0
}
chunks := c.chunkField(field, size, budget)
if chunks == nil {
return nil
}
parts = append(parts, chunks...)
continue
}
if currentSize+size > budget {
if len(current) > 0 {
parts = append(parts, current)
}
current, currentSize = nil, 0
}
current = append(current, field)
currentSize += size
}
if len(current) > 0 {
parts = append(parts, current)
}
return parts
}
// partBudget returns the free space of a part. It reports false when the limit
// is smaller than the envelope, for example when the message itself does not fit
// into a part.
func (c *splittingCore) partBudget(
entry zapcore.Entry,
logPackageId string,
requestIdField zapcore.Field,
hasRequestId bool,
) (int, bool) {
baseline, err := c.partBaseline(entry, logPackageId, requestIdField, hasRequestId)
if err != nil {
return 0, false
}
budget := c.maxLineBytes - baseline
if budget <= 0 {
return 0, false
}
return budget, true
}
// partBaseline returns the size of a part without user fields: the envelope,
// the fields of the logger and the package meta fields. The meta fields are
// measured with the maximum 9-digit part numbers, so the baseline is an upper
// bound for any real part.
func (c *splittingCore) partBaseline(
entry zapcore.Entry,
logPackageId string,
requestIdField zapcore.Field,
hasRequestId bool,
) (int, error) {
probeFields := metaFields(logPackageId, probePartNumber, probePartNumber, requestIdField, hasRequestId)
line, err := c.encoder.EncodeEntry(entry, probeFields)
if err != nil {
return 0, err
}
defer line.Free()
return recordLen(line), nil
}
// chunkField cuts a string field into pieces that fit into a part. The field
// keeps its name. It returns nil when the field cannot be cut without losing
// data: it is not a string field, it contains invalid UTF-8 or it is encoded
// with additional fields (for example, an error with causes).
func (c *splittingCore) chunkField(field zapcore.Field, fieldSize int, budget int) [][]zapcore.Field {
if !utf8.ValidString(field.Key) {
return nil
}
value, ok := stringValue(field)
if !ok || !utf8.ValidString(value) {
return nil
}
keyOverhead := fieldKeyOverhead(field.Key)
// A string field is encoded as a JSON string by definition. An error and a
// stringer field are encoded as a string only when their size matches the
// key and the escaped value: an error with causes, a verbose message or a
// panicking stringer adds extra fields.
if (field.Type == zapcore.ErrorType || field.Type == zapcore.StringerType) &&
fieldSize != keyOverhead+fieldValueQuotesWidth+escapedWidth(value) {
return nil
}
valueBudget := budget - keyOverhead - fieldValueQuotesWidth
if valueBudget <= 0 {
return nil
}
chunks, ok := chunkByBytes(value, valueBudget)
if !ok {
return nil
}
parts := make([][]zapcore.Field, 0, len(chunks))
for _, chunk := range chunks {
parts = append(parts, []zapcore.Field{zap.String(field.Key, chunk)})
}
return parts
}
// fieldSizer returns the size of a field without encoding it when the type
// allows, and falls back to a measuring encoder otherwise.
type fieldSizer struct {
encConfig zapcore.EncoderConfig
encoder zapcore.Encoder
// limit is the limit of the whole record. A string field longer than limit
// cannot fit into a record and is not measured.
limit int
}
// size returns the size of the field in a record, including the separating
// comma.
func (s *fieldSizer) size(field zapcore.Field) (int, error) {
if size, ok := oversizedStringSize(field, s.limit); ok {
return size, nil
}
if size, ok := cheapFieldSize(field); ok {
return size, nil
}
if s.encoder == nil {
s.encoder = zapcore.NewJSONEncoder(s.encConfig)
}
return fieldSize(s.encoder, field)
}
// fieldSize returns the size of a field in a record, including the separating
// comma, by encoding it on its own. A namespace field has no size of its own:
// it changes the structure of the record and cannot be packed field by field.
func fieldSize(encoder zapcore.Encoder, field zapcore.Field) (int, error) {
if field.Type == zapcore.SkipType {
return 0, nil
}
if field.Type == zapcore.NamespaceType {
return 0, errCannotSplit
}
line, err := encoder.EncodeEntry(zapcore.Entry{}, []zapcore.Field{field})
if err != nil {
return 0, err
}
defer line.Free()
// The encoded field is {"key":value}; the braces are not a part of the
// field, the comma before it is.
size := recordLen(line) - jsonBraces
if size < 0 {
return 0, errCannotSplit
}
return size + 1, nil
}
// oversizedStringSize reports whether a string field alone is longer than the
// whole record limit. The returned size is a placeholder larger than any part
// budget: only the fact that the field does not fit is used.
func oversizedStringSize(field zapcore.Field, limit int) (int, bool) {
if limit <= 0 {
return 0, false
}
if field.Type == zapcore.StringType {
if len(field.String) > limit {
return limit + 1, true
}
return 0, false
}
if field.Type == zapcore.ByteStringType {
value, ok := field.Interface.([]byte)
if ok && len(value) > limit {
return limit + 1, true
}
}
return 0, false
}
// cheapFieldSize returns the exact size of a field without encoding it. It
// supports the types with a known JSON representation: skip, strings, byte
// strings, booleans and integers. Invalid UTF-8 and other types are measured by
// an encoder.
func cheapFieldSize(field zapcore.Field) (int, bool) {
if !utf8.ValidString(field.Key) {
return 0, false
}
keyOverhead := fieldKeyOverhead(field.Key)
if field.Type == zapcore.SkipType {
return 0, true
}
if field.Type == zapcore.StringType {
if !utf8.ValidString(field.String) {
return 0, false
}
return keyOverhead + fieldValueQuotesWidth + escapedWidth(field.String), true
}
if field.Type == zapcore.ByteStringType {
value, ok := field.Interface.([]byte)
if !ok || !utf8.Valid(value) {
return 0, false
}
return keyOverhead + fieldValueQuotesWidth + escapedBytesWidth(value), true
}
if field.Type == zapcore.BoolType {
if field.Integer == 1 {
return keyOverhead + len("true"), true
}
return keyOverhead + len("false"), true
}
if isIntFieldType(field.Type) {
return keyOverhead + intWidth(field.Integer), true
}
if isUintFieldType(field.Type) {
// The unsigned value is stored in the signed integer field as a bit
// pattern, see [zapcore.Field].
return keyOverhead + uintWidth(uint64(field.Integer)), true //nolint:gosec
}
return 0, false
}
// isIntFieldType reports whether the field type encodes a signed integer.
func isIntFieldType(fieldType zapcore.FieldType) bool {
return fieldType == zapcore.Int64Type ||
fieldType == zapcore.Int32Type ||
fieldType == zapcore.Int16Type ||
fieldType == zapcore.Int8Type
}
// isUintFieldType reports whether the field type encodes an unsigned integer.
func isUintFieldType(fieldType zapcore.FieldType) bool {
return fieldType == zapcore.Uint64Type ||
fieldType == zapcore.Uint32Type ||
fieldType == zapcore.Uint16Type ||
fieldType == zapcore.Uint8Type ||
fieldType == zapcore.UintptrType
}
// intWidth returns the number of decimal digits of value with the sign.
func intWidth(value int64) int {
var buff [20]byte
return len(strconv.AppendInt(buff[:0], value, decimalBase))
}
// uintWidth returns the number of decimal digits of value.
func uintWidth(value uint64) int {
var buff [20]byte
return len(strconv.AppendUint(buff[:0], value, decimalBase))
}
// metaFields returns the fields that identify a part in a package. The request
// id goes first and the package fields follow it, so the root of a part carries
// level, time and request id, and then the package metadata.
func metaFields(
logPackageId string,
index int,
total int,
requestIdField zapcore.Field,
hasRequestId bool,
) []zapcore.Field {
fields := make([]zapcore.Field, 0, metaFieldsCapacity)
if hasRequestId {
fields = append(fields, requestIdField)
}
fields = append(fields,
zap.String(SplitFieldPackageId, logPackageId),
zap.Int(SplitFieldPackageIndex, index),
zap.Int(SplitFieldPackageTotal, total),
)
return fields
}
// findRequestId returns the request id from the log fields.
func findRequestId(fields []zapcore.Field) (zapcore.Field, bool) {
for i := range fields {
if fields[i].Key == requestid.LogKey && fields[i].Type == zapcore.StringType {
return fields[i], true
}
}
return zapcore.Field{}, false
}
// hasField reports whether the fields contain a field with the given key.
func hasField(fields []zapcore.Field, key string) bool {
for i := range fields {
if fields[i].Key == key {
return true
}
}
return false
}
// withoutField returns the fields and their sizes without all fields with the
// given key.
func withoutField(fields []zapcore.Field, sizes []int, key string) ([]zapcore.Field, []int) {
index := -1
for i := range fields {
if fields[i].Key == key {
index = i
break
}
}
if index < 0 {
return fields, sizes
}
filteredFields := make([]zapcore.Field, 0, len(fields)-1)
filteredSizes := make([]int, 0, len(sizes)-1)
for i := range fields {
if fields[i].Key == key {
continue
}
filteredFields = append(filteredFields, fields[i])
filteredSizes = append(filteredSizes, sizes[i])
}
return filteredFields, filteredSizes
}
// hasNamespaceField reports whether the fields contain a namespace field.
func hasNamespaceField(fields []zapcore.Field) bool {
for i := range fields {
if fields[i].Type == zapcore.NamespaceType {
return true
}
}
return false
}
// stringValue returns the raw value of a string field: a string field, a byte
// string, an error or a stringer. The value of other fields is encoded as JSON
// and cannot be restored by a simple concatenation.
func stringValue(field zapcore.Field) (value string, ok bool) {
if field.Type == zapcore.StringType {
return field.String, true
}
if field.Type == zapcore.ByteStringType {
bytes, isBytes := field.Interface.([]byte)
if !isBytes {
return "", false
}
return string(bytes), true
}
if field.Type == zapcore.ErrorType {
err, isError := field.Interface.(error)
if !isError || err == nil {
return "", false
}
return errorValue(err)
}
if field.Type == zapcore.StringerType {
stringer, isStringer := field.Interface.(fmt.Stringer)
if !isStringer {
return "", false
}
return stringerValue(stringer)
}
return "", false
}
// errorValue returns the text of an error. The Error method may panic, for
// example when the error is a typed nil pointer; such a field cannot be cut.
func errorValue(err error) (value string, ok bool) {
defer func() {
if recover() != nil {
value, ok = "", false
}
}()
return err.Error(), true
}
// stringerValue returns the text of a stringer. The String method may panic,
// for example when the value is a typed nil pointer; such a field cannot be
// cut.
func stringerValue(stringer fmt.Stringer) (value string, ok bool) {
defer func() {
if recover() != nil {
value, ok = "", false
}
}()
return stringer.String(), true
}
// escapedByteWidth maps a byte of a raw string to the number of bytes it takes
// in the JSON output: 1 for a regular byte, 2 for a character escaped with a
// backslash, 6 for a control character escaped as \u00XX and 1 for every byte of
// a valid multibyte UTF-8 sequence.
var escapedByteWidth = newEscapedByteWidth()
func newEscapedByteWidth() *[256]byte {
widths := new([256]byte)
for i := range widths {
widths[i] = 1
}
for i := range controlByteBoundary {
widths[i] = escapedControlWidth
}
widths['"'] = escapedSpecialWidth
widths['\\'] = escapedSpecialWidth
widths['\n'] = escapedSpecialWidth
widths['\r'] = escapedSpecialWidth
widths['\t'] = escapedSpecialWidth
return widths
}
// escapedWidth returns the number of bytes s takes in the JSON output. s must be
// valid UTF-8.
func escapedWidth(s string) int {
width := 0
for i := range len(s) {
width += int(escapedByteWidth[s[i]])
}
return width
}
// escapedBytesWidth returns the number of bytes s takes in the JSON output. s
// must be valid UTF-8.
func escapedBytesWidth(s []byte) int {
width := 0
for i := range s {
width += int(escapedByteWidth[s[i]])
}
return width
}
// fieldKeyOverhead returns the number of bytes taken by the key of a field and
// its separating comma: ,"key":.
func fieldKeyOverhead(key string) int {
return fieldKeySyntaxWidth + escapedWidth(key)
}
// chunkByBytes splits s into chunks whose JSON-escaped size does not exceed
// budget. It never splits a UTF-8 sequence. It returns false when a chunk
// cannot be built: s is not valid UTF-8 or a single character does not fit the
// budget.
func chunkByBytes(s string, budget int) ([]string, bool) {
if budget <= 0 {
return nil, false
}
if s == "" {
return []string{""}, true
}
if !utf8.ValidString(s) {
return nil, false
}
chunks := make([]string, 0, len(s)/budget+1)
for len(s) > 0 {
used := 0
index := 0
for index < len(s) {
width := int(escapedByteWidth[s[index]])
if used+width > budget {
break
}
used += width
index++
}
// Do not cut a UTF-8 sequence: step back to the beginning of the
// current character.
for index > 0 && index < len(s) && isUTF8Continuation(s[index]) {
index--
}
if index == 0 {
// The first character does not fit the budget.
return nil, false
}
chunks = append(chunks, s[:index])
s = s[index:]
}
return chunks, true
}
// isUTF8Continuation reports whether b is a continuation byte of a UTF-8
// sequence (10xxxxxx).
func isUTF8Continuation(b byte) bool {
return b&utf8ContinuationMask == utf8ContinuationPrefix
}
// recordLen returns the size of the encoded record without the line ending.
func recordLen(line *buffer.Buffer) int {
data := line.Bytes()
size := len(data)
if size > 0 && data[size-1] == '\n' {
size--
}
if size > 0 && data[size-1] == '\r' {
size--
}
return size
}
-5
View File
@@ -8,14 +8,9 @@
Проверяет, что удалённая конфигурация соответствует сгенерированной JSON-схеме и проходит валидацию.
* Проверяет отсутствие устаревшего тега `valid`
* Проверяет соответствие структуры JSON-схеме
* Выполняет валидацию значений с помощью `validator.Default`
### `FindTag[T any](v T, tag string) bool`
Рекурсивно проверяет, содержит ли структура тег с указанным именем.
## Usage
### Example usage in test
-36
View File
@@ -5,7 +5,6 @@ package rct
import (
"os"
"reflect"
"testing"
"github.com/stretchr/testify/require"
@@ -21,7 +20,6 @@ import (
// validator tag validation.
//
// The function checks:
// - No deprecated 'valid' tags are present
// - The default configuration file is valid against the generated schema
// - The configuration can be unmarshaled from the file
// - The configuration passes validation
@@ -30,9 +28,6 @@ import (
func Test[T any](t *testing.T, defaultRemoteConfigPath string, remoteConfig T) {
require := require.New(t)
validatorTag := "valid"
require.Falsef(FindTag(remoteConfig, validatorTag), "'%s' tag is not longer supported, you must use https://github.com/go-playground/validator", validatorTag)
defaultRemoteConfig, err := os.ReadFile(defaultRemoteConfigPath)
require.NoError(err)
@@ -58,34 +53,3 @@ func Test[T any](t *testing.T, defaultRemoteConfigPath string, remoteConfig T) {
err = validator.Default.ValidateToError(remoteConfig)
require.NoError(err)
}
// FindTag recursively searches a type for the specified struct tag.
// It traverses structs, maps, arrays, and slices to find any field
// that has the given tag. Returns true if the tag is found.
func FindTag[T any](v T, tag string) bool {
t := reflect.TypeOf(v)
if t == nil {
return false
}
queue := []reflect.Type{t}
for len(queue) > 0 {
cur := queue[0]
queue = queue[1:]
if cur.Kind() == reflect.Ptr {
cur = cur.Elem()
}
// nolint:exhaustive
switch cur.Kind() {
case reflect.Struct:
for field := range cur.Fields() {
if field.Tag.Get(tag) != "" {
return true
}
queue = append(queue, field.Type)
}
case reflect.Map, reflect.Array, reflect.Slice:
queue = append(queue, cur.Elem())
}
}
return false
}