Skip to content

Commit

Permalink
Move GetURL to nginx module
Browse files Browse the repository at this point in the history
  • Loading branch information
KS Chan committed Sep 11, 2016
1 parent a009078 commit 1ce3fc3
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 215 deletions.
2 changes: 1 addition & 1 deletion metricbeat/module/nginx/pluscache/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func eventMapping(m *MetricSet, body io.ReadCloser, hostname string, metricset s
for name, cache := range caches {
event := common.MapStr{
"hostname": hostname,
"name": name,
"name": name,
}

for k, v := range cache.(map[string]interface{}) {
Expand Down
37 changes: 2 additions & 35 deletions metricbeat/module/nginx/pluscache/pluscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ package pluscache
import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/nginx"
)

const (
Expand Down Expand Up @@ -54,7 +53,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, err
}

u, err := getURL(config.ServerStatusPath, base.Host())
u, err := nginx.GetURL(config.ServerStatusPath, base.Host())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,35 +82,3 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) {

return eventMapping(m, resp.Body, m.Host(), m.Name())
}

// getURL constructs a URL from the rawHost value and path if one was not set in the rawHost value.
func getURL(statusPath, rawHost string) (*url.URL, error) {
u, err := url.Parse(rawHost)
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}

if u.Scheme == "" {
// Add scheme and re-parse.
u, err = url.Parse(fmt.Sprintf("%s://%s", "http", rawHost))
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}
}

if u.Host == "" {
return nil, fmt.Errorf("error parsing nginx host: empty host")
}

if u.Path == "" {
// The path given in the host config takes precedence over the
// server_status_path config value.
path := statusPath
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
u.Path = path
}

return u, nil
}
2 changes: 1 addition & 1 deletion metricbeat/module/nginx/plustcpupstream/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func eventMapping(m *MetricSet, body io.ReadCloser, hostname string, metricset s
for name, tcpupstream := range tcpupstreams {
event := common.MapStr{
"hostname": hostname,
"name": name,
"name": name,
}

for k, v := range tcpupstream.(map[string]interface{}) {
Expand Down
37 changes: 2 additions & 35 deletions metricbeat/module/nginx/plustcpupstream/plustcpupstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ package plustcpupstream
import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/nginx"
)

const (
Expand Down Expand Up @@ -54,7 +53,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, err
}

u, err := getURL(config.ServerStatusPath, base.Host())
u, err := nginx.GetURL(config.ServerStatusPath, base.Host())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,35 +82,3 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) {

return eventMapping(m, resp.Body, m.Host(), m.Name())
}

// getURL constructs a URL from the rawHost value and path if one was not set in the rawHost value.
func getURL(statusPath, rawHost string) (*url.URL, error) {
u, err := url.Parse(rawHost)
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}

if u.Scheme == "" {
// Add scheme and re-parse.
u, err = url.Parse(fmt.Sprintf("%s://%s", "http", rawHost))
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}
}

if u.Host == "" {
return nil, fmt.Errorf("error parsing nginx host: empty host")
}

if u.Path == "" {
// The path given in the host config takes precedence over the
// server_status_path config value.
path := statusPath
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
u.Path = path
}

return u, nil
}
2 changes: 1 addition & 1 deletion metricbeat/module/nginx/plustcpzone/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func eventMapping(m *MetricSet, body io.ReadCloser, hostname string, metricset s
for name, tcpzone := range tcpzones {
event := common.MapStr{
"hostname": hostname,
"name": name,
"name": name,
}

for k, v := range tcpzone.(map[string]interface{}) {
Expand Down
37 changes: 2 additions & 35 deletions metricbeat/module/nginx/plustcpzone/plustcpzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ package plustcpzone
import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/nginx"
)

const (
Expand Down Expand Up @@ -54,7 +53,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, err
}

u, err := getURL(config.ServerStatusPath, base.Host())
u, err := nginx.GetURL(config.ServerStatusPath, base.Host())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,35 +82,3 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) {

return eventMapping(m, resp.Body, m.Host(), m.Name())
}

// getURL constructs a URL from the rawHost value and path if one was not set in the rawHost value.
func getURL(statusPath, rawHost string) (*url.URL, error) {
u, err := url.Parse(rawHost)
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}

if u.Scheme == "" {
// Add scheme and re-parse.
u, err = url.Parse(fmt.Sprintf("%s://%s", "http", rawHost))
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}
}

