Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate add_custom_parameter(s) API #655

Merged
merged 7 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 48 additions & 51 deletions newrelic/admin/validate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ def _run_validation_test():
from newrelic.api.error_trace import error_trace
from newrelic.api.external_trace import external_trace
from newrelic.api.function_trace import function_trace
from newrelic.api.transaction import add_custom_parameter
from newrelic.api.time_trace import notice_error
from newrelic.api.transaction import add_custom_attribute
from newrelic.api.wsgi_application import wsgi_application

@external_trace(library='test',
url='http://localhost/test', method='GET')
@external_trace(library="test", url="http://localhost/test", method="GET")
def _external1():
time.sleep(0.1)

@function_trace(label='label',
params={'fun-key-1': '1', 'fun-key-2': 2, 'fun-key-3': 3.0})
@function_trace(label="label", params={"fun-key-1": "1", "fun-key-2": 2, "fun-key-3": 3.0})
def _function1():
_external1()

Expand All @@ -47,33 +45,29 @@ def _function2():
@error_trace()
@function_trace()
def _function3():
add_custom_parameter('txn-key-1', 1)
add_custom_attribute("txn-key-1", 1)

_function4()

raise RuntimeError('This is a test error and can be ignored.')
raise RuntimeError("This is a test error and can be ignored.")

@function_trace()
def _function4(params=None, application=None):
try:
_function5()
except:
notice_error(attributes=(params or {
'err-key-2': 2, 'err-key-3': 3.0}),
application=application)
notice_error(attributes=(params or {"err-key-2": 2, "err-key-3": 3.0}), application=application)

@function_trace()
def _function5():
raise NotImplementedError(
'This is a test error and can be ignored.')
raise NotImplementedError("This is a test error and can be ignored.")

@wsgi_application()
def _wsgi_application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
status = "200 OK"
output = "Hello World!"

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
response_headers = [("Content-type", "text/plain"), ("Content-Length", str(len(output)))]
start_response(status, response_headers)

for i in range(10):
Expand Down Expand Up @@ -107,16 +101,15 @@ def _background_task():
def _start_response(*args):
pass

_environ = { 'SCRIPT_NAME': '', 'PATH_INFO': '/test',
'QUERY_STRING': 'key=value' }
_environ = {"SCRIPT_NAME": "", "PATH_INFO": "/test", "QUERY_STRING": "key=value"}

_iterable = _wsgi_application(_environ, _start_response)
_iterable.close()

_background_task()

_function4(params={'err-key-4': 4, 'err-key-5': 5.0},
application=application())
_function4(params={"err-key-4": 4, "err-key-5": 5.0}, application=application())


_user_message = """
Running Python agent test.
Expand All @@ -136,19 +129,23 @@ def _start_response(*args):
data to the New Relic UI.
"""

@command('validate-config', 'config_file [log_file]',
"""Validates the syntax of <config_file>. Also tests connectivity to New

@command(
"validate-config",
"config_file [log_file]",
"""Validates the syntax of <config_file>. Also tests connectivity to New
Relic core application by connecting to the account corresponding to the
license key listed in the configuration file, and reporting test data under
the application name 'Python Agent Test'.""")
the application name 'Python Agent Test'.""",
)
def validate_config(args):
import logging
import os
import sys
import logging
import time

if len(args) == 0:
usage('validate-config')
usage("validate-config")
sys.exit(1)

from newrelic.api.application import register_application
Expand All @@ -158,7 +155,7 @@ def validate_config(args):
if len(args) >= 2:
log_file = args[1]
else:
log_file = '/tmp/python-agent-test.log'
log_file = "/tmp/python-agent-test.log" # nosec

log_level = logging.DEBUG

Expand All @@ -168,21 +165,20 @@ def validate_config(args):
pass

config_file = args[0]
environment = os.environ.get('NEW_RELIC_ENVIRONMENT')
environment = os.environ.get("NEW_RELIC_ENVIRONMENT")

if config_file == '-':
config_file = os.environ.get('NEW_RELIC_CONFIG_FILE')
if config_file == "-":
config_file = os.environ.get("NEW_RELIC_CONFIG_FILE")

initialize(config_file, environment, ignore_errors=False,
log_file=log_file, log_level=log_level)
initialize(config_file, environment, ignore_errors=False, log_file=log_file, log_level=log_level)

_logger = logging.getLogger(__name__)

_logger.debug('Starting agent validation.')
_logger.debug("Starting agent validation.")

_settings = global_settings()

app_name = os.environ.get('NEW_RELIC_TEST_APP_NAME', 'Python Agent Test')
app_name = os.environ.get("NEW_RELIC_TEST_APP_NAME", "Python Agent Test")

_settings.app_name = app_name
_settings.transaction_tracer.transaction_threshold = 0
Expand All @@ -194,17 +190,17 @@ def validate_config(args):

print(_user_message % dict(app_name=app_name, log_file=log_file))

_logger.debug('Register test application.')
_logger.debug("Register test application.")

_logger.debug('Collector host is %r.', _settings.host)
_logger.debug('Collector port is %r.', _settings.port)
_logger.debug("Collector host is %r.", _settings.host)
_logger.debug("Collector port is %r.", _settings.port)

_logger.debug('Proxy scheme is %r.', _settings.proxy_scheme)
_logger.debug('Proxy host is %r.', _settings.proxy_host)
_logger.debug('Proxy port is %r.', _settings.proxy_port)
_logger.debug('Proxy user is %r.', _settings.proxy_user)
_logger.debug("Proxy scheme is %r.", _settings.proxy_scheme)
_logger.debug("Proxy host is %r.", _settings.proxy_host)
_logger.debug("Proxy port is %r.", _settings.proxy_port)
_logger.debug("Proxy user is %r.", _settings.proxy_user)

_logger.debug('License key is %r.', _settings.license_key)
_logger.debug("License key is %r.", _settings.license_key)

_timeout = 30.0

Expand All @@ -215,24 +211,25 @@ def validate_config(args):
_duration = _end - _start

if not _application.active:
_logger.error('Unable to register application for test, '
'connection could not be established within %s seconds.',
_timeout)
_logger.error(
"Unable to register application for test, " "connection could not be established within %s seconds.",
_timeout,
)
return

if hasattr(_application.settings, 'messages'):
if hasattr(_application.settings, "messages"):
for message in _application.settings.messages:
if message['message'].startswith('Reporting to:'):
parts = message['message'].split('Reporting to:')
if message["message"].startswith("Reporting to:"):
parts = message["message"].split("Reporting to:")
url = parts[1].strip()
print('Registration successful. Reporting to:')
print("Registration successful. Reporting to:")
print()
print(' %s' % url)
print(" %s" % url)
print()
break

_logger.debug('Registration took %s seconds.', _duration)
_logger.debug("Registration took %s seconds.", _duration)

_logger.debug('Run the validation test.')
_logger.debug("Run the validation test.")

_run_validation_test()
Loading