diff --git a/CHANGELOG.md b/CHANGELOG.md index bfefa12af..572b7de27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +* Renamed `{retry,table}.WithID` option to `{retry,table}.WithLabel` * Added `ydb.WithTraceRetry` option * Moved `internal/allocator.Buffers` to package `internal/xstring` * Bumped `golang.org/x/sync` to `v0.3.0` diff --git a/internal/table/retry.go b/internal/table/retry.go index 7bd6d79a9..33dea8fc7 100644 --- a/internal/table/retry.go +++ b/internal/table/retry.go @@ -46,12 +46,8 @@ func doTx( if opts.Trace == nil { opts.Trace = &trace.Table{} } - attempts, onIntermediate := 0, trace.TableOnDoTx( - opts.Trace, - &ctx, - opts.ID, - opts.Idempotent, - isRetryCalledAbove(ctx), + attempts, onIntermediate := 0, trace.TableOnDoTx(opts.Trace, &ctx, + opts.Label, opts.Label, opts.Idempotent, isRetryCalledAbove(ctx), ) defer func() { onIntermediate(err)(attempts, err) @@ -119,7 +115,9 @@ func do( if opts.Trace == nil { opts.Trace = &trace.Table{} } - attempts, onIntermediate := 0, trace.TableOnDo(opts.Trace, &ctx, opts.ID, opts.Idempotent, isRetryCalledAbove(ctx)) + attempts, onIntermediate := 0, trace.TableOnDo(opts.Trace, &ctx, + opts.Label, opts.Label, opts.Idempotent, isRetryCalledAbove(ctx), + ) defer func() { onIntermediate(err)(attempts, err) }() diff --git a/log/retry.go b/log/retry.go index e7ec38f19..4f0c582b6 100644 --- a/log/retry.go +++ b/log/retry.go @@ -25,17 +25,17 @@ func internalRetry(l *wrapper, d trace.Detailer) (t trace.Retry) { return nil } ctx := with(*info.Context, TRACE, "ydb", "retry") - id := info.ID + label := info.Label idempotent := info.Idempotent l.Log(ctx, "start", - String("id", id), + String("label", label), Bool("idempotent", idempotent), ) start := time.Now() return func(info trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) { if info.Error == nil { l.Log(ctx, "attempt done", - String("id", id), + String("label", label), latencyField(start), ) } else { @@ -46,7 +46,7 @@ func internalRetry(l *wrapper, d trace.Detailer) (t trace.Retry) { m := retry.Check(info.Error) l.Log(WithLevel(ctx, lvl), "attempt failed", Error(info.Error), - String("id", id), + String("label", label), latencyField(start), Bool("retryable", m.MustRetry(idempotent)), Int64("code", m.StatusCode()), @@ -57,7 +57,7 @@ func internalRetry(l *wrapper, d trace.Detailer) (t trace.Retry) { return func(info trace.RetryLoopDoneInfo) { if info.Error == nil { l.Log(ctx, "done", - String("id", id), + String("label", label), latencyField(start), Int("attempts", info.Attempts), ) @@ -69,7 +69,7 @@ func internalRetry(l *wrapper, d trace.Detailer) (t trace.Retry) { m := retry.Check(info.Error) l.Log(WithLevel(ctx, lvl), "failed", Error(info.Error), - String("id", id), + String("label", label), latencyField(start), Int("attempts", info.Attempts), Bool("retryable", m.MustRetry(idempotent)), diff --git a/log/table.go b/log/table.go index 9e70d5183..0570dcbf1 100644 --- a/log/table.go +++ b/log/table.go @@ -28,8 +28,10 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { } ctx := with(*info.Context, TRACE, "ydb", "table", "do") idempotent := info.Idempotent + label := info.Label l.Log(ctx, "start", Bool("idempotent", idempotent), + String("label", label), ) start := time.Now() return func(info trace.TableDoIntermediateInfo) func(trace.TableDoDoneInfo) { @@ -37,6 +39,7 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { l.Log(ctx, "done", latencyField(start), Bool("idempotent", idempotent), + String("label", label), ) } else { lvl := WARN @@ -47,6 +50,7 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { l.Log(WithLevel(ctx, lvl), "failed", latencyField(start), Bool("idempotent", idempotent), + String("label", label), Error(info.Error), Bool("retryable", m.MustRetry(idempotent)), Int64("code", m.StatusCode()), @@ -59,6 +63,7 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { l.Log(ctx, "done", latencyField(start), Bool("idempotent", idempotent), + String("label", label), Int("attempts", info.Attempts), ) } else { @@ -70,6 +75,7 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { l.Log(WithLevel(ctx, lvl), "done", latencyField(start), Bool("idempotent", idempotent), + String("label", label), Int("attempts", info.Attempts), Error(info.Error), Bool("retryable", m.MustRetry(idempotent)), @@ -93,8 +99,10 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { } ctx := with(*info.Context, TRACE, "ydb", "table", "do", "tx") idempotent := info.Idempotent + label := info.Label l.Log(ctx, "start", Bool("idempotent", idempotent), + String("label", label), ) start := time.Now() return func(info trace.TableDoTxIntermediateInfo) func(trace.TableDoTxDoneInfo) { @@ -102,6 +110,7 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { l.Log(ctx, "done", latencyField(start), Bool("idempotent", idempotent), + String("label", label), ) } else { lvl := ERROR @@ -112,6 +121,7 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { l.Log(WithLevel(ctx, lvl), "done", latencyField(start), Bool("idempotent", idempotent), + String("label", label), Error(info.Error), Bool("retryable", m.MustRetry(idempotent)), Int64("code", m.StatusCode()), @@ -124,6 +134,7 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { l.Log(ctx, "done", latencyField(start), Bool("idempotent", idempotent), + String("label", label), Int("attempts", info.Attempts), ) } else { @@ -135,6 +146,7 @@ func internalTable(l *wrapper, d trace.Detailer) (t trace.Table) { l.Log(WithLevel(ctx, lvl), "done", latencyField(start), Bool("idempotent", idempotent), + String("label", label), Int("attempts", info.Attempts), Error(info.Error), Bool("retryable", m.MustRetry(idempotent)), diff --git a/metrics/table.go b/metrics/table.go index 961bcabcf..01c2dd9f2 100644 --- a/metrics/table.go +++ b/metrics/table.go @@ -36,14 +36,14 @@ func table(config Config) (t trace.Table) { trace.TableDoDoneInfo, ) { var ( - name = info.ID + label = info.Label start = time.Now() ) return func(info trace.TableDoIntermediateInfo) func(trace.TableDoDoneInfo) { if info.Error != nil && config.Details()&trace.TableEvents != 0 { doIntermediateErrors.With(map[string]string{ "status": errorBrief(info.Error), - "name": name, + "label": label, }).Inc() } return func(info trace.TableDoDoneInfo) { @@ -51,11 +51,11 @@ func table(config Config) (t trace.Table) { doAttempts.With(nil).Record(float64(info.Attempts)) doErrors.With(map[string]string{ "status": errorBrief(info.Error), - "name": name, + "label": label, }).Inc() doLatency.With(map[string]string{ "status": errorBrief(info.Error), - "name": name, + "label": label, }).Record(time.Since(start)) } } @@ -67,14 +67,14 @@ func table(config Config) (t trace.Table) { trace.TableDoTxDoneInfo, ) { var ( - name = info.ID + label = info.Label start = time.Now() ) return func(info trace.TableDoTxIntermediateInfo) func(trace.TableDoTxDoneInfo) { if info.Error != nil && config.Details()&trace.TableEvents != 0 { doTxIntermediateErrors.With(map[string]string{ "status": errorBrief(info.Error), - "name": name, + "label": label, }).Inc() } return func(info trace.TableDoTxDoneInfo) { @@ -82,11 +82,11 @@ func table(config Config) (t trace.Table) { doTxAttempts.With(nil).Record(float64(info.Attempts)) doTxErrors.With(map[string]string{ "status": errorBrief(info.Error), - "name": name, + "label": label, }).Inc() doTxLatency.With(map[string]string{ "status": errorBrief(info.Error), - "name": name, + "label": label, }).Record(time.Since(start)) } } diff --git a/retry/retry.go b/retry/retry.go index 2cada7b09..ad5be538e 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -17,7 +17,7 @@ import ( type retryOperation func(context.Context) (err error) type retryOptions struct { - id string + label string trace *trace.Retry idempotent bool stackTrace bool @@ -31,25 +31,25 @@ type Option interface { ApplyRetryOption(opts *retryOptions) } -var _ Option = idOption("") +var _ Option = labelOption("") -type idOption string +type labelOption string -func (id idOption) ApplyDoOption(opts *doOptions) { - opts.retryOptions = append(opts.retryOptions, WithID(string(id))) +func (label labelOption) ApplyDoOption(opts *doOptions) { + opts.retryOptions = append(opts.retryOptions, WithLabel(string(label))) } -func (id idOption) ApplyDoTxOption(opts *doTxOptions) { - opts.retryOptions = append(opts.retryOptions, WithID(string(id))) +func (label labelOption) ApplyDoTxOption(opts *doTxOptions) { + opts.retryOptions = append(opts.retryOptions, WithLabel(string(label))) } -func (id idOption) ApplyRetryOption(opts *retryOptions) { - opts.id = string(id) +func (label labelOption) ApplyRetryOption(opts *retryOptions) { + opts.label = string(label) } -// WithID applies id for identification call Retry in trace.Retry.OnRetry -func WithID(id string) idOption { - return idOption(id) +// WithLabel applies label for identification call Retry in trace.Retry.OnRetry +func WithLabel(label string) labelOption { + return labelOption(label) } var _ Option = stackTraceOption{} @@ -241,7 +241,9 @@ func Retry(ctx context.Context, op retryOperation, opts ...Option) (err error) { attempts int code = int64(0) - onIntermediate = trace.RetryOnRetry(options.trace, &ctx, options.id, options.idempotent, isRetryCalledAbove(ctx)) + onIntermediate = trace.RetryOnRetry(options.trace, &ctx, + options.label, options.label, options.idempotent, isRetryCalledAbove(ctx), + ) ) defer func() { onIntermediate(err)(attempts, err) diff --git a/retry/sql.go b/retry/sql.go index 8df0ad1cd..f921755e4 100644 --- a/retry/sql.go +++ b/retry/sql.go @@ -20,7 +20,7 @@ type doOption interface { var ( _ doOption = doRetryOptionsOption(nil) - _ doOption = idOption("") + _ doOption = labelOption("") ) type doRetryOptionsOption []Option diff --git a/table/table.go b/table/table.go index 171b3b5e6..953f557e9 100644 --- a/table/table.go +++ b/table/table.go @@ -560,7 +560,7 @@ func ValueParam(name string, v types.Value) ParameterOption { } type Options struct { - ID string + Label string Idempotent bool TxSettings *TransactionSettings TxCommitOptions []options.CommitTransactionOption @@ -572,17 +572,17 @@ type Option interface { ApplyTableOption(opts *Options) } -var _ Option = idOption("") +var _ Option = labelOption("") -type idOption string +type labelOption string -func (id idOption) ApplyTableOption(opts *Options) { - opts.ID = string(id) - opts.RetryOptions = append(opts.RetryOptions, retry.WithID(string(id))) +func (label labelOption) ApplyTableOption(opts *Options) { + opts.Label = string(label) + opts.RetryOptions = append(opts.RetryOptions, retry.WithLabel(string(label))) } -func WithID(id string) idOption { - return idOption(id) +func WithLabel(label string) labelOption { + return labelOption(label) } var _ Option = retryOptionsOption{} diff --git a/tests/integration/with_trace_retry_test.go b/tests/integration/with_trace_retry_test.go index 0d27ecf06..42bc1958b 100644 --- a/tests/integration/with_trace_retry_test.go +++ b/tests/integration/with_trace_retry_test.go @@ -29,7 +29,7 @@ func TestWithTraceRetry(t *testing.T) { OnRetry: func( info trace.RetryLoopStartInfo, ) func(trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) { - retryCalled[info.ID] = true + retryCalled[info.Label] = true return nil }, }), @@ -40,14 +40,14 @@ func TestWithTraceRetry(t *testing.T) { func(ctx context.Context, s table.Session) error { return nil }, - table.WithID("db.Table().Do"), + table.WithLabel("db.Table().Do"), )) require.NoError(t, db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error { return nil }, - table.WithID("db.Table().DoTx"), + table.WithLabel("db.Table().DoTx"), )) for _, key := range []string{ @@ -67,7 +67,7 @@ func TestWithTraceRetry(t *testing.T) { OnRetry: func( info trace.RetryLoopStartInfo, ) func(trace.RetryLoopIntermediateInfo) func(trace.RetryLoopDoneInfo) { - retryCalled[info.ID] = true + retryCalled[info.Label] = true return nil }, }), @@ -78,14 +78,14 @@ func TestWithTraceRetry(t *testing.T) { func(ctx context.Context, cc *sql.Conn) error { return nil }, - retry.WithID("retry.Do"), + retry.WithLabel("retry.Do"), )) require.NoError(t, retry.DoTx(ctx, db, func(ctx context.Context, tx *sql.Tx) error { return nil }, - retry.WithID("retry.DoTx"), + retry.WithLabel("retry.DoTx"), )) for _, key := range []string{ diff --git a/trace/retry.go b/trace/retry.go index 4daa7e6f5..6626429a8 100644 --- a/trace/retry.go +++ b/trace/retry.go @@ -19,8 +19,12 @@ type ( // Pointer to context provide replacement of context in trace callback function. // Warning: concurrent access to pointer on client side must be excluded. // Safe replacement of context are provided only inside callback function - Context *context.Context - ID string + Context *context.Context + + // Deprecated: use Label field instead + ID string + + Label string Idempotent bool NestedCall bool // flag when Retry called inside head Retry } diff --git a/trace/retry_gtrace.go b/trace/retry_gtrace.go index ee8f7c75a..b2fcb3719 100644 --- a/trace/retry_gtrace.go +++ b/trace/retry_gtrace.go @@ -110,10 +110,11 @@ func (t *Retry) onRetry(r RetryLoopStartInfo) func(RetryLoopIntermediateInfo) fu return res } } -func RetryOnRetry(t *Retry, c *context.Context, iD string, idempotent bool, nestedCall bool) func(error) func(attempts int, _ error) { +func RetryOnRetry(t *Retry, c *context.Context, iD string, label string, idempotent bool, nestedCall bool) func(error) func(attempts int, _ error) { var p RetryLoopStartInfo p.Context = c p.ID = iD + p.Label = label p.Idempotent = idempotent p.NestedCall = nestedCall res := t.onRetry(p) diff --git a/trace/table.go b/trace/table.go index 8fd59d2a1..049ef55d7 100644 --- a/trace/table.go +++ b/trace/table.go @@ -378,8 +378,12 @@ type ( // Pointer to context provide replacement of context in trace callback function. // Warning: concurrent access to pointer on client side must be excluded. // Safe replacement of context are provided only inside callback function - Context *context.Context - ID string + Context *context.Context + + // Deprecated: use Label field instead + ID string + + Label string Idempotent bool NestedCall bool // flag when Retry called inside head Retry } @@ -395,8 +399,12 @@ type ( // Pointer to context provide replacement of context in trace callback function. // Warning: concurrent access to pointer on client side must be excluded. // Safe replacement of context are provided only inside callback function - Context *context.Context - ID string + Context *context.Context + + // Deprecated: use Label field instead + ID string + + Label string Idempotent bool NestedCall bool // flag when Retry called inside head Retry } diff --git a/trace/table_gtrace.go b/trace/table_gtrace.go index 706859753..a34fc178d 100644 --- a/trace/table_gtrace.go +++ b/trace/table_gtrace.go @@ -1420,10 +1420,11 @@ func TableOnClose(t *Table, c *context.Context) func(error) { res(p) } } -func TableOnDo(t *Table, c *context.Context, iD string, idempotent bool, nestedCall bool) func(error) func(attempts int, _ error) { +func TableOnDo(t *Table, c *context.Context, iD string, label string, idempotent bool, nestedCall bool) func(error) func(attempts int, _ error) { var p TableDoStartInfo p.Context = c p.ID = iD + p.Label = label p.Idempotent = idempotent p.NestedCall = nestedCall res := t.onDo(p) @@ -1439,10 +1440,11 @@ func TableOnDo(t *Table, c *context.Context, iD string, idempotent bool, nestedC } } } -func TableOnDoTx(t *Table, c *context.Context, iD string, idempotent bool, nestedCall bool) func(error) func(attempts int, _ error) { +func TableOnDoTx(t *Table, c *context.Context, iD string, label string, idempotent bool, nestedCall bool) func(error) func(attempts int, _ error) { var p TableDoTxStartInfo p.Context = c p.ID = iD + p.Label = label p.Idempotent = idempotent p.NestedCall = nestedCall res := t.onDoTx(p)