PHPackages                             mmo/guzzle-metrics-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. mmo/guzzle-metrics-middleware

ActiveLibrary[HTTP &amp; Networking](/categories/http)

mmo/guzzle-metrics-middleware
=============================

00PHPCI passing

Since Jul 11Pushed 1mo agoCompare

[ Source](https://github.com/morawskim/guzzle-metrics-middleware)[ Packagist](https://packagist.org/packages/mmo/guzzle-metrics-middleware)[ RSS](/packages/mmo-guzzle-metrics-middleware/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (2)Used By (0)

Guzzle Metrics Middleware
=========================

[](#guzzle-metrics-middleware)

A Guzzle HTTP client middleware that measures the duration of requests and collects metrics. It helps in monitoring external service calls by tracking request execution time, HTTP methods, target URIs, and response status codes.

Features
--------

[](#features)

- **Duration Tracking**: Measures the time elapsed for each request (in milliseconds).
- **Metric Abstraction**: Decoupled from specific monitoring systems through the `DurationMetricCollectorInterface`.
- **Prometheus Support**: Includes a collector for Prometheus (using `promphp/prometheus_client_php`).
- **Path Normalization**: Supports URI path templates to prevent high cardinality in metrics (e.g., grouping `/users/1` and `/users/2` under `/users/`).

Installation
------------

[](#installation)

```
composer require mmo/guzzle-metrics-middleware
```

Usage
-----

[](#usage)

### 1. Initialize the Collector

[](#1-initialize-the-collector)

You need a collector that implements `DurationMetricCollectorInterface`.

### Prometheus

[](#prometheus)

For Prometheus, you can use `PrometheusHistogramDurationMetricCollector`.

```
use Mmo\GuzzleMetricsMiddleware\Metric\Duration\PrometheusHistogramDurationMetricCollector;
use Prometheus\CollectorRegistry;

/** @var CollectorRegistry $registry */
$histogram = $registry->getOrRegisterHistogram(
    'myapp',
    'request_duration_ms',
    'Guzzle request duration',
    ['method', 'uri', 'status_code']
);

$collector = new PrometheusHistogramDurationMetricCollector($histogram);
```

### 2. Set up the Middleware

[](#2-set-up-the-middleware)

Create the middleware and push it onto the Guzzle handler stack.

```
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Mmo\GuzzleMetricsMiddleware\DurationMetricMiddleware;

$middleware = new DurationMetricMiddleware($collector);

$stack = HandlerStack::create();
$stack->push($middleware);

$client = new Client(['handler' => $stack]);
```

### 3. Path Templating (Optional)

[](#3-path-templating-optional)

To avoid creating a separate metric for every unique URL (which can lead to performance issues in monitoring systems), you can provide a template for the URI path using the `DurationMetricMiddleware::URI_PATH_TEMPLATE` option.

```
$client->request('GET', '/users/123', [
    DurationMetricMiddleware::URI_PATH_TEMPLATE => '/users/'
]);
```

The metric will then use `example.com/users/` instead of `example.com/users/123`.

#### Relative vs Absolute Templates

[](#relative-vs-absolute-templates)

- **Relative templates**: If you use a `base_uri` in Guzzle, a relative template will be appended to it.
- **Absolute templates**: Starting with a `/` will replace the path portion of the URI in the metrics.

License
-------

[](#license)

MIT

###  Health Score

20

—

LowBetter than 12% of packages

Maintenance59

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/5f48903acba8913dc7efd484ee70d647f77d41f24a261aab64cb27231ea0b7aa?d=identicon)[morawskim](/maintainers/morawskim)

---

Top Contributors

[![morawskim](https://avatars.githubusercontent.com/u/1105278?v=4)](https://github.com/morawskim "morawskim (2 commits)")

---

Tags

guzzleguzzle-middlewareguzzlehttpprometheus-metrics

### Embed Badge

![Health badge](/badges/mmo-guzzle-metrics-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/mmo-guzzle-metrics-middleware/health.svg)](https://phpackages.com/packages/mmo-guzzle-metrics-middleware)
```

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25127.4M87](/packages/php-http-cache-plugin)[gopay/payments-sdk-php

GoPay's PHP SDK for Payments REST API

861.8M16](/packages/gopay-payments-sdk-php)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

861.0M135](/packages/httpsoft-http-message)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
