Skip to content
agentgateway has joined the Agentic AI FoundationLearn more

For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.

Page as Markdown

Fault injection

Verified Code examples on this page have been automatically tested and verified.

Inject artificial latency into requests to test how your clients and services handle slow responses.

Inject artificial latency into requests with an AgentgatewayPolicy to test how your clients and upstream services handle slow responses. Fault injection is useful for verifying that timeouts, retries, and client-side deadlines behave as expected. To learn more, you can use fault injection alongside Timeouts and Retries.

The injected delay counts against the request timeout. If the delay is longer than the configured request timeout, the request times out.

Before you begin

  1. Set up an agentgateway proxy.
  2. Install the httpbin sample app.

Inject a delay

  1. Create an HTTPRoute for the httpbin app.

    kubectl apply -n httpbin -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: httpbin-delay
      namespace: httpbin
    spec:
      hostnames:
      - faultinjection.example
      parentRefs:
      - name: agentgateway-proxy
        namespace: agentgateway-system
      rules:
      - backendRefs:
        - kind: Service
          name: httpbin
          port: 8000
    EOF
  2. Create an AgentgatewayPolicy that injects a fixed 2-second delay into the requests that the HTTPRoute handles.

    kubectl apply -f- <<EOF
    apiVersion: agentgateway.dev/v1alpha1
    kind: AgentgatewayPolicy
    metadata:
      name: httpbin-delay
      namespace: httpbin
    spec:
      targetRefs:
      - group: gateway.networking.k8s.io
        kind: HTTPRoute
        name: httpbin-delay
      traffic:
        delay:
          duration: 2s
    EOF
    Review the following table to understand this configuration.
    FieldDescription
    targetRefsThe route, gateway, or listener to apply the delay to. In this example, the policy targets the httpbin-delay HTTPRoute.
    traffic.delay.durationThe latency to inject before the request is forwarded to the backend. Set either a duration string, such as 2s or 500ms, or a CEL expression that is evaluated against the request. For more information, see Inject a probabilistic or random delay.
  3. Send a request along the route and verify that the response is delayed by about 2 seconds.

    curl -s -o /dev/null -w "%{time_total}s\n" http://$INGRESS_GW_ADDRESS/get -H "host: faultinjection.example"

    Example output:

    2.01s
    
  4. Optional: Verify that the delay policy is applied in the proxy configuration.

    1. Port-forward the gateway proxy on port 15000.

      kubectl port-forward deploy/agentgateway-proxy -n agentgateway-system 15000
    2. Find the delay policy in the config dump.

      curl -s http://localhost:15000/config_dump | jq '[.policies[] | select(.policy.traffic.delay?)] | .[0]'

Inject a probabilistic or random delay

Because duration accepts a CEL expression, you can inject latency into only a subset of requests, or add jitter. The expression is evaluated against each request and returns either a duration or a number that is interpreted as milliseconds. A non-positive result injects no delay.

ExpressionEffect
duration("500ms")A fixed 500ms delay, expressed as a CEL duration.
random() < 0.1 ? 500 : 0A 500ms delay on approximately 10% of requests, and no delay otherwise.
int(random() * 500.0)A random delay between 0 and 500ms (jitter) on every request.

For example, the following policy delays approximately 10% of requests by 500ms.

kubectl apply -f- <<EOF
apiVersion: agentgateway.dev/v1alpha1
kind: AgentgatewayPolicy
metadata:
  name: httpbin-delay
  namespace: httpbin
spec:
  targetRefs:
  - group: gateway.networking.k8s.io
    kind: HTTPRoute
    name: httpbin-delay
  traffic:
    delay:
      duration: "random() < 0.1 ? 500 : 0"
EOF

Cleanup

You can remove the resources that you created in this guide. Run the following commands.

kubectl delete httproute httpbin-delay -n httpbin
kubectl delete AgentgatewayPolicy httpbin-delay -n httpbin
Was this page helpful?
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.