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

Make route metric names configurable #154

Closed
wants to merge 2 commits into from
Closed
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
21 changes: 11 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ type Runtime struct {
}

type Metrics struct {
Target string
Prefix string
Interval time.Duration
GraphiteAddr string
StatsDAddr string
CirconusAPIKey string
CirconusAPIApp string
CirconusAPIURL string
CirconusCheckID string
CirconusBrokerID string
Target string
Prefix string
Interval time.Duration
GraphiteAddr string
StatsDAddr string
CirconusAPIKey string
CirconusAPIApp string
CirconusAPIURL string
CirconusCheckID string
CirconusBrokerID string
RouteMetricNameTemplate string
}

type Registry struct {
Expand Down
7 changes: 4 additions & 3 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ var Default = &Config{
Color: "light-green",
},
Metrics: Metrics{
Prefix: "default",
Interval: 30 * time.Second,
CirconusAPIApp: "fabio",
Prefix: "default",
Interval: 30 * time.Second,
CirconusAPIApp: "fabio",
RouteMetricNameTemplate: "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}",
},
CertSources: map[string]CertSource{},
}
1 change: 1 addition & 0 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func load(p *properties.Properties) (cfg *Config, err error) {
f.StringVar(&cfg.Metrics.CirconusAPIURL, "metrics.circonus.apiurl", Default.Metrics.CirconusAPIURL, "Circonus API URL")
f.StringVar(&cfg.Metrics.CirconusBrokerID, "metrics.circonus.brokerid", Default.Metrics.CirconusBrokerID, "Circonus Broker ID")
f.StringVar(&cfg.Metrics.CirconusCheckID, "metrics.circonus.checkid", Default.Metrics.CirconusCheckID, "Circonus Check ID")
f.StringVar(&cfg.Metrics.RouteMetricNameTemplate, "metrics.routemetricnametemplate", Default.Metrics.RouteMetricNameTemplate, "route metric name template")
f.StringVar(&cfg.Registry.Backend, "registry.backend", Default.Registry.Backend, "registry backend")
f.StringVar(&cfg.Registry.File.Path, "registry.file.path", Default.Registry.File.Path, "path to file based routing table")
f.StringVar(&cfg.Registry.Static.Routes, "registry.static.routes", Default.Registry.Static.Routes, "static routes")
Expand Down
22 changes: 12 additions & 10 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ metrics.circonus.apiapp = circonus-apiapp
metrics.circonus.apiurl = circonus-apiurl
metrics.circonus.brokerid = circonus-brokerid
metrics.circonus.checkid = circonus-checkid
metrics.routemetricnametemplate = {{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}
runtime.gogc = 666
runtime.gomaxprocs = 12
ui.addr = 7.8.9.0:1234
Expand Down Expand Up @@ -123,16 +124,17 @@ aws.apigw.cert.cn = furb
},
},
Metrics: Metrics{
Target: "graphite",
Prefix: "someprefix",
Interval: 5 * time.Second,
GraphiteAddr: "5.6.7.8:9999",
StatsDAddr: "6.7.8.9:9999",
CirconusAPIKey: "circonus-apikey",
CirconusAPIApp: "circonus-apiapp",
CirconusAPIURL: "circonus-apiurl",
CirconusBrokerID: "circonus-brokerid",
CirconusCheckID: "circonus-checkid",
Target: "graphite",
Prefix: "someprefix",
Interval: 5 * time.Second,
GraphiteAddr: "5.6.7.8:9999",
StatsDAddr: "6.7.8.9:9999",
CirconusAPIKey: "circonus-apikey",
CirconusAPIApp: "circonus-apiapp",
CirconusAPIURL: "circonus-apiurl",
CirconusBrokerID: "circonus-brokerid",
CirconusCheckID: "circonus-checkid",
RouteMetricNameTemplate: "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}",
},
Runtime: Runtime{
GOGC: 666,
Expand Down
23 changes: 23 additions & 0 deletions fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,29 @@
# metrics.circonus.checkid =


# metrics.routemetricnametemplate provides the ability to customize
# the route metric names. Variables available for template expansion
# are: service host path and target.
#
# Given a route rule of: route add testservice www.example.com/ http://10.1.2.3:12345/
# The template variables would be:
#
# .Service = testservice
# .Host = www.example.com
# .Path = /
# .TargetURL.Host = 10.1.2.3:12345
#
# Function: clean - lowercases value and replaces '.' and ':' with '_'
#
# The resulting metric name (using the default template):
#
# testservice.www_example_com./.10_1_2_3_12345
#
# The default is
#
# metrics.routemetricnametemplate = {{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}


# runtime.gogc configures GOGC (the GC target percentage).
#
# Setting runtime.gogc is equivalent to setting the GOGC
Expand Down
66 changes: 59 additions & 7 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
package metrics

import (
"bytes"
"log"
"net/url"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/eBay/fabio/config"
"github.com/eBay/fabio/exit"
Expand All @@ -19,13 +21,32 @@ import (
// DefaultRegistry stores the metrics library provider.
var DefaultRegistry Registry = NoopRegistry{}

var routeMetricNameTemplate *template.Template

type routeMetricNameAttributes struct {
Service, Host, Path string
TargetURL *url.URL
}

// NewRegistry creates a new metrics registry.
func NewRegistry(cfg config.Metrics) (r Registry, err error) {
prefix := cfg.Prefix
if prefix == "default" {
prefix = defaultPrefix()
}

funcMap := template.FuncMap{
"clean": clean,
}

routeMetricNameTemplate, err = template.New("routeMetricName").Funcs(funcMap).Parse(cfg.RouteMetricNameTemplate)
if err != nil {
return nil, err
}
if err := verifyTemplate(); err != nil {
return nil, err
}

switch cfg.Target {
case "stdout":
log.Printf("[INFO] Sending metrics to stdout")
Expand Down Expand Up @@ -55,13 +76,21 @@ func NewRegistry(cfg config.Metrics) (r Registry, err error) {
}

// TargetName returns the metrics name from the given parameters.
func TargetName(service, host, path string, targetURL *url.URL) string {
return strings.Join([]string{
clean(service),
clean(host),
clean(path),
clean(targetURL.Host),
}, ".")
func TargetName(service, host, path string, targetURL *url.URL) (string, error) {
var name bytes.Buffer

data := &routeMetricNameAttributes{
Service: service,
Host: host,
Path: path,
TargetURL: targetURL,
}

if err := routeMetricNameTemplate.Execute(&name, data); err != nil {
return "", err
}

return name.String(), nil
}

// clean creates safe names for graphite reporting by replacing
Expand Down Expand Up @@ -89,3 +118,26 @@ func defaultPrefix() string {
exe := filepath.Base(os.Args[0])
return clean(host) + "." + clean(exe)
}

// verifyTemplate checks the route metric name template syntax
func verifyTemplate() error {
var name bytes.Buffer

testURL, err := url.Parse("http://127.0.0.1:12345/")
if err != nil {
return err
}

data := &routeMetricNameAttributes{
Service: "testservice",
Host: "test.example.com",
Path: "/test",
TargetURL: testURL,
}

if err := routeMetricNameTemplate.Execute(&name, data); err != nil {
return err
}

return nil
}
19 changes: 18 additions & 1 deletion metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"
"os"
"testing"
"text/template"
)

func TestDefaultPrefix(t *testing.T) {
Expand All @@ -27,12 +28,28 @@ func TestTargetName(t *testing.T) {
{"", "", "", "http://1.2.3.4:1234/bar", "_._._.1_2_3_4_1234"},
}

funcMap := template.FuncMap{
"clean": clean,
}

var err error
ts := "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}"
routeMetricNameTemplate, err = template.New("routeMetricName").Funcs(funcMap).Parse(ts)
if err != nil {
t.Fatalf("Template %s: %v", ts, err)
}

for i, tt := range tests {
u, err := url.Parse(tt.target)
if err != nil {
t.Fatalf("%d: %v", i, err)
}
if got, want := TargetName(tt.service, tt.host, tt.path, u), tt.name; got != want {

got, err := TargetName(tt.service, tt.host, tt.path, u)
if err != nil {
t.Fatalf("%d: %v", i, err)
}
if want := tt.name; got != want {
t.Errorf("%d: got %q want %q", i, got, want)
}
}
Expand Down
2 changes: 1 addition & 1 deletion route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (r *Route) addTarget(service string, targetURL *url.URL, fixedWeight float6
fixedWeight = 0
}

name := metrics.TargetName(service, r.Host, r.Path, targetURL)
name, _ := metrics.TargetName(service, r.Host, r.Path, targetURL)
timer := ServiceRegistry.GetTimer(name)

t := &Target{Service: service, Tags: tags, URL: targetURL, FixedWeight: fixedWeight, Timer: timer, timerName: name}
Expand Down