PHPackages                             tuupola/instrument-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. tuupola/instrument-middleware

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

tuupola/instrument-middleware
=============================

PSR-7 Middleware for instrumenting PHP applications

0.5.0(9y ago)102.9kMITPHPPHP ^5.5 || ^7.0

Since Oct 9Pushed 4y ago2 watchersCompare

[ Source](https://github.com/tuupola/instrument-middleware)[ Packagist](https://packagist.org/packages/tuupola/instrument-middleware)[ Docs](https://github.com/tuupola/instrument-middleware)[ RSS](/packages/tuupola-instrument-middleware/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (2)Used By (0)

Instrument middleware
=====================

[](#instrument-middleware)

[![Latest Version](https://camo.githubusercontent.com/a34aaec457fbb2b854052d2cd1c69063792433fcd4877f421d26d74719217ba4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f747575706f6c612f696e737472756d656e742d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tuupola/instrument-middleware)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build Status](https://camo.githubusercontent.com/166bf7fc5a70a9b871f51e1ab91bc04c5716af8cc91bb46ebb73b7f0db64df36/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f747575706f6c612f696e737472756d656e742d6d6964646c65776172652f54657374732f6d61737465723f7374796c653d666c61742d737175617265)](https://github.com/tuupola/instrument-middleware/actions)[![Coverage](https://camo.githubusercontent.com/4f08b87a8b51c745238827b1407ebaf1cc4adf5c82dd05736833662352d8490a/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f747575706f6c612f696e737472756d656e742d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/tuupola/instrument-middleware)

Companion middleware for [Instrument](https://github.com/tuupola/instrument). Automates basic instrumenting of PSR-7 based application code.

[![Instrument Middleware](https://camo.githubusercontent.com/1428d4deb62310a8f92374d0f5e307c168e800a127ed69b505ab2a35624e1fc7/687474703a2f2f7777772e617070656c7369696e692e6e65742f696d672f696e737472756d656e742d6d6964646c65776172652d313430302e706e67)](https://camo.githubusercontent.com/1428d4deb62310a8f92374d0f5e307c168e800a127ed69b505ab2a35624e1fc7/687474703a2f2f7777772e617070656c7369696e692e6e65742f696d672f696e737472756d656e742d6d6964646c65776172652d313430302e706e67)

Install
-------

[](#install)

Install using [composer](https://getcomposer.org/).

```
$ composer require tuupola/instrument-middleware
```

Usage
-----

[](#usage)

You must have access to [InfluxDB](https://influxdata.com/) database to store the data. Configure the [Instrument](https://github.com/tuupola/instrument) instance and pass it to the middleware. This is the only mandatory parameter. Heads up! The order of middlewares is important. Instrument middleware *must* be the last one added.

```
require __DIR__ . "/vendor/autoload.php";

$app = new \Slim\App;

$influxdb = InfluxDB\Client::fromDSN("http+influxdb://foo:bar@localhost:8086/instrument");

$app->add(new Instrument\Middleware([
    "instrument" => new Instrument\Instrument([
        "adapter" => new Instrument\Adapter\InfluxDB($influxdb),
        "transformer" => new Instrument\Transformer\InfluxDB
    ])
]));
```

Or if you are using Slim 3 containers which is a bit cleaner.

```
require __DIR__ . "/vendor/autoload.php";

$app = new \Slim\App;
$container = $app->getContainer();

$container["influxdb"] = function ($container) {
    return InfluxDB\Client::fromDSN("http+influxdb://foo:bar@localhost:8086/instrument");
};

$container["instrument"] = function ($container) {
    return new Instrument\Instrument([
        "adapter" => new Instrument\Adapter\InfluxDB($container["influxdb"]),
        "transformer" => new Instrument\Transformer\InfluxDB
    ]);
};

$container["instrumentMiddleware"] = function ($container) {
    return new Instrument\Middleware([
        "instrument" => $container["instrument"]
    ]);
};

$app->add("instrumentMiddleware");
```

What is logged?
---------------

[](#what-is-logged)

Let's assume you have the following routes.

```
$app->get("/", function ($request, $response, $arguments) {
    return $response->write("Here be dragons...\n");
});

$app->get("/hello/{name}", function ($request, $response, $arguments) {
    return $response->write("Hello {$arguments['name']}!\n");
});
```

When request is made the middleware saves basic instrumentation data to the database.

```
$ curl http://192.168.50.53/
Here be dragons...
$ curl http://192.168.50.53/hello/foo
Hello foo!
```

```
> select * from instrument
name: instrument
----------------
time                 bootstrap  memory   method  process  route       status  total
1475316633441185508  158        1048576  GET     53       /           200     213
1475316763025260932  140        1048576  GET     69       /hello/foo  200     211
```

Field `bootstrap` is the time elapsed between start of the request and executing the first middleware. Field `total` is the time elapsed between starting the request and exiting the last middleware. Note again that Instrument middleware *must* be the last one added so it will be executed first when entering and last when exiting the middleware stack.

Fields `memory` and `process` are the peak PHP memory usage and elapsed time during the processing of the request. This includes the route or controller and all other middlewares.

Tags `method` and `status` are the request method and the HTTP status code of the response. Tag `route` is the requested URI without query string.

Adding or overriding tags
-------------------------

[](#adding-or-overriding-tags)

You can add tags by using `tags` parameter. It can be either an array or anonymous function returning an array. Function receives both `$request` and `$response` objects as parameters. If you return any of the default tags it will override the value otherwise set by the middleware.

```
$app->add(new Instrument\Middleware([
    "instrument" => $instrument,
    "tags" => ["host" => "localhost", "method" => "XXX"]
]));
```

Is essentially the same as code below.

```
$app->add(new Instrument\Middleware([
    "instrument" => $instrument,
    "tags" => function ($request, $response) {
        return ["host" => "localhost", "method" => "XXX"];
    }
]));
```

```
> select * from instrument
name: instrument
----------------
time                 bootstrap  memory   host       method  process  route       status  total
1475316633441185508  158        1048576  localhost  XXX     53       /           200     213
1475316763025260932  140        1048576  localhost  XXX     69       /hello/foo  200     211
```

Customising field and tag names
-------------------------------

[](#customising-field-and-tag-names)

All field and tag names can be customized. Following example changes all tag and field names. It also changes the measurement name. In InfluxDB lingo `MEASUREMENT`is the same as `TABLE` in SQL world.

```
$app->add(new Instrument\Middleware([
    "instrument" => $instrument,
    "measurement" = "api",
    "bootstrap" = "startup",
    "process" = "execution",
    "total" = "all",
    "memory" = "mem",
    "status" = "code",
    "route" = "uri",
    "method" = "verb"
]));
```

```
> select * from api
name: api
----------------
time                 startup  mem      verb  execution  uri         code  all
1475316633441185508  158      1048576  GET   53         /           200   213
1475316763025260932  140      1048576  GET   69         /hello/foo  200   211
```

To disable a tag or field set it to `false`.

```
$app->add(new Instrument\Middleware([
    "instrument" => $instrument,
    "measurement" = "api",
    "bootstrap" = "startup",
    "process" = false,
    "total" = "total",
    "memory" = false,
    "status" = false,
    "route" = false,
    "method" = false
]));
```

```
> select * from api
name: api
----------------
time                 startup  total
1475316633441185508  158      213
1475316763025260932  140      211
```

Manually adding data
--------------------

[](#manually-adding-data)

You can also manually add additional data to the measurement.

```
$app->get("/manual", function ($request, $response, $arguments) {
    $timing = $this->instrument->timing("instrument");
    $timing->start("db");
    /* Some expensive database queries. */
    $timing->stop("db");
    return $response->write("Manually adding additional data...\n");
});
```

```
$ curl http://192.168.50.53/manual
Manually adding additional data...
```

```
> select * from instrument
name: instrument
----------------
time                 bootstrap  db   memory   method  process  route    status  total
1475318315949095876  155        411  1048576  GET     466      /manual  200     623
```

Testing
-------

[](#testing)

You can run tests either manually...

```
$ make test
```

... or automatically on every code change.

```
$ make watch
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3500d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3325405a7d8a43bc40dd0e760a4b7f268fba32a7150cf0327f64f13d1661df0b?d=identicon)[tuupola](/maintainers/tuupola)

---

Top Contributors

[![tuupola](https://avatars.githubusercontent.com/u/21913?v=4)](https://github.com/tuupola "tuupola (33 commits)")

---

Tags

influxdbinstrumentationmiddlewarephppsr-7psr-7middlewareMetricsinfluxdbinstrument

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/tuupola-instrument-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/tuupola-instrument-middleware/health.svg)](https://phpackages.com/packages/tuupola-instrument-middleware)
```

###  Alternatives

[psr/http-server-middleware

Common interface for HTTP server-side middleware

18091.2M1.5k](/packages/psr-http-server-middleware)[mezzio/mezzio

PSR-15 Middleware Microframework

3883.6M97](/packages/mezzio-mezzio)[tuupola/slim-basic-auth

PSR-7 and PSR-15 HTTP Basic Authentication Middleware

4442.0M26](/packages/tuupola-slim-basic-auth)[relay/relay

A PSR-15 server request handler.

3302.1M86](/packages/relay-relay)[tuupola/cors-middleware

PSR-7 and PSR-15 CORS middleware

1331.8M24](/packages/tuupola-cors-middleware)[laminas/laminas-stratigility

PSR-7 middleware foundation for building and dispatching middleware pipelines

586.6M80](/packages/laminas-laminas-stratigility)

PHPackages © 2026

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