Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PHP-PFM module #3450

Merged
merged 1 commit into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion metricbeat/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ services:
depends_on:
- apache
- couchbase
- mongodb
- haproxy
- kafka
- mongodb
- mysql
- nginx
- phpfpm
- postgresql
- prometheus
- redis
Expand All @@ -33,6 +34,8 @@ services:
- MYSQL_DSN=root:test@tcp(mysql:3306)/
- MYSQL_HOST=mysql
- MYSQL_PORT=3306
- PHPFPM_HOST=phpfpm
- PHPFPM_PORT=81
- POSTGRESQL_DSN=postgres://postgresql:5432?sslmode=disable
- POSTGRESQL_HOST=postgresql
- POSTGRESQL_PORT=5432
Expand Down Expand Up @@ -78,6 +81,9 @@ services:
haproxy:
build: ${PWD}/module/haproxy/_meta

phpfpm:
build: ${PWD}/module/php_fpm/_meta

postgresql:
image: postgres:9.5.3

Expand Down
2 changes: 2 additions & 0 deletions metricbeat/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ waitFor ${APACHE_HOST} ${APACHE_PORT} Apache
waitFor ${COUCHBASE_HOST} ${COUCHBASE_PORT} Couchbase
waitFor ${HAPROXY_HOST} ${HAPROXY_PORT} HAProxy
waitFor ${KAFKA_HOST} ${KAFKA_PORT} Kafka
waitFor ${MONGODB_HOST} ${MONGODB_PORT} MongoDB
waitFor ${MYSQL_HOST} ${MYSQL_PORT} MySQL
waitFor ${NGINX_HOST} ${NGINX_PORT} Nginx
waitFor ${PHPFPM_HOST} ${PHPFPM_PORT} PHP_FPM
waitFor ${POSTGRESQL_HOST} ${POSTGRESQL_PORT} Postgresql
waitFor ${PROMETHEUS_HOST} ${PROMETHEUS_PORT} Prometheus
waitFor ${REDIS_HOST} ${REDIS_PORT} Redis
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -3826,7 +3826,7 @@ experimental[]


[float]
=== php_fpm.pool.pool
=== php_fpm.pool.name

type: keyword

Expand Down
2 changes: 1 addition & 1 deletion metricbeat/metricbeat.template-es2x.json
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,7 @@
}
}
},
"pool": {
"name": {
"ignore_above": 1024,
"index": "not_analyzed",
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/metricbeat.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,7 @@
}
}
},
"pool": {
"name": {
"ignore_above": 1024,
"type": "keyword"
},
Expand Down
2 changes: 2 additions & 0 deletions metricbeat/module/php_fpm/_meta/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ FROM richarvey/nginx-php-fpm

RUN echo "pm.status_path = /status" >> /etc/php7/php-fpm.d/www.conf
ADD ./php-fpm.conf /etc/nginx/sites-enabled

EXPOSE 81
59 changes: 0 additions & 59 deletions metricbeat/module/php_fpm/php_fpm.go

This file was deleted.

30 changes: 0 additions & 30 deletions metricbeat/module/php_fpm/php_fpm_test.go

This file was deleted.

51 changes: 27 additions & 24 deletions metricbeat/module/php_fpm/pool/_meta/data.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
{
"@timestamp": "2017-01-18T23:57:23.960Z",
"beat": {
"hostname": "host.example.com",
"name": "host.example.com"
},
"metricset": {
"host": "localhost:8081",
"module": "php_fpm",
"name": "pool",
"rtt": 1237
},
"php_fpm": {
"pool": {
"connections.accepted": 803,
"connections.queued": 0,
"hostname": "localhost:8081",
"pool": "www",
"processes.active": 1,
"processes.idle": 2,
"requests.slow": 0
}
},
"type": "metricsets"
}
"@timestamp": "2016-05-23T08:05:34.853Z",
"beat": {
"hostname": "host.example.com",
"name": "host.example.com"
},
"metricset": {
"host": "127.0.0.1:81",
"module": "php_fpm",
"name": "pool",
"rtt": 115
},
"php_fpm": {
"pool": {
"connections": {
"accepted": 13,
"queued": 0
},
"pool": "www",
"processes": {
"active": 1,
"idle": 2
},
"slow_requests": 0
}
},
"type": "metricsets"
}
2 changes: 1 addition & 1 deletion metricbeat/module/php_fpm/pool/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
`pool` contains the metrics that were obtained from the PHP-FPM process
pool.
fields:
- name: pool
- name: name
type: keyword
description: >
The name of the pool.
Expand Down
35 changes: 19 additions & 16 deletions metricbeat/module/php_fpm/pool/data.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package pool

type poolStats struct {
Pool string `json:"pool"`
ProcessManager string `json:"process manager"`
StartTime int `json:"start time"`
StartSince int `json:"start since"`
AcceptedConn int `json:"accepted conn"`
ListenQueue int `json:"listen queue"`
MaxListenQueue int `json:"max listen queue"`
ListenQueueLen int `json:"listen queue len"`
IdleProcesses int `json:"idle processes"`
ActiveProcesses int `json:"active processes"`
TotalProcesses int `json:"total processes"`
MaxActiveProcesses int `json:"max active processes"`
MaxChildrenReached int `json:"max children reached"`
SlowRequests int `json:"slow requests"`
}
import (
s "github.com/elastic/beats/metricbeat/schema"
c "github.com/elastic/beats/metricbeat/schema/mapstriface"
)

var (
schema = s.Schema{
"name": c.Str("pool"),
"connections": s.Object{
"accepted": c.Int("accepted conn"),
"queued": c.Int("listen queue"),
},
"processes": s.Object{
"idle": c.Int("idle processes"),
"active": c.Int("active processes"),
},
"slow_requests": c.Int("slow requests"),
}
)
59 changes: 24 additions & 35 deletions metricbeat/module/php_fpm/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,58 @@ import (

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"

"github.com/elastic/beats/metricbeat/module/php_fpm"
"github.com/elastic/beats/metricbeat/mb/parse"
)

// init registers the MetricSet with the central registry.
// The New method will be called after the setup of the module and before starting to fetch data
func init() {
if err := mb.Registry.AddMetricSet("php_fpm", "pool", New, php_fpm.HostParser); err != nil {
if err := mb.Registry.AddMetricSet("php_fpm", "pool", New, HostParser); err != nil {
panic(err)
}
}

const (
defaultScheme = "http"
defaultPath = "/status"
)

// HostParser is used for parsing the configured php-fpm hosts.
var HostParser = parse.URLHostParserBuilder{
DefaultScheme: defaultScheme,
DefaultPath: defaultPath,
QueryParams: "json",
PathConfigKey: "status_path",
}.Build()

// MetricSet type defines all fields of the MetricSet
// As a minimum it must inherit the mb.BaseMetricSet fields, but can be extended with
// additional entries. These variables can be used to persist data or configuration between
// multiple fetch calls.
type MetricSet struct {
mb.BaseMetricSet
client *php_fpm.StatsClient // StatsClient that is reused across requests.
*helper.HTTP
}

// New create a new instance of the MetricSet
// Part of new is also setting up the configuration by processing additional
// configuration entries if needed.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
logp.Warn("EXPERIMENTAL: The php-fpm pool metricset is experimental")
return &MetricSet{
BaseMetricSet: base,
client: php_fpm.NewStatsClient(base),
base,
helper.NewHTTP(base),
}, nil
}

// Fetch methods implements the data gathering and data conversion to the right format
// It returns the event which is then forward to the output. In case of an error, a
// descriptive error must be returned.
// Fetch gathers data for the pool metricset
func (m *MetricSet) Fetch() (common.MapStr, error) {
body, err := m.client.Fetch()

content, err := m.HTTP.FetchContent()
if err != nil {
return nil, err
}

defer body.Close()

stats := &poolStats{}
err = json.NewDecoder(body).Decode(stats)
var stats map[string]interface{}
err = json.Unmarshal(content, &stats)
if err != nil {
return nil, fmt.Errorf("error parsing json: %v", err)
}

return common.MapStr{
"hostname": m.Host(),

"pool": stats.Pool,
"connections": common.MapStr{
"queue": stats.ListenQueue,
"accepted": stats.AcceptedConn,
},
"processes": common.MapStr{
"idle": stats.IdleProcesses,
"active": stats.ActiveProcesses,
},
"slow_requests": stats.SlowRequests,
}, nil
return schema.Apply(stats), nil
}
21 changes: 20 additions & 1 deletion metricbeat/module/php_fpm/pool/pool_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package pool

import (
"os"
"testing"

mbtest "github.com/elastic/beats/metricbeat/mb/testing"
Expand All @@ -20,6 +21,24 @@ func getConfig() map[string]interface{} {
return map[string]interface{}{
"module": "php_fpm",
"metricsets": []string{"pool"},
"hosts": []string{"127.0.0.1:81"},
"hosts": []string{GetEnvHost() + ":" + GetEnvPort()},
}
}

func GetEnvHost() string {
host := os.Getenv("PHPFPM_HOST")

if len(host) == 0 {
host = "127.0.0.1"
}
return host
}

func GetEnvPort() string {
port := os.Getenv("PHPFPM_PORT")

if len(port) == 0 {
port = "81"
}
return port
}
Loading