How to monitor panics with Raygun for Golang
Posted Apr 8, 2015 | 4 min. (652 words)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
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:
go get github.com/MindscapeHQ/raygun4go
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.
import "github.com/mindscapehq/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.
raygun, err := raygun4go.New("Hello", "YOUR_APP_API_KEY")
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.
if err != nil {
fmt.Println("Unable to create Raygun client: ", err.Error())
}
The last step required to set up Raygun4Go is to defer to the HandleError method.
defer raygun.HandleError()
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:
panic("Panic for no reason at all")
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.
raygun.CreateError("No need to panic")
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:
raygun.Version("1.0.0").Tags([]string{"Urgent", "Critical", "Fix it now!"}).User("Robbie Robot")
And here is the full listing of my hello.go program:
package main
import "fmt"
import "github.com/mindscapehq/raygun4go"
func main() {
raygun, err := raygun4go.New("Hello", "YOUR_APP_API_KEY")
if err != nil {
fmt.Println("Unable to create Raygun client: ", err.Error())
}
defer raygun.HandleError()
raygun.Version("1.0.0").Tags([]string{"Urgent", "Critical", "Fix it now!"}).User("Robbie Robot")
raygun.CreateError("No need to panic") // Manually send an error message to Raygun
sayHello()
}
func sayHello() {
fmt.Println("Hello, world.")
panic("Panic for no reason at all") // panic will be sent to Raygun
}
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.