Skip to content

httpcaddyfile: accept duration strings for log sampling interval - #7694

Merged
francislavoie merged 1 commit into
caddyserver:masterfrom
tomholford:th/log-sampling-interval-string
May 7, 2026
Merged

francislavoie merged 1 commit into
caddyserver:masterfrom
tomholford:th/log-sampling-interval-string

Conversation

@tomholford

@tomholford tomholford commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The Caddyfile adapter for log { sampling { interval ... } } rejects the
duration-string syntax shown in the docs. interval 1s (the example on the
log directive page)
currently fails with failed to parse interval: time: unknown unit "sns" in duration "1sns" because parseLogHelper appends "ns" to the literal token
before passing it to time.ParseDuration.

In practice the adapter only accepts bare integers (interpreted as
nanoseconds) — what the existing fixtures cover — but a sub-microsecond
sampling window isn't a useful configuration and isn't documented anywhere
user-facing. The result is a directive whose docs and implementation
disagree on every example.

Projected impact is small. A GitHub code search for non-fork Caddyfiles
that contain a sampling { ... interval ... first ... thereafter ... }
block returns zero hits outside caddyserver/caddy itself and its forks
(test fixtures, mirrors, fuzzing clones). Francis Lavoie noted in the
forum thread that motivated #6682

that sampling "is just not a very popular feature so I guess it just got
forgotten" — consistent with the docs/impl mismatch sitting unnoticed
since the directive shipped. Anyone who did configure a bare-integer
interval can append ns and get bit-identical output; the new error
message points them there.

⚠️ Breaking change. Configs that use bare integers for interval
(e.g. interval 100) will fail to parse after this change. They were
never documented and only made sense at nanosecond resolution. The fix
is to add a unit (100ns, 5m, etc.) — the error message now points
the user at this directly.

Changes

  • caddyconfig/httpcaddyfile/builtins.go — switch interval from
    time.ParseDuration(d.Val() + "ns") to caddy.ParseDuration(d.Val()).
    This matches the three other duration fields parsed in the same file
    (acme_dns propagation delay/timeout, ACME dns_ttl) and accepts the
    units documented for caddy.ParseDuration.
  • caddytest/integration/caddyfile_adapt/log_sampling.caddyfiletest and
    global_options_log_sampling.caddyfiletest — exercise the documented
    syntax (interval 5m → "interval": 300000000000).
  • caddyconfig/httpcaddyfile/builtins_test.go — TestLogDirectiveSyntax
    asserts a duration string (interval 2s → "interval": 2000000000).

Compatibility

Before After
interval 1s → ❌ parse error interval 1s → ✅ 1_000_000_000 ns
interval 100 → ✅ 100 ns (undocumented) interval 100 → ❌ parse error w/ guidance
interval 100ns → ❌ parse error interval 100ns → ✅ 100 ns

Motivation

I hit this trying to use interval 1s per the docs and traced it to the
"ns"-appending parser. Original feature work was #6682 / #6680.

Assistance Disclosure

Claude assisted with research, the code change, and the test updates; I
reviewed every diff, ran the build and tests against a freshly built
binary, and pushed back where appropriate.


Appendix: reproduce build + verify steps

What it verifies

  • The fork builds cleanly.
  • TestLogDirectiveSyntax (builtins_test.go) and the two *log_sampling.caddyfiletest adapt fixtures pass under the new duration-string syntax.
  • A locally built Caddy binary adapts the documented interval 1s form to "interval": 1000000000 (positive case).
  • The same binary rejects interval 100 with time: missing unit in duration "100" (negative case — confirms the breaking-change migration path is mechanical).

Reproduction steps

# 1. Check out the branch
git fetch origin pull/7694/head:pr-7694
git checkout pr-7694

# 2. Build
go build ./...

# 3. Targeted unit + adapt tests
go test -run TestLogDirectiveSyntax ./caddyconfig/httpcaddyfile/...
go test -run 'TestCaddyfileAdaptToJSON/(global_options_)?log_sampling' ./caddytest/integration/

# 4. End-to-end positive case — documented duration string
cat > /tmp/dur-str.caddy <<'EOF_CFG'
{
  log {
    sampling {
      interval 1s
      first 5
      thereafter 10
    }
  }
}
EOF_CFG
go run ./cmd/caddy adapt --config /tmp/dur-str.caddy --adapter caddyfile

# 5. End-to-end negative case — bare integer must now error with guidance
cat > /tmp/bare-int.caddy <<'EOF_CFG'
{
  log {
    sampling {
      interval 100
      first 1
      thereafter 200
    }
  }
}
EOF_CFG
go run ./cmd/caddy adapt --config /tmp/bare-int.caddy --adapter caddyfile

Observed result

$ go build ./...
(exit 0)

$ go test -run TestLogDirectiveSyntax ./caddyconfig/httpcaddyfile/...
ok  	github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile  0.627s

