Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse_proxy: add placeholder http.reverse_proxy.retries #6553

Merged
merged 3 commits into from
Aug 30, 2024

Conversation

steffenbusch
Copy link
Contributor

@steffenbusch steffenbusch commented Aug 29, 2024

The reverse_proxy directive in Caddy already supports a variety of useful placeholders, as documented here

This pull request proposes the addition of a new placeholder: {http.reverse_proxy.retries}. This placeholder tracks the number of retries actually performed to communicate with an upstream. I believe this would be a valuable enhancement, providing more detailed insights into load balancing behavior.

For example, in a Caddyfile, this placeholder might be used with log_append like this:

:8080 {
	log {
		output file /var/log/access.json
	}

	reverse_proxy {
		to localhost:7070
		to localhost:7071
		to localhost:7072

		health_uri /ping
		health_body pong
		health_interval 15s

		lb_policy random
		lb_try_duration 5s
		lb_try_interval 700ms
	}

	log_append * rp_upstream_host "{http.reverse_proxy.upstream.hostport}"
	log_append * rp_upstream_duration_ms "{http.reverse_proxy.upstream.duration_ms}"
	log_append * rp_upstream_latency_ms "{http.reverse_proxy.upstream.latency_ms}"

	log_append * rp_retries "{http.reverse_proxy.retries}"
}

In the access.json, this would then appear like this:

{
  "level": "info",
  "ts": 1725040156.150035,
  "logger": "http.log.access.log1",
  "msg": "handled request",
  "request": {
...
    "tls": {
      "resumed": true,
      "version": "TLSv1.3",
      "cipher_suite": "TLS_AES_128_GCM_SHA256",
      "proto": "h3",
      "server_name": "healthchecks.rocky.stbu.net"
    }
  },
  "bytes_read": 0,
  "user_id": "",
  "duration": 3.890395979,
  "size": 9449,
  "status": 200,
  "resp_headers": {
  ...
  },
  "rp_retries": 3,
  "rp_upstream_latency_ms": 1787.609998,
  "rp_upstream_duration_ms": 1787.975152,
  "rp_upstream_host": "localhost:7072"
}

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; yeah if this works for you I guess we can do this. Just one nit on the naming.

modules/caddyhttp/reverseproxy/reverseproxy.go Outdated Show resolved Hide resolved
@mholt mholt changed the title reverse_proxy: add placeholder http.reverse_proxy.lb.retries reverse_proxy: add placeholder http.reverse_proxy.retries Aug 30, 2024
@mholt mholt merged commit c050a37 into caddyserver:master Aug 30, 2024
30 of 33 checks passed
@steffenbusch steffenbusch deleted the num_retries branch August 30, 2024 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants