Skip to content

Commit

Permalink
fixups from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
chelseakomlo committed Sep 4, 2017
1 parent 7bacaff commit 9c32be1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
12 changes: 8 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,6 @@ func NewClient(cfg *config.Config, consulCatalog consul.CatalogAPI, consulServic
// Start collecting stats
go c.emitStats()

// Assign labels at the latest possible moment so the information expected
// is ready
c.baseLabels = []metrics.Label{{Name: "node_id", Value: c.Node().ID}, {Name: "datacenter", Value: c.Node().Datacenter}}

c.logger.Printf("[INFO] client: Node ID %q", c.Node().ID)
return c, nil
}
Expand Down Expand Up @@ -1843,6 +1839,10 @@ DISCOLOOP:

// emitStats collects host resource usage stats periodically
func (c *Client) emitStats() {
// Assign labels directly before emitting stats so the information expected
// is ready
c.baseLabels = []metrics.Label{{Name: "node_id", Value: c.Node().ID}, {Name: "datacenter", Value: c.Node().Datacenter}}

// Start collecting host stats right away and then keep collecting every
// collection interval
next := time.NewTimer(0)
Expand All @@ -1869,6 +1869,7 @@ func (c *Client) emitStats() {
}
}

// setGaugeForMemoryStats proxies metrics for memory specific statistics
func (c *Client) setGaugeForMemoryStats(nodeID string, hStats *stats.HostStats) {
if !c.config.DisableTaggedMetrics {
metrics.SetGaugeWithLabels([]string{"client", "host", "memory", "total"}, float32(hStats.Memory.Total), c.baseLabels)
Expand All @@ -1885,6 +1886,7 @@ func (c *Client) setGaugeForMemoryStats(nodeID string, hStats *stats.HostStats)
}
}

// setGaugeForCPUStats proxies metrics for CPU specific statistics
func (c *Client) setGaugeForCPUStats(nodeID string, hStats *stats.HostStats) {
for _, cpu := range hStats.CPU {
if !c.config.DisableTaggedMetrics {
Expand All @@ -1905,6 +1907,7 @@ func (c *Client) setGaugeForCPUStats(nodeID string, hStats *stats.HostStats) {
}
}

// setGaugeForDiskStats proxies metrics for disk specific statistics
func (c *Client) setGaugeForDiskStats(nodeID string, hStats *stats.HostStats) {
for _, disk := range hStats.DiskStats {
if !c.config.DisableTaggedMetrics {
Expand All @@ -1927,6 +1930,7 @@ func (c *Client) setGaugeForDiskStats(nodeID string, hStats *stats.HostStats) {
}
}

// setGaugeForAllocationStats proxies metrics for allocation specific statistics
func (c *Client) setGaugeForAllocationStats(nodeID string) {
node := c.configCopy.Node
c.configLock.RUnlock()
Expand Down
5 changes: 4 additions & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ func TestClient_BaseLabels(t *testing.T) {
t.Fatalf("err: %v", err)
}

baseLabels := client.baseLabels
assert.NotEqual(0, len(baseLabels))

nodeID := client.Node().ID
for _, e := range client.baseLabels {
for _, e := range baseLabels {
if e.Name == "node_id" {
assert.Equal(nodeID, e.Value)
}
Expand Down
8 changes: 6 additions & 2 deletions client/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1849,11 +1849,15 @@ func (r *TaskRunner) setGaugeForCPU(ru *cstructs.TaskResourceUsage) {
// emitStats emits resource usage stats of tasks to remote metrics collector
// sinks
func (r *TaskRunner) emitStats(ru *cstructs.TaskResourceUsage) {
if ru.ResourceUsage.MemoryStats != nil && r.config.PublishAllocationMetrics {
if !r.config.PublishAllocationMetrics {
return
}

if ru.ResourceUsage.MemoryStats != nil {
r.setGaugeForMemory(ru)
}

if ru.ResourceUsage.CpuStats != nil && r.config.PublishAllocationMetrics {
if ru.ResourceUsage.CpuStats != nil {
r.setGaugeForCPU(ru)
}
}
10 changes: 6 additions & 4 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,12 @@ func TestConfig_Merge(t *testing.T) {
ReservedPorts: "2,10-30,55",
ParsedReservedPorts: []int{1, 2, 3},
},
GCInterval: 6 * time.Second,
GCParallelDestroys: 6,
GCDiskUsageThreshold: 71,
GCInodeUsageThreshold: 86,
GCInterval: 6 * time.Second,
GCParallelDestroys: 6,
GCDiskUsageThreshold: 71,
GCInodeUsageThreshold: 86,
DisableTaggedMetrics: true,
BackwardsCompatibleMetrics: true,
},
Server: &ServerConfig{
Enabled: true,
Expand Down

0 comments on commit 9c32be1

Please sign in to comment.