51
51
52
52
53
53
def normalize_arguments (doc_type , body = None ):
54
- if major_version == 7 :
55
- return { "document" : body } if body else {}
56
- return (
57
- { "body" : body , "doc_type" : doc_type }
58
- if body
59
- else { "doc_type" : doc_type }
60
- )
54
+ if major_version < 7 :
55
+ return (
56
+ { "body" : body , "doc_type" : doc_type }
57
+ if body
58
+ else { "doc_type" : doc_type }
59
+ )
60
+ return { "document" : body } if body else {}
61
61
62
62
63
63
def get_elasticsearch_client (* args , ** kwargs ):
64
64
client = Elasticsearch (* args , ** kwargs )
65
- if major_version == 7 :
65
+ if major_version == 8 :
66
+ client ._verified_elasticsearch = True
67
+ elif major_version == 7 :
66
68
client .transport ._verified_elasticsearch = True
67
69
return client
68
70
69
71
70
- @mock .patch (
71
- "elasticsearch.connection.http_urllib3.Urllib3HttpConnection.perform_request"
72
- )
72
+ @mock .patch (helpers .perform_request_mock_path )
73
73
class TestElasticsearchIntegration (TestBase ):
74
74
search_attributes = {
75
75
SpanAttributes .DB_SYSTEM : "elasticsearch" ,
@@ -96,7 +96,7 @@ def tearDown(self):
96
96
ElasticsearchInstrumentor ().uninstrument ()
97
97
98
98
def test_instrumentor (self , request_mock ):
99
- request_mock .return_value = ( 1 , {}, "{}" )
99
+ request_mock .return_value = helpers . mock_response ( "{}" )
100
100
101
101
es = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
102
102
es .index (
@@ -147,7 +147,7 @@ def test_prefix_arg(self, request_mock):
147
147
prefix = "prefix-from-env"
148
148
ElasticsearchInstrumentor ().uninstrument ()
149
149
ElasticsearchInstrumentor (span_name_prefix = prefix ).instrument ()
150
- request_mock .return_value = ( 1 , {}, "{}" )
150
+ request_mock .return_value = helpers . mock_response ( "{}" )
151
151
self ._test_prefix (prefix )
152
152
153
153
def test_prefix_env (self , request_mock ):
@@ -156,7 +156,7 @@ def test_prefix_env(self, request_mock):
156
156
os .environ [env_var ] = prefix
157
157
ElasticsearchInstrumentor ().uninstrument ()
158
158
ElasticsearchInstrumentor ().instrument ()
159
- request_mock .return_value = ( 1 , {}, "{}" )
159
+ request_mock .return_value = helpers . mock_response ( "{}" )
160
160
del os .environ [env_var ]
161
161
self ._test_prefix (prefix )
162
162
@@ -174,10 +174,8 @@ def _test_prefix(self, prefix):
174
174
self .assertTrue (span .name .startswith (prefix ))
175
175
176
176
def test_result_values (self , request_mock ):
177
- request_mock .return_value = (
178
- 1 ,
179
- {},
180
- '{"found": false, "timed_out": true, "took": 7}' ,
177
+ request_mock .return_value = helpers .mock_response (
178
+ '{"found": false, "timed_out": true, "took": 7}'
181
179
)
182
180
es = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
183
181
es .get (
@@ -201,8 +199,11 @@ def test_trace_error_unknown(self, request_mock):
201
199
202
200
def test_trace_error_not_found (self , request_mock ):
203
201
msg = "record not found"
204
- exc = elasticsearch .exceptions .NotFoundError (404 , msg )
205
- request_mock .return_value = (1 , {}, "{}" )
202
+ if major_version == 8 :
203
+ exc = elasticsearch .exceptions .NotFoundError (404 , msg , body = None )
204
+ else :
205
+ exc = elasticsearch .exceptions .NotFoundError (404 , msg )
206
+ request_mock .return_value = helpers .mock_response ("{}" )
206
207
request_mock .side_effect = exc
207
208
self ._test_trace_error (StatusCode .ERROR , exc )
208
209
@@ -227,7 +228,7 @@ def _test_trace_error(self, code, exc):
227
228
)
228
229
229
230
def test_parent (self , request_mock ):
230
- request_mock .return_value = ( 1 , {}, "{}" )
231
+ request_mock .return_value = helpers . mock_response ( "{}" )
231
232
es = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
232
233
with self .tracer .start_as_current_span ("parent" ):
233
234
es .index (
@@ -245,7 +246,7 @@ def test_parent(self, request_mock):
245
246
self .assertEqual (child .parent .span_id , parent .context .span_id )
246
247
247
248
def test_multithread (self , request_mock ):
248
- request_mock .return_value = ( 1 , {}, "{}" )
249
+ request_mock .return_value = helpers . mock_response ( "{}" )
249
250
es = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
250
251
ev = threading .Event ()
251
252
@@ -292,7 +293,9 @@ def target2():
292
293
self .assertIsNone (s3 .parent )
293
294
294
295
def test_dsl_search (self , request_mock ):
295
- request_mock .return_value = (1 , {}, '{"hits": {"hits": []}}' )
296
+ request_mock .return_value = helpers .mock_response (
297
+ '{"hits": {"hits": []}}'
298
+ )
296
299
297
300
client = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
298
301
search = Search (using = client , index = "test-index" ).filter (
@@ -310,7 +313,9 @@ def test_dsl_search(self, request_mock):
310
313
)
311
314
312
315
def test_dsl_search_sanitized (self , request_mock ):
313
- request_mock .return_value = (1 , {}, '{"hits": {"hits": []}}' )
316
+ request_mock .return_value = helpers .mock_response (
317
+ '{"hits": {"hits": []}}'
318
+ )
314
319
client = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
315
320
search = Search (using = client , index = "test-index" ).filter (
316
321
"term" , author = "testing"
@@ -327,7 +332,7 @@ def test_dsl_search_sanitized(self, request_mock):
327
332
)
328
333
329
334
def test_dsl_create (self , request_mock ):
330
- request_mock .return_value = ( 1 , {}, "{}" )
335
+ request_mock .return_value = helpers . mock_response ( "{}" )
331
336
client = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
332
337
Article .init (using = client )
333
338
@@ -354,7 +359,7 @@ def test_dsl_create(self, request_mock):
354
359
)
355
360
356
361
def test_dsl_create_sanitized (self , request_mock ):
357
- request_mock .return_value = ( 1 , {}, "{}" )
362
+ request_mock .return_value = helpers . mock_response ( "{}" )
358
363
client = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
359
364
Article .init (using = client )
360
365
@@ -370,7 +375,9 @@ def test_dsl_create_sanitized(self, request_mock):
370
375
)
371
376
372
377
def test_dsl_index (self , request_mock ):
373
- request_mock .return_value = (1 , {}, helpers .dsl_index_result [2 ])
378
+ request_mock .return_value = helpers .mock_response (
379
+ helpers .dsl_index_result [2 ]
380
+ )
374
381
375
382
client = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
376
383
article = Article (
@@ -404,6 +411,9 @@ def test_request_hook(self, request_mock):
404
411
request_hook_kwargs_attribute = "request_hook.kwargs"
405
412
406
413
def request_hook (span , method , url , kwargs ):
414
+ # FIXME: this is done only to get tests passing, need a more clueful solution
415
+ if major_version == 8 :
416
+ kwargs = dict (kwargs ["headers" ])
407
417
attributes = {
408
418
request_hook_method_attribute : method ,
409
419
request_hook_url_attribute : url ,
@@ -416,10 +426,8 @@ def request_hook(span, method, url, kwargs):
416
426
ElasticsearchInstrumentor ().uninstrument ()
417
427
ElasticsearchInstrumentor ().instrument (request_hook = request_hook )
418
428
419
- request_mock .return_value = (
420
- 1 ,
421
- {},
422
- '{"found": false, "timed_out": true, "took": 7}' ,
429
+ request_mock .return_value = helpers .mock_response (
430
+ '{"found": false, "timed_out": true, "took": 7}'
423
431
)
424
432
es = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
425
433
index = "test-index"
@@ -439,12 +447,19 @@ def request_hook(span, method, url, kwargs):
439
447
"GET" , spans [0 ].attributes [request_hook_method_attribute ]
440
448
)
441
449
expected_url = f"/{ index } /_doc/{ doc_id } "
450
+ if major_version == 8 :
451
+ expected_url += "?realtime=true&refresh=true"
442
452
self .assertEqual (
443
453
expected_url ,
444
454
spans [0 ].attributes [request_hook_url_attribute ],
445
455
)
446
456
447
- if major_version == 7 :
457
+ if major_version == 8 :
458
+ # FIXME: kwargs passed to request_hook on 8 are completely different
459
+ expected_kwargs = {
460
+ "accept" : "application/vnd.elasticsearch+json; compatible-with=8"
461
+ }
462
+ elif major_version == 7 :
448
463
expected_kwargs = {
449
464
** kwargs ,
450
465
"headers" : {"accept" : "application/json" },
@@ -461,6 +476,9 @@ def test_response_hook(self, request_mock):
461
476
462
477
def response_hook (span , response ):
463
478
if span and span .is_recording ():
479
+ # FIXME: something more clean
480
+ if major_version == 8 :
481
+ response = response .body
464
482
span .set_attribute (
465
483
response_attribute_name , json .dumps (response )
466
484
)
@@ -492,7 +510,9 @@ def response_hook(span, response):
492
510
},
493
511
}
494
512
495
- request_mock .return_value = (1 , {}, json .dumps (response_payload ))
513
+ request_mock .return_value = helpers .mock_response (
514
+ json .dumps (response_payload )
515
+ )
496
516
es = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
497
517
es .get (
498
518
index = "test-index" , ** normalize_arguments (doc_type = "_doc" ), id = 1
@@ -512,7 +532,7 @@ def test_no_op_tracer_provider(self, request_mock):
512
532
tracer_provider = trace .NoOpTracerProvider ()
513
533
)
514
534
response_payload = '{"found": false, "timed_out": true, "took": 7}'
515
- request_mock .return_value = ( 1 , {}, response_payload )
535
+ request_mock .return_value = helpers . mock_response ( response_payload )
516
536
es = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
517
537
res = es .get (
518
538
index = "test-index" , ** normalize_arguments (doc_type = "_doc" ), id = 1
@@ -543,7 +563,7 @@ def test_body_sanitization(self, _):
543
563
)
544
564
545
565
def test_bulk (self , request_mock ):
546
- request_mock .return_value = ( 1 , {}, "{}" )
566
+ request_mock .return_value = helpers . mock_response ( "{}" )
547
567
548
568
es = get_elasticsearch_client (hosts = ["http://localhost:9200" ])
549
569
es .bulk (
0 commit comments