Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

Optional dependency on github.com/prometheus/* #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package healthcheck
package healthcheck_test

import (
"database/sql"
Expand All @@ -23,27 +23,28 @@ import (
"strings"
"time"

sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"

"github.com/heptiolabs/healthcheck"
"github.com/heptiolabs/healthcheck/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)

func Example() {
// Create a Handler that we can use to register liveness and readiness checks.
health := NewHandler()
health := healthcheck.NewHandler()

// Add a readiness check to make sure an upstream dependency resolves in DNS.
// If this fails we don't want to receive requests, but we shouldn't be
// restarted or rescheduled.
upstreamHost := "upstream.example.com"
health.AddReadinessCheck(
"upstream-dep-dns",
DNSResolveCheck(upstreamHost, 50*time.Millisecond))
healthcheck.DNSResolveCheck(upstreamHost, 50*time.Millisecond))

// Add a liveness check to detect Goroutine leaks. If this fails we want
// to be restarted/rescheduled.
health.AddLivenessCheck("goroutine-threshold", GoroutineCountCheck(100))
health.AddLivenessCheck("goroutine-threshold", healthcheck.GoroutineCountCheck(100))

// Serve http://0.0.0.0:8080/live and http://0.0.0.0:8080/ready endpoints.
// go http.ListenAndServe("0.0.0.0:8080", health)
Expand All @@ -65,11 +66,11 @@ func Example_database() {
database = connectToDatabase()

// Create a Handler that we can use to register liveness and readiness checks.
health := NewHandler()
health := healthcheck.NewHandler()

// Add a readiness check to we don't receive requests unless we can reach
// the database with a ping in <1 second.
health.AddReadinessCheck("database", DatabasePingCheck(database, 1*time.Second))
health.AddReadinessCheck("database", healthcheck.DatabasePingCheck(database, 1*time.Second))

// Serve http://0.0.0.0:8080/live and http://0.0.0.0:8080/ready endpoints.
// go http.ListenAndServe("0.0.0.0:8080", health)
Expand All @@ -89,7 +90,7 @@ func Example_database() {

func Example_advanced() {
// Create a Handler that we can use to register liveness and readiness checks.
health := NewHandler()
health := healthcheck.NewHandler()

// Make sure we can connect to an upstream dependency over TCP in less than
// 50ms. Run this check asynchronously in the background every 10 seconds
Expand All @@ -100,16 +101,16 @@ func Example_advanced() {
upstreamAddr := "upstream.example.com:5432"
health.AddReadinessCheck(
"upstream-dep-tcp",
Async(TCPDialCheck(upstreamAddr, 50*time.Millisecond), 10*time.Second))
healthcheck.Async(healthcheck.TCPDialCheck(upstreamAddr, 50*time.Millisecond), 10*time.Second))

// Add a readiness check against the health of an upstream HTTP dependency
upstreamURL := "http://upstream-svc.example.com:8080/healthy"
health.AddReadinessCheck(
"upstream-dep-http",
HTTPGetCheck(upstreamURL, 500*time.Millisecond))
healthcheck.HTTPGetCheck(upstreamURL, 500*time.Millisecond))

// Implement a custom check with a 50 millisecond timeout.
health.AddLivenessCheck("custom-check-with-timeout", Timeout(func() error {
health.AddLivenessCheck("custom-check-with-timeout", healthcheck.Timeout(func() error {
// Simulate some work that could take a long time
time.Sleep(time.Millisecond * 100)
return nil
Expand Down Expand Up @@ -143,7 +144,7 @@ func Example_metrics() {

// Create a metrics-exposing Handler for the Prometheus registry
// The healthcheck related metrics will be prefixed with the provided namespace
health := NewMetricsHandler(registry, "example")
health := metrics.NewMetricsHandler(registry, "example")

// Add a simple readiness check that always fails.
health.AddReadinessCheck("failing-check", func() error {
Expand Down
15 changes: 8 additions & 7 deletions metrics_handler.go → metrics/metrics_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,36 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package healthcheck
package metrics

import (
"net/http"

"github.com/heptiolabs/healthcheck"
"github.com/prometheus/client_golang/prometheus"
)

type metricsHandler struct {
handler Handler
handler healthcheck.Handler
registry prometheus.Registerer
namespace string
}

// NewMetricsHandler returns a healthcheck Handler that also exposes metrics
// into the provided Prometheus registry.
func NewMetricsHandler(registry prometheus.Registerer, namespace string) Handler {
func NewMetricsHandler(registry prometheus.Registerer, namespace string) healthcheck.Handler {
return &metricsHandler{
handler: NewHandler(),
handler: healthcheck.NewHandler(),
registry: registry,
namespace: namespace,
}
}

func (h *metricsHandler) AddLivenessCheck(name string, check Check) {
func (h *metricsHandler) AddLivenessCheck(name string, check healthcheck.Check) {
h.handler.AddLivenessCheck(name, h.wrap(name, check))
}

func (h *metricsHandler) AddReadinessCheck(name string, check Check) {
func (h *metricsHandler) AddReadinessCheck(name string, check healthcheck.Check) {
h.handler.AddReadinessCheck(name, h.wrap(name, check))
}

Expand All @@ -56,7 +57,7 @@ func (h *metricsHandler) ReadyEndpoint(w http.ResponseWriter, r *http.Request) {
h.handler.ReadyEndpoint(w, r)
}

func (h *metricsHandler) wrap(name string, check Check) Check {
func (h *metricsHandler) wrap(name string, check healthcheck.Check) healthcheck.Check {
h.registry.MustRegister(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: h.namespace,
Expand Down
3 changes: 1 addition & 2 deletions metrics_handler_test.go → metrics/metrics_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package healthcheck
package metrics

import (
"fmt"
Expand All @@ -23,7 +23,6 @@ import (
"testing"

"github.com/prometheus/client_golang/prometheus"

"github.com/stretchr/testify/assert"
)

Expand Down