if u.Host == "" {
return nil, fmt.Errorf("error parsing nginx host: empty host")
}

if u.Path == "" {
// The path given in the host config takes precedence over the
// server_status_path config value.
path := statusPath
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
u.Path = path
}

return u, nil
}
2 changes: 1 addition & 1 deletion metricbeat/module/nginx/plusupstream/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func eventMapping(m *MetricSet, body io.ReadCloser, hostname string, metricset s
for name, upstream := range upstreams {
event := common.MapStr{
"hostname": hostname,
"name": name,
"name": name,
}

for k, v := range upstream.(map[string]interface{}) {
Expand Down
37 changes: 2 additions & 35 deletions metricbeat/module/nginx/plusupstream/plusupstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ package plusupstream
import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/nginx"
)

const (
Expand Down Expand Up @@ -54,7 +53,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, err
}

u, err := getURL(config.ServerStatusPath, base.Host())
u, err := nginx.GetURL(config.ServerStatusPath, base.Host())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,35 +82,3 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) {

return eventMapping(m, resp.Body, m.Host(), m.Name())
}

// getURL constructs a URL from the rawHost value and path if one was not set in the rawHost value.
func getURL(statusPath, rawHost string) (*url.URL, error) {
u, err := url.Parse(rawHost)
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}

if u.Scheme == "" {
// Add scheme and re-parse.
u, err = url.Parse(fmt.Sprintf("%s://%s", "http", rawHost))
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}
}

if u.Host == "" {
return nil, fmt.Errorf("error parsing nginx host: empty host")
}

if u.Path == "" {
// The path given in the host config takes precedence over the
// server_status_path config value.
path := statusPath
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
u.Path = path
}

return u, nil
}
2 changes: 1 addition & 1 deletion metricbeat/module/nginx/pluszone/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func eventMapping(m *MetricSet, body io.ReadCloser, hostname string, metricset s
for name, zone := range zones {
event := common.MapStr{
"hostname": hostname,
"name": name,
"name": name,
}

for k, v := range zone.(map[string]interface{}) {
Expand Down
37 changes: 2 additions & 35 deletions metricbeat/module/nginx/pluszone/pluszone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ package pluszone
import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/nginx"
)

const (
Expand Down Expand Up @@ -54,7 +53,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, err
}

u, err := getURL(config.ServerStatusPath, base.Host())
u, err := nginx.GetURL(config.ServerStatusPath, base.Host())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,35 +82,3 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) {

return eventMapping(m, resp.Body, m.Host(), m.Name())
}

// getURL constructs a URL from the rawHost value and path if one was not set in the rawHost value.
func getURL(statusPath, rawHost string) (*url.URL, error) {
u, err := url.Parse(rawHost)
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}

if u.Scheme == "" {
// Add scheme and re-parse.
u, err = url.Parse(fmt.Sprintf("%s://%s", "http", rawHost))
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}
}

if u.Host == "" {
return nil, fmt.Errorf("error parsing nginx host: empty host")
}

if u.Path == "" {
// The path given in the host config takes precedence over the
// server_status_path config value.
path := statusPath
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
u.Path = path
}

return u, nil
}
37 changes: 2 additions & 35 deletions metricbeat/module/nginx/stubstatus/stubstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ package stubstatus
import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/nginx"
)

const (
Expand Down Expand Up @@ -54,7 +53,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, err
}

u, err := getURL(config.ServerStatusPath, base.Host())
u, err := nginx.GetURL(config.ServerStatusPath, base.Host())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,35 +82,3 @@ func (m *MetricSet) Fetch() (common.MapStr, error) {

return eventMapping(m, resp.Body, m.Host(), m.Name())
}

// getURL constructs a URL from the rawHost value and path if one was not set in the rawHost value.
func getURL(statusPath, rawHost string) (*url.URL, error) {
u, err := url.Parse(rawHost)
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}

if u.Scheme == "" {
// Add scheme and re-parse.
u, err = url.Parse(fmt.Sprintf("%s://%s", "http", rawHost))
if err != nil {
return nil, fmt.Errorf("error parsing nginx host: %v", err)
}
}

if u.Host == "" {
return nil, fmt.Errorf("error parsing nginx host: empty host")
}

if u.Path == "" {
// The path given in the host config takes precedence over the
// server_status_path config value.
path := statusPath
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
u.Path = path
}

return u, nil
}
Loading

0 comments on commit 1ce3fc3

Please sign in to comment.