Skip to content

Instantly share code, notes, and snippets.

@toffer
Created June 3, 2014 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toffer/0f9cfcc915ef3f875261 to your computer and use it in GitHub Desktop.
Save toffer/0f9cfcc915ef3f875261 to your computer and use it in GitHub Desktop.
An example of manually instrumenting a background task, without using the newrelic-admin wrapper script.
#!/usr/bin/env python
import os
import newrelic.agent
# Initialize the New Relic agent as early as possible.
config_file = os.environ.get('NEW_RELIC_CONFIG_FILE')
newrelic.agent.initialize(config_file)
# Import modules after agent initialization, so that they
# get instrumented.
import requests
def get_content(url):
response = requests.get(url)
return response.content
# Decorate the function you want to monitor. Since this is not a
# web transaction, it should be monitored as a background task.
@newrelic.agent.background_task()
def task():
print len(get_content('http://newrelic.com'))
print len(get_content('https://www.google.com'))
print len(get_content('https://github.com'))
if __name__ == '__main__':
# Force registration, so that data from the first
# transaction is captured.
newrelic.agent.register_application(timeout=10.0)
task()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment