There is a function called record_exception() that you can use if you want to send an error to newrelic.
The question is how to record an error when you don't have an exception in hand? I just want to decide somewhere in the code to record an event to newrelic.
To record generic events that are not errors, you can create a custom metric and then use record_custom_metric, like this:
newrelic.agent.record_custom_metric('Custom/MyValue', 42)
Please see the naming reference to make sure you are naming the metric correctly.
Related
I want to update this Lambda function to python3.9 from 3.6. I want to create a test event to make sure everything still works after I do this. I'm new to Lambda and this function was created before my time. Any help or a point in the right direction would be greatly appreciated.
Below is the function code:
I would recommend that you look at recent CloudWatch Logs for this Lambda function. The code is logging the received event (which can be one of 2 types: Create and Update). Try to find a log for each event type. Then fabricate some test events from those logs.
And don't rely on Lambda function test events because they are not persistent (as you have seen). Create your own 'real' tests and revision-control them like code.
I have a program that works in 2 copies of the game. They both write logs to two different files under two different accounts.
How can I read these logs in real time and as soon as both accounts have the message "match_id:" in a certain line and the same id perform actions?
I could not find anything similar, can you tell me how I can implement this in Python?
You need logging.Handler. Override the emit(record) function to detect when a specific output happens. Note that you may need inspect.currentframe() to get the function that is currently running.
Is it possible to add a Custom Message globally on runtime-erros? I would like to have a time-stamp as this would help figuring out if a file eventually was written by that execution process.
Replacing sys.excepthook with an appropriate function will allow you to do whatever you like upon every occurrence of an uncaught exception.
Take a look at the Python docs (2, 3) on handling exceptions. You can catch the RuntimeError and print the original message along with a custom timestamp. For more information on accessing the stack trace and exception messages, check out this question.
Ignacio's solution is also great if you'd like to set the message globally.
What is my requirement ?
--> I need Exception notifier which will email to some specific configured user, about any sort of exception occurring in plain python app and web.py.
I want something similar to this http://matharvard.ca/posts/2011/jul/31/exception-notification-for-rails-3/
Is there anything same sort present ??
Please reply asap.
Thanks.
You can get what you want by:
Wrapping your code in try..except clause.
Using logging module to log the exceptions with a certain level of severity e.g ERROR.
Setting an SMTPHandler for exceptions of and above certain level.
This way is quite flexible. Your messages can be send to several places (like log files) and you can reconfigure your settings easily.
If you are not using any python heavy weight framework, try: https://github.com/fossilet/exception-notifier , it seems to be similar to the Rails' Exception notification, but quite simple.
If you are using django, seems you can use its built-in feature:https://docs.djangoproject.com/en/dev/howto/error-reporting/ ( and also see this: https://github.com/pinax/django-notification)
If using tornado, try this: https://github.com/kanevski/tornadotoad this is the most similar solution in python comparing to rails. )
You can overwrite the excepthook function from the sys module, and handle any uncought exceptions there.
I have the same requirement with you. I write a simple module to mail uncaught exceptions to the developers, as well as to record them to log files. It is used in our teams' cron scripts written in Python. Hope it can be useful for you too.
For my python/django site I need to build a "dashboard" that will update me on the status of dozens of error/heartbeat/unexpected events going on.
There are a few types of "events" that I'm currently tracking by having the Django site send emails to the admin accounts:
1) Something that normally should happen goes wrong. We synch files to different services and other machines every few hours and I send error emails when this goes wrong.
2) When something that should happen actually happens. Sometimes events in item #1 fail so horribly that they don't even send emails (try: except: around an event should always work, but things can get deleted from the crontab, the system configuration can get knocked askew where things won't run, etc. where I won't even get an error email and the lack of a success/heartbeat email will let me know something that should have happened didn't happen.)
3) When anything unexpected happens. We've made a lot of assumptions on how backend operations will run and if any of these assumptions are violated (e.g. we find two users who have the same email address) we want to know about it. These events aren't necessarily errors, more like warnings to investigate.
So I want to build a dashboard that I can easily update from python/django to give me a bird's eye view of all of these types of activity so I can stop sending hundreds of emails out per week (which is already unmanageble.)
Sounds like you want to create a basic logging system that outputs to a web page.
So you could write something simple app called, say, systemevents that creates an Event record each time something happens on the site. You'd add a signal hook so that anywhere in the rest of the site you could code something like:
from systemevents.signals import record_event
...
try:
# code goes here
except Exception, inst:
record_event("Error occurred while taunting %s: %s" % (obj, inst,), type="Error")
else:
record_event("Successfully taunted %s" % (obj, ), type="Success")
Then you can pretty easily create a view that lists these events.
However, keep in mind that this is adding a layer of complexity that is highly problematic. What if the error lies in your database? Then each time you try to record an error event, another error occurs!
Far better to use something like a built-in logging system to create a text-based log file, then whip up something that can import that text file and lay it out in a somewhat more readable fashion.
One more tip: in order to change how Django handles exceptions, you have to write a custom view for 500 errors. If using systemevents, you'd write something like:
from django.views.defaults import server_error
def custom_error_view(request)
try:
import sys
type, value, tb = sys.exc_info()
error_message = "" # create an error message from the values above
record_event("Error occurred: %s" % (error_message,), type="Error")
except Exception:
pass
return server_error(request)
Note that none of this code has been tested for correctness. It's just meant as a guide.
Have you tried looking at django-sentry?
http://dcramer.github.com/django-sentry/