Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Oct 21, 2024
1 parent 9289a08 commit a9786a7
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 100 deletions.
5 changes: 4 additions & 1 deletion internal/balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ func (b *Balancer) clusterDiscoveryAttempt(ctx context.Context) (err error) {
onDone = trace.DriverOnBalancerClusterDiscoveryAttempt(
b.driverConfig.Trace(), &ctx,
stack.FunctionID(
"github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer.(*Balancer).clusterDiscoveryAttempt"),
"github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer.(*Balancer).clusterDiscoveryAttempt",
),
address,
b.driverConfig.Database(),
)
endpoints []endpoint.Endpoint
localDC string
Expand Down Expand Up @@ -130,6 +132,7 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi
stack.FunctionID(
"github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer.(*Balancer).applyDiscoveredEndpoints"),
b.config.DetectNearestDC,
b.driverConfig.Database(),
)
previous = b.connections().All()
)
Expand Down
10 changes: 3 additions & 7 deletions internal/repeater/repeater.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,8 @@ func (r *repeater) Force() {
}
}

func (r *repeater) wakeUp(ctx context.Context, e Event) (err error) {
if err = ctx.Err(); err != nil {
return err
}

ctx = WithEvent(ctx, e)
func (r *repeater) wakeUp(e Event) (err error) {
ctx := WithEvent(context.Background(), e)

onDone := trace.DriverOnRepeaterWakeUp(r.trace, &ctx,
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/repeater.(*repeater).wakeUp"),
Expand Down Expand Up @@ -203,7 +199,7 @@ func (r *repeater) worker(ctx context.Context, tick clockwork.Ticker) {
if event == EventCancel {
return
}
if err := r.wakeUp(ctx, event); err != nil {
if err := r.wakeUp(event); err != nil {
forceIndex++
} else {
forceIndex = 0
Expand Down
1 change: 1 addition & 0 deletions log/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ func internalDriver(l Logger, d trace.Detailer) trace.Driver {
ctx := with(*info.Context, TRACE, "ydb", "driver", "balancer", "update")
l.Log(ctx, "start",
kv.Bool("needLocalDC", info.NeedLocalDC),
kv.String("database", info.Database),
)
start := time.Now()

Expand Down
72 changes: 0 additions & 72 deletions otel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package otel
import (
"context"

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/kv"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)

Expand All @@ -27,73 +25,3 @@ type (
Start(ctx context.Context, operationName string, attributes ...KeyValue) (context.Context, Span)
}
)

func childSpanWithReplaceCtx(
cfg Config,
ctx *context.Context,
operationName string,
fields ...KeyValue,
) (s Span) {
*ctx, s = childSpan(cfg, *ctx, operationName, fields...)

return s
}

func childSpan(
cfg Config,
ctx context.Context, //nolint:revive
operationName string,
fields ...KeyValue,
) (context.Context, Span) {
return cfg.Start(ctx,
operationName,
fields...,
)
}

func finish(
s Span,
err error,
fields ...KeyValue,
) {
if err != nil {
s.Msg(err.Error(), kv.Error(err))
}
s.End(fields...)
}

func logError(
s Span,
err error,
fields ...KeyValue,
) {
var ydbErr ydb.Error
if xerrors.As(err, &ydbErr) {
fields = append(fields,
kv.Error(err),
kv.Int("error.ydb.code", int(ydbErr.Code())),
kv.String("error.ydb.name", ydbErr.Name()),
)
}
s.Msg(err.Error(), fields...)
}

func logToParentSpan(
cfg Config,
ctx context.Context, //nolint:revive
msg string,
fields ...KeyValue, //nolint:unparam
) {
parent := cfg.SpanFromContext(ctx)
parent.Msg(msg, fields...)
}

func logToParentSpanError(
cfg Config,
ctx context.Context, //nolint:revive
err error,
fields ...KeyValue, //nolint:unparam
) {
parent := cfg.SpanFromContext(ctx)
logError(parent, err, fields...)
}
12 changes: 7 additions & 5 deletions otel/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func driver(cfg Config) trace.Driver { //nolint:gocyclo,funlen
cfg,
info.Context,
info.Call.FunctionID(),
kv.String("address", info.Address),
)

return func(info trace.DriverBalancerClusterDiscoveryAttemptDoneInfo) {
Expand All @@ -275,9 +276,11 @@ func driver(cfg Config) trace.Driver { //nolint:gocyclo,funlen
if cfg.Details()&trace.DriverBalancerEvents == 0 {
return nil
}
needLocalDC := info.NeedLocalDC
functionID := info.Call.FunctionID()
s := cfg.SpanFromContext(*info.Context)
start := childSpanWithReplaceCtx(cfg, info.Context,
info.Call.FunctionID(),
kv.String("database", info.Database),
kv.Bool("need_local_dc", info.NeedLocalDC),
)

return func(info trace.DriverBalancerUpdateDoneInfo) {
var (
Expand All @@ -294,12 +297,11 @@ func driver(cfg Config) trace.Driver { //nolint:gocyclo,funlen
for i, e := range info.Dropped {
dropped[i] = e.String()
}
s.Msg(functionID,
start.End(
kv.String("local_dc", info.LocalDC),
kv.Strings("endpoints", endpoints),
kv.Strings("added", added),
kv.Strings("dropped", dropped),
kv.Bool("need_local_dc", needLocalDC),
)
}
},
Expand Down
79 changes: 79 additions & 0 deletions otel/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package otel

import (
"context"

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/kv"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
)

func childSpanWithReplaceCtx(
cfg Config,
ctx *context.Context,
operationName string,
fields ...KeyValue,
) (s Span) {
*ctx, s = childSpan(cfg, *ctx, operationName, fields...)

return s
}

func childSpan(
cfg Config,
ctx context.Context, //nolint:revive
operationName string,
fields ...KeyValue,
) (context.Context, Span) {
return cfg.Start(ctx,
operationName,
fields...,
)
}

func finish(
s Span,
err error,
fields ...KeyValue,
) {
if err != nil {
s.Msg(err.Error(), kv.Error(err))
}
s.End(fields...)
}

func logError(
s Span,
err error,
fields ...KeyValue,
) {
var ydbErr ydb.Error
if xerrors.As(err, &ydbErr) {
fields = append(fields,
kv.Error(err),
kv.Int("error.ydb.code", int(ydbErr.Code())),
kv.String("error.ydb.name", ydbErr.Name()),
)
}
s.Msg(err.Error(), fields...)
}

func logToParentSpan(
cfg Config,
ctx context.Context, //nolint:revive
msg string,
fields ...KeyValue, //nolint:unparam
) {
parent := cfg.SpanFromContext(ctx)
parent.Msg(msg, fields...)
}

func logToParentSpanError(
cfg Config,
ctx context.Context, //nolint:revive
err error,
fields ...KeyValue, //nolint:unparam
) {
parent := cfg.SpanFromContext(ctx)
logError(parent, err, fields...)
}
20 changes: 10 additions & 10 deletions otel/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ type (
noopSpan struct{}
)

func (noopSpan) Relation(Span) {}

func (noopSpan) TraceID() string {
return ""
}

func (n noopSpan) Msg(string, ...KeyValue) {}

func (n noopSpan) End(...KeyValue) {}

func (noopConfig) Details() trace.Details {
return 0
}
Expand All @@ -37,3 +27,13 @@ func (noopConfig) SpanFromContext(context.Context) Span {
func (noopConfig) Start(ctx context.Context, _ string, _ ...KeyValue) (context.Context, Span) {
return ctx, noopSpan{}
}

func (noopSpan) Relation(Span) {}

func (noopSpan) TraceID() string {
return ""
}

func (n noopSpan) Msg(string, ...KeyValue) {}

func (n noopSpan) End(...KeyValue) {}
8 changes: 5 additions & 3 deletions trace/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ type (
Context *context.Context
Call call
NeedLocalDC bool
Database string
}
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
DriverBalancerUpdateDoneInfo struct {
Expand All @@ -203,9 +204,10 @@ 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
Call call
Address string
Context *context.Context
Call call
Address string
Database string
}
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
DriverBalancerClusterDiscoveryAttemptDoneInfo struct {
Expand Down
6 changes: 4 additions & 2 deletions trace/driver_gtrace.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a9786a7

Please sign in to comment.