notifier

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 10 Imported by: 0

README

GitHub go.mod Go version GoDoc Latest release artifacts Go Report Card Go [lint, test] CodeQL coverbadger-tag-do-not-edit

notifier

Package notifier provides functionality to send notifications.

Examples

Notifier

ExampleNotifier shows how to create a new notifier with a list of notifiers and send a message.

package main

import (
	"bytes"
	"context"
	"fmt"
	"os"
	"time"

	"github.com/obalunenko/notifier"
)

const (
	// Test telegram credentials.
	testTGTokenEnv  = "TEST_TELEGRAM_TOKEN"
	testTGChatIDEnv = "TEST_TELEGRAM_CHAT_ID"
)

// ExampleNotifier shows how to create a new notifier with a list of notifiers and send a message.
func main() {
	ctx := context.Background()

	var buf bytes.Buffer

	// Declare list of notifiers.
	var notifiers []notifier.Notifier

	// Create a new io.Writer notifier to visualize alerts.
	wn, err := notifier.NewIOWriterNotifier(&buf)
	if err != nil {
		// Handle error in your way.
		panic(err)
	}

	notifiers = append(notifiers, wn)

	// Create a new telegram notifier if token and chatID env set.
	if token, chatID := os.Getenv(testTGTokenEnv), os.Getenv(testTGChatIDEnv); token != "" && chatID != "" {
		tgn, err := notifier.NewTelegram(token, chatID)
		if err != nil {
			// Handle error in your way.
			panic(err)
		}

		notifiers = append(notifiers, tgn)
	}

	// Add the notifier to the list of notifiers.
	n, err := notifier.NewMultiNotifier(notifiers...)
	if err != nil {
		// Handle error in your way.
		panic(err)
	}

	// Add notifier metadata to context.
	ctx = notifier.ContextWithMetadata(ctx, notifier.Metadata{
		AppName:      "test_app",
		InstanceName: "test_instance",
		Commit:       "test_commit",
		BuildDate:    time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.DateTime),
	})

	// Send alert.
	err = n.Alert(ctx, notifier.SeverityWarning, "[NOTIFIER_TEST]: example message")
	if err != nil {
		// Handle error in your way.
		panic(err)
	}

	fmt.Println(buf.String())
}

Documentation

Overview

Package notifier provides functionality to send notifications.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyTelegramToken is returned when the telegram token is empty.
	ErrEmptyTelegramToken = errors.New("telegram token is empty")
	// ErrEmptyTelegramChatID is returned when the telegram chat id is empty.
	ErrEmptyTelegramChatID = errors.New("telegram chat id is empty")
	// ErrEmptyMessage is returned when the message is empty.
	ErrEmptyMessage = errors.New("message is empty")
	// ErrInvalidToken is returned when the telegram token is invalid.
	ErrInvalidToken = errors.New("invalid token")
	// ErrEmptyNotifiers is returned when the notifiers' list is empty.
	ErrEmptyNotifiers = errors.New("notifiers list is empty")
	// ErrInvalidSeverity is returned when the severity is invalid.
	ErrInvalidSeverity = errors.New("invalid severity")
)

Functions

func ContextWithMetadata

func ContextWithMetadata(ctx context.Context, metadata Metadata) context.Context

ContextWithMetadata returns a new context with the given metadata. If ctx is nil, context.Background() is used.

Types

type Metadata

type Metadata struct {
	AppName      string
	InstanceName string
	Commit       string
	BuildDate    string
	Extra        map[string]string
}

Metadata contains information about the application.

func MetadataFromContext

func MetadataFromContext(ctx context.Context) (*Metadata, bool)

MetadataFromContext returns the metadata stored in ctx, if any.

type Notifier

type Notifier interface {
	// Alert sends a message to the notifier.
	Alert(ctx context.Context, severity Severity, message string) error
	// Kind returns the notifier kind.
	Kind() string
}

Notifier declares notifier contract.

Example

ExampleNotifier shows how to create a new notifier with a list of notifiers and send a message.

package main

import (
	"bytes"
	"context"
	"fmt"
	"os"
	"time"

	"github.com/obalunenko/notifier"
)

const (
	// Test telegram credentials.
	testTGTokenEnv  = "TEST_TELEGRAM_TOKEN"
	testTGChatIDEnv = "TEST_TELEGRAM_CHAT_ID"
)

// ExampleNotifier shows how to create a new notifier with a list of notifiers and send a message.
func main() {
	ctx := context.Background()

	var buf bytes.Buffer

	// Declare list of notifiers.
	var notifiers []notifier.Notifier

	// Create a new io.Writer notifier to visualize alerts.
	wn, err := notifier.NewIOWriterNotifier(&buf)
	if err != nil {
		// Handle error in your way.
		panic(err)
	}

	notifiers = append(notifiers, wn)

	// Create a new telegram notifier if token and chatID env set.
	if token, chatID := os.Getenv(testTGTokenEnv), os.Getenv(testTGChatIDEnv); token != "" && chatID != "" {
		var tgn notifier.Notifier

		tgn, err = notifier.NewTelegram(token, chatID)
		if err != nil {
			// Handle error in your way.
			panic(err)
		}

		notifiers = append(notifiers, tgn)
	}

	// Add the notifier to the list of notifiers.
	n, err := notifier.NewMultiNotifier(notifiers...)
	if err != nil {
		// Handle error in your way.
		panic(err)
	}

	// Add notifier metadata to context.
	ctx = notifier.ContextWithMetadata(ctx, notifier.Metadata{
		AppName:      "test_app",
		InstanceName: "test_instance",
		Commit:       "test_commit",
		BuildDate:    time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.DateTime),
	})

	// Send alert.
	err = n.Alert(ctx, notifier.SeverityWarning, "[NOTIFIER_TEST]: example message")
	if err != nil {
		// Handle error in your way.
		panic(err)
	}

	fmt.Println(buf.String())
}
Output:

<b>⚠️ Severity:</b> WARNING
<b>Alert Message:</b> [NOTIFIER_TEST]: example message
<b>Meta:</b>
	• app_name: test_app
	• build_date: 2020-01-01 00:00:00
	• commit: test_commit
	• instance_name: test_instance

func NewIOWriterNotifier

func NewIOWriterNotifier(w io.Writer, kind ...string) (Notifier, error)

NewIOWriterNotifier creates a new notifier that writes messages to io.Writer. Useful for testing. If kind is not provided, "iowriter" is used.

func NewMultiNotifier

func NewMultiNotifier(notifiers ...Notifier) (Notifier, error)

NewMultiNotifier returns a new multiNotifier notifier. Useful when it's needed to send messages to multiple telegram chats or other notifiers.

func NewTelegram

func NewTelegram(token, chatID string) (Notifier, error)

NewTelegram returns a new telegram notifier.

type Severity

type Severity int

Severity represents the severity of an alert.

const (

	// SeverityInfo represents an info alert.
	SeverityInfo Severity // INFO
	// SeverityWarning represents a warning alert.
	SeverityWarning // WARNING
	// SeverityCritical represents a critical alert.
	SeverityCritical // CRITICAL

)

func (Severity) String

func (i Severity) String() string

func (Severity) Valid

func (s Severity) Valid() bool

Valid checks if the severity is valid.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL