summaryrefslogtreecommitdiff
path: root/src/Controllers/Metrics/MetricsEngine.php
blob: 21ae8fd0805ebe074761ed588bbbd1f9557072af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

namespace Engelsystem\Controllers\Metrics;

use Engelsystem\Renderer\EngineInterface;

class MetricsEngine implements EngineInterface
{
    /**
     * Render metrics
     *
     * @param string  $path
     * @param mixed[] $data
     * @return string
     *
     * @example $data = ['foo' => [['labels' => ['foo'=>'bar'], 'value'=>42]], 'bar'=>123]
     */
    public function get(string $path, array $data = []): string
    {
        $return = [];
        foreach ($data as $name => $list) {
            if (is_int($name)) {
                $return[] = '# ' . $this->escape($list);
                continue;
            }

            $list = is_array($list) ? $list : [$list];
            $name = 'engelsystem_' . $name;

            if (isset($list['help'])) {
                $return[] = sprintf('# HELP %s %s', $name, $this->escape($list['help']));
                unset($list['help']);
            }

            if (isset($list['type'])) {
                $return[] = sprintf('# TYPE %s %s', $name, $list['type']);
                unset($list['type']);
            }

            $list = (!isset($list['value']) || !isset($list['labels'])) ? $list : [$list];
            foreach ($list as $row) {
                $row = is_array($row) ? $row : [$row];

                $return[] = $this->formatData($name, $row);
            }
        }

        return implode("\n", $return);
    }

    /**
     * @param string $path
     * @return bool
     */
    public function canRender(string $path): bool
    {
        return $path == '/metrics';
    }

    /**
     * @param string      $name
     * @param array|mixed $row
     * @return string
     * @see https://prometheus.io/docs/instrumenting/exposition_formats/
     */
    protected function formatData($name, $row): string
    {
        return sprintf(
            '%s%s %s',
            $name,
            $this->renderLabels($row),
            $this->renderValue($row)
        );
    }

    /**
     * @param array|mixed $row
     * @return mixed
     */
    protected function renderLabels($row): string
    {
        $labels = [];
        if (!is_array($row) || empty($row['labels'])) {
            return '';
        }

        foreach ($row['labels'] as $type => $value) {
            $labels[$type] = $type . '="' . $this->formatValue($value) . '"';
        }

        return '{' . implode(',', $labels) . '}';
    }

    /**
     * @param array|mixed $row
     * @return mixed
     */
    protected function renderValue($row)
    {
        if (isset($row['value'])) {
            return $this->formatValue($row['value']);
        }

        return $this->formatValue(array_pop($row));
    }

    /**
     * @param mixed $value
     * @return mixed
     */
    protected function formatValue($value)
    {
        if (is_bool($value)) {
            return (int)$value;
        }

        return $this->escape($value);
    }

    /**
     * @param mixed $value
     * @return mixed
     */
    protected function escape($value)
    {
        $replace = [
            '\\' => '\\\\',
            '"'  => '\\"',
            "\n" => '\\n',
        ];

        return str_replace(
            array_keys($replace),
            array_values($replace),
            $value
        );
    }

    /**
     * Does nothing as shared data will onyly result in unexpected behaviour
     *
     * @param string|mixed[] $key
     * @param mixed          $value
     */
    public function share($key, $value = null) { }
}