Server Timing - Allow description and make duration optional#2379
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## trunk #2379 +/- ##
==========================================
+ Coverage 69.64% 69.72% +0.07%
==========================================
Files 90 90
Lines 7736 7753 +17
==========================================
+ Hits 5388 5406 +18
+ Misses 2348 2347 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…and improve type definitions - Replace string|mixed param with native string type hint in set_description(), removing manual type check - Explicitly initialize $description = null - Combine addcslashes() calls into single expression - Add @Covers annotations to new description test methods - Import MetricArguments phpstan type and replace mixed with array<string, MetricArguments> in test param annotations
There was a problem hiding this comment.
Pull request overview
Updates the Performance Lab Server-Timing API to better match the Server-Timing header spec by allowing metrics to include an optional description (desc) and by making duration (dur) optional when formatting header segments.
Changes:
- Add
descriptionsupport toPerflab_Server_Timing_Metricvia new setter/getter. - Update Server-Timing header formatting to conditionally include
dur=and/ordesc=and sanitizedescfor safe header output. - Extend unit tests to cover duration+description combinations and escaping/CRLF stripping edge cases.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| plugins/performance-lab/includes/server-timing/class-perflab-server-timing.php | Formats metric segments with optional dur and desc, including description sanitization. |
| plugins/performance-lab/includes/server-timing/class-perflab-server-timing-metric.php | Adds a new $description field plus set_description() / get_description(). |
| plugins/performance-lab/tests/includes/server-timing/test-perflab-server-timing.php | Adds tests for header output with descriptions and special-character edge cases. |
| plugins/performance-lab/tests/includes/server-timing/test-perflab-server-timing-metric.php | Adds unit tests for description getter/setter behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // If neither value nor description is set, skip this metric. | ||
| if ( null === $value && null === $description ) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
The PR description mentions supporting a “name only” header format (e.g. wp-metric), but this implementation still skips metrics when both duration and description are null (returns null), so name-only metrics will never be emitted. If MDN compliance is the goal, consider returning the metric name when both are null and adding a corresponding test case.
There was a problem hiding this comment.
I'll update it to remove the "name only" format since that was inaccurate.Metric with neither duration nor description provides no useful information in the header. This PR only adds description as a new optional field alongside the existing optional duration.
There was a problem hiding this comment.
Metric with neither duration nor description provides no useful information in the header.
Is this so? In the MDN docs, this example is provided:
// A metric with a name only
Server-Timing: missedCache
There was a problem hiding this comment.
@westonruter Added support for name only.
…n and improved performance.
|
@westonruter I’ve added support for name-only metrics as suggested. When you get a chance, could you please take a look? |
Replace the vague `@return array<string, mixed>` on the new `data_get_header_with_description()` and `data_get_header_with_description_edge_cases()` data providers with the precise `[expected, metrics]` tuple shape, reusing the imported `MetricArguments` type. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Cover the `_doing_it_wrong` guard in `Perflab_Server_Timing_Metric::set_description()`, mirroring the existing `set_value` guard test. Default-metric registration is detached before firing `perflab_server_timing_send_header` so the test does not mutate the shared singleton. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
`set_description()` now normalizes an empty string to `null` so the metric is emitted without a `desc` parameter rather than an empty `desc=""`. The `@param` docblock is relaxed from `non-empty-string` to `string` to match the actual contract. Adds unit and `get_header()` coverage for the empty-description case. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Strip the full C0 control range plus DEL (not just CR/LF) before escaping, since those characters are not representable in an RFC 7230 quoted-string. Also skip the `desc` parameter entirely when the description reduces to an empty string after sanitization, preserving the no-empty-desc invariant for control-character-only input. Adds coverage for both cases. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Summary
Fixes #879
Relevant technical choices
Enhance Server-Timing API for MDN Specification Compliance
Enhances the Server-Timing API to be fully compliant with the MDN Server-Timing specification by making
durationoptional and adding support for adesc(description) field.Changes
class-perflab-server-timing-metric.php$descriptionproperty toPerflab_Server_Timing_Metricset_description()andget_description()methodsstringtype hint forset_description()instead ofstring|mixedwith manual type checking$description = nullfor clarityclass-perflab-server-timing.phpformat_metric_header_value()to conditionally includedur=anddesc=parameters\and"addcslashes()callTests
@coversannotations to all new test methods",\,\n,\r)@phpstan-param array<string, mixed>with:@param array<string, MetricArguments>@phpstan-import-typefor accurate type definitionsSupported Header Formats
wp-metric;dur=45.23wp-metric;desc="Cache hit"wp-metric;dur=45.23;desc="Database queries"Testing
Fixes
Fixes #879
Use of AI Tools
Github Copilot CLI was used to write tests. Reviewed the code afterwards.