Skip to content

Commit 734bf7a

Browse files
committed
TEMP
1 parent c85310e commit 734bf7a

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

newrelic/hooks/datastore_elasticsearch.py

+30-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
from newrelic.api.datastore_trace import DatastoreTrace
1515
from newrelic.api.transaction import current_transaction
16+
from newrelic.api.time_trace import current_trace
1617
from newrelic.common.object_wrapper import function_wrapper, wrap_function_wrapper
1718
from newrelic.common.package_version_utils import get_package_version_tuple
1819

@@ -117,6 +118,7 @@ def _nr_wrapper_Elasticsearch_method_(wrapped, instance, args, kwargs):
117118

118119
if transaction is None:
119120
return wrapped(*args, **kwargs)
121+
120122
# When index is None, it means there is no target field
121123
# associated with this method. Hence this method will only
122124
# create an operation metric and no statement metric. This is
@@ -157,6 +159,11 @@ async def _nr_wrapper_AsyncElasticsearch_method_(wrapped, instance, args, kwargs
157159
if transaction is None:
158160
return await wrapped(*args, **kwargs)
159161

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.
160167
if arg_extractor is None:
161168
index = None
162169
else:
@@ -174,12 +181,13 @@ async def _nr_wrapper_AsyncElasticsearch_method_(wrapped, instance, args, kwargs
174181
with dt:
175182
result = await wrapped(*args, **kwargs)
176183

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
183191

184192
return result
185193

@@ -817,27 +825,31 @@ def _nr_get_connection_wrapper(wrapped, instance, args, kwargs):
817825
return conn
818826

819827

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+
"""
822834

823-
transaction = current_transaction()
835+
trace = current_trace()
824836

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)
827839

828-
conn = await wrapped(*args, **kwargs)
840+
conn = wrapped(*args, **kwargs)
829841

830-
instance_info = (None, None, None)
842+
host = port_path_or_id = "unknown"
831843
try:
832-
tracer_settings = transaction.settings.datastore_tracer
844+
tracer_settings = trace.settings.datastore_tracer
833845

834846
if tracer_settings.instance_reporting.enabled:
835847
host, port_path_or_id = conn._nr_host_port
836-
instance_info = (host, port_path_or_id, None)
837848
except Exception:
838-
instance_info = ("unknown", "unknown", None)
849+
pass
839850

840-
transaction._nr_datastore_instance_info = instance_info
851+
trace.host = host
852+
trace.port_path_or_id = port_path_or_id
841853

842854
return conn
843855

@@ -865,7 +877,7 @@ async def _nr_async_perform_request_wrapper(wrapped, instance, args, kwargs):
865877
return await wrapped(*args, **kwargs)
866878

867879
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)
869881
instance.node_pool.get._nr_wrapped = True
870882

871883
return await wrapped(*args, **kwargs)

tests/datastore_elasticsearch/test_async_transport.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
from newrelic.api.transaction import current_transaction
2121

2222
try:
23-
from elasticsearch.connection.http_aiohttp import AiohttpHttpConnection
24-
from elasticsearch.connection.http_httpx import HttpxHttpConnection
25-
from elasticsearch.transport import AsyncTransport, Transport
23+
from elasticsearch._async.http_aiohttp import AIOHttpConnection
24+
from elasticsearch._async.transport import AsyncTransport
25+
HttpxAsyncHttpNode = None # Not implemented in v7
2626

2727
NodeConfig = dict
2828
except ImportError:
2929
from elastic_transport._async_transport import AsyncTransport
3030
from elastic_transport._models import NodeConfig
31-
from elastic_transport._node._http_aiohttp import AiohttpHttpNode as AiohttpHttpConnection
32-
from elastic_transport._node._http_httpx import HttpxAsyncHttpNode as HttpxHttpConnection
31+
from elastic_transport._node._http_aiohttp import AiohttpHttpNode as AIOHttpConnection
32+
from elastic_transport._node._http_httpx import HttpxAsyncHttpNode
3333

3434

3535
IS_V8 = ES_VERSION >= (8,)
@@ -56,19 +56,19 @@
5656
"transport_kwargs, perform_request_kwargs",
5757
[
5858
pytest.param(
59-
{"node_class": AiohttpHttpConnection},
59+
{"node_class": AIOHttpConnection},
6060
{"headers": HEADERS, "body": DATA},
61-
id="AiohttpHttpConnectionV8",
61+
id="AIOHttpConnectionV8",
6262
marks=RUN_IF_V8,
6363
),
6464
pytest.param(
65-
{"node_class": HttpxHttpConnection},
65+
{"node_class": HttpxAsyncHttpNode},
6666
{"headers": HEADERS, "body": DATA},
67-
id="HttpxHttpConnectionV8",
67+
id="HttpxAsyncHttpNodeV8",
6868
marks=RUN_IF_V8,
6969
),
7070
pytest.param(
71-
{"node_class": AiohttpHttpConnection}, {"body": DATA}, id="AiohttpHttpConnectionV7", marks=RUN_IF_V7
71+
{"node_class": AIOHttpConnection}, {"body": DATA}, id="AIOHttpConnectionV7", marks=RUN_IF_V7
7272
),
7373
],
7474
)

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ deps =
262262
datastore_cassandradriver-cassandralatest: twisted
263263
datastore_elasticsearch: requests
264264
datastore_elasticsearch: httpx
265-
datastore_elasticsearch-elasticsearch07: elasticsearch<8.0
265+
datastore_elasticsearch-elasticsearch07: elasticsearch[async]<8.0
266266
datastore_elasticsearch-elasticsearch08: elasticsearch[async]<9.0
267267
datastore_firestore: google-cloud-firestore
268268
datastore_memcache-memcached01: python-memcached<2

0 commit comments

Comments
 (0)