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

     1  package log
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
     7  )
     8  
     9  // Scripting returns trace.Scripting with logging events from details
    10  func Scripting(l Logger, d trace.Detailer, opts ...Option) (t trace.Scripting) {
    11  	return internalScripting(wrapLogger(l, opts...), d)
    12  }
    13  
    14  func internalScripting(l *wrapper, d trace.Detailer) (t trace.Scripting) {
    15  	t.OnExecute = func(info trace.ScriptingExecuteStartInfo) func(trace.ScriptingExecuteDoneInfo) {
    16  		if d.Details()&trace.ScriptingEvents == 0 {
    17  			return nil
    18  		}
    19  		ctx := with(*info.Context, TRACE, "ydb", "scripting", "execute")
    20  		l.Log(ctx, "start")
    21  		start := time.Now()
    22  
    23  		return func(info trace.ScriptingExecuteDoneInfo) {
    24  			if info.Error == nil {
    25  				l.Log(ctx, "done",
    26  					latencyField(start),
    27  					Int("resultSetCount", info.Result.ResultSetCount()),
    28  					NamedError("resultSetError", info.Result.Err()),
    29  				)
    30  			} else {
    31  				l.Log(WithLevel(ctx, ERROR), "failed",
    32  					Error(info.Error),
    33  					latencyField(start),
    34  					versionField(),
    35  				)
    36  			}
    37  		}
    38  	}
    39  	t.OnExplain = func(info trace.ScriptingExplainStartInfo) func(trace.ScriptingExplainDoneInfo) {
    40  		if d.Details()&trace.ScriptingEvents == 0 {
    41  			return nil
    42  		}
    43  		ctx := with(*info.Context, TRACE, "ydb", "scripting", "explain")
    44  		l.Log(ctx, "start")
    45  		start := time.Now()
    46  
    47  		return func(info trace.ScriptingExplainDoneInfo) {
    48  			if info.Error == nil {
    49  				l.Log(ctx, "done",
    50  					latencyField(start),
    51  					String("plan", info.Plan),
    52  				)
    53  			} else {
    54  				l.Log(WithLevel(ctx, ERROR), "failed",
    55  					Error(info.Error),
    56  					latencyField(start),
    57  					versionField(),
    58  				)
    59  			}
    60  		}
    61  	}
    62  	t.OnStreamExecute = func(
    63  		info trace.ScriptingStreamExecuteStartInfo,
    64  	) func(
    65  		trace.ScriptingStreamExecuteIntermediateInfo,
    66  	) func(
    67  		trace.ScriptingStreamExecuteDoneInfo,
    68  	) {
    69  		if d.Details()&trace.ScriptingEvents == 0 {
    70  			return nil
    71  		}
    72  		ctx := with(*info.Context, TRACE, "ydb", "scripting", "stream", "execute")
    73  		query := info.Query
    74  		l.Log(ctx, "start",
    75  			appendFieldByCondition(l.logQuery,
    76  				String("query", query),
    77  			)...,
    78  		)
    79  		start := time.Now()
    80  
    81  		return func(
    82  			info trace.ScriptingStreamExecuteIntermediateInfo,
    83  		) func(
    84  			trace.ScriptingStreamExecuteDoneInfo,
    85  		) {
    86  			if info.Error == nil {
    87  				l.Log(ctx, "intermediate")
    88  			} else {
    89  				l.Log(WithLevel(ctx, WARN), "intermediate failed",
    90  					Error(info.Error),
    91  					versionField(),
    92  				)
    93  			}
    94  
    95  			return func(info trace.ScriptingStreamExecuteDoneInfo) {
    96  				if info.Error == nil {
    97  					l.Log(ctx, "done",
    98  						appendFieldByCondition(l.logQuery,
    99  							String("query", query),
   100  							latencyField(start),
   101  						)...,
   102  					)
   103  				} else {
   104  					l.Log(WithLevel(ctx, ERROR), "failed",
   105  						appendFieldByCondition(l.logQuery,
   106  							String("query", query),
   107  							Error(info.Error),
   108  							latencyField(start),
   109  							versionField(),
   110  						)...,
   111  					)
   112  				}
   113  			}
   114  		}
   115  	}
   116  	t.OnClose = func(info trace.ScriptingCloseStartInfo) func(trace.ScriptingCloseDoneInfo) {
   117  		if d.Details()&trace.ScriptingEvents == 0 {
   118  			return nil
   119  		}
   120  		ctx := with(*info.Context, TRACE, "ydb", "scripting", "close")
   121  		l.Log(ctx, "start")
   122  		start := time.Now()
   123  
   124  		return func(info trace.ScriptingCloseDoneInfo) {
   125  			if info.Error == nil {
   126  				l.Log(ctx, "done",
   127  					latencyField(start),
   128  				)
   129  			} else {
   130  				l.Log(WithLevel(ctx, WARN), "failed",
   131  					Error(info.Error),
   132  					latencyField(start),
   133  					versionField(),
   134  				)
   135  			}
   136  		}
   137  	}
   138  
   139  	return t
   140  }