Skip to content

Commit

Permalink
Simplify hostname handiling
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Aug 9, 2013
1 parent bdbca2c commit fc262d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
19 changes: 17 additions & 2 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,55 @@ import (
)

func (m *Metrics) SetGauge(key []string, val float32) {
if m.EnableHostname {
key = insert(0, m.hostName, key)
if m.HostName != "" {
key = insert(0, m.HostName, key)
}
if m.EnableTypePrefix {
key = insert(0, "gauge", key)
}
if m.ServiceName != "" {
key = insert(0, m.ServiceName, key)
}
m.sink.SetGauge(key, val)
}

func (m *Metrics) EmitKey(key []string, val float32) {
if m.EnableTypePrefix {
key = insert(0, "kv", key)
}
if m.ServiceName != "" {
key = insert(0, m.ServiceName, key)
}
m.sink.EmitKey(key, val)
}

func (m *Metrics) IncrCounter(key []string, val float32) {
if m.EnableTypePrefix {
key = insert(0, "counter", key)
}
if m.ServiceName != "" {
key = insert(0, m.ServiceName, key)
}
m.sink.IncrCounter(key, val)
}

func (m *Metrics) AddSample(key []string, val float32) {
if m.EnableTypePrefix {
key = insert(0, "sample", key)
}
if m.ServiceName != "" {
key = insert(0, m.ServiceName, key)
}
m.sink.AddSample(key, val)
}

func (m *Metrics) MeasureSince(key []string, start time.Time) {
if m.EnableTypePrefix {
key = insert(0, "timer", key)
}
if m.ServiceName != "" {
key = insert(0, m.ServiceName, key)
}
now := time.Now()
elapsed := now.Sub(start)
msec := float32(elapsed.Nanoseconds()) / float32(m.TimerGranularity)
Expand Down
25 changes: 10 additions & 15 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// Config is used to configure metrics settings
type Config struct {
ServiceName string // Prefixed with keys to seperate services
HostName string // Hostname to use. If not provided and EnableHostname, it will be os.Hostname
EnableHostname bool // Enable prefixing gauge values with hostname
EnableRuntimeMetrics bool // Enables profiling of runtime metrics (GC, Goroutines, Memory)
EnableTypePrefix bool // Prefixes key with a type ("counter", "gauge", "timer")
Expand All @@ -20,7 +21,6 @@ type Config struct {
// be used to emit
type Metrics struct {
Config
hostName string
lastNumGC uint32
sink MetricSink
}
Expand All @@ -35,32 +35,27 @@ func init() {

// DefaultConfig provides a sane default configuration
func DefaultConfig(serviceName string) *Config {
return &Config{
serviceName, // Use client provided service
c := &Config{
serviceName, // Use client provided service
"",
true, // Enable hostname prefix
true, // Enable runtime profiling
true, // Enable type prefix
time.Millisecond, // Timers are in milliseconds
time.Second, // Poll runtime every second
}

// Try to get the hostname
name, _ := os.Hostname()
c.HostName = name
return c
}

// New is used to create a new instance of Metrics. It takes a
// service name which is prefixed to all keys (unless blank), a
// bool to enableHostname when emiting gauges, and a sink implementation.
// New is used to create a new instance of Metrics
func New(conf *Config, sink MetricSink) (*Metrics, error) {
met := &Metrics{}
met.Config = *conf

// Get the hostname
if conf.EnableHostname {
hostName, err := os.Hostname()
if err != nil {
return nil, fmt.Errorf("Failed to get hostname! Got: %s", err)
}
met.hostName = hostName
}

// Start the runtime collector
if conf.EnableRuntimeMetrics {
go met.collectStats()
Expand Down

0 comments on commit fc262d6

Please sign in to comment.