How to monitor panics with Raygun for Golang

Jason FauchelleGeneralLeave a Comment

golang

Today I’m going to show you how to use our new official Raygun4Go provider created by Jakob Anders and Christoph Rahles from Käuferportal

Raygun4Go

Raygun is an error reporting service for logging errors in your applications and providing you with all the information you need to solve them. Get valuable awareness of how well your published/deployed applications are really doing now with Raygun for Golang.

Basic usage

Install

This tutorial assumes you know the basics of Golang. To keep things simple, I’m trying out Raygun4Go in the hello world test program described here. To get started, install the Raygun4Go package using go get in a terminal:

To ensure full panic coverage in your Golang program, we recommend setting up Raygun4Go to be one of the first steps in your main method (or in webservers, your request handling method). Open the .go file containing the main method (in my case, hello.go), and then add an import for Raygun4Go.

Set up

Now in the main method, create a raygun client object by calling the New method which requires an application name, and your Raygun API key that you want to use for this app. The application name can be whatever you want, and the API key is shown to you when you create a new Application in your Raygun account, or can be viewed in your application settings.

In the spirit of good error handling, lets also check that the raygun client creation didn’t cause an error, and print it to the console if it did.

The last step required to set up Raygun4Go is to defer to the HandleError method.

Try it out

To test this out, call panic any where after the defer, then run the program to see the message and stack trace show up in your Raygun dashboard:

GolangError

Manually sending messages

The raygun client object also has a CreateError method for manually sending an error message to your Raygun dashboard. This is great for reporting that something has gone wrong, but a panic doesn’t need to be invoked – for example you may want to report errors from within an error handler, or send a message when a bad status code is returned from a web request. The error report sent to Raygun will even include the stack trace.

Features

The raygun client object returned from the New method has several chainable features as follows:

  • Silent(bool) If set to true, errors will be printed to the console instead of being sent to Raygun.
  • Version(string) Sets the program version number to be sent with each error report to Raygun.
  • Request(*http.Request) Provides information about any http request responsible for the issue.
  • User(string) Sets the user identifier, e.g. a name, email or database id.
  • Tags([]string) Sets a list of tags which can help you filter errors in your Raygun dashboard.
  • CustomData(interface{}) Sets additional information that can help you debug the problem. (Must work with json.Marshal).

Here is an example of using a few of these features:

And here is the full listing of my hello.go program:

Try it yourself

If you want awareness of the issues occurring in your go programs, try Raygun4Go using the simple steps above. If you don’t have an account yet, start a free Raygun trial here, no credit card required.

Next level software intelligence across your entire stack.

Leave a Reply

Your email address will not be published. Required fields are marked *