http

package
v0.9.4 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2026 License: BSD-3-Clause Imports: 18 Imported by: 0

README

HTTP Provider

The HTTP Provider extends the platform-health server to enable health checking of HTTP/HTTPS services with powerful response validation using CEL (Common Expression Language) expressions.

Features

  • HTTP/HTTPS requests with configurable methods (HEAD, GET, POST, PUT, DELETE, etc.)
  • Request body and custom headers support
  • TLS certificate verification and optional detail extraction
  • Response validation using CEL expressions
  • JSON response parsing and field validation
  • Status code, header, and body validation

Usage

Once the HTTP Provider is configured, any query to the platform health server will trigger an HTTP request to each configured endpoint. The server will report each instance as "healthy" if the request succeeds and all CEL checks pass, or "unhealthy" otherwise.

Configuration

The HTTP Provider is configured through the platform-health server's configuration file. Each instance is defined with its name as the YAML key under components.

  • type (required): Must be http.
  • timeout (optional): Per-instance timeout override.
  • spec: Provider-specific configuration:
    • url (required): Target URL.
    • method (default: HEAD): HTTP method.
    • body (optional): Request body.
    • headers (optional): Custom HTTP headers as a map.
    • insecure (default: false): Skip TLS certificate validation.
    • detail (default: false): Include TLS certificate details in response.
  • checks: A list of CEL expressions to validate the response. Each check has:
    • check (required): A CEL expression that must evaluate to true for the response to be healthy.
    • message (optional): Custom error message when the check fails.

CEL Check Context

The following variables are available in CEL expressions:

Request Context
  • request.method: HTTP method (string)
  • request.body: Request body text (string)
  • request.headers: Request headers as map (lowercase keys)
  • request.url: Target URL (string)
Response Context
  • response.status: HTTP status code (int)
  • response.body: Raw response body as text (string)
  • response.json: Parsed JSON response (map, nil if not JSON)
  • response.headers: Response headers as map (lowercase keys)
Example CEL Expressions
// Status code validation
response.status == 200

// Status range validation
response.status >= 200 && response.status < 300

// Multiple accepted statuses
response.status == 200 || response.status == 201

// JSON field validation
response.json.status == "healthy"

// Nested JSON validation
response.json.data.database.connected == true

// Array length validation
size(response.json.items) > 0

// Header validation
response.headers["content-type"].contains("application/json")

// Body text validation
response.body.contains("success")

// Regex pattern matching
response.body.matches("(?i)all systems operational")

Default Health Behavior

When no checks are configured, the HTTP provider considers any response with a status code in the range 200-399 as healthy. Status codes 400 and above are reported as unhealthy. To customize this behavior, use CEL checks.

TLS Details

When detail: true is set and the request uses HTTPS, the response will include TLS certificate information:

  • Common Name
  • Subject Alternative Names (SANs)
  • Certificate validity period
  • Signature algorithm
  • Public key algorithm
  • TLS version
  • Cipher suite
  • Protocol (e.g., "h2" for HTTP/2)
  • Certificate chain

Examples

Simple Health Check
components:
  google:
    type: http
    spec:
      url: https://google.com
API Health Check with JSON Validation
components:
  api-health:
    type: http
    timeout: 5s
    spec:
      url: https://api.example.com/health
      method: GET
    checks:
      - check: 'response.status == 200'
        message: "API returned non-200 status"
      - check: 'response.json.status == "healthy"'
        message: "API reports unhealthy status"
      - check: 'response.json.database.connected == true'
        message: "Database connection failed"
Authenticated API Check
components:
  auth-api:
    type: http
    timeout: 10s
    spec:
      url: https://api.example.com/v1/status
      method: POST
      body: '{"action": "healthcheck"}'
      headers:
        Authorization: "Bearer ${API_TOKEN}"
        Content-Type: "application/json"
    checks:
      - 'response.status == 200'
      - check: 'response.json.authenticated == true'
        message: "Authentication failed"
DNS-over-HTTPS Check
components:
  dns-check:
    type: http
    spec:
      url: https://dns.google.com/resolve?name=example.com&type=A
      method: GET
    checks:
      - check: 'response.status == 200'
        message: "DNS query failed"
      - check: 'response.json.Status == 0'
        message: "DNS lookup returned error"
      - check: 'size(response.json.Answer) > 0'
        message: "No DNS records found"

Documentation

Overview

Package http provides an HTTP health check provider with response validation capabilities using CEL (Common Expression Language).

The HTTP provider supports:

  • HTTP/HTTPS requests with configurable methods and bodies
  • TLS certificate verification and detail extraction
  • Response validation using CEL expressions
  • JSON response parsing and field validation
  • Status code, header, and body validation via CEL

CEL Expression Context: The following variables are available in CEL expressions:

Request context:

  • request.method (string): HTTP method (GET, POST, etc.)
  • request.body (string): Request body text
  • request.headers (map[string]string): Request headers (lowercase keys)
  • request.url (string): Target URL

Response context:

  • response.json (map[string]any): Parsed JSON response body (nil if not JSON)
  • response.body (string): Raw response body as text
  • response.status (int): HTTP status code
  • response.headers (map[string]string): Response headers (lowercase keys)

Example CEL expressions:

  • response.status == 200 // Status code validation
  • response.status >= 200 && response.status < 300 // Status range validation
  • response.json.status == "healthy" // JSON field validation
  • response.headers["x-health-status"] == "ok" // Header validation
  • response.body.contains("success") // Body text validation
  • response.headers["content-type"].contains("application/json") // Content type validation
  • request.method == "POST" && response.status == 201 // Method-specific validation
  • response.headers["content-type"] == request.headers["accept"] // Content negotiation

Index

Constants

View Source
const (
	ProviderType   = "http"
	DefaultTimeout = 10 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Component added in v0.6.0

type Component struct {
	provider.Base
	provider.BaseWithChecks

	URL      string            `mapstructure:"url"`
	Method   string            `mapstructure:"method" default:"HEAD"`
	Body     string            `mapstructure:"body"`
	Headers  map[string]string `mapstructure:"headers"`
	Insecure bool              `mapstructure:"insecure"`
	Detail   bool              `mapstructure:"detail"`
}

Component provides HTTP health checks with CEL-based response validation

func (*Component) GetCheckConfig added in v0.7.0

func (c *Component) GetCheckConfig() *checks.CEL

GetCheckConfig returns the HTTP provider's CEL variable declarations.

func (*Component) GetCheckContext added in v0.7.0

func (c *Component) GetCheckContext(ctx context.Context) (map[string]any, error)

GetCheckContext satisfies the CheckContextProvider interface.

func (*Component) GetHealth added in v0.6.0

func (c *Component) GetHealth(ctx context.Context) *ph.HealthCheckResponse

func (*Component) GetType added in v0.6.0

func (c *Component) GetType() string

func (*Component) LogValue added in v0.6.0

func (c *Component) LogValue() slog.Value

func (*Component) SetChecks added in v0.7.0

func (c *Component) SetChecks(exprs []checks.Expression) error

SetChecks sets and compiles CEL expressions.

func (*Component) Setup added in v0.6.0

func (c *Component) Setup() error

Jump to

Keyboard shortcuts

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