Skip to content

Instantly share code, notes, and snippets.

@ls-michielrensen
Last active October 9, 2020 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ls-michielrensen/07c648d4b180bd0552902a80b5e668e5 to your computer and use it in GitHub Desktop.
Save ls-michielrensen/07c648d4b180bd0552902a80b5e668e5 to your computer and use it in GitHub Desktop.
Google Stackdriver php-fpm curl_json plugin example

Description

Google Stackdriver has various plugins for metrics on third-party applications. Looking for an integration with php-fpm, I came across this thread: https://groups.google.com/g/google-stackdriver-discussion/c/_okXG7SYqGg

While there was a mention of success, there was no description, example or explanation. I set out to find a solution myself and want to share that here.

The end-result is basically a combination of a CollectD example for php-fpm with the example in the Stackdriver documents for custom metrics using Nginx

Note: in order for the stackdriver-agent to gather stats on your php-fpm installation, the path needs to be exposed by the webserver. I used Apache2 (example attached), but something similar can be easily achieved with Nginx as well.

<VirtualHost *:80>
ServerName _
ServerAlias localhost 127.0.0.1 local-stackdriver-agent.stackdriver.com
<LocationMatch "/_php/status">
Order Allow,Deny
Allow from 127.0.0.1
ProxyPass "unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/_php/status"
</LocationMatch>
DocumentRoot /var/www/html/
<Directory /var/www/html>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
SetEnvIf Remote_Addr "::1" loopback
SetEnvIf Remote_Addr "127\.0\.0\.1" loopback
ErrorLog /var/log/apache2/health.log
LogLevel warn
CustomLog /dev/null combined
</VirtualHost>
LoadPlugin curl_json
<Plugin curl_json>
<URL "http://127.0.0.1/_php/status?json">
Instance "phpfpm"
<Key "accepted conn">
Type "derive"
Instance "accepted_connections"
</Key>
<Key "slow requests">
Type "derive"
Instance "slow_requests"
</Key>
<Key "listen queue">
Type "gauge"
Instance "listen_queue"
</Key>
<Key "active processes">
Type "gauge"
Instance "active_processes"
</Key>
<Key "idle processes">
Type "gauge"
Instance "idle_processes"
</Key>
<Key "total processes">
Type "gauge"
Instance "total"
</Key>
<Key "max active processes">
Type "gauge"
Instance "max_active"
</Key>
</URL>
</Plugin>
LoadPlugin match_regex
LoadPlugin target_set
LoadPlugin target_replace
PreCacheChain "PreCache"
<Chain "PreCache">
<Rule "jump_to_custom_metrics_from_curl_json">
<Match regex>
Plugin "^curl_json$"
PluginInstance "^phpfpm$"
</Match>
<Target "jump">
Chain "PreCache_curl_json"
</Target>
</Rule>
</Chain>
<Chain "PreCache_curl_json">
<Rule "rewrite_curl_json_phpfpm">
<Match regex>
Plugin "^curl_json$" # Match on plugin.
PluginInstance "^phpfpm.*$" # Match on plugin instance.
</Match>
<Target "set">
MetaData "stackdriver_metric_type" "custom.googleapis.com/phpfpm/%{type_instance}"
MetaData "label:service_name" "%{plugin_instance}"
</Target>
<Target "replace">
MetaData "label:service_name" "phpfpm_" ""
</Target>
</Rule>
<Rule "go_back">
Target "return"
</Rule>
</Chain>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment