Skip to content

Commit

Permalink
Merge pull request #3 from auwaerter/master
Browse files Browse the repository at this point in the history
Migration to a PSR-15 Middleware for Flow 8 compatibility
  • Loading branch information
robertlemke authored Jan 15, 2025
2 parents 0a3badc + 23af1c3 commit 94232f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
24 changes: 15 additions & 9 deletions Classes/HttpMetricsCollectorComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
*/

use Flownative\Prometheus\CollectorRegistry;
use Neos\Flow\Http\Component\ComponentContext;
use Neos\Flow\Http\Component\ComponentInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;

/**
* HTTP component which collects HTTP-related metrics for Prometheus
* PSR-15 Middleware that collects HTTP-related metrics for Prometheus
*/
class HttpMetricsCollectorComponent implements ComponentInterface
class HttpMetricsCollectorComponent implements MiddlewareInterface
{
/**
* @var CollectorRegistry
Expand All @@ -32,14 +34,18 @@ public function injectCollectorRegistry(CollectorRegistry $collectorRegistry): v
$this->collectorRegistry = $collectorRegistry;
}

/**
* @param ComponentContext $componentContext
*/
public function handle(ComponentContext $componentContext): void
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// Handle the request and get the response from the next middleware in the chain
$response = $handler->handle($request);

// Collect metrics based on the HTTP response status
$this->collectorRegistry->getCounter('neos_flow_http_requests_total')
->inc(1, [
'status' => $componentContext->getHttpResponse()->getStatusCode()
'status' => $response->getStatusCode()
]);

// Return the processed response
return $response;
}
}
11 changes: 4 additions & 7 deletions Configuration/Settings.Http.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
Neos:
Flow:
http:
chain:
'postprocess':
chain:
'Flownative.Prometheus:metricsExporter':
'position': 'end'
component: 'Flownative\Prometheus\FlowMetrics\HttpMetricsCollectorComponent'
componentOptions: []
middlewares:
'metrics':
position: 'start'
middleware: 'Flownative\Prometheus\FlowMetrics\HttpMetricsCollectorComponent'
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "flownative/prometheus-flow-metrics",
"license": "MIT",
"type": "neos-package",
"description": "Prometheus metrics for Neos Flow applications",
"license": "MIT",
"authors": [
{
"name": "Robert Lemke",
Expand All @@ -12,7 +12,7 @@
}
],
"require": {
"neos/flow": "5.* || 6.* || @dev",
"neos/flow": "7.* || 8.* || @dev",
"flownative/prometheus": "0.* || @dev"
},
"suggest": {
Expand Down

0 comments on commit 94232f9

Please sign in to comment.