44import logging
55from collections import namedtuple
66
7+ import elasticsearch
78import wrapt
89
910from scout_apm .compat import get_pos_args , unwrap_decorators
1011from scout_apm .core .tracked_request import TrackedRequest
1112
1213try :
13- from elasticsearch import Elasticsearch , Transport
14+ from elasticsearch import Elasticsearch
1415except ImportError : # pragma: no cover
1516 Elasticsearch = None
16- Transport = None
17+
18+ try :
19+ # Transport was moved to elastic_transport as of v8.0.0
20+ from elastic_transport import Transport
21+ except ImportError : # pragma: no cover
22+ try :
23+ from elasticsearch import Transport
24+ except ImportError : # pragma: no cover
25+ Transport = None
1726
1827logger = logging .getLogger (__name__ )
1928
2029
2130def ensure_installed ():
2231 logger .debug ("Instrumenting elasticsearch." )
2332
24- if Elasticsearch is None :
25- logger .debug (
26- "Couldn't import elasticsearch.Elasticsearch - probably not installed."
27- )
33+ if Elasticsearch is None or Transport is None :
34+ if Elasticsearch is None :
35+ logger .debug (
36+ "Couldn't import elasticsearch.Elasticsearch "
37+ "- probably not installed."
38+ )
39+ if Transport is None :
40+ logger .debug (
41+ "Couldn't import elasticsearch.Transport or "
42+ "elastic_transport.Transport - probably not installed."
43+ )
2844 else :
2945 ensure_client_instrumented ()
3046 ensure_transport_instrumented ()
3147
3248
3349ClientMethod = namedtuple ("ClientMethod" , ["name" , "takes_index_argument" ])
3450
35- CLIENT_METHODS = [
36- ClientMethod ("bulk" , True ),
37- ClientMethod ("clear_scroll" , False ),
38- ClientMethod ("close" , False ),
39- ClientMethod ("close_point_in_time" , False ),
40- ClientMethod ("count" , True ),
41- ClientMethod ("create" , True ),
42- ClientMethod ("delete" , True ),
43- ClientMethod ("delete_by_query" , True ),
44- ClientMethod ("delete_by_query_rethrottle" , False ),
45- ClientMethod ("delete_script" , False ),
46- ClientMethod ("exists" , True ),
47- ClientMethod ("exists_source" , True ),
48- ClientMethod ("explain" , True ),
49- ClientMethod ("field_caps" , True ),
50- ClientMethod ("get" , True ),
51- ClientMethod ("get_script" , False ),
52- ClientMethod ("get_script_context" , False ),
53- ClientMethod ("get_script_languages" , False ),
54- ClientMethod ("get_source" , True ),
55- ClientMethod ("index" , True ),
56- ClientMethod ("info" , False ),
57- ClientMethod ("mget" , True ),
58- ClientMethod ("msearch" , True ),
59- ClientMethod ("msearch_template" , True ),
60- ClientMethod ("mtermvectors" , True ),
61- ClientMethod ("open_point_in_time" , True ),
62- ClientMethod ("ping" , False ),
63- ClientMethod ("put_script" , False ),
64- ClientMethod ("rank_eval" , True ),
65- ClientMethod ("reindex" , False ),
66- ClientMethod ("reindex_rethrottle" , False ),
67- ClientMethod ("render_search_template" , False ),
68- ClientMethod ("scripts_painless_execute" , False ),
69- ClientMethod ("scroll" , False ),
70- ClientMethod ("search" , True ),
71- ClientMethod ("search_mvt" , True ),
72- ClientMethod ("search_shards" , True ),
73- ClientMethod ("search_template" , True ),
74- ClientMethod ("termvectors" , True ),
75- ClientMethod ("terms_enum" , True ),
76- ClientMethod ("update" , True ),
77- ClientMethod ("update_by_query" , True ),
78- ClientMethod ("update_by_query_rethrottle" , False ),
79- ]
51+ VERSIONED_CLIENT_METHODS = {
52+ "v7" : [
53+ ClientMethod ("bulk" , True ),
54+ ClientMethod ("clear_scroll" , False ),
55+ ClientMethod ("close" , False ),
56+ ClientMethod ("close_point_in_time" , False ),
57+ ClientMethod ("count" , True ),
58+ ClientMethod ("create" , True ),
59+ ClientMethod ("delete" , True ),
60+ ClientMethod ("delete_by_query" , True ),
61+ ClientMethod ("delete_by_query_rethrottle" , False ),
62+ ClientMethod ("delete_script" , False ),
63+ ClientMethod ("exists" , True ),
64+ ClientMethod ("exists_source" , True ),
65+ ClientMethod ("explain" , True ),
66+ ClientMethod ("field_caps" , True ),
67+ ClientMethod ("get" , True ),
68+ ClientMethod ("get_script" , False ),
69+ ClientMethod ("get_script_context" , False ),
70+ ClientMethod ("get_script_languages" , False ),
71+ ClientMethod ("get_source" , True ),
72+ ClientMethod ("index" , True ),
73+ ClientMethod ("info" , False ),
74+ ClientMethod ("mget" , True ),
75+ ClientMethod ("msearch" , True ),
76+ ClientMethod ("msearch_template" , True ),
77+ ClientMethod ("mtermvectors" , True ),
78+ ClientMethod ("open_point_in_time" , True ),
79+ ClientMethod ("ping" , False ),
80+ ClientMethod ("put_script" , False ),
81+ ClientMethod ("rank_eval" , True ),
82+ ClientMethod ("reindex" , False ),
83+ ClientMethod ("reindex_rethrottle" , False ),
84+ ClientMethod ("render_search_template" , False ),
85+ ClientMethod ("scripts_painless_execute" , False ),
86+ ClientMethod ("scroll" , False ),
87+ ClientMethod ("search" , True ),
88+ ClientMethod ("search_mvt" , True ),
89+ ClientMethod ("search_shards" , True ),
90+ ClientMethod ("search_template" , True ),
91+ ClientMethod ("termvectors" , True ),
92+ ClientMethod ("terms_enum" , True ),
93+ ClientMethod ("update" , True ),
94+ ClientMethod ("update_by_query" , True ),
95+ ClientMethod ("update_by_query_rethrottle" , False ),
96+ ],
97+ "v8" : [
98+ ClientMethod ("knn_search" , True ),
99+ ],
100+ }
101+
102+ CLIENT_METHODS = VERSIONED_CLIENT_METHODS ["v7" ][:]
103+ if elasticsearch .VERSION > (8 , 0 , 0 ):
104+ CLIENT_METHODS += VERSIONED_CLIENT_METHODS ["v8" ]
80105
81106
82107have_patched_client = False
@@ -185,8 +210,11 @@ def _sanitize_name(name):
185210 "bench" ,
186211 "bulk" ,
187212 "count" ,
213+ "delete_by_query" ,
214+ "execute" ,
188215 "exists" ,
189216 "explain" ,
217+ "field_caps" ,
190218 "field_stats" ,
191219 "health" ,
192220 "mget" ,
@@ -195,14 +223,21 @@ def _sanitize_name(name):
195223 "msearch" ,
196224 "mtermvectors" ,
197225 "percolate" ,
226+ "pit" ,
198227 "query" ,
228+ "rank_eval" ,
229+ "reindex" ,
230+ "script_context" ,
231+ "script_language" ,
199232 "scroll" ,
200233 "search_shards" ,
201234 "source" ,
202235 "suggest" ,
203236 "template" ,
204237 "termvectors" ,
238+ "terms_enum" ,
205239 "update" ,
240+ "update_by_query" ,
206241 "search" ,
207242 )
208243 if op in known_names :
0 commit comments