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 5, 2017
1 parent 3c07100 commit 681a3f3
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 @@ -296,10 +296,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 @@ -1851,6 +1847,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 @@ -1877,6 +1877,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 @@ -1893,6 +1894,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 @@ -1913,6 +1915,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 @@ -1935,6 +1938,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 @@ -148,8 +148,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 @@ -224,10 +224,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 681a3f3

Please sign in to comment.