Documentation ¶
Overview ¶
Ayd plugin library
Example (AlertPlugin) ¶
args, err := ayd.ParseAlertPluginArgs() if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } logger := ayd.NewLogger(args.AlertURL) // Fetch extra information from Ayd json API aydURL, err := url.Parse(os.Getenv("AYD_URL")) if err != nil { logger.Failure("failed to get Ayd URL") return } resp, err := ayd.Fetch(aydURL) if err != nil { logger.Failure("failed to fetch status") return } incidents, err := resp.CurrentIncidents() if err != nil { logger.Failure("failed to get current incidents") return } _ = incidents // you can use this extra inciden information logger = logger.StartTimer() // start timer for measure time to send alert // send alert to somewhere logger.Healthy("alert sent")
Output:
Example (ProbePlugin) ¶
args, err := ayd.ParseProbePluginArgs() if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } logger := ayd.NewLogger(args.TargetURL).StartTimer() // check your target here ok := true if ok { logger.Healthy("target is healthy!") } else { logger.Failure("target is down") }
Output:
Index ¶
- Variables
- type AlertPluginArgs
- type Incident
- type Logger
- func (l Logger) Aborted(message string) error
- func (l Logger) Failure(message string) error
- func (l Logger) Healthy(message string) error
- func (l Logger) Print(r Record) error
- func (l Logger) StartTimer() Logger
- func (l Logger) StopTimer() Logger
- func (l Logger) Unknown(message string) error
- func (l Logger) WithTarget(target *url.URL) Logger
- func (l Logger) WithTime(startTime time.Time, latency time.Duration) Logger
- type ProbePluginArgs
- type Record
- type Response
- type Status
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmptyTarget is the error means target URL of Record was empty ErrEmptyTarget = errors.New("the target URL is required") )
var (
ErrInvalidArgument = errors.New("invalid argument")
)
var (
ErrNoSuchTarget = errors.New("no such target")
)
Functions ¶
This section is empty.
Types ¶
type AlertPluginArgs ¶
type AlertPluginArgs struct { AlertURL *url.URL TargetURL *url.URL Status Status CheckedAt time.Time }
AlertPluginArgs is arguments for alert plugin
func ParseAlertPluginArgs ¶
func ParseAlertPluginArgs() (AlertPluginArgs, error)
ParseAlertPluginArgs is get arguments for alert plugin
This function is shorthand of `ayd.ParseAlertPluginArgs(os.Args)`.
func ParseAlertPluginArgsFrom ¶
func ParseAlertPluginArgsFrom(args []string) (AlertPluginArgs, error)
ParseAlertPluginArgsFrom is parse arguments for alert plugin
type Incident ¶
type Incident struct { Target *url.URL Status Status Message string // CausedAt is the first detected time the target is unhealthy status CausedAt time.Time // ResolvedAt is the earliest time that detected the target back to healthy status ResolvedAt time.Time }
Incident is a period of failure or unknown status that has the same status and message
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the logger for Ayd plugin
Example ¶
target, _ := url.Parse("foobar:your-plugin-url") logger := ayd.NewLogger(target) logger.Healthy("hello world")
Output:
Example (SetExtraValues) ¶
logger := ayd.NewLogger(nil) // change target URL target, _ := url.Parse("foobar:your-plugin-url") logger = logger.WithTarget(target) // set check time and latency of the target startTime, _ := time.Parse(time.RFC3339, "2001-02-03T16:05:06+09:00") latency := 123 * time.Millisecond logger = logger.WithTime(startTime, latency) // report target status with a message logger.Healthy("target is healthy") logger.Failure("target seems down") logger.Unknown("failed to check, so target status is unknown") logger.Aborted("the check was aborted by user or something")
Output: 2001-02-03T16:05:06+09:00 HEALTHY 123.000 foobar:your-plugin-url target is healthy 2001-02-03T16:05:06+09:00 FAILURE 123.000 foobar:your-plugin-url target seems down 2001-02-03T16:05:06+09:00 UNKNOWN 123.000 foobar:your-plugin-url failed to check, so target status is unknown 2001-02-03T16:05:06+09:00 ABORTED 123.000 foobar:your-plugin-url the check was aborted by user or something
func NewLogger ¶
NewLogger makes new Logger
This is the shorthand to `ayd.NewLoggerWithWriter(os.Stdout, target)`.
func NewLoggerWithWriter ¶
NewLoggerWithWriter makes new Logger with a io.Writer
func (Logger) Print ¶
Print prints a Record
Example ¶
logger := ayd.NewLogger(nil) logger.Print(ayd.Record{ Target: &url.URL{Scheme: "foo", Host: "bar"}, Status: ayd.StatusHealthy, CheckedAt: time.Date(2001, 2, 3, 16, 5, 6, 7, time.UTC), Message: "hello world", }) logger.Print(ayd.Record{ Target: &url.URL{Scheme: "foo", Host: "bar"}, CheckedAt: time.Date(2001, 2, 3, 16, 5, 7, 0, time.UTC), Message: "without status", }) err := logger.Print(ayd.Record{ CheckedAt: time.Date(2001, 2, 3, 16, 5, 8, 0, time.UTC), Message: "without target", }) fmt.Println("error:", err)
Output: 2001-02-03T16:05:06Z HEALTHY 0.000 foo://bar hello world 2001-02-03T16:05:07Z UNKNOWN 0.000 foo://bar without status error: the target URL is required
func (Logger) StartTimer ¶
StartTimer makes new Logger that set start time as current time, and start timer for latency from now.
You can stop the timer with StopTimer method, or just call print method like Healthy or Failure.
Example ¶
target, _ := url.Parse("foobar:your-plugin-url") logger := ayd.NewLogger(target) l := logger.StartTimer() // check your target status l.Healthy("hello world")
Output:
func (Logger) StopTimer ¶
StopTimer stops latency timer that started by StartTimer method, and makes new Logger with measured latency.
Example ¶
target, _ := url.Parse("foobar:your-plugin-url") logger := ayd.NewLogger(target) l := logger.StartTimer() // check your target status l = l.StopTimer() // do something, for example calculate result of the check l.Healthy("hello world")
Output:
func (Logger) WithTarget ¶
WithTarget makes new Logger with new target URL
Example ¶
logger := ayd.NewLogger(nil) target, _ := url.Parse("foobar:your-plugin-url") logger.WithTarget(target).Healthy("hello world")
Output:
func (Logger) WithTime ¶
WithTime makes new Logger with start time and latency value
Example ¶
target, _ := url.Parse("foobar:your-plugin-url") logger := ayd.NewLogger(target) startTime, _ := time.Parse(time.RFC3339, "2001-02-03T16:05:06+09:00") latency := 123 * time.Millisecond logger.WithTime(startTime, latency).Healthy("hello world")
Output: 2001-02-03T16:05:06+09:00 HEALTHY 123.000 foobar:your-plugin-url hello world
type ProbePluginArgs ¶
ProbePluginArgs is arguments for probe plugin
func ParseProbePluginArgs ¶
func ParseProbePluginArgs() (ProbePluginArgs, error)
ParseProbePluginArgs is get arguments for probe plugin
This function is shorthand of `ayd.ParseProbePluginArgs(os.Args)`.
func ParseProbePluginArgsFrom ¶
func ParseProbePluginArgsFrom(args []string) (ProbePluginArgs, error)
ParseProbePluginArgsFrom is parse arguments for probe plugin
type Record ¶
type Record struct { // CheckedAt is the time the check started CheckedAt time.Time Status Status Latency time.Duration Target *url.URL // Message is the reason of the status, or extra informations of the check Message string }
Record is a record in Ayd log
func ParseRecord ¶
ParseRecord is parse string as a Record row in the log
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response is a response from Ayd server
func Fetch ¶
Fetch is fetch Ayd json API and returns Response
Example ¶
aydURL, _ := url.Parse("http://localhost:9000") // fetch status from Ayd server resp, err := ayd.Fetch(aydURL) if err != nil { panic(err) } // parse status of all targets allRecords, err := resp.AllRecords() if err != nil { panic(err) } // show targets status for _, targetRecords := range allRecords { record := targetRecords[len(targetRecords)-1] fmt.Println(record.Target.String(), ":", record.Status) }
Output:
func (Response) AllRecords ¶
AllRecords returns Record history of all targets
func (Response) CurrentIncidents ¶
CurrentIncidents returns list of Incident that current causing
func (Response) IncidentHistory ¶
IncidentHistory returns list of Incident that already resolved
If you want get current causing incidents, please use CurrentIncidents method.
type Status ¶
type Status int8
Status is the status of target service
const ( // StatusUnknown means UNKNOWN current status because failed to check the target status StatusUnknown Status = iota // StatusHealthy means success to status check and the target is HEALTHY StatusHealthy // StatusFailure means the target is in FAILURE, but status check is success StatusFailure // StatusAborted means the status check ABORTED because stop Ayd server StatusAborted )
func ParseStatus ¶
ParseStatus is parse status string
If passed unsupported status, it will returns StatusUnknown
func (Status) MarshalText ¶
MarshalText is marshal Status as text
func (*Status) UnmarshalText ¶
UnmarshalText is unmarshal text as status
This function always returns nil. This parses as StatusUnknown instead of returns error if unsupported status passed.