@@ -26,7 +26,7 @@ import (
26
26
27
27
const (
28
28
// Version is the current version of Elastic.
29
- Version = "7.0.29 "
29
+ Version = "7.0.30 "
30
30
31
31
// DefaultURL is the default endpoint of Elasticsearch on the local machine.
32
32
// It is used e.g. when initializing a new Client without a specific URL.
@@ -84,6 +84,9 @@ const (
84
84
)
85
85
86
86
var (
87
+ // nilByte is used in JSON marshal/unmarshal
88
+ nilByte = []byte ("null" )
89
+
87
90
// ErrNoClient is raised when no Elasticsearch node is available.
88
91
ErrNoClient = errors .New ("no Elasticsearch node available" )
89
92
@@ -798,7 +801,7 @@ func (c *Client) Start() {
798
801
c .running = true
799
802
c .mu .Unlock ()
800
803
801
- c .infof ("elastic: client started" )
804
+ c .infof (context . Background (), "elastic: client started" )
802
805
}
803
806
804
807
// Stop stops the background processes that the client is running,
@@ -828,27 +831,39 @@ func (c *Client) Stop() {
828
831
c .running = false
829
832
c .mu .Unlock ()
830
833
831
- c .infof ("elastic: client stopped" )
834
+ c .infof (context . Background (), "elastic: client stopped" )
832
835
}
833
836
834
837
// errorf logs to the error log.
835
- func (c * Client ) errorf (format string , args ... interface {}) {
838
+ func (c * Client ) errorf (ctx context. Context , format string , args ... interface {}) {
836
839
if c .errorlog != nil {
837
- c .errorlog .Printf (format , args ... )
840
+ if logger , ok := c .errorlog .(LoggerWithContext ); ok {
841
+ logger .PrintfWithContext (ctx , format , args ... )
842
+ } else {
843
+ c .errorlog .Printf (format , args ... )
844
+ }
838
845
}
839
846
}
840
847
841
848
// infof logs informational messages.
842
- func (c * Client ) infof (format string , args ... interface {}) {
849
+ func (c * Client ) infof (ctx context. Context , format string , args ... interface {}) {
843
850
if c .infolog != nil {
844
- c .infolog .Printf (format , args ... )
851
+ if logger , ok := c .infolog .(LoggerWithContext ); ok {
852
+ logger .PrintfWithContext (ctx , format , args ... )
853
+ } else {
854
+ c .infolog .Printf (format , args ... )
855
+ }
845
856
}
846
857
}
847
858
848
859
// tracef logs to the trace log.
849
- func (c * Client ) tracef (format string , args ... interface {}) {
860
+ func (c * Client ) tracef (ctx context. Context , format string , args ... interface {}) {
850
861
if c .tracelog != nil {
851
- c .tracelog .Printf (format , args ... )
862
+ if logger , ok := c .tracelog .(LoggerWithContext ); ok {
863
+ logger .PrintfWithContext (ctx , format , args ... )
864
+ } else {
865
+ c .tracelog .Printf (format , args ... )
866
+ }
852
867
}
853
868
}
854
869
@@ -857,7 +872,7 @@ func (c *Client) dumpRequest(r *http.Request) {
857
872
if c .tracelog != nil {
858
873
out , err := httputil .DumpRequestOut (r , true )
859
874
if err == nil {
860
- c .tracef ("%s\n " , string (out ))
875
+ c .tracef (r . Context (), "%s\n " , string (out ))
861
876
}
862
877
}
863
878
}
@@ -867,7 +882,7 @@ func (c *Client) dumpResponse(resp *http.Response) {
867
882
if c .tracelog != nil {
868
883
out , err := httputil .DumpResponse (resp , true )
869
884
if err == nil {
870
- c .tracef ("%s\n " , string (out ))
885
+ c .tracef (context . Background (), "%s\n " , string (out ))
871
886
}
872
887
}
873
888
}
@@ -1055,7 +1070,7 @@ func (c *Client) updateConns(conns []*conn) {
1055
1070
}
1056
1071
if ! found {
1057
1072
// New connection didn't exist, so add it to our list of new conns.
1058
- c .infof ("elastic: %s joined the cluster" , conn .URL ())
1073
+ c .infof (context . Background (), "elastic: %s joined the cluster" , conn .URL ())
1059
1074
newConns = append (newConns , conn )
1060
1075
}
1061
1076
}
@@ -1147,19 +1162,19 @@ func (c *Client) healthcheck(parentCtx context.Context, timeout time.Duration, f
1147
1162
// Wait for the Goroutine (or its timeout)
1148
1163
select {
1149
1164
case <- ctx .Done (): // timeout
1150
- c .errorf ("elastic: %s is dead" , conn .URL ())
1165
+ c .errorf (ctx , "elastic: %s is dead" , conn .URL ())
1151
1166
conn .MarkAsDead ()
1152
1167
case err := <- errc :
1153
1168
if err != nil {
1154
- c .errorf ("elastic: %s is dead" , conn .URL ())
1169
+ c .errorf (ctx , "elastic: %s is dead" , conn .URL ())
1155
1170
conn .MarkAsDead ()
1156
1171
break
1157
1172
}
1158
1173
if status >= 200 && status < 300 {
1159
1174
conn .MarkAsAlive ()
1160
1175
} else {
1161
1176
conn .MarkAsDead ()
1162
- c .errorf ("elastic: %s is dead [status=%d]" , conn .URL (), status )
1177
+ c .errorf (ctx , "elastic: %s is dead [status=%d]" , conn .URL (), status )
1163
1178
}
1164
1179
}
1165
1180
}
@@ -1256,7 +1271,7 @@ func (c *Client) next() (*conn, error) {
1256
1271
// So we are marking them as alive--if sniffing is disabled.
1257
1272
// They'll then be picked up in the next call to PerformRequest.
1258
1273
if ! c .snifferEnabled {
1259
- c .errorf ("elastic: all %d nodes marked as dead; resurrecting them to prevent deadlock" , len (c .conns ))
1274
+ c .errorf (context . Background (), "elastic: all %d nodes marked as dead; resurrecting them to prevent deadlock" , len (c .conns ))
1260
1275
for _ , conn := range c .conns {
1261
1276
conn .MarkAsAlive ()
1262
1277
}
@@ -1380,13 +1395,13 @@ func (c *Client) PerformRequest(ctx context.Context, opt PerformRequestOptions)
1380
1395
continue // try again
1381
1396
}
1382
1397
if err != nil {
1383
- c .errorf ("elastic: cannot get connection from pool" )
1398
+ c .errorf (ctx , "elastic: cannot get connection from pool" )
1384
1399
return nil , err
1385
1400
}
1386
1401
1387
1402
req , err = NewRequest (opt .Method , conn .URL ()+ pathWithParams )
1388
1403
if err != nil {
1389
- c .errorf ("elastic: cannot create request for %s %s: %v" , strings .ToUpper (opt .Method ), conn .URL ()+ pathWithParams , err )
1404
+ c .errorf (ctx , "elastic: cannot create request for %s %s: %v" , strings .ToUpper (opt .Method ), conn .URL ()+ pathWithParams , err )
1390
1405
return nil , err
1391
1406
}
1392
1407
if basicAuth {
@@ -1415,7 +1430,7 @@ func (c *Client) PerformRequest(ctx context.Context, opt PerformRequestOptions)
1415
1430
if opt .Body != nil {
1416
1431
err = req .SetBody (opt .Body , gzipEnabled )
1417
1432
if err != nil {
1418
- c .errorf ("elastic: couldn't set body %+v for request: %v" , opt .Body , err )
1433
+ c .errorf (ctx , "elastic: couldn't set body %+v for request: %v" , opt .Body , err )
1419
1434
return nil , err
1420
1435
}
1421
1436
}
@@ -1433,12 +1448,12 @@ func (c *Client) PerformRequest(ctx context.Context, opt PerformRequestOptions)
1433
1448
n ++
1434
1449
wait , ok , rerr := retrier .Retry (ctx , n , (* http .Request )(req ), res , err )
1435
1450
if rerr != nil {
1436
- c .errorf ("elastic: %s is dead" , conn .URL ())
1451
+ c .errorf (ctx , "elastic: %s is dead" , conn .URL ())
1437
1452
conn .MarkAsDead ()
1438
1453
return nil , rerr
1439
1454
}
1440
1455
if ! ok {
1441
- c .errorf ("elastic: %s is dead" , conn .URL ())
1456
+ c .errorf (ctx , "elastic: %s is dead" , conn .URL ())
1442
1457
conn .MarkAsDead ()
1443
1458
return nil , err
1444
1459
}
@@ -1450,7 +1465,7 @@ func (c *Client) PerformRequest(ctx context.Context, opt PerformRequestOptions)
1450
1465
n ++
1451
1466
wait , ok , rerr := retrier .Retry (ctx , n , (* http .Request )(req ), res , err )
1452
1467
if rerr != nil {
1453
- c .errorf ("elastic: %s is dead" , conn .URL ())
1468
+ c .errorf (ctx , "elastic: %s is dead" , conn .URL ())
1454
1469
conn .MarkAsDead ()
1455
1470
return nil , rerr
1456
1471
}
@@ -1473,7 +1488,7 @@ func (c *Client) PerformRequest(ctx context.Context, opt PerformRequestOptions)
1473
1488
if len (res .Header ["Warning" ]) > 0 {
1474
1489
c .deprecationlog ((* http .Request )(req ), res )
1475
1490
for _ , warning := range res .Header ["Warning" ] {
1476
- c .errorf ("Deprecation warning: %s" , warning )
1491
+ c .errorf (ctx , "Deprecation warning: %s" , warning )
1477
1492
}
1478
1493
}
1479
1494
@@ -1497,7 +1512,7 @@ func (c *Client) PerformRequest(ctx context.Context, opt PerformRequestOptions)
1497
1512
}
1498
1513
1499
1514
duration := time .Now ().UTC ().Sub (start )
1500
- c .infof ("%s %s [status:%d, request:%.3fs]" ,
1515
+ c .infof (ctx , "%s %s [status:%d, request:%.3fs]" ,
1501
1516
strings .ToUpper (opt .Method ),
1502
1517
req .URL ,
1503
1518
resp .StatusCode ,
0 commit comments