monitor

package
v0.0.0-...-26b160e Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

README

Monitor GoDoc

Provides a high level pluggable abstraction for monitoring.

Interface

Allows the ability for user defined healthchecks. Exactly what's required for monitoring business logic as opposed to lower level things like pings, tcp checks, etc. Also collates stats about the service and periodically publishes.

// The monitor aggregates, observes and publishes
// information about the current process.
// This includes status; started/running/stopped,
// stats; cpu, memory, runtime and healthchecks.
type Monitor interface {
	Close() error
	Checker
	Stats
	String() string
}

// Checker interface allows creation of healthchecks
type Checker interface {
	NewHealthChecker(id, desc string, fn HealthCheck) HealthChecker
	Register(HealthChecker) error
	Deregister(HealthChecker) error
	HealthChecks() ([]HealthChecker, error)
}

// represents a healthcheck function
type HealthChecker interface {
	// Unique id of the healthcheck
	Id() string
	// Description of what the healthcheck does
	Description() string
	// Runs the the healthcheck
	Run() (map[string]string, error)
	// Returns the status of the last run
	// Better where the healthcheck is expensive to run
	Status() (map[string]string, error)
}

// stats interface allows recording of endpoint stats
type Stats interface {
	RecordStat(r Request, d time.Duration, err error)
}

type HealthCheck func() (map[string]string, error)

func NewMonitor(opts ...Option) Monitor {
	return newOS(opts...)
}

Supported Backends

Usage


import (
	"errors"
	"time"

	"github.com/micro/go-micro"
	"github.com/micro/go-os/monitor"
)

...

m := monitor.NewMonitor(
	// publish intervale
	monitor.Interval(time.Second * 10),
)
defer m.Close()

hc := m.NewHealthChecker(
	// check id
	"com.example.check.myproduct",
	// description
	"Checking product status",
	// healthcheck function
	func() (map[string]string, error) {
		// check some business log
		stats := ProductMetrics()

		if stats.Errors > 10 {
			return nil, errors.New("Product is failing")
		}

		return map[string]string{
			"stats": stats.Metrics(),
			"info":  stats.Info(),
		}, nil
	},
)

m.Register(hc)

// Additionally use client and handler wrappers to let the monitoring service keep track of endpoint stats.

service := micro.NewService(
	micro.Name("com.example.srv.service"),
	micro.WrapClient(monitor.ClientWrapper(m)),
	micro.WrapHandler(monitor.HandlerWrapper(m)),
)

Documentation

Overview

Package monitor is an interface for monitoring.

Index

Constants

This section is empty.

Variables

View Source
var (
	HealthCheckTopic = "micro.monitor.healthcheck"
	StatusTopic      = "micro.monitor.status"
	StatsTopic       = "micro.monitor.stats"
)
View Source
var (
	ErrAlreadyExists = errors.New("already exists")
	ErrNotAvailable  = errors.New("not available")
)

Functions

func ClientWrapper

func ClientWrapper(m Monitor) client.Wrapper

func HandlerWrapper

func HandlerWrapper(m Monitor) server.HandlerWrapper

func NewContext

func NewContext(ctx context.Context, c Monitor) context.Context

Types

type Checker

type Checker interface {
	NewHealthChecker(id, desc string, fn HealthCheck) HealthChecker
	Register(HealthChecker) error
	Deregister(HealthChecker) error
	HealthChecks() ([]HealthChecker, error)
}

Checker interface allows creation of healthchecks

type HealthCheck

type HealthCheck func() (map[string]string, error)

type HealthChecker

type HealthChecker interface {
	// Unique id of the healthcheck
	Id() string
	// Description of what the healthcheck does
	Description() string
	// Runs the the healthcheck
	Run() (map[string]string, error)
	// Returns the status of the last run
	// Better where the healthcheck is expensive to run
	Status() (map[string]string, error)
}

represents a healthcheck function

type Monitor

type Monitor interface {
	Checker
	Stats
	Close() error
	String() string
}

The monitor aggregates, observes and publishes information about the current process. This includes status; started/running/stopped, stats; cpu, memory, runtime and healthchecks.

func FromContext

func FromContext(ctx context.Context) (Monitor, bool)

func NewMonitor

func NewMonitor(opts ...Option) Monitor

type Option

type Option func(*Options)

func Client

func Client(c client.Client) Option

func Interval

func Interval(i time.Duration) Option

func Server

func Server(s server.Server) Option

type Options

type Options struct {
	Interval time.Duration
	Client   client.Client
	Server   server.Server
}

type Request

type Request interface {
	Service() string
	Method() string
}

could be client or server request

type Stats

type Stats interface {
	RecordStat(r Request, d time.Duration, err error)
}

stats interface allows recording of endpoint stats

Directories

Path Synopsis
Package go_micro_os_monitor is a generated protocol buffer package.
Package go_micro_os_monitor is a generated protocol buffer package.
Package web is a web dashboard
Package web is a web dashboard

Jump to

Keyboard shortcuts

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