Skip to content

Commit 1e1adce

Browse files
committed
feat: add script parsing for custom metrics
1 parent a04f921 commit 1e1adce

File tree

5 files changed

+246
-976
lines changed

5 files changed

+246
-976
lines changed

cmd/server.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import (
1818
"net/http"
1919
"os"
2020
"os/signal"
21+
"strings"
2122
"syscall"
2223
"time"
2324

2425
"github.com/hipages/php-fpm_exporter/phpfpm"
26+
"github.com/hipages/php-fpm_exporter/scripts"
2527
"github.com/prometheus/client_golang/prometheus"
2628
"github.com/prometheus/client_golang/prometheus/promhttp"
2729
"github.com/spf13/cobra"
@@ -33,6 +35,7 @@ var (
3335
metricsEndpoint string
3436
scrapeURIs []string
3537
fixProcessCount bool
38+
scriptsPaths []string
3639
)
3740

3841
// serverCmd represents the server command
@@ -71,7 +74,30 @@ to quickly create a Cobra application.`,
7174
IdleTimeout: time.Second * 60,
7275
}
7376

74-
http.Handle(metricsEndpoint, promhttp.Handler())
77+
gatherers := prometheus.Gatherers{
78+
prometheus.DefaultGatherer,
79+
}
80+
81+
// add scripts gatherer if scripts are defined
82+
if len(scriptsPaths) > 0 {
83+
84+
addresses := make([]string, len(scrapeURIs))
85+
for i, uri := range scrapeURIs {
86+
addresses[i] = strings.SplitN(uri, ";", 2)[0]
87+
}
88+
89+
log.Debugf("Scripts to execute: %v", scriptsPaths)
90+
log.Debugf("Scrape URIs: %v", addresses)
91+
92+
sGather := &scripts.Gathered{
93+
Scripts: scriptsPaths,
94+
Sockets: addresses,
95+
}
96+
97+
gatherers = append(gatherers, sGather)
98+
}
99+
100+
http.Handle(metricsEndpoint, promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{}))
75101
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
76102
_, err := w.Write([]byte(`<html>
77103
<head><title>php-fpm_exporter</title></head>
@@ -125,6 +151,7 @@ func init() {
125151
serverCmd.Flags().StringVar(&metricsEndpoint, "web.telemetry-path", "/metrics", "Path under which to expose metrics.")
126152
serverCmd.Flags().StringSliceVar(&scrapeURIs, "phpfpm.scrape-uri", []string{"tcp://127.0.0.1:9000/status"}, "FastCGI address, e.g. unix:///tmp/php.sock;/status or tcp://127.0.0.1:9000/status")
127153
serverCmd.Flags().BoolVar(&fixProcessCount, "phpfpm.fix-process-count", false, "Enable to calculate process numbers via php-fpm_exporter since PHP-FPM sporadically reports wrong active/idle/total process numbers.")
154+
serverCmd.Flags().StringSliceVar(&scriptsPaths, "scripts.path", []string{}, "PHP scripts to execute to get additional metrics.")
128155

129156
// Workaround since vipers BindEnv is currently not working as expected (see https://github.com/spf13/viper/issues/461)
130157

contrib/php_opcache_exporter.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
//
4+
// Reports status, statistics and configuration directives
5+
// from OPcache in Prometheus format.
6+
//
7+
// Copyright 2017 Kumina, https://kumina.nl/
8+
// Licensed under the Apache License, Version 2.0 (the "License");
9+
// you may not use this file except in compliance with the License.
10+
// You may obtain a copy of the License at
11+
//
12+
// http://www.apache.org/licenses/LICENSE-2.0
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
//
20+
header('Content-Type: text/plain; version=0.0.5');
21+
22+
// Iterate and print in the Prometheus format
23+
function dump_as_prometheus_metric($root, $prefix)
24+
{
25+
if (is_numeric($root)) {
26+
echo "$prefix $root\n";
27+
} elseif (is_bool($root)) {
28+
if ($root) {
29+
print "$prefix 1\n";
30+
} else {
31+
print "$prefix 0\n";
32+
}
33+
} elseif (is_string($root)) {
34+
// Skip, as Prometheus doesn't support string values.
35+
} elseif (is_array($root)) {
36+
foreach ($root as $key => $value) {
37+
dump_as_prometheus_metric($value, $prefix.'_'.str_replace('.', '_', $key));
38+
}
39+
} else {
40+
var_dump($root);
41+
die('Encountered unsupported value type');
42+
}
43+
}
44+
45+
// Report status & statistics
46+
dump_as_prometheus_metric(opcache_get_status(false), 'opcache_status');
47+
48+
// Report configuration directives
49+
dump_as_prometheus_metric(opcache_get_configuration(), 'opcache_configuration');

go.mod

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ require (
55
github.com/gosuri/uitable v0.0.4
66
github.com/mattn/go-runewidth v0.0.8 // indirect
77
github.com/mitchellh/go-homedir v1.1.0
8-
github.com/prometheus/client_golang v1.17.0
9-
github.com/sirupsen/logrus v1.9.3
10-
github.com/spf13/cobra v1.7.0
11-
github.com/spf13/viper v1.16.0
12-
github.com/stretchr/testify v1.8.4
8+
github.com/prometheus/client_golang v1.13.0
9+
github.com/prometheus/client_model v0.3.0
10+
github.com/prometheus/common v0.37.0
11+
github.com/sirupsen/logrus v1.9.0
12+
github.com/spf13/cobra v1.6.1
13+
github.com/spf13/viper v1.13.0
14+
github.com/stretchr/testify v1.8.2
1315
github.com/tomasen/fcgi_client v0.0.0-20180423082037-2bb3d819fd19
1416
)
1517

0 commit comments

Comments
 (0)