probe

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package probe converts database, HTTP, and custom ping functions into readiness/liveness helpers. See ExampleNewPingProbe, ExampleNewHTTPProbe, and ExampleNewHTTPProbe_withOptions for quick-start patterns.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBPinger

type DBPinger interface {
	PingContext(ctx context.Context) error
}

DBPinger captures the subset of *sql.DB used for readiness checks.

type Func

type Func func(ctx context.Context) error

Func represents a health check that returns an error when the resource is unavailable.

func NewDBPingProbe

func NewDBPingProbe(name string, db DBPinger) Func

NewDBPingProbe creates a Func that pings databases such as PostgreSQL using the provided client.

Example
package main

import (
	"context"
	"fmt"

	"github.com/drblury/apiweaver/probe"
)

type stubDB struct {
	err error
}

func (s *stubDB) PingContext(ctx context.Context) error {
	return s.err
}

func main() {
	probeFunc := probe.NewDBPingProbe("postgres", &stubDB{})
	fmt.Println(probeFunc(context.Background()))
}
Output:

<nil>

func NewHTTPProbe

func NewHTTPProbe(name, method, target string, client HTTPDoer, opts ...HTTPProbeOption) Func

NewHTTPProbe creates a Func that performs an HTTP request against the supplied endpoint. The probe succeeds when the response status code is within the 2xx range.

Example
package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/drblury/apiweaver/probe"
)

func main() {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "ok")
	}))
	defer server.Close()

	probeFunc := probe.NewHTTPProbe("docs", http.MethodGet, server.URL, server.Client())
	fmt.Println(probeFunc(context.Background()))
}
Output:

<nil>
Example (DefaultClient)
package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/drblury/apiweaver/probe"
)

func main() {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	}))
	defer ts.Close()

	probeFunc := probe.NewHTTPProbe("docs", http.MethodGet, ts.URL, nil)
	fmt.Println(probeFunc(context.Background()))
}
Output:

<nil>
Example (WithOptions)
package main

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/drblury/apiweaver/probe"
)

func main() {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Header.Get("Authorization") != "Bearer demo" {
			w.WriteHeader(http.StatusUnauthorized)
			return
		}
		w.Header().Set("X-Version", "123")
		w.WriteHeader(http.StatusAccepted)
	}))
	defer server.Close()

	probeFunc := probe.NewHTTPProbe(
		"docs",
		http.MethodGet,
		server.URL,
		nil,
		probe.WithHTTPRequestMutator(func(req *http.Request) error {
			req.Header.Set("Authorization", "Bearer demo")
			return nil
		}),
		probe.WithHTTPAllowedStatuses(http.StatusAccepted),
		probe.WithHTTPResponseValidator(func(resp *http.Response) error {
			if resp.Header.Get("X-Version") == "" {
				return errors.New("missing version header")
			}
			return nil
		}),
	)

	fmt.Println(probeFunc(context.Background()))
}
Output:

<nil>

func NewMongoPingProbe

func NewMongoPingProbe(client MongoPinger, readPref *readpref.ReadPref) Func

NewMongoPingProbe creates a Func that pings MongoDB using the provided client. If readPref is nil it defaults to readpref.Primary.

func NewPingProbe

func NewPingProbe(name string, fn PingFunc) Func

NewPingProbe wraps a PingFunc with standardised error handling suitable for InfoHandler probes.

Example
package main

import (
	"context"
	"fmt"

	"github.com/drblury/apiweaver/probe"
)

func main() {
	probeFunc := probe.NewPingProbe("noop", func(ctx context.Context) error {
		return nil
	})
	fmt.Println(probeFunc(context.Background()))
}
Output:

<nil>

type HTTPDoer

type HTTPDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPDoer represents the subset of *http.Client required by the HTTP probe helper.

type HTTPProbeOption added in v0.2.0

type HTTPProbeOption func(*httpProbeConfig)

HTTPProbeOption configures the behaviour of NewHTTPProbe.

func WithHTTPAllowedStatuses added in v0.2.0

func WithHTTPAllowedStatuses(statuses ...int) HTTPProbeOption

WithHTTPAllowedStatuses restricts the probe to succeed only for the provided status codes.

func WithHTTPClient added in v0.2.0

func WithHTTPClient(client HTTPDoer) HTTPProbeOption

WithHTTPClient overrides the HTTP client used for the probe.

func WithHTTPDrainResponseBody added in v0.2.0

func WithHTTPDrainResponseBody(enabled bool) HTTPProbeOption

WithHTTPDrainResponseBody toggles draining of the response body after validation.

func WithHTTPRequestMutator added in v0.2.0

func WithHTTPRequestMutator(mutator HTTPRequestMutator) HTTPProbeOption

WithHTTPRequestMutator registers a mutator that runs before the request is dispatched.

func WithHTTPResponseValidator added in v0.2.0

func WithHTTPResponseValidator(validator HTTPResponseValidator) HTTPProbeOption

WithHTTPResponseValidator registers a validator that runs after a response is received.

func WithHTTPStatusExpectation added in v0.2.0

func WithHTTPStatusExpectation(expect HTTPStatusExpectation) HTTPProbeOption

WithHTTPStatusExpectation installs a custom status validation function.

type HTTPRequestMutator added in v0.2.0

type HTTPRequestMutator func(req *http.Request) error

HTTPRequestMutator allows callers to tweak the outbound request prior to dispatch.

type HTTPResponseValidator added in v0.2.0

type HTTPResponseValidator func(resp *http.Response) error

HTTPResponseValidator inspects the received response and can veto the probe.

type HTTPStatusExpectation added in v0.2.0

type HTTPStatusExpectation func(status int) bool

HTTPStatusExpectation determines whether a given HTTP status code is acceptable.

type MongoPinger

type MongoPinger interface {
	Ping(ctx context.Context, rp *readpref.ReadPref) error
}

MongoPinger captures the subset of the MongoDB client used for readiness checks.

type PingFunc

type PingFunc func(ctx context.Context) error

PingFunc represents a health check that returns an error when the resource is unavailable.

Jump to

Keyboard shortcuts

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