github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/log/discovery.go (about)

     1  package log
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
     7  )
     8  
     9  // Discovery makes trace.Discovery with logging events from details
    10  func Discovery(l Logger, d trace.Detailer, opts ...Option) (t trace.Discovery) {
    11  	return internalDiscovery(wrapLogger(l, opts...), d)
    12  }
    13  
    14  func internalDiscovery(l Logger, d trace.Detailer) (t trace.Discovery) {
    15  	t.OnDiscover = func(info trace.DiscoveryDiscoverStartInfo) func(trace.DiscoveryDiscoverDoneInfo) {
    16  		if d.Details()&trace.DiscoveryEvents == 0 {
    17  			return nil
    18  		}
    19  		ctx := with(*info.Context, DEBUG, "ydb", "discovery", "list", "endpoints")
    20  		l.Log(ctx, "start",
    21  			String("address", info.Address),
    22  			String("database", info.Database),
    23  		)
    24  		start := time.Now()
    25  
    26  		return func(info trace.DiscoveryDiscoverDoneInfo) {
    27  			if info.Error == nil {
    28  				l.Log(WithLevel(ctx, INFO), "done",
    29  					latencyField(start),
    30  					Stringer("endpoints", endpoints(info.Endpoints)),
    31  				)
    32  			} else {
    33  				l.Log(WithLevel(ctx, ERROR), "failed",
    34  					Error(info.Error),
    35  					latencyField(start),
    36  					versionField(),
    37  				)
    38  			}
    39  		}
    40  	}
    41  	t.OnWhoAmI = func(info trace.DiscoveryWhoAmIStartInfo) func(doneInfo trace.DiscoveryWhoAmIDoneInfo) {
    42  		if d.Details()&trace.DiscoveryEvents == 0 {
    43  			return nil
    44  		}
    45  		ctx := with(*info.Context, TRACE, "ydb", "discovery", "whoAmI")
    46  		l.Log(ctx, "start")
    47  		start := time.Now()
    48  
    49  		return func(info trace.DiscoveryWhoAmIDoneInfo) {
    50  			if info.Error == nil {
    51  				l.Log(ctx, "done",
    52  					latencyField(start),
    53  					String("user", info.User),
    54  					Strings("groups", info.Groups),
    55  				)
    56  			} else {
    57  				l.Log(WithLevel(ctx, WARN), "failed",
    58  					Error(info.Error),
    59  					latencyField(start),
    60  					versionField(),
    61  				)
    62  			}
    63  		}
    64  	}
    65  
    66  	return t
    67  }