13
13
# limitations under the License.
14
14
from newrelic .api .datastore_trace import DatastoreTrace
15
15
from newrelic .api .transaction import current_transaction
16
+ from newrelic .api .time_trace import current_trace
16
17
from newrelic .common .object_wrapper import function_wrapper , wrap_function_wrapper
17
18
from newrelic .common .package_version_utils import get_package_version_tuple
18
19
@@ -117,6 +118,7 @@ def _nr_wrapper_Elasticsearch_method_(wrapped, instance, args, kwargs):
117
118
118
119
if transaction is None :
119
120
return wrapped (* args , ** kwargs )
121
+
120
122
# When index is None, it means there is no target field
121
123
# associated with this method. Hence this method will only
122
124
# create an operation metric and no statement metric. This is
@@ -157,6 +159,11 @@ async def _nr_wrapper_AsyncElasticsearch_method_(wrapped, instance, args, kwargs
157
159
if transaction is None :
158
160
return await wrapped (* args , ** kwargs )
159
161
162
+ # When index is None, it means there is no target field
163
+ # associated with this method. Hence this method will only
164
+ # create an operation metric and no statement metric. This is
165
+ # handled by setting the target to None when calling the
166
+ # DatastoreTraceWrapper.
160
167
if arg_extractor is None :
161
168
index = None
162
169
else :
@@ -174,12 +181,13 @@ async def _nr_wrapper_AsyncElasticsearch_method_(wrapped, instance, args, kwargs
174
181
with dt :
175
182
result = await wrapped (* args , ** kwargs )
176
183
177
- # TODO Fix this
178
- instance_info = transaction ._nr_datastore_instance_info
179
- host , port_path_or_id , _ = instance_info
180
-
181
- dt .host = host
182
- dt .port_path_or_id = port_path_or_id
184
+ try :
185
+ node_config = result .meta .node
186
+ dt .host = node_config .host
187
+ port = node_config .port
188
+ dt .port_path_or_id = str (port ) if port is not None else None
189
+ except Exception :
190
+ pass
183
191
184
192
return result
185
193
@@ -817,27 +825,31 @@ def _nr_get_connection_wrapper(wrapped, instance, args, kwargs):
817
825
return conn
818
826
819
827
820
- async def _nr_get_async_connection_wrapper (wrapped , instance , args , kwargs ):
821
- """Read instance info from async Connection and stash on Transaction."""
828
+ def _nr_get_async_connection_wrapper (wrapped , instance , args , kwargs ):
829
+ """
830
+ Read instance info from async Connection and stash on Transaction.
831
+
832
+ Only necessary for elasticsearch v7 and below, as v8 supplies metadata with the response.
833
+ """
822
834
823
- transaction = current_transaction ()
835
+ trace = current_trace ()
824
836
825
- if transaction is None :
826
- return await wrapped (* args , ** kwargs )
837
+ if trace is None or not isinstance ( trace , DatastoreTrace ) :
838
+ return wrapped (* args , ** kwargs )
827
839
828
- conn = await wrapped (* args , ** kwargs )
840
+ conn = wrapped (* args , ** kwargs )
829
841
830
- instance_info = ( None , None , None )
842
+ host = port_path_or_id = "unknown"
831
843
try :
832
- tracer_settings = transaction .settings .datastore_tracer
844
+ tracer_settings = trace .settings .datastore_tracer
833
845
834
846
if tracer_settings .instance_reporting .enabled :
835
847
host , port_path_or_id = conn ._nr_host_port
836
- instance_info = (host , port_path_or_id , None )
837
848
except Exception :
838
- instance_info = ( "unknown" , "unknown" , None )
849
+ pass
839
850
840
- transaction ._nr_datastore_instance_info = instance_info
851
+ trace .host = host
852
+ trace .port_path_or_id = port_path_or_id
841
853
842
854
return conn
843
855
@@ -865,7 +877,7 @@ async def _nr_async_perform_request_wrapper(wrapped, instance, args, kwargs):
865
877
return await wrapped (* args , ** kwargs )
866
878
867
879
if not hasattr (instance .node_pool .get , "_nr_wrapped" ):
868
- instance .node_pool .get = function_wrapper (_nr_get_connection_wrapper )(instance .node_pool .get )
880
+ instance .node_pool .get = function_wrapper (_nr_get_async_connection_wrapper )(instance .node_pool .get )
869
881
instance .node_pool .get ._nr_wrapped = True
870
882
871
883
return await wrapped (* args , ** kwargs )
0 commit comments