feat(metrics): add HTTP server metrics#30
Merged
jstojiljkovic merged 11 commits intoMay 12, 2026
Conversation
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
jstojiljkovic
approved these changes
May 10, 2026
| namespace Traceway\OpenTelemetryBundle\EventSubscriber; | ||
|
|
||
| use OpenTelemetry\API\Globals; | ||
| use OpenTelemetry\API\Metrics\CounterInterface; |
Collaborator
There was a problem hiding this comment.
nitpick: imported but never used
Adds automatic metrics for incoming HTTP requests, sibling of the existing
OpenTelemetrySubscriber (trace side). Off by default, gated on
metrics.http_server.enabled which requires metrics.enabled.
Four instruments emitted on main requests only (sub-requests are already
included in the main request duration):
- http.server.request.duration (Histogram, 's', Stable)
- http.server.active_requests (UpDownCounter, '{request}', Development)
- http.server.request.body.size (Histogram, 'By', Development)
- http.server.response.body.size (Histogram, 'By', Development)
Attributes follow OTel HTTP semantic conventions:
- http.request.method, url.scheme, server.address, server.port (required)
- http.route (conditional, once routing resolves)
- http.response.status_code (conditional, on response)
- error.type (conditional, on failure)
Body size histograms are emitted only when Content-Length is set.
Implementation mirrors ConsoleSubscriber: lazy MeterInterface resolved via
Globals::meterProvider(), instruments cached locally, ResetInterface to
clear cached state between worker iterations or long-running processes.
Per-request state lives in a WeakMap<Request, array> so dead requests are
garbage-collected naturally.
New nested config block:
metrics:
http_server:
enabled: false
excluded_paths: []
Validation rejects metrics.http_server.enabled without metrics.enabled.
Subscriber unit test (10 cases):
- subscribed events
- main request emits duration + active count
- route template substitution (/api/items/{id})
- exception adds error.type attribute
- excluded paths emit nothing
- sub-requests emit nothing
- body size histograms from Content-Length headers
- no body metrics without Content-Length
- active_requests increments on request, decrements on finish
- reset clears cached meter and pending request state
BundleBootTest additions:
- http_server disabled by default when metrics are enabled
- http_server enabled registers the subscriber
- validation rejects metrics.http_server.enabled without metrics.enabled
- Extends the Metrics section with an HTTP Server table listing the four instruments emitted, their stability status and attribute sets. - Updates the main config YAML block with metrics.http_server.enabled and metrics.http_server.excluded_paths. - Notes that only main requests are measured; sub-requests are already covered by the main request duration.
Same issue as the metrics foundation fix: iterator_to_array() on PHP 8.1 requires a strict Traversable, but the OTel SDK types dataPoints as iterable which can be either an array or a Traversable. The spread operator [...$x] accepts both and works on PHP 8.1+.
Use FQCN per OTel semconv with a parent-class fallback for anonymous classes, mirroring the messenger middleware fix.
…est.duration The OTel SDK defaults are tuned for milliseconds and collapse all sub-5s requests into the first bucket, making p95/p99 unusable.
…dling Wrap each event listener's instrument calls in a swallowing try/catch so that a broken meter provider cannot leak into Symfony's HttpKernel flow. Track active_counted only when the counter increment succeeded to keep up/down balance.
Replaces the inline anonymous-class fallback in the subscriber with a reusable Util\ErrorTypeResolver, so the messenger middleware and the HTTP client decorator can share the same canonical implementation.
Adds Configuration tests for the http_server.excluded_paths normalization closure (mixed string/non-string entries, prepended slash) and its default. Adds subscriber cases covering the data-null short-circuits (every event firing on an untracked main request) and extends the sub-request test to also exercise onRoute and onException.
srekcud
force-pushed
the
feat/metrics-http-server
branch
from
May 10, 2026 13:38
4bc8b9e to
d6d617c
Compare
Collaborator
|
If you could just resolve conflits and I will merge it, thanks 🙏 |
…-server # Conflicts: # README.md # src/DependencyInjection/Configuration.php # src/DependencyInjection/OpenTelemetryExtension.php # tests/Functional/BundleBootTest.php
Contributor
Author
|
My bad i forgot a push 🙈 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds OpenTelemetry metrics for incoming requests handled by the Symfony HttpKernel, sibling
of
OpenTelemetrySubscriberon the trace side. Off by default.Instruments
http.server.request.duration(Histogram,s) — semconv Stablehttp.server.active_requests(UpDownCounter,{request}) — Developmenthttp.server.request.body.size(Histogram,By) — Developmenthttp.server.response.body.size(Histogram,By) — DevelopmentAttributes:
http.request.method,url.scheme,http.route(from the route template, notthe raw path),
http.response.status_code,server.address,server.port,error.typeonfailure.
Config
Behaviour
kernel.exception / kernel.finish_request. Only main requests are measured (sub-requests are
already inside the main duration).
/api/items/42.
Notes
fallback, duration histogram declares second-based buckets, every event listener wraps its
instrument calls in a swallowing try/catch so a broken meter provider cannot leak into
Symfony's HttpKernel flow.
balanced even when the meter provider is broken on the increment path.
first lands it on master, the others rebase clean.
sub-request exclusion, body sizes, active count balance, broken-counter swallow.