github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/log/driver.go (about) 1 package log 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/ydb-platform/ydb-go-sdk/v3/internal/secret" 8 "github.com/ydb-platform/ydb-go-sdk/v3/trace" 9 ) 10 11 // Driver makes trace.Driver with logging events from details 12 func Driver(l Logger, d trace.Detailer, opts ...Option) (t trace.Driver) { 13 return internalDriver(wrapLogger(l, opts...), d) 14 } 15 16 func internalDriver(l Logger, d trace.Detailer) (t trace.Driver) { //nolint:gocyclo 17 t.OnResolve = func( 18 info trace.DriverResolveStartInfo, 19 ) func( 20 trace.DriverResolveDoneInfo, 21 ) { 22 if d.Details()&trace.DriverResolverEvents == 0 { 23 return nil 24 } 25 ctx := with(context.Background(), TRACE, "ydb", "driver", "resolver", "update") 26 target := info.Target 27 addresses := info.Resolved 28 l.Log(ctx, "start", 29 String("target", target), 30 Strings("resolved", addresses), 31 ) 32 33 return func(info trace.DriverResolveDoneInfo) { 34 if info.Error == nil { 35 l.Log(ctx, "done", 36 String("target", target), 37 Strings("resolved", addresses), 38 ) 39 } else { 40 l.Log(WithLevel(ctx, WARN), "failed", 41 Error(info.Error), 42 String("target", target), 43 Strings("resolved", addresses), 44 versionField(), 45 ) 46 } 47 } 48 } 49 t.OnInit = func(info trace.DriverInitStartInfo) func(trace.DriverInitDoneInfo) { 50 if d.Details()&trace.DriverEvents == 0 { 51 return nil 52 } 53 endpoint := info.Endpoint 54 database := info.Database 55 secure := info.Secure 56 ctx := with(*info.Context, DEBUG, "ydb", "driver", "resolver", "init") 57 l.Log(ctx, "start", 58 String("endpoint", endpoint), 59 String("database", database), 60 Bool("secure", secure), 61 ) 62 start := time.Now() 63 64 return func(info trace.DriverInitDoneInfo) { 65 if info.Error == nil { 66 l.Log(ctx, "done", 67 String("endpoint", endpoint), 68 String("database", database), 69 Bool("secure", secure), 70 latencyField(start), 71 ) 72 } else { 73 l.Log(WithLevel(ctx, ERROR), "failed", 74 Error(info.Error), 75 String("endpoint", endpoint), 76 String("database", database), 77 Bool("secure", secure), 78 latencyField(start), 79 versionField(), 80 ) 81 } 82 } 83 } 84 t.OnClose = func(info trace.DriverCloseStartInfo) func(trace.DriverCloseDoneInfo) { 85 if d.Details()&trace.DriverEvents == 0 { 86 return nil 87 } 88 ctx := with(*info.Context, TRACE, "ydb", "driver", "resolver", "close") 89 l.Log(ctx, "start") 90 start := time.Now() 91 92 return func(info trace.DriverCloseDoneInfo) { 93 if info.Error == nil { 94 l.Log(ctx, "done", 95 latencyField(start), 96 ) 97 } else { 98 l.Log(WithLevel(ctx, WARN), "failed", 99 Error(info.Error), 100 latencyField(start), 101 versionField(), 102 ) 103 } 104 } 105 } 106 t.OnConnDial = func(info trace.DriverConnDialStartInfo) func(trace.DriverConnDialDoneInfo) { 107 if d.Details()&trace.DriverConnEvents == 0 { 108 return nil 109 } 110 ctx := with(*info.Context, TRACE, "ydb", "driver", "conn", "dial") 111 endpoint := info.Endpoint 112 l.Log(ctx, "start", 113 Stringer("endpoint", endpoint), 114 ) 115 start := time.Now() 116 117 return func(info trace.DriverConnDialDoneInfo) { 118 if info.Error == nil { 119 l.Log(ctx, "done", 120 Stringer("endpoint", endpoint), 121 latencyField(start), 122 ) 123 } else { 124 l.Log(WithLevel(ctx, WARN), "failed", 125 Error(info.Error), 126 Stringer("endpoint", endpoint), 127 latencyField(start), 128 versionField(), 129 ) 130 } 131 } 132 } 133 t.OnConnStateChange = func(info trace.DriverConnStateChangeStartInfo) func(trace.DriverConnStateChangeDoneInfo) { 134 if d.Details()&trace.DriverConnEvents == 0 { 135 return nil 136 } 137 ctx := with(context.Background(), TRACE, "ydb", "driver", "conn", "state", "change") 138 endpoint := info.Endpoint 139 l.Log(ctx, "start", 140 Stringer("endpoint", endpoint), 141 Stringer("state", info.State), 142 ) 143 start := time.Now() 144 145 return func(info trace.DriverConnStateChangeDoneInfo) { 146 l.Log(ctx, "done", 147 Stringer("endpoint", endpoint), 148 latencyField(start), 149 Stringer("state", info.State), 150 ) 151 } 152 } 153 t.OnConnPark = func(info trace.DriverConnParkStartInfo) func(trace.DriverConnParkDoneInfo) { 154 if d.Details()&trace.DriverConnEvents == 0 { 155 return nil 156 } 157 ctx := with(*info.Context, TRACE, "ydb", "driver", "conn", "park") 158 endpoint := info.Endpoint 159 l.Log(ctx, "start", 160 Stringer("endpoint", endpoint), 161 ) 162 start := time.Now() 163 164 return func(info trace.DriverConnParkDoneInfo) { 165 if info.Error == nil { 166 l.Log(ctx, "done", 167 Stringer("endpoint", endpoint), 168 latencyField(start), 169 ) 170 } else { 171 l.Log(WithLevel(ctx, WARN), "failed", 172 Error(info.Error), 173 Stringer("endpoint", endpoint), 174 latencyField(start), 175 versionField(), 176 ) 177 } 178 } 179 } 180 t.OnConnClose = func(info trace.DriverConnCloseStartInfo) func(trace.DriverConnCloseDoneInfo) { 181 if d.Details()&trace.DriverConnEvents == 0 { 182 return nil 183 } 184 ctx := with(*info.Context, TRACE, "ydb", "driver", "conn", "close") 185 endpoint := info.Endpoint 186 l.Log(ctx, "start", 187 Stringer("endpoint", endpoint), 188 ) 189 start := time.Now() 190 191 return func(info trace.DriverConnCloseDoneInfo) { 192 if info.Error == nil { 193 l.Log(ctx, "done", 194 Stringer("endpoint", endpoint), 195 latencyField(start), 196 ) 197 } else { 198 l.Log(WithLevel(ctx, WARN), "failed", 199 Error(info.Error), 200 Stringer("endpoint", endpoint), 201 latencyField(start), 202 versionField(), 203 ) 204 } 205 } 206 } 207 t.OnConnInvoke = func(info trace.DriverConnInvokeStartInfo) func(trace.DriverConnInvokeDoneInfo) { 208 if d.Details()&trace.DriverConnEvents == 0 { 209 return nil 210 } 211 ctx := with(*info.Context, TRACE, "ydb", "driver", "conn", "invoke") 212 endpoint := info.Endpoint 213 method := string(info.Method) 214 l.Log(ctx, "start", 215 Stringer("endpoint", endpoint), 216 String("method", method), 217 ) 218 start := time.Now() 219 220 return func(info trace.DriverConnInvokeDoneInfo) { 221 if info.Error == nil { 222 l.Log(ctx, "done", 223 Stringer("endpoint", endpoint), 224 String("method", method), 225 latencyField(start), 226 Stringer("metadata", metadata(info.Metadata)), 227 ) 228 } else { 229 l.Log(WithLevel(ctx, WARN), "failed", 230 Error(info.Error), 231 Stringer("endpoint", endpoint), 232 String("method", method), 233 latencyField(start), 234 Stringer("metadata", metadata(info.Metadata)), 235 versionField(), 236 ) 237 } 238 } 239 } 240 t.OnConnNewStream = func( 241 info trace.DriverConnNewStreamStartInfo, 242 ) func( 243 trace.DriverConnNewStreamRecvInfo, 244 ) func( 245 trace.DriverConnNewStreamDoneInfo, 246 ) { 247 if d.Details()&trace.DriverConnEvents == 0 { 248 return nil 249 } 250 ctx := with(*info.Context, TRACE, "ydb", "driver", "conn", "new", "stream") 251 endpoint := info.Endpoint 252 method := string(info.Method) 253 l.Log(ctx, "start", 254 Stringer("endpoint", endpoint), 255 String("method", method), 256 ) 257 start := time.Now() 258 259 return func(info trace.DriverConnNewStreamRecvInfo) func(trace.DriverConnNewStreamDoneInfo) { 260 if info.Error == nil { 261 l.Log(ctx, "intermediate receive", 262 Stringer("endpoint", endpoint), 263 String("method", method), 264 latencyField(start), 265 ) 266 } else { 267 l.Log(WithLevel(ctx, WARN), "intermediate fail", 268 Error(info.Error), 269 Stringer("endpoint", endpoint), 270 String("method", method), 271 latencyField(start), 272 versionField(), 273 ) 274 } 275 276 return func(info trace.DriverConnNewStreamDoneInfo) { 277 if info.Error == nil { 278 l.Log(ctx, "done", 279 Stringer("endpoint", endpoint), 280 String("method", method), 281 latencyField(start), 282 Stringer("metadata", metadata(info.Metadata)), 283 ) 284 } else { 285 l.Log(WithLevel(ctx, WARN), "failed", 286 Error(info.Error), 287 Stringer("endpoint", endpoint), 288 String("method", method), 289 latencyField(start), 290 Stringer("metadata", metadata(info.Metadata)), 291 versionField(), 292 ) 293 } 294 } 295 } 296 } 297 t.OnConnBan = func(info trace.DriverConnBanStartInfo) func(trace.DriverConnBanDoneInfo) { 298 if d.Details()&trace.DriverConnEvents == 0 { 299 return nil 300 } 301 ctx := with(*info.Context, TRACE, "ydb", "driver", "conn", "ban") 302 endpoint := info.Endpoint 303 l.Log(ctx, "start", 304 Stringer("endpoint", endpoint), 305 NamedError("cause", info.Cause), 306 versionField(), 307 ) 308 start := time.Now() 309 310 return func(info trace.DriverConnBanDoneInfo) { 311 l.Log(WithLevel(ctx, WARN), "done", 312 Stringer("endpoint", endpoint), 313 latencyField(start), 314 Stringer("state", info.State), 315 versionField(), 316 ) 317 } 318 } 319 t.OnConnAllow = func(info trace.DriverConnAllowStartInfo) func(trace.DriverConnAllowDoneInfo) { 320 if d.Details()&trace.DriverConnEvents == 0 { 321 return nil 322 } 323 ctx := with(*info.Context, TRACE, "ydb", "driver", "conn", "allow") 324 endpoint := info.Endpoint 325 l.Log(ctx, "start", 326 Stringer("endpoint", endpoint), 327 ) 328 start := time.Now() 329 330 return func(info trace.DriverConnAllowDoneInfo) { 331 l.Log(ctx, "done", 332 Stringer("endpoint", endpoint), 333 latencyField(start), 334 Stringer("state", info.State), 335 ) 336 } 337 } 338 t.OnRepeaterWakeUp = func(info trace.DriverRepeaterWakeUpStartInfo) func(trace.DriverRepeaterWakeUpDoneInfo) { 339 if d.Details()&trace.DriverRepeaterEvents == 0 { 340 return nil 341 } 342 ctx := with(*info.Context, TRACE, "ydb", "driver", "repeater", "wake", "up") 343 name := info.Name 344 event := info.Event 345 l.Log(ctx, "start", 346 String("name", name), 347 String("event", event), 348 ) 349 start := time.Now() 350 351 return func(info trace.DriverRepeaterWakeUpDoneInfo) { 352 if info.Error == nil { 353 l.Log(ctx, "done", 354 String("name", name), 355 String("event", event), 356 latencyField(start), 357 ) 358 } else { 359 l.Log(WithLevel(ctx, ERROR), "failed", 360 Error(info.Error), 361 String("name", name), 362 String("event", event), 363 latencyField(start), 364 versionField(), 365 ) 366 } 367 } 368 } 369 t.OnBalancerInit = func(info trace.DriverBalancerInitStartInfo) func(trace.DriverBalancerInitDoneInfo) { 370 if d.Details()&trace.DriverBalancerEvents == 0 { 371 return nil 372 } 373 ctx := with(*info.Context, TRACE, "ydb", "driver", "balancer", "init") 374 l.Log(ctx, "start") 375 start := time.Now() 376 377 return func(info trace.DriverBalancerInitDoneInfo) { 378 l.Log(WithLevel(ctx, INFO), "done", 379 latencyField(start), 380 ) 381 } 382 } 383 t.OnBalancerClose = func(info trace.DriverBalancerCloseStartInfo) func(trace.DriverBalancerCloseDoneInfo) { 384 if d.Details()&trace.DriverBalancerEvents == 0 { 385 return nil 386 } 387 ctx := with(*info.Context, TRACE, "ydb", "driver", "balancer", "close") 388 l.Log(ctx, "start") 389 start := time.Now() 390 391 return func(info trace.DriverBalancerCloseDoneInfo) { 392 if info.Error == nil { 393 l.Log(ctx, "done", 394 latencyField(start), 395 ) 396 } else { 397 l.Log(WithLevel(ctx, WARN), "failed", 398 Error(info.Error), 399 latencyField(start), 400 versionField(), 401 ) 402 } 403 } 404 } 405 t.OnBalancerChooseEndpoint = func( 406 info trace.DriverBalancerChooseEndpointStartInfo, 407 ) func( 408 trace.DriverBalancerChooseEndpointDoneInfo, 409 ) { 410 if d.Details()&trace.DriverBalancerEvents == 0 { 411 return nil 412 } 413 ctx := with(*info.Context, TRACE, "ydb", "driver", "balancer", "choose", "endpoint") 414 l.Log(ctx, "start") 415 start := time.Now() 416 417 return func(info trace.DriverBalancerChooseEndpointDoneInfo) { 418 if info.Error == nil { 419 l.Log(ctx, "done", 420 latencyField(start), 421 Stringer("endpoint", info.Endpoint), 422 ) 423 } else { 424 l.Log(WithLevel(ctx, ERROR), "failed", 425 Error(info.Error), 426 latencyField(start), 427 versionField(), 428 ) 429 } 430 } 431 } 432 t.OnBalancerUpdate = func( 433 info trace.DriverBalancerUpdateStartInfo, 434 ) func( 435 trace.DriverBalancerUpdateDoneInfo, 436 ) { 437 if d.Details()&trace.DriverBalancerEvents == 0 { 438 return nil 439 } 440 ctx := with(*info.Context, TRACE, "ydb", "driver", "balancer", "update") 441 l.Log(ctx, "start", 442 Bool("needLocalDC", info.NeedLocalDC), 443 ) 444 start := time.Now() 445 446 return func(info trace.DriverBalancerUpdateDoneInfo) { 447 l.Log(ctx, "done", 448 latencyField(start), 449 Stringer("endpoints", endpoints(info.Endpoints)), 450 Stringer("added", endpoints(info.Added)), 451 Stringer("dropped", endpoints(info.Dropped)), 452 String("detectedLocalDC", info.LocalDC), 453 ) 454 } 455 } 456 t.OnGetCredentials = func(info trace.DriverGetCredentialsStartInfo) func(trace.DriverGetCredentialsDoneInfo) { 457 if d.Details()&trace.DriverCredentialsEvents == 0 { 458 return nil 459 } 460 ctx := with(*info.Context, TRACE, "ydb", "driver", "credentials", "get") 461 l.Log(ctx, "start") 462 start := time.Now() 463 464 return func(info trace.DriverGetCredentialsDoneInfo) { 465 if info.Error == nil { 466 l.Log(ctx, "done", 467 latencyField(start), 468 String("token", secret.Token(info.Token)), 469 ) 470 } else { 471 l.Log(WithLevel(ctx, ERROR), "done", 472 Error(info.Error), 473 latencyField(start), 474 String("token", secret.Token(info.Token)), 475 versionField(), 476 ) 477 } 478 } 479 } 480 481 return t 482 }