$ go test -run 'TestCaddyfileAdaptToJSON/(global_options_)?log_sampling' ./caddytest/integration/
=== RUN   TestCaddyfileAdaptToJSON/global_options_log_sampling.caddyfiletest
=== RUN   TestCaddyfileAdaptToJSON/log_sampling.caddyfiletest
PASS

$ go run ./cmd/caddy adapt --config /tmp/dur-str.caddy --adapter caddyfile
{"logging":{"logs":{"default":{"sampling":{"interval":1000000000,"first":5,"thereafter":10}}}}}

$ go run ./cmd/caddy adapt --config /tmp/bare-int.caddy --adapter caddyfile
Error: parsing caddyfile tokens for 'log': failed to parse interval: time: missing unit in duration "100", at /tmp/bare-int.caddy:4

The Caddyfile adapter for `log { sampling { interval ... } }` was
appending "ns" to the literal value, so the docs example
`interval 1s` failed to parse. Switch to caddy.ParseDuration, which
matches every other duration field in the same file and lets users
write the syntax shown in the official docs.

Updates the two adapt fixtures and TestLogDirectiveSyntax to use
duration strings so they exercise the documented form.

BREAKING CHANGE: configs using bare integers for the sampling
interval (e.g. `interval 100`) now error with "missing unit in
duration"; append a unit (`100ns`, `5m`, etc.) to migrate.
@tomholford
tomholford marked this pull request as ready for review May 7, 2026 18:03

@francislavoie francislavoie left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hah, interesting. LGTM

The backwards compat break is unfortunate but it was nonsense anyway I think.

@francislavoie francislavoie added the bug 🐞 Something isn't working label May 7, 2026
@francislavoie francislavoie added this to the v2.11.3 milestone May 7, 2026
@francislavoie
francislavoie enabled auto-merge (squash) May 7, 2026 18:26
@francislavoie
francislavoie merged commit 0780d44 into caddyserver:master May 7, 2026
26 checks passed
@tomholford
tomholford deleted the th/log-sampling-interval-string branch May 8, 2026 20:52
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
caddy 2.11.3

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.

- [ ] `resource` blocks have been checked for updates.
<details>
  <summary>release notes</summary>
  <pre>This release improves several aspects of Caddy with minor features, bug fixes, and security patches. Thank you to everyone and their bots who contributed to help make this release the best one yet!

**Security patches:**

- fastcgi: Carrying over a patch from FrankenPHP for a bug that could allow non-PHP files to be executed; collaborated on by @dunglas, @KC1zs4, and @chenjj.
- vars: A more thorough fix for GHSA-m2w3-8f23-hxxf, collaborated by @everping and @vnxme.
- admin: Array index normalization to prevent remote admin socket auth bypass, by @Amemoyoi and bot.
- admin: More rigorous path prefix matching to prevent remote admin socket auth bypass, by @Amemoyoi and bot.

We've also merged a couple PRs that fix upstream security bugs in other projects like quic-go and CertMagic. Thank you to @marten-seemann for maintaining quic-go so diligently!

