httphealth

package module
v0.0.0-...-e377863 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2020 License: MIT Imports: 10 Imported by: 0

README

httphealth

HTTPHealth provides a simple component that offers an http interface for health checking and monitoring using Prometheus.

Usage

package main

import (
	"errors"
	"log"
	"math/rand"
	"net"

	"github.com/sirupsen/logrus"

	"github.com/luisguillenc/httphealth"
	"github.com/luisguillenc/ipfilter"
)

func main() {
	//creates listener
	lis, err := net.Listen("tcp", "0.0.0.0:8081")
	if err != nil {
		log.Fatalf("listening: %v", err)
	}

	//constructs whitelist
	wl := ipfilter.Whitelist(
		[]string{"127.0.0.1", "192.168.1.0/24"},
	)

	//constructs logger
	logger := logrus.New()
	logger.SetLevel(logrus.DebugLevel)

	health := httphealth.New(
		&service{},
		httphealth.SetIPFilter(wl),   //sets ipfilter
		httphealth.SetLogger(logger), //sets logger
		httphealth.Metrics(true),     //enables prometheus metrics
	)
	health.Serve(lis)
}

type service struct{}

func (s service) Ping() error {
	if rand.Intn(10) > 8 {
		return errors.New("error in supervised")
	}
	return nil
}

Documentation

Overview

Package httphealth provides a simple component that offers an http interface for health checking and monitoring using Prometheus.

This package is a work in progress and makes no API stability promises.

Example
package main

import (
	"errors"
	"log"
	"math/rand"
	"net"

	"github.com/luisguillenc/httphealth"
)

// service is a supervised object
type service struct{}

// Ping implements httphealth.Pingable interface
func (s service) Ping() error {
	if rand.Intn(10) > 8 {
		return errors.New("error in supervised")
	}
	return nil
}

func main() {
	lis, err := net.Listen("tcp", "127.0.0.1:8081")
	if err != nil {
		log.Fatalf("listening: %v", err)
	}
	health := httphealth.New(&service{})
	health.Serve(lis)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option encapsules options for server

func Metrics

func Metrics(b bool) Option

Metrics enabled to expose prometheus metrics in the health server

func Profile

func Profile(b bool) Option

Profile enabled to expose pprof in the health server

func SetIPFilter

func SetIPFilter(f ipfilter.Filter) Option

SetIPFilter sets an ip filter for the health server

func SetLogger

func SetLogger(l yalogi.Logger) Option

SetLogger sets a logger for the component

type Pingable

type Pingable interface {
	Ping() error
}

Pingable must be implemented by the service to be monitored

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is an http server wrapped that provides a health service

func New

func New(supervised Pingable, opt ...Option) *Server

New construct a new health server that supervised the 'Pingable' object

func (*Server) Close

func (s *Server) Close() error

Close calls the same function from the http.Server contained in the struct

func (*Server) Serve

func (s *Server) Serve(lis net.Listener) error

Serve calls the same function from the http.Server contained in the struct

func (*Server) ServeTLS

func (s *Server) ServeTLS(lis net.Listener, certFile string, keyFile string) error

ServeTLS calls the same function from the http.Server contained in the struct

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown calls the same function from the http.Server contained in the struct

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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