Skip to content

Commit a77a099

Browse files
committed
Extract interfaces for Registry and Renderer
The registry and the renderer are classes that are usually injected by users into their own code, having interfaces for them allow users to mock based on the API and not on the concrete implementation which avoids a common code smell. This also allows better custom implementations if users so desire. Signed-off-by: Rafael Dohms <rdohms@gmail.com>
1 parent 1f2e17a commit a77a099

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

src/Prometheus/CollectorRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Prometheus\Storage\Adapter;
1010
use Prometheus\Storage\Redis;
1111

12-
class CollectorRegistry
12+
class CollectorRegistry implements RegistryInterface
1313
{
1414
/**
1515
* @var CollectorRegistry
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Prometheus;
4+
5+
use Prometheus\Exception\MetricNotFoundException;
6+
use Prometheus\Exception\MetricsRegistrationException;
7+
8+
interface RegistryInterface
9+
{
10+
/**
11+
* @return MetricFamilySamples[]
12+
*/
13+
public function getMetricFamilySamples(): array;
14+
15+
/**
16+
* @param string $namespace e.g. cms
17+
* @param string $name e.g. duration_seconds
18+
* @param string $help e.g. The duration something took in seconds.
19+
* @param array $labels e.g. ['controller', 'action']
20+
*
21+
* @return Gauge
22+
* @throws MetricsRegistrationException
23+
*/
24+
public function registerGauge($namespace, $name, $help, $labels = []): Gauge;
25+
26+
/**
27+
* @param string $namespace
28+
* @param string $name
29+
*
30+
* @return Gauge
31+
* @throws MetricNotFoundException
32+
*/
33+
public function getGauge($namespace, $name): Gauge;
34+
35+
/**
36+
* @param string $namespace e.g. cms
37+
* @param string $name e.g. duration_seconds
38+
* @param string $help e.g. The duration something took in seconds.
39+
* @param array $labels e.g. ['controller', 'action']
40+
*
41+
* @return Gauge
42+
* @throws MetricsRegistrationException
43+
*/
44+
public function getOrRegisterGauge($namespace, $name, $help, $labels = []): Gauge;
45+
46+
/**
47+
* @param string $namespace e.g. cms
48+
* @param string $name e.g. requests
49+
* @param string $help e.g. The number of requests made.
50+
* @param array $labels e.g. ['controller', 'action']
51+
*
52+
* @return Counter
53+
* @throws MetricsRegistrationException
54+
*/
55+
public function registerCounter($namespace, $name, $help, $labels = []): Counter;
56+
57+
/**
58+
* @param string $namespace
59+
* @param string $name
60+
*
61+
* @return Counter
62+
* @throws MetricNotFoundException
63+
*/
64+
public function getCounter($namespace, $name): Counter;
65+
66+
/**
67+
* @param string $namespace e.g. cms
68+
* @param string $name e.g. requests
69+
* @param string $help e.g. The number of requests made.
70+
* @param array $labels e.g. ['controller', 'action']
71+
*
72+
* @return Counter
73+
* @throws MetricsRegistrationException
74+
*/
75+
public function getOrRegisterCounter($namespace, $name, $help, $labels = []): Counter;
76+
77+
/**
78+
* @param string $namespace e.g. cms
79+
* @param string $name e.g. duration_seconds
80+
* @param string $help e.g. A histogram of the duration in seconds.
81+
* @param array $labels e.g. ['controller', 'action']
82+
* @param array $buckets e.g. [100, 200, 300]
83+
*
84+
* @return Histogram
85+
* @throws MetricsRegistrationException
86+
*/
87+
public function registerHistogram($namespace, $name, $help, $labels = [], $buckets = null): Histogram;
88+
89+
/**
90+
* @param string $namespace
91+
* @param string $name
92+
*
93+
* @return Histogram
94+
* @throws MetricNotFoundException
95+
*/
96+
public function getHistogram($namespace, $name): Histogram;
97+
98+
/**
99+
* @param string $namespace e.g. cms
100+
* @param string $name e.g. duration_seconds
101+
* @param string $help e.g. A histogram of the duration in seconds.
102+
* @param array $labels e.g. ['controller', 'action']
103+
* @param array $buckets e.g. [100, 200, 300]
104+
*
105+
* @return Histogram
106+
* @throws MetricsRegistrationException
107+
*/
108+
public function getOrRegisterHistogram($namespace, $name, $help, $labels = [], $buckets = null): Histogram;
109+
}

src/Prometheus/RenderTextFormat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Prometheus;
66

7-
class RenderTextFormat
7+
class RenderTextFormat implements RendererInterface
88
{
99
const MIME_TYPE = 'text/plain; version=0.0.4';
1010

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Prometheus;
4+
5+
interface RendererInterface
6+
{
7+
/**
8+
* @param MetricFamilySamples[] $metrics
9+
*
10+
* @return string
11+
*/
12+
public function render(array $metrics): string;
13+
}

0 commit comments

Comments
 (0)