pagerduty

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2025 License: MIT Imports: 6 Imported by: 0

README

PagerDuty Notifications

Prerequisites

Ensure you have a valid PagerDuty API token to authenticate requests.

Compatibility

This service is compatible with the PagerDuty API for creating incidents.

Usage

package main

import (
    "context"
    "log"

    "github.com/atwallets/notify"
    "github.com/atwallets/notify/service/pagerduty"
)

func main() {
    // Create a new PagerDuty service. Replace 'your_pagerduty_api_token' with your actual PagerDuty API token.
    pagerDutyService, err := pagerduty.New("your_pagerduty_api_token")
    if err != nil {
        log.Fatalf("failed to create pagerduty service: %s", err)
    }

    // Set the sender address and add receivers. (required)
    pagerDutyService.SetFromAddress("sender@example.com")
    pagerDutyService.AddReceivers("ServiceDirectory1", "ServiceDirectory2")

    // Set the urgency, priority ID, and notification type. (optional)
     pagerDutyService.SetUrgency("high")
     pagerDutyService.SetPriorityID("P123456")
     pagerDutyService.SetNotificationType("incident")

    // Create a notifier instance and register the PagerDuty service to it.
    notifier := notify.New()
    notifier.UseServices(pagerDutyService)

    // Send a notification.
    err = notifier.Send(context.Background(), "Test Alert", "This is a test alert from PagerDuty service.")
    if err != nil {
        log.Fatalf("failed to send notification: %s", err)
    }

    log.Println("Notification sent successfully")
}

Configuration

Required Properties
  • API Token: Your PagerDuty API token.
  • From Address: The email address of the sender. The author of the incident.
  • Receivers: List of PagerDuty service directories to send the incident to.
Optional Properties
  • Urgency: The urgency of the incident (e.g., "high", "low").
  • PriorityID: The ID of the priority level assigned to the incident.
  • NotificationType: Type of notification (default is "incident").

These properties can be set using the respective setter methods provided by the Config struct:

  • SetFromAddress(string)
  • AddReceivers(...string)
  • SetUrgency(string)
  • SetPriorityID(string)
  • SetNotificationType(string)

Documentation

Overview

Package pagerduty provides a notifier implementation that sends notifications via PagerDuty. It uses the PagerDuty API to create incidents based on the provided configuration.

Usage: To use this package, you need to create a new instance of PagerDuty with your API token. You can then configure it with the necessary details like the sender's address and receivers. Finally, you can send notifications which will create incidents in PagerDuty.

Example:

package main

import (
    "context"
    "log"

    "github.com/atwallets/notify"
    "github.com/atwallets/notify/service/pagerduty"
)

func main() {
    // Create a new PagerDuty service. Replace 'your_pagerduty_api_token' with your actual PagerDuty API token.
    pagerDutyService, err := pagerduty.New("your_pagerduty_api_token")
    if err != nil {
        log.Fatalf("failed to create pagerduty service: %s", err)
    }

    // Set the sender address and add receivers.
    pagerDutyService.SetFromAddress("sender@example.com")
    pagerDutyService.AddReceivers("ServiceDirectory1", "ServiceDirectory2")

    // Create a notifier instance and register the PagerDuty service to it.
    notifier := notify.New()
    notifier.UseServices(pagerDutyService)

    // Send a notification.
    err = notifier.Send(context.Background(), "Test Alert", "This is a test alert from PagerDuty service.")
    if err != nil {
        log.Fatalf("failed to send notification: %s", err)
    }

    log.Println("Notification sent successfully")
}

This package requires a valid PagerDuty API token to authenticate requests.

Index

Constants

View Source
const (
	APIReferenceType        = "service_reference"
	APIPriorityReference    = "priority_reference"
	DefaultNotificationType = "incident"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	CreateIncidentWithContext(ctx context.Context, from string, options *pagerduty.CreateIncidentOptions) (*pagerduty.Incident, error) //nolint:lll // acceptable in this case, alternative makes the interface even less readable
}

type Config

type Config struct {
	FromAddress      string
	Receivers        []string
	NotificationType string
	Urgency          string
	PriorityID       string
}

Config contains the configuration for the PagerDuty service.

func NewConfig

func NewConfig() *Config

func (*Config) AddReceivers

func (c *Config) AddReceivers(receivers ...string)

AddReceivers appends the receivers to the configuration.

func (*Config) OK

func (c *Config) OK() error

OK checks if the configuration is valid. It returns an error if the configuration is invalid.

func (*Config) PriorityReference

func (c *Config) PriorityReference() *pagerduty.APIReference

PriorityReference returns the PriorityID reference if it is set, otherwise it returns nil.

func (*Config) SetFromAddress

func (c *Config) SetFromAddress(fromAddress string)

SetFromAddress sets the from address in the configuration.

func (*Config) SetNotificationType

func (c *Config) SetNotificationType(notificationType string)

SetNotificationType sets the notification type in the configuration. If the notification type is empty, it will be set to the default value "incident".

func (*Config) SetPriorityID

func (c *Config) SetPriorityID(priorityID string)

SetPriorityID sets the PriorityID in the configuration.

func (*Config) SetUrgency

func (c *Config) SetUrgency(urgency string)

SetUrgency sets the urgency in the configuration.

type PagerDuty

type PagerDuty struct {
	*Config
	Client Client
}

func New

func New(token string, clientOptions ...pagerduty.ClientOptions) (*PagerDuty, error)

func (*PagerDuty) IncidentOptions

func (s *PagerDuty) IncidentOptions(subject, message string) *pagerduty.CreateIncidentOptions

func (*PagerDuty) Send

func (s *PagerDuty) Send(ctx context.Context, subject, message string) error

Jump to

Keyboard shortcuts

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