## What's Changed
* caddyhttp: Sync placeholder expansion in `vars` and `vars_regexp` by @vnxme in caddyserver/caddy#7573
* caddytls: Avoid ACME fallback for implicit Tailscale *.ts.net policies by @steadytao in caddyserver/caddy#7577
* chore: Resolve recent CI failures by @mholt in caddyserver/caddy#7593
* caddytls: Consolidate empty APs more smartly by @mholt in caddyserver/caddy#7567
* rewrite: skip query rename when source key is absent by @steadytao in caddyserver/caddy#7599
* root: introduce down-propagating Helper.BlockState for other directives/plugins to use by @henderkes in caddyserver/caddy#7594
* http: make zstd checksum configurable by @ottenhoff in caddyserver/caddy#7586
* notify: Always send "READY=1" even after an error by @francislavoie in caddyserver/caddy#7597
* reverseproxy: Fix check for `header_up Host {upstream_hostport}` redundancy by @yubiuser in caddyserver/caddy#7564
* caddytls: Expand placeholders in dns_challenge override_domain tls parameter by @pberkel in caddyserver/caddy#7609
* tls: add system and combined CA pool modules by @HarshPatel5940 in caddyserver/caddy#7406
* vars: Don't expand placeholders in values by @vnxme in caddyserver/caddy#7629
* build(deps): bump go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp from 1.42.0 to 1.43.0 by @dependabot[bot] in caddyserver/caddy#7637
* build(deps): bump the all-updates group across 1 directory with 11 updates by @dependabot[bot] in caddyserver/caddy#7641
* reverseproxy: make stream copy buffer size configurable by @steadytao in caddyserver/caddy#7627
* vars: Add matcher placeholder handling tests by @steadytao in caddyserver/caddy#7640
* build(deps): bump github.com/go-jose/go-jose/v4 from 4.1.3 to 4.1.4 by @dependabot[bot] in caddyserver/caddy#7621
* logging: Add journald encoder wrapper by @steadytao in caddyserver/caddy#7623
* caddyfile: Improve import/global options UX for imports before global options by @steadytao in caddyserver/caddy#7642
* chore: replace `interface{}` with `any` for modernization by @tsinglua in caddyserver/caddy#7571
* chore: bump timberjack to v1.4.1 by @DeRuina in caddyserver/caddy#7618
* logging: Preserve ts for journald-wrapped JSON logs by @steadytao in caddyserver/caddy#7644
* fileserver: show symlink targets verbatim (#7476) by @maxtruxa in caddyserver/caddy#7579
* fix(caddyfile): {block} in snippet by @prettysunflower in caddyserver/caddy#7558
* caddyhttp: Document missing placeholders for escaped URI and prefixed query by @steffenbusch in caddyserver/caddy#7659
* chore: add `AGENTS.md` by @mohammed90 in caddyserver/caddy#7652
* build(deps): bump github.com/jackc/pgx/v5 from 5.8.0 to 5.9.0 by @dependabot[bot] in caddyserver/caddy#7655
* admin: Redact sensitive request headers in API logs by @steadytao in caddyserver/caddy#7578
* reverseproxy: add `lb_retry_match` condition on response status by @seroperson in caddyserver/caddy#7569
* caddyhttp: prefer port 443 in auto-HTTPS and add tests by @mholt in caddyserver/caddy#7666
* fix: Propagate ECH keys to the QUIC listener by @steadytao in caddyserver/caddy#7670
* chore: Use atomics where appropriate by @francislavoie in caddyserver/caddy#7648
* metrics: Implement pushing via OLTP by @dunglas in caddyserver/caddy#7664
* logging: Add regression coverage for rotated file mode by @steadytao in caddyserver/caddy#7620
* httpcaddyfile: Inherit global ACME issuer settings in tls shortcuts by @steadytao in caddyserver/caddy#7617
* build(deps): bump github.com/jackc/pgx/v5 from 5.9.0 to 5.9.2 by @dependabot[bot] in caddyserver/caddy#7668
* admin: require path segment boundary in remote access control by @Amemoyoi in caddyserver/caddy#7673
* reverseproxy: Add ability to clear dynamic upstreams cache during retries by @mholt in caddyserver/caddy#7662
* listeners: clean up stale Unix socket files on Windows by @mfrischknecht in caddyserver/caddy#7676
* admin: reject non-canonical config array indices by @Amemoyoi in caddyserver/caddy#7592
* caddytls: Expand ACME credentials by @tribut in caddyserver/caddy#7554
* caddyauth: set user placeholders before auth rejection by @cyphercodes in caddyserver/caddy#7685
* caddyauth: revert user placeholders on auth rejection by @steadytao in caddyserver/caddy#7688
* chore: Fix golangci-lint 2.12.1 findings by @steadytao in caddyserver/caddy#7690
* httpcaddyfile: accept duration strings for log sampling interval by @tomholford in caddyserver/caddy#7694
* tls: Add alpn to managed HTTPS records by @steadytao in caddyserver/caddy#7653
* caddytls: avoid duplicate automation for wildcard-covered hosts by @Rijul-A in caddyserver/caddy#7697
* docs: add documentation for fileExists and fileStat template functions by @steffenbusch in caddyserver/caddy#7700
* rewrite: escape file matcher paths before rewriting by @cyphercodes in caddyserver/caddy#7683
* metrics: Add nil check for metricsHandler in AdminMetrics.serveHTTP by @Br1an67 in caddyserver/caddy#7553

## New Contributors
* @steadytao made their first contribution in caddyserver/caddy#7577
* @henderkes made their first contribution in caddyserver/caddy#7594
* @yubiuser made their first contribution in caddyserver/caddy#7564
* @pberkel made their first contribution in caddyserver/caddy#7609
* @HarshPatel5940 made their first contribution in caddyserver/caddy#7406
* @tsinglua made their first contribution in caddyserver/caddy#7571
* @maxtruxa made their first contribution in caddyserver/caddy#7579
* @seroperson made their first contribution in caddyserver/caddy#7569
* @Amemoyoi made their first contribution in caddyserver/caddy#7673
* @mfrischknecht made their first contribution in caddyserver/caddy#7676
* @tribut made their first contribution in caddyserver/caddy#7554
* @cyphercodes made their first contribution in caddyserver/caddy#7685
* @tomholford made their first contribution in caddyserver/caddy#7694
* @Rijul-A made their first contribution in caddyserver/caddy#7697
* @Br1an67 made their first contribution in caddyserver/caddy#7553

**Full Changelog**: https://github.com/caddyserver/caddy/compare/v2.11.2...v2.11.3</pre>
  <p>View the full release notes at <a href="https://github.com/caddyserver/caddy/releases/tag/v2.11.3">https://github.com/caddyserver/caddy/releases/tag/v2.11.3</a>.</p>
</details>
<hr>



See merge request: Harmonybrew/homebrew-core!842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants