Created
June 3, 2014 17:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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