Skip to content

Commit

Permalink
Merge branch 'memory-usage-sampler-config' of github.com:newrelic/new…
Browse files Browse the repository at this point in the history
…relic-python-agent into memory-usage-sampler-config
  • Loading branch information
umaannamalai committed Apr 11, 2024
2 parents 9c5c589 + 583776c commit 2855610
Show file tree
Hide file tree
Showing 20 changed files with 783 additions and 753 deletions.
6 changes: 4 additions & 2 deletions newrelic/hooks/external_botocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def create_chat_completion_message_event(
for index, message in enumerate(output_message_list):
index += len(input_message_list)
content = message.get("content", "")
# For anthropic models run via langchain, a list is returned with a dictionary of content inside
# We only want to report the raw dictionary in the LLM message event
if isinstance(content, list) and len(content) == 1:
content = content[0]

if response_id:
id_ = "%s-%d" % (response_id, index) # Response ID was set, append message index to it.
Expand Down Expand Up @@ -262,7 +266,6 @@ def extract_bedrock_claude_model_request(request_body, bedrock_attrs):
]
else:
input_message_list = [{"role": "user", "content": request_body.get("prompt")}]

bedrock_attrs["request.max_tokens"] = request_body.get("max_tokens_to_sample")
bedrock_attrs["request.temperature"] = request_body.get("temperature")
bedrock_attrs["input_message_list"] = input_message_list
Expand All @@ -276,7 +279,6 @@ def extract_bedrock_claude_model_response(response_body, bedrock_attrs):
role = response_body.get("role", "assistant")
content = response_body.get("content") or response_body.get("completion")
output_message_list = [{"role": role, "content": content}]

bedrock_attrs["response.choices.finish_reason"] = response_body.get("stop_reason")
bedrock_attrs["output_message_list"] = output_message_list

Expand Down
2 changes: 1 addition & 1 deletion newrelic/packages/urllib3/contrib/securetransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
import threading
import weakref

import six
from ..packages import six

from .. import util
from ..util.ssl_ import PROTOCOL_TLS_CLIENT
Expand Down
2 changes: 1 addition & 1 deletion tests/agent_features/test_asgi_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import json

import pytest
import six
from bs4 import BeautifulSoup
from testing_support.asgi_testing import AsgiTest
from testing_support.fixtures import override_application_settings
Expand All @@ -34,6 +33,7 @@
get_browser_timing_header,
)
from newrelic.common.encoding_utils import deobfuscate
from newrelic.packages import six

_runtime_error_name = RuntimeError.__module__ + ":" + RuntimeError.__name__

Expand Down
2 changes: 1 addition & 1 deletion tests/agent_features/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import re
import sys

import six
import webtest
from testing_support.fixtures import override_application_settings
from testing_support.validators.validate_custom_parameters import (
Expand All @@ -35,6 +34,7 @@
from newrelic.api.web_transaction import web_transaction
from newrelic.api.wsgi_application import wsgi_application
from newrelic.common.encoding_utils import deobfuscate
from newrelic.packages import six

_runtime_error_name = RuntimeError.__module__ + ":" + RuntimeError.__name__

Expand Down
86 changes: 55 additions & 31 deletions tests/agent_features/test_browser_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,97 +13,121 @@
# limitations under the License.

import pytest
import six
import webtest
from testing_support.fixtures import (
capture_transaction_metrics,
override_application_settings,
)

from newrelic.api.wsgi_application import wsgi_application
from newrelic.packages import six

from testing_support.fixtures import (override_application_settings,
capture_transaction_metrics)

PAGE_CONTENTS = b'Hello World'
PAGE_CONTENTS = b"Hello World"

_browser_enabled_settings = {
'browser_monitoring.enabled': True,
"browser_monitoring.enabled": True,
}

_browser_disabled_settings = {
'browser_monitoring.enabled': False,
"browser_monitoring.enabled": False,
}


@wsgi_application()
def _app_list(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
status = "200 OK"
response_headers = [("Content-type", "text/plain")]
start_response(status, response_headers)
return [PAGE_CONTENTS]


target_application_list = webtest.TestApp(_app_list)


@wsgi_application()
def _app_iter(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
status = "200 OK"
response_headers = [("Content-type", "text/plain")]
start_response(status, response_headers)
yield PAGE_CONTENTS


target_application_iter = webtest.TestApp(_app_iter)


@wsgi_application()
def _app_str(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
status = "200 OK"
response_headers = [("Content-type", "text/plain")]
start_response(status, response_headers)
return PAGE_CONTENTS


target_application_str = webtest.TestApp(_app_str)


@wsgi_application()
def _app_list_exc_1(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
status = "200 OK"
response_headers = [("Content-type", "text/plain")]
start_response(status, response_headers)
1/0
1 / 0
return [PAGE_CONTENTS]


target_application_list_exc_1 = webtest.TestApp(_app_list_exc_1)


@wsgi_application()
def _app_list_exc_2(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
1/0
status = "200 OK"
response_headers = [("Content-type", "text/plain")]
1 / 0
start_response(status, response_headers)
return [PAGE_CONTENTS]


target_application_list_exc_2 = webtest.TestApp(_app_list_exc_2)


@wsgi_application()
def _app_iter_exc_1(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
status = "200 OK"
response_headers = [("Content-type", "text/plain")]
start_response(status, response_headers)
1/0
1 / 0
yield PAGE_CONTENTS


target_application_iter_exc_1 = webtest.TestApp(_app_iter_exc_1)


@wsgi_application()
def _app_iter_exc_2(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
1/0
status = "200 OK"
response_headers = [("Content-type", "text/plain")]
1 / 0
start_response(status, response_headers)
yield PAGE_CONTENTS


target_application_iter_exc_2 = webtest.TestApp(_app_iter_exc_2)

_target_applications = [
target_application_list,
target_application_iter,
pytest.param(target_application_str, marks=pytest.mark.skipif(
six.PY3, reason='PY3 webtest expects type(byte) '
'so this test doesnt apply')),
pytest.param(
target_application_str,
marks=pytest.mark.skipif(six.PY3, reason="PY3 webtest expects type(byte) " "so this test doesnt apply"),
),
target_application_list_exc_1,
target_application_list_exc_2,
target_application_iter_exc_1,
target_application_iter_exc_2,
]

@pytest.mark.parametrize('target_application', _target_applications)

@pytest.mark.parametrize("target_application", _target_applications)
def test_metrics_same_with_and_without_browser_middleware(target_application):
with_browser_metrics = []
without_browser_metrics = []
Expand All @@ -112,7 +136,7 @@ def test_metrics_same_with_and_without_browser_middleware(target_application):
@override_application_settings(_browser_enabled_settings)
def run_app_with_browser():
try:
resp = target_application.get('/')
resp = target_application.get("/")
except ZeroDivisionError:
pass
else:
Expand All @@ -122,7 +146,7 @@ def run_app_with_browser():
@override_application_settings(_browser_disabled_settings)
def run_app_without_browser():
try:
resp = target_application.get('/')
resp = target_application.get("/")
except ZeroDivisionError:
pass
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/agent_features/test_exception_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.

import pytest
import six
from testing_support.fixtures import (
reset_core_stats_engine,
set_default_encoding,
Expand All @@ -25,6 +24,7 @@
from newrelic.api.application import application_instance as application
from newrelic.api.background_task import background_task
from newrelic.api.time_trace import notice_error
from newrelic.packages import six

# Turn off black formatting for this section of the code.
# While Python 2 has been EOL'd since 2020, New Relic still
Expand Down
Loading

0 comments on commit 2855610

Please sign in to comment.