Actuator Starter

Introduction

The Actuator Starter provides production-ready features to help you monitor and manage your Hiboot application. It includes health checks, application info, and metrics endpoints that are essential for cloud-native deployments.

Installation

Import the actuator starter in your application:

import "github.com/hidevopsio/hiboot/pkg/starter/actuator"

Configuration

Enable actuator in your application.yml:

app:
  profiles:
    include:
    - actuator

actuator:
  enabled: true

Or enable it programmatically:

package main

import (
	"github.com/hidevopsio/hiboot/pkg/app"
	"github.com/hidevopsio/hiboot/pkg/app/web"
	"github.com/hidevopsio/hiboot/pkg/starter/actuator"
)

func main() {
	web.NewApplication().
		SetProperty(app.ProfilesInclude, actuator.Profile).
		Run()
}

Built-in Endpoints

Health Endpoint

The /health endpoint provides application health status:

curl http://localhost:8080/health

Response:

{
    "code": 200,
    "data": {
        "status": "UP"
    },
    "message": "Success"
}

Application Info

Get application information via the health endpoint:

{
    "code": 200,
    "data": {
        "status": "UP",
        "app": {
            "name": "myapp",
            "version": "1.0.0"
        }
    },
    "message": "Success"
}

Custom Health Indicators

Create custom health indicators to monitor specific components:

package health

import (
	"github.com/hidevopsio/hiboot/pkg/app"
	"github.com/hidevopsio/hiboot/pkg/starter/actuator"
)

// DatabaseHealthIndicator checks database connectivity
type DatabaseHealthIndicator struct {
	actuator.HealthIndicator
}

func init() {
	app.Register(newDatabaseHealthIndicator)
}

func newDatabaseHealthIndicator() *DatabaseHealthIndicator {
	return &DatabaseHealthIndicator{}
}

// Name returns the indicator name
func (h *DatabaseHealthIndicator) Name() string {
	return "database"
}

// Health performs the health check
func (h *DatabaseHealthIndicator) Health() *actuator.Health {
	// Check database connectivity
	if isDatabaseConnected() {
		return &actuator.Health{
			Status: actuator.StatusUp,
			Details: map[string]interface{}{
				"database": "connected",
				"latency":  "5ms",
			},
		}
	}

	return &actuator.Health{
		Status: actuator.StatusDown,
		Details: map[string]interface{}{
			"error": "database connection failed",
		},
	}
}

func isDatabaseConnected() bool {
	// Implement your database connectivity check
	return true
}

Multiple Health Indicators

Register multiple health indicators for comprehensive monitoring:

package health

import "github.com/hidevopsio/hiboot/pkg/app"

func init() {
	app.Register(
		newDatabaseHealthIndicator,
		newCacheHealthIndicator,
		newExternalAPIHealthIndicator,
	)
}

Health response with multiple indicators:

{
    "code": 200,
    "data": {
        "status": "UP",
        "components": {
            "database": {
                "status": "UP",
                "details": {
                    "database": "connected",
                    "latency": "5ms"
                }
            },
            "cache": {
                "status": "UP",
                "details": {
                    "provider": "redis",
                    "connections": 10
                }
            },
            "externalAPI": {
                "status": "UP",
                "details": {
                    "endpoint": "https://api.example.com",
                    "responseTime": "120ms"
                }
            }
        }
    },
    "message": "Success"
}

Health Status Values

Status Description
UP Component is healthy and functioning normally
DOWN Component is unhealthy or unavailable
UNKNOWN Component health cannot be determined
OUT_OF_SERVICE Component is intentionally taken offline

Kubernetes Integration

Liveness Probe

Configure Kubernetes liveness probe:

livenessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 30
  periodSeconds: 10
  timeoutSeconds: 5
  failureThreshold: 3

Readiness Probe

Configure Kubernetes readiness probe:

readinessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 5
  timeoutSeconds: 3
  failureThreshold: 3

Complete Deployment Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:latest
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5
        resources:
          limits:
            cpu: "500m"
            memory: "256Mi"
          requests:
            cpu: "100m"
            memory: "128Mi"

Docker Health Check

Configure Docker health check in Dockerfile:

FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o main .

FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/main .
COPY config/ config/

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
  CMD wget -qO- http://localhost:8080/health || exit 1

CMD ["./main"]

Configuration Reference

Property Description Default
actuator.enabled Enable/disable actuator true

What’s Next?