From 62ccf4296c860c927327cb2d74f5ffe55ed62104 Mon Sep 17 00:00:00 2001 From: Maria Katsamperi Date: Mon, 27 Jan 2020 16:57:40 +0200 Subject: [PATCH 1/3] Add path in guzzle middleware --- src/GuzzleMiddleware.php | 7 ++++++- src/GuzzleServiceProvider.php | 2 +- tests/GuzzleMiddlewareTest.php | 28 ++++++++++++++++++++++++++-- tests/GuzzleServiceProviderTest.php | 2 +- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/GuzzleMiddleware.php b/src/GuzzleMiddleware.php index a05454d..a868e9e 100644 --- a/src/GuzzleMiddleware.php +++ b/src/GuzzleMiddleware.php @@ -10,6 +10,8 @@ class GuzzleMiddleware { + const DEFAULT_GUZZLE_MESSAGE = 'No route given'; + /** * @var Histogram */ @@ -36,13 +38,16 @@ public function __invoke(callable $handler) : callable return function (Request $request, array $options) use ($handler) { $start = microtime(true); return $handler($request, $options)->then( - function (Response $response) use ($request, $start) { + function (Response $response) use ($request, $start, $options) { + $path = !empty($options['actionURI']) ? $options['actionURI'] : self::DEFAULT_GUZZLE_MESSAGE; + $this->histogram->observe( microtime(true) - $start, [ $request->getMethod(), $request->getUri()->getHost(), $response->getStatusCode(), + $path ] ); return $response; diff --git a/src/GuzzleServiceProvider.php b/src/GuzzleServiceProvider.php index a33f755..3d968c7 100644 --- a/src/GuzzleServiceProvider.php +++ b/src/GuzzleServiceProvider.php @@ -22,7 +22,7 @@ public function register() : void return $app['prometheus']->getOrRegisterHistogram( 'guzzle_response_duration', 'Guzzle response duration histogram', - ['method', 'external_endpoint', 'status_code'] + ['method', 'external_endpoint', 'status_code', 'path'] ); }); $this->app->singleton('prometheus.guzzle.handler', function ($app) { diff --git a/tests/GuzzleMiddlewareTest.php b/tests/GuzzleMiddlewareTest.php index df84a1c..59e3ecf 100644 --- a/tests/GuzzleMiddlewareTest.php +++ b/tests/GuzzleMiddlewareTest.php @@ -31,6 +31,30 @@ public function testMiddleware() $client = new Client(['handler' => $stack]); $client->get('http://example.org'); $this->assertGreaterThan(0, $value); - $this->assertSame(['GET', 'example.org', 200], $labels); + $this->assertSame(['GET', 'example.org', 200, 'No route given'], $labels); } -} \ No newline at end of file + + public function testMiddlewareWithOptions() + { + $value = null; + $labels = null; + $observe = function (float $time, array $data) use (&$value, &$labels) { + $value = $time; + $labels = $data; + }; + $histogram = Mockery::mock(Histogram::class); + $histogram->shouldReceive('observe')->andReturnUsing($observe); + $middleware = new GuzzleMiddleware($histogram); + $stack = new HandlerStack(); + $stack->setHandler(new MockHandler([new Response()])); + $stack->push($middleware); + $client = new Client(['handler' => $stack]); + + $options = [ + 'actionURI' => '/test' + ]; + $client->get('http://example.org', $options); + $this->assertGreaterThan(0, $value); + $this->assertSame(['GET', 'example.org', 200, '/test'], $labels); + } +} diff --git a/tests/GuzzleServiceProviderTest.php b/tests/GuzzleServiceProviderTest.php index 8816919..99a7f02 100644 --- a/tests/GuzzleServiceProviderTest.php +++ b/tests/GuzzleServiceProviderTest.php @@ -31,7 +31,7 @@ public function testHistogramShouldHaveCorrectData() /* @var \Prometheus\Histogram $histogram */ $histogram = $this->app->get('prometheus.guzzle.client.histogram'); $this->assertInstanceOf(Histogram::class, $histogram); - $this->assertSame(['method', 'external_endpoint', 'status_code'], $histogram->getLabelNames()); + $this->assertSame(['method', 'external_endpoint', 'status_code', 'path'], $histogram->getLabelNames()); $this->assertSame('app_guzzle_response_duration', $histogram->getName()); $this->assertSame('Guzzle response duration histogram', $histogram->getHelp()); } From 3ae05da15f6ef3e03f6f09778b3c0faa5ecc3189 Mon Sep 17 00:00:00 2001 From: Maria Katsamperi Date: Mon, 27 Jan 2020 17:23:40 +0200 Subject: [PATCH 2/3] Change var name --- src/GuzzleMiddleware.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GuzzleMiddleware.php b/src/GuzzleMiddleware.php index a868e9e..a7c547f 100644 --- a/src/GuzzleMiddleware.php +++ b/src/GuzzleMiddleware.php @@ -10,7 +10,7 @@ class GuzzleMiddleware { - const DEFAULT_GUZZLE_MESSAGE = 'No route given'; + const NO_ROUTE = 'No route given'; /** * @var Histogram @@ -39,7 +39,7 @@ public function __invoke(callable $handler) : callable $start = microtime(true); return $handler($request, $options)->then( function (Response $response) use ($request, $start, $options) { - $path = !empty($options['actionURI']) ? $options['actionURI'] : self::DEFAULT_GUZZLE_MESSAGE; + $path = !empty($options['actionURI']) ? $options['actionURI'] : self::NO_ROUTE; $this->histogram->observe( microtime(true) - $start, From 3277748ca34204e1af47020ab68bcf487a0b0b85 Mon Sep 17 00:00:00 2001 From: Maria Katsamperi Date: Mon, 27 Jan 2020 18:52:50 +0200 Subject: [PATCH 3/3] Change message --- src/GuzzleMiddleware.php | 2 +- tests/GuzzleMiddlewareTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GuzzleMiddleware.php b/src/GuzzleMiddleware.php index a7c547f..375fb1d 100644 --- a/src/GuzzleMiddleware.php +++ b/src/GuzzleMiddleware.php @@ -10,7 +10,7 @@ class GuzzleMiddleware { - const NO_ROUTE = 'No route given'; + const NO_ROUTE = 'Undefined'; /** * @var Histogram diff --git a/tests/GuzzleMiddlewareTest.php b/tests/GuzzleMiddlewareTest.php index 59e3ecf..9e35626 100644 --- a/tests/GuzzleMiddlewareTest.php +++ b/tests/GuzzleMiddlewareTest.php @@ -31,7 +31,7 @@ public function testMiddleware() $client = new Client(['handler' => $stack]); $client->get('http://example.org'); $this->assertGreaterThan(0, $value); - $this->assertSame(['GET', 'example.org', 200, 'No route given'], $labels); + $this->assertSame(['GET', 'example.org', 200, 'Undefined'], $labels); } public function testMiddlewareWithOptions()