chaos

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const HeaderDashboard = "X-Gopherstack-Dashboard"

HeaderDashboard is the HTTP request header that dashboard clients set to bypass chaos fault injection and network effects. When this header is present with the value "true", the chaos middleware passes the request through unchanged.

Variables

This section is empty.

Functions

func Middleware

func Middleware(store *FaultStore) func(echo.HandlerFunc) echo.HandlerFunc

Middleware returns an Echo middleware that evaluates fault rules and network effects from the provided FaultStore against each incoming AWS API request.

The middleware is intended to be registered via Registry.Use() so that it runs as an outer wrapper around the telemetry+handler chain. It extracts the AWS service, operation, and region directly from the HTTP request headers — specifically the SigV4 Authorization header (for service and region) and the X-Amz-Target header (for JSON-protocol service operations) — so that it does not depend on any context values that are set only after the handler is called.

Request lifecycle when a fault fires:

  1. Middleware extracts service / operation / region from request headers.
  2. FaultStore.Match selects the first matching rule.
  3. FaultRule.ShouldTrigger applies the probability check.
  4. If triggered, an error response is written and the chain is short-circuited.

Network effects (latency) are applied regardless of fault injection and run before the rest of the handler chain.

func RegisterRoutes

func RegisterRoutes(group *echo.Group, store *FaultStore, registry *service.Registry)

RegisterRoutes mounts the chaos REST API under the /_gopherstack/chaos prefix.

  • GET /_gopherstack/chaos/query — return combined state (faults + effects + activity)
  • GET /_gopherstack/chaos/faults — return current fault rules
  • POST /_gopherstack/chaos/faults — replace entire fault configuration
  • PATCH /_gopherstack/chaos/faults — append rules to existing configuration
  • DELETE /_gopherstack/chaos/faults — remove matching rules
  • POST /_gopherstack/chaos/faults/clear — clear all fault rules
  • DELETE /_gopherstack/chaos/faults/by-index — remove a fault rule by index
  • GET /_gopherstack/chaos/effects — return current network effect settings
  • POST /_gopherstack/chaos/effects — update network effect configuration
  • POST /_gopherstack/chaos/effects/reset — reset network effects to defaults
  • GET /_gopherstack/chaos/targets — return auto-discovered injectable targets
  • GET /_gopherstack/chaos/activity — return recent fault injection events

Types

type ActivityEvent

type ActivityEvent struct {
	Timestamp    time.Time `json:"timestamp"`
	Service      string    `json:"service"`
	Operation    string    `json:"operation"`
	Region       string    `json:"region"`
	FaultApplied string    `json:"faultApplied"`
	Probability  float64   `json:"probability"`
	Triggered    bool      `json:"triggered"`
}

ActivityEvent records a single fault injection event emitted by the middleware.

type FaultError

type FaultError struct {
	Code       string `json:"code"`
	StatusCode int    `json:"statusCode"`
}

FaultError defines a custom HTTP error response returned when a fault is triggered.

type FaultRule

type FaultRule struct {
	Error       *FaultError `json:"error,omitempty"`
	Service     string      `json:"service,omitempty"`
	Region      string      `json:"region,omitempty"`
	Operation   string      `json:"operation,omitempty"`
	Probability float64     `json:"probability,omitempty"`
}

FaultRule defines a single fault injection rule. All fields are optional; omitting a field means "match any". Rules are evaluated sequentially; the first match wins.

func (FaultRule) EffectiveError

func (r FaultRule) EffectiveError() FaultError

EffectiveError returns the FaultError to use, falling back to the default.

func (FaultRule) ShouldTrigger

func (r FaultRule) ShouldTrigger() bool

ShouldTrigger reports whether this fault should fire based on its probability. A probability of 0 is treated as 1.0 (always fire).

type FaultStore

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

FaultStore is a thread-safe store for fault rules and network effects.

func NewFaultStore

func NewFaultStore() *FaultStore

NewFaultStore creates a new empty FaultStore.

func (*FaultStore) AppendRules

func (s *FaultStore) AppendRules(rules []FaultRule)

AppendRules appends rules to the existing rule list.

func (*FaultStore) DeleteRuleByIndex

func (s *FaultStore) DeleteRuleByIndex(index int)

DeleteRuleByIndex removes the fault rule at the given index. If the index is out of range, the call is a no-op.

func (*FaultStore) DeleteRules

func (s *FaultStore) DeleteRules(rules []FaultRule)

DeleteRules removes rules that match any rule in the provided list. Two rules match when all non-empty fields are equal.

func (*FaultStore) GetActivity

func (s *FaultStore) GetActivity() []ActivityEvent

GetActivity returns a copy of the activity log in reverse-chronological order (newest first).

func (*FaultStore) GetEffects

func (s *FaultStore) GetEffects() NetworkEffects

GetEffects returns a copy of the current network effects.

func (*FaultStore) GetRules

func (s *FaultStore) GetRules() []FaultRule

GetRules returns a copy of the current fault rules.

func (*FaultStore) Match

func (s *FaultStore) Match(svc, op, region string) (FaultRule, bool)

Match finds the first fault rule that matches the given service, operation, and region. Returns the matched rule and true, or a zero FaultRule and false if no rule matches.

func (*FaultStore) RecordActivity

func (s *FaultStore) RecordActivity(event ActivityEvent)

RecordActivity appends an activity event to the ring buffer. When the buffer exceeds activityLogMaxSize, the oldest entries are discarded. A fresh backing slice is allocated to allow the GC to reclaim old entries.

func (*FaultStore) SetEffects

func (s *FaultStore) SetEffects(effects NetworkEffects)

SetEffects replaces the current network effects.

func (*FaultStore) SetRules

func (s *FaultStore) SetRules(rules []FaultRule)

SetRules replaces the entire fault rule list.

type LatencyRange

type LatencyRange struct {
	Min int `json:"min"`
	Max int `json:"max"`
}

LatencyRange defines a min/max range for randomized latency.

type NetworkEffects

type NetworkEffects struct {
	LatencyRange *LatencyRange `json:"latencyRange,omitempty"`
	Latency      int           `json:"latency,omitempty"`
	Jitter       int           `json:"jitter,omitempty"`
}

NetworkEffects defines dynamic network simulation parameters. This supersedes the static LATENCY_MS configuration at runtime when any field is non-zero.

func (NetworkEffects) TotalDelayMs

func (n NetworkEffects) TotalDelayMs() int

TotalDelayMs computes the total simulated delay in milliseconds from the network effects configuration. Returns 0 when no delay is configured.

Jump to

Keyboard shortcuts

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