Skip to content

Commit

Permalink
Make graphite metric prefix more flexible
Browse files Browse the repository at this point in the history
Add "pattern" that have 2 pre-defined variables that's populated out of other settings or OS
  • Loading branch information
Vladimir Smirnov committed Sep 6, 2017
1 parent b7a336d commit 2fac229
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
6 changes: 4 additions & 2 deletions example.conf
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
listen: ":8080"
maxProcs: 0
graphite:
# host: "localhost"
host: "localhost:2003"
interval: "60s"
# prefix: "carbon.zipper"
prefix: "carbon.zipper"
# defines pattern of metric name. If present, {prefix} will be replaced with content of "prefix", {fqdn} with fqdn
pattern: "{prefix}.{fqdn}"
# Number of 100ms buckets to track request distribution in. Used to build
# 'carbon.zipper.hostname.requests_in_0ms_to_100ms' metric and friends.
# Requests beyond the last bucket are logged as slow
Expand Down
52 changes: 31 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"github.com/dgryski/httputil"
"github.com/facebookgo/grace/gracehttp"
"github.com/facebookgo/pidfile"
cu "github.com/go-graphite/carbonzipper/util/apictx"
pb3 "github.com/go-graphite/carbonzipper/carbonzipperpb3"
"github.com/go-graphite/carbonzipper/intervalset"
"github.com/go-graphite/carbonzipper/mstats"
"github.com/go-graphite/carbonzipper/pathcache"
cu "github.com/go-graphite/carbonzipper/util/apictx"
util "github.com/go-graphite/carbonzipper/util/zipperctx"
"github.com/go-graphite/carbonzipper/zipper"
pickle "github.com/lomik/og-rek"
Expand All @@ -47,6 +47,7 @@ var defaultLoggerConfig = zapwriter.Config{

// GraphiteConfig contains configuration bits to send internal stats to Graphite
type GraphiteConfig struct {
Pattern string
Host string
Interval time.Duration
Prefix string
Expand Down Expand Up @@ -78,6 +79,7 @@ var config = struct {
Graphite: GraphiteConfig{
Interval: 60 * time.Second,
Prefix: "carbon.zipper",
Pattern: "{prefix}.{fqdn}",
},
Listen: ":8080",
Buckets: 10,
Expand Down Expand Up @@ -647,6 +649,10 @@ func main() {
}
}

if config.Graphite.Pattern == "" {
config.Graphite.Pattern = "{prefix}.{fqdn}"
}

if config.Graphite.Prefix == "" {
config.Graphite.Prefix = "carbon.zipper"
}
Expand All @@ -662,39 +668,43 @@ func main() {

prefix := config.Graphite.Prefix

graphite.Register(fmt.Sprintf("%s.%s.find_requests", prefix, hostname), Metrics.FindRequests)
graphite.Register(fmt.Sprintf("%s.%s.find_errors", prefix, hostname), Metrics.FindErrors)
pattern := config.Graphite.Pattern
pattern = strings.Replace(pattern, "{prefix}", prefix, -1)
pattern = strings.Replace(pattern, "{fqdn}", hostname, -1)

graphite.Register(fmt.Sprintf("%s.find_requests", pattern), Metrics.FindRequests)
graphite.Register(fmt.Sprintf("%s.find_errors", pattern), Metrics.FindErrors)

graphite.Register(fmt.Sprintf("%s.%s.render_requests", prefix, hostname), Metrics.RenderRequests)
graphite.Register(fmt.Sprintf("%s.%s.render_errors", prefix, hostname), Metrics.RenderErrors)
graphite.Register(fmt.Sprintf("%s.render_requests", pattern), Metrics.RenderRequests)
graphite.Register(fmt.Sprintf("%s.render_errors", pattern), Metrics.RenderErrors)

graphite.Register(fmt.Sprintf("%s.%s.info_requests", prefix, hostname), Metrics.InfoRequests)
graphite.Register(fmt.Sprintf("%s.%s.info_errors", prefix, hostname), Metrics.InfoErrors)
graphite.Register(fmt.Sprintf("%s.info_requests", pattern), Metrics.InfoRequests)
graphite.Register(fmt.Sprintf("%s.info_errors", pattern), Metrics.InfoErrors)

graphite.Register(fmt.Sprintf("%s.%s.timeouts", prefix, hostname), Metrics.Timeouts)
graphite.Register(fmt.Sprintf("%s.timeouts", pattern), Metrics.Timeouts)

for i := 0; i <= config.Buckets; i++ {
graphite.Register(fmt.Sprintf("%s.%s.requests_in_%dms_to_%dms", prefix, hostname, i*100, (i+1)*100), bucketEntry(i))
graphite.Register(fmt.Sprintf("%s.requests_in_%dms_to_%dms", pattern, i*100, (i+1)*100), bucketEntry(i))
}

graphite.Register(fmt.Sprintf("%s.%s.cache_size", prefix, hostname), Metrics.CacheSize)
graphite.Register(fmt.Sprintf("%s.%s.cache_items", prefix, hostname), Metrics.CacheItems)
graphite.Register(fmt.Sprintf("%s.cache_size", pattern), Metrics.CacheSize)
graphite.Register(fmt.Sprintf("%s.cache_items", pattern), Metrics.CacheItems)

graphite.Register(fmt.Sprintf("%s.%s.search_cache_size", prefix, hostname), Metrics.SearchCacheSize)
graphite.Register(fmt.Sprintf("%s.%s.search_cache_items", prefix, hostname), Metrics.SearchCacheItems)
graphite.Register(fmt.Sprintf("%s.search_cache_size", pattern), Metrics.SearchCacheSize)
graphite.Register(fmt.Sprintf("%s.search_cache_items", pattern), Metrics.SearchCacheItems)

graphite.Register(fmt.Sprintf("%s.%s.cache_hits", prefix, hostname), Metrics.CacheHits)
graphite.Register(fmt.Sprintf("%s.%s.cache_misses", prefix, hostname), Metrics.CacheMisses)
graphite.Register(fmt.Sprintf("%s.cache_hits", pattern), Metrics.CacheHits)
graphite.Register(fmt.Sprintf("%s.cache_misses", pattern), Metrics.CacheMisses)

graphite.Register(fmt.Sprintf("%s.%s.search_cache_hits", prefix, hostname), Metrics.SearchCacheHits)
graphite.Register(fmt.Sprintf("%s.%s.search_cache_misses", prefix, hostname), Metrics.SearchCacheMisses)
graphite.Register(fmt.Sprintf("%s.search_cache_hits", pattern), Metrics.SearchCacheHits)
graphite.Register(fmt.Sprintf("%s.search_cache_misses", pattern), Metrics.SearchCacheMisses)

go mstats.Start(config.Graphite.Interval)

graphite.Register(fmt.Sprintf("%s.%s.alloc", prefix, hostname), &mstats.Alloc)
graphite.Register(fmt.Sprintf("%s.%s.total_alloc", prefix, hostname), &mstats.TotalAlloc)
graphite.Register(fmt.Sprintf("%s.%s.num_gc", prefix, hostname), &mstats.NumGC)
graphite.Register(fmt.Sprintf("%s.%s.pause_ns", prefix, hostname), &mstats.PauseNS)
graphite.Register(fmt.Sprintf("%s.alloc", pattern), &mstats.Alloc)
graphite.Register(fmt.Sprintf("%s.total_alloc", pattern), &mstats.TotalAlloc)
graphite.Register(fmt.Sprintf("%s.num_gc", pattern), &mstats.NumGC)
graphite.Register(fmt.Sprintf("%s.pause_ns", pattern), &mstats.PauseNS)
}

if *pidFile != "" {
Expand Down

0 comments on commit 2fac229

Please sign in to comment.