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

Add http client #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
61 changes: 41 additions & 20 deletions phpfpm/phpfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
Expand Down Expand Up @@ -47,6 +48,9 @@ const PoolProcessRequestInfo74 string = "Getting request information"
// PoolProcessRequestEnding defines a process that is about to end.
const PoolProcessRequestEnding string = "Ending"

// ProgramIdentifier is send to the php-fpm as SERVER_SOFTWARE
const ProgramIdentifier string = "go / php-fpm_exporter"

var log logger

type logger interface {
Expand Down Expand Up @@ -156,35 +160,52 @@ func (p *Pool) Update() (err error) {
return p.error(err)
}

fcgi, err := fcgiclient.DialTimeout(scheme, address, time.Duration(3)*time.Second)
if err != nil {
return p.error(err)
}

defer fcgi.Close()

env := map[string]string{
"SCRIPT_FILENAME": path,
"SCRIPT_NAME": path,
"SERVER_SOFTWARE": "go / php-fpm_exporter",
"REMOTE_ADDR": "127.0.0.1",
"QUERY_STRING": "json&full",
}

resp, err := fcgi.Get(env)
if err != nil {
return p.error(err)
var t = 3 * time.Second
var resp *http.Response

if scheme == "http" || scheme == "https" {
req, err := http.NewRequest("GET", p.Address, nil)
req.URL.RawQuery = "json&full"
req.Header = http.Header{
"SCRIPT_FILENAME": []string{path},
"SCRIPT_NAME": []string{path},
"SERVER_SOFTWARE": []string{ProgramIdentifier},
"REMOTE_ADDR": []string{"127.0.0.1"},
}
if err != nil {
return p.error(err)
}
client := http.Client{Timeout: t}
resp, err = client.Do(req)
if err != nil {
return p.error(err)
}
} else {
fcgi, err := fcgiclient.DialTimeout(scheme, address, t)
if err != nil {
return p.error(err)
}
defer fcgi.Close()
env := map[string]string{
"SCRIPT_FILENAME": path,
"SCRIPT_NAME": path,
"SERVER_SOFTWARE": ProgramIdentifier,
"REMOTE_ADDR": "127.0.0.1",
"QUERY_STRING": "json&full",
}
resp, err = fcgi.Get(env)
if err != nil {
return p.error(err)
}
}

defer resp.Body.Close()

content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return p.error(err)
}

content = JSONResponseFixer(content)

log.Debugf("Pool[%v]: %v", p.Address, string(content))

if err = json.Unmarshal(content, &p); err != nil {
Expand Down
1 change: 1 addition & 0 deletions phpfpm/phpfpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func TestParseURL(t *testing.T) {
}{
{"tcp://127.0.0.1:9000/status", []string{"tcp", "127.0.0.1:9000", "/status"}, nil},
{"tcp://127.0.0.1", []string{"tcp", "127.0.0.1", ""}, nil},
{"http://127.0.0.1/status", []string{"http", "127.0.0.1", "/status"}, nil},
{"unix:///tmp/php.sock;/status", []string{"unix", "/tmp/php.sock", "/status"}, nil},
{"unix:///tmp/php.sock", []string{"unix", "/tmp/php.sock", ""}, nil},
}
Expand Down