diff --git a/client/client.go b/client/client.go index bf4a9d58661..f9f020c5902 100644 --- a/client/client.go +++ b/client/client.go @@ -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 } @@ -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) @@ -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) @@ -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 { @@ -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 { @@ -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() diff --git a/client/client_test.go b/client/client_test.go index 50a60a45fd6..94331854fb3 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -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) } diff --git a/client/task_runner.go b/client/task_runner.go index 0a6346f5091..cd5afbd9197 100644 --- a/client/task_runner.go +++ b/client/task_runner.go @@ -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) } } diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 2e8d207106e..5ddea6be839 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -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,