JackFazackerley / logrus-opsgenie-hook

Hook for logrus that sends errors to OpsGenie as alerts.
MIT License
0 stars 0 forks source link
golang logging logrus opsgenie

OpsGenie Hook for Logrus :walrus: Build Status godoc reference

This hook is used to send your errors to OpsGenie as an alert. It uses the opsgenie-go-sdk to handle the requests. The levels that are blocked by this hook are log.Error, log.Fatal, and log.Panic.

Usage

The only configuration needed for this hook is the OpsGenie API key you wish to use. However the alert struct must be created yourself with the fields you wish to use and added as a logrus.Entry using WithField("request", alert).

The message of the alert will default to the log level message: Error("some error"), although if WithError(err) is used that will become a priority.

import (
    "fmt"

    "github.com/jackfazackerley/logrus-opsgenie-hook"
    "github.com/opsgenie/opsgenie-go-sdk/alertsv2"
    "github.com/sirupsen/logrus"
)

func main() {
    log := logrus.New()

    hook, err := opsgenie.NewHook("some API key")
    if err != nil {
        panic(err)
    }

    log.AddHook(hook)

    alert := alertsv2.CreateAlertRequest{
        Alias: "some alias here",
        Description: "some description here",
        Teams: []alertsv2.TeamRecipient{
            &alertsv2.Team{
                Name: "Dev",
            },
        },
        Source:   "some source here",
        Priority: alertsv2.P5,
    }

    // Uses default message as the alert message
    log.WithField("alert", alert).Error("default message value")

    // WithError is made priority for the alert message
    log.WithField("alert", alert).WithError(fmt.Errorf("made priority")).Error("default message value")
}

The structure for the OpsGenie alert is documented Here.