Skip to content

[HttpKernel] Add a "short" trace header format, make header configurable #30964

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

Merged
merged 1 commit into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CHANGELOG
* renamed `GetResponseForExceptionEvent` to `ExceptionEvent`
* renamed `PostResponseEvent` to `TerminateEvent`
* added `HttpClientKernel` for handling requests with an `HttpClientInterface` instance
* added `trace_header` and `trace_level` configuration options to `HttpCache`

4.2.0
-----
Expand Down
36 changes: 33 additions & 3 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
*
* The available options are:
*
* * debug: If true, the traces are added as a HTTP header to ease debugging
* * debug If true, exceptions are thrown when things go wrong. Otherwise, the cache
* will try to carry on and deliver a meaningful response.
*
* * trace_level May be one of 'none', 'short' and 'full'. For 'short', a concise trace of the
* master request will be added as an HTTP header. 'full' will add traces for all
* requests (including ESI subrequests). (default: 'full' if in debug; 'none' otherwise)
*
* * trace_header Header name to use for traces. (default: X-Symfony-Cache)
*
* * default_ttl The number of seconds that a cache entry should be considered
* fresh when no explicit freshness information is provided in
Expand Down Expand Up @@ -87,7 +94,13 @@ public function __construct(HttpKernelInterface $kernel, StoreInterface $store,
'allow_revalidate' => false,
'stale_while_revalidate' => 2,
'stale_if_error' => 60,
'trace_level' => 'none',
'trace_header' => 'X-Symfony-Cache',
], $options);

if (!isset($options['trace_level']) && $this->options['debug']) {
$this->options['trace_level'] = 'full';
}
}

/**
Expand All @@ -110,6 +123,23 @@ public function getTraces()
return $this->traces;
}

private function addTraces(Response $response)
{
$traceString = null;

if ('full' === $this->options['trace_level']) {
$traceString = $this->getLog();
}

if ('short' === $this->options['trace_level'] && $masterId = array_key_first($this->traces)) {
$traceString = implode('/', $this->traces[$masterId]);
}

if (null !== $traceString) {
$response->headers->add([$this->options['trace_header'] => $traceString]);
}
}

/**
* Returns a log message for the events of the last request processing.
*
Expand Down Expand Up @@ -194,8 +224,8 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ

$this->restoreResponseBody($request, $response);

if (HttpKernelInterface::MASTER_REQUEST === $type && $this->options['debug']) {
$response->headers->set('X-Symfony-Cache', $this->getLog());
if (HttpKernelInterface::MASTER_REQUEST === $type) {
$this->addTraces($response);
}

if (null !== $this->surrogate) {
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,44 @@ public function testUsesOriginalRequestForSurrogate()
// Surrogate request
$cache->handle($request, HttpKernelInterface::SUB_REQUEST);
}

public function testTraceHeaderNameCanBeChanged()
{
$this->cacheConfig['trace_header'] = 'X-My-Header';
$this->setNextResponse();
$this->request('GET', '/');

$this->assertTrue($this->response->headers->has('X-My-Header'));
}

public function testTraceLevelDefaultsToFullIfDebug()
{
$this->setNextResponse();
$this->request('GET', '/');

$this->assertTrue($this->response->headers->has('X-Symfony-Cache'));
$this->assertEquals('GET /: miss', $this->response->headers->get('X-Symfony-Cache'));
}

public function testTraceLevelDefaultsToNoneIfNotDebug()
{
$this->cacheConfig['debug'] = false;
$this->setNextResponse();
$this->request('GET', '/');

$this->assertFalse($this->response->headers->has('X-Symfony-Cache'));
}

public function testTraceLevelShort()
{
$this->cacheConfig['trace_level'] = 'short';

$this->setNextResponse();
$this->request('GET', '/');

$this->assertTrue($this->response->headers->has('X-Symfony-Cache'));
$this->assertEquals('miss', $this->response->headers->get('X-Symfony-Cache'));
}
}

class TestKernel implements HttpKernelInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public function request($method, $uri = '/', $server = [], $cookies = [], $esi =

$this->store = new Store(sys_get_temp_dir().'/http_cache');

$this->cacheConfig['debug'] = true;
if (!isset($this->cacheConfig['debug'])) {
$this->cacheConfig['debug'] = true;
}

$this->esi = $esi ? new Esi() : null;
$this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"symfony/http-foundation": "^4.1.1",
"symfony/debug": "~3.4|~4.0",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-php73": "^1.9",
"psr/log": "~1.0"
},
"require-dev": {
Expand Down