{"id":18694322,"url":"https://github.com/zlodes/php-prometheus-client","last_synced_at":"2026-01-11T16:40:36.840Z","repository":{"id":162862457,"uuid":"637228116","full_name":"zlodes/php-prometheus-client","owner":"zlodes","description":"Collect and export PHP App metrics with ease","archived":false,"fork":false,"pushed_at":"2025-04-04T21:34:07.000Z","size":264,"stargazers_count":17,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-04T22:27:51.349Z","etag":null,"topics":["hacktoberfest","prometheus-client","prometheus-metrics"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zlodes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-06T22:44:08.000Z","updated_at":"2025-04-04T21:34:11.000Z","dependencies_parsed_at":"2025-04-04T22:24:42.369Z","dependency_job_id":"2176928f-2c53-41f4-9a18-724508e77621","html_url":"https://github.com/zlodes/php-prometheus-client","commit_stats":null,"previous_names":["zlodes/php-prometheus-client","zlodes/php-prometheus-exporter"],"tags_count":34,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zlodes%2Fphp-prometheus-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zlodes%2Fphp-prometheus-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zlodes%2Fphp-prometheus-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zlodes%2Fphp-prometheus-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zlodes","download_url":"https://codeload.github.com/zlodes/php-prometheus-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248530569,"owners_count":21119597,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","prometheus-client","prometheus-metrics"],"created_at":"2024-11-07T11:09:46.984Z","updated_at":"2025-10-06T14:15:29.539Z","avatar_url":"https://github.com/zlodes.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Prometheus Client\n\n[![codecov](https://codecov.io/gh/zlodes/php-prometheus-client/branch/master/graph/badge.svg?token=ROMQ8VBN0A)](https://codecov.io/gh/zlodes/php-prometheus-client)\n\nThis package provides you an ability to collect and export [Prometheus](https://prometheus.io/) metrics from any modern PHP application.\n\n## Why?\n\n* Until now, there was no working Prometheus client for modern PHP\n* Framework-agnostic\n* Almost zero dependencies\n* Won't break your business logic even if something is wrong with Metrics Storage\n* Ready to use with static analysis tools (PHPStan, Psalm)\n\n## Adapters\n* For Laravel: [zlodes/prometheus-client-laravel](https://github.com/zlodes/php-prometheus-client-laravel)\n\n## Installation\n\n```shell\ncomposer require zlodes/prometheus-client\n```\n\n## Flow\n\nTL;DR: Read [Simple example](#simple-example).\n\n### 1. Preparation\n\n1. Set up a storage to store metrics. There are four interfaces can be implemented:\n    1. [CounterStorage](./src/Storage/Contracts/CounterStorage.php)\n    2. [GaugeStorage](./src/Storage/Contracts/GaugeStorage.php)\n    3. [HistogramStorage](./src/Storage/Contracts/HistogramStorage.php)\n    4. [SummaryStorage](./src/Storage/Contracts/SummaryStorage.php)\n2. Set up a [Registry](./src/Registry/Registry.php) to register your metrics. [ArrayRegistry](./src/Registry/ArrayRegistry.php) is a default implementation.\n3. Register your metrics using the Registry from step 2.\n\n### 2. Collecting\n\n1. Get a collector for your metric from a [CollectorFactory](./src/Collector/CollectorFactory.php)\n2. Call metric update method (e.g. `increment` on CounterCollector)\n\n### 3. Exporting\n\n1. Create a controller to export metrics. Your controller should use [Exporter](./src/Exporter/Exporter.php). [FetcherExporter](./src/Exporter/FetcherExporter.php) is a default implementation.\n2. Set up a Prometheus to scrape metrics from your application using the controller from step 1.\n\n![](./docs/export.png)\n\n## Simple example\n\n```php\n\u003c?php\n\nuse Psr\\Log\\NullLogger;\nuse Zlodes\\PrometheusClient\\Collector\\CollectorFactory;\nuse Zlodes\\PrometheusClient\\Exporter\\FetcherExporter;\nuse Zlodes\\PrometheusClient\\Metric\\Counter;\nuse Zlodes\\PrometheusClient\\Metric\\Gauge;\nuse Zlodes\\PrometheusClient\\Metric\\Histogram;\nuse Zlodes\\PrometheusClient\\Registry\\ArrayRegistry;\nuse Zlodes\\PrometheusClient\\Storage\\InMemory\\InMemoryCounterStorage;\nuse Zlodes\\PrometheusClient\\Storage\\InMemory\\InMemoryGaugeStorage;\nuse Zlodes\\PrometheusClient\\Storage\\InMemory\\InMemoryHistogramStorage;\nuse Zlodes\\PrometheusClient\\Storage\\InMemory\\InMemorySummaryStorage;\nuse Zlodes\\PrometheusClient\\Metric\\Summary;\nuse Zlodes\\PrometheusClient\\Fetcher\\StoredMetricsFetcher;\n\n$registry = new ArrayRegistry();\n\n$counterStorage = new InMemoryCounterStorage();\n$gaugeStorage = new InMemoryGaugeStorage();\n$histogramStorage = new InMemoryHistogramStorage();\n$summaryStorage = new InMemorySummaryStorage();\n\n// Register your metrics\n$registry\n    -\u003eregisterMetric(\n        new Gauge('body_temperature', 'Body temperature in Celsius')\n    )\n    -\u003eregisterMetric(\n        new Counter('steps', 'Steps count')\n    )\n    -\u003eregisterMetric(\n        (new Histogram('http_request_duration_seconds', 'HTTP Request duration'))\n            -\u003ewithBuckets([0.1, 0.5, 1]),\n    )\n    -\u003eregisterMetric(\n        (new Summary('memory_used', 'Used memory in bytes'))\n            -\u003ewithQuantiles([0.5, 0.9, 0.99])\n    );\n\n// Create a Collector factory\n\n$collectorFactory = new CollectorFactory(\n    $registry,\n    $counterStorage,\n    $gaugeStorage,\n    $histogramStorage,\n    $summaryStorage,\n    new NullLogger(),\n);\n\n// Collect metrics\n$bodyTemperatureGauge = $collectorFactory-\u003egauge('body_temperature');\n\n$bodyTemperatureGauge\n    -\u003ewithLabels(['source' =\u003e 'armpit'])\n    -\u003eupdate(36.6);\n\n$bodyTemperatureGauge\n    -\u003ewithLabels(['source' =\u003e 'ass'])\n    -\u003eupdate(37.2);\n\n$collectorFactory\n    -\u003ecounter('steps')\n    -\u003eincrement();\n\n$requestTimer = $collectorFactory\n    -\u003ehistogram('http_request_duration_seconds')\n    -\u003estartTimer();\n\nusleep(50_000);\n\n$requestTimer-\u003estop();\n\n$collectorFactory\n    -\u003esummary('memory_used')\n    -\u003eupdate(100);\n\n$collectorFactory\n    -\u003esummary('memory_used')\n    -\u003eupdate(200);\n\n// Export metrics\n$fetcher = new StoredMetricsFetcher(\n    $registry,\n    $counterStorage,\n    $gaugeStorage,\n    $histogramStorage,\n    $summaryStorage,\n);\n\n$exporter = new FetcherExporter($fetcher);\n\nforeach ($exporter-\u003eexport() as $metricOutput) {\n    echo $metricOutput . \"\\n\\n\";\n}\n```\n\nOutput example:\n```\n# HELP steps Steps count\n# TYPE steps counter\nsteps 1\n\n# HELP body_temperature Body temperature in Celsius\n# TYPE body_temperature gauge\nbody_temperature{source=\"armpit\"} 36.6\nbody_temperature{source=\"ass\"} 37.2\n\n# HELP http_request_duration_seconds HTTP Request duration\n# TYPE http_request_duration_seconds histogram\nhttp_request_duration_seconds{le=\"0.1\"} 1\nhttp_request_duration_seconds{le=\"0.5\"} 1\nhttp_request_duration_seconds{le=\"1\"} 1\nhttp_request_duration_seconds{le=\"+Inf\"} 1\nhttp_request_duration_seconds_sum 0.050071506\nhttp_request_duration_seconds_count 1\n\n# HELP memory_used Used memory in bytes\n# TYPE memory_used summary\nmemory_used{quantile=\"0.5\"} 150\nmemory_used{quantile=\"0.9\"} 190\nmemory_used{quantile=\"0.99\"} 199\nmemory_used_sum 300\nmemory_used_count 2\n```\n\n## Testing\n\n### Run tests\n\n```shell\nphp ./vendor/bin/phpunit\n```\n\n### Creating your own Storage\n\n#### Keys serialization\n\nThere is a [Serializer](PrometheusClient/KeySerialization/Serializer.php) interface (with JSON-based implementation) to simplify work with a key-value storage.\n\nExample can be found in [InMemoryStorage](PrometheusClient/Storage/InMemoryStorage.php).\n\n#### Storage Testing\n\nThere are four useful traits to simplify your storage testing:\n\n1. [CounterStorageTesting](./src/Storage/Testing/CounterStorageTesting.php)\n2. [GaugeStorageTesting](./src/Storage/Testing/GaugeStorageTesting.php)\n3. [HistogramStorageTesting](./src/Storage/Testing/HistogramStorageTesting.php)\n4. [SummaryStorageTesting](./src/Storage/Testing/SummaryStorageTesting.php)\n\nThey provide you a set of tests to check your storage implementation to be compatible with the library.\n\nExample:\n\n```php\n\u003c?php\n\nuse PHPUnit\\Framework\\TestCase;\nuse Zlodes\\PrometheusClient\\Storage\\Contracts\\CounterStorage;\nuse Zlodes\\PrometheusClient\\Storage\\InMemory\\InMemoryCounterStorage;\nuse Zlodes\\PrometheusClient\\Storage\\Testing\\CounterStorageTesting;\n\nclass InMemoryCounterStorageTest extends TestCase\n{\n    use CounterStorageTesting;\n\n    private function createStorage(): CounterStorage\n    {\n        return new InMemoryCounterStorage();\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzlodes%2Fphp-prometheus-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzlodes%2Fphp-prometheus-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzlodes%2Fphp-prometheus-client/lists"}