diff --git a/client/client.go b/client/client.go index 4165ab0dce0..5e5c633e506 100644 --- a/client/client.go +++ b/client/client.go @@ -191,8 +191,6 @@ func NewClient(cfg *config.Config, consulCatalog consul.CatalogAPI, consulServic serversDiscoveredCh: make(chan struct{}), } - c.baseLabels = []metrics.Label{{"node_id", c.Node().ID}, {"datacenter", c.Node().Datacenter}} - // Initialize the client if err := c.init(); err != nil { return nil, fmt.Errorf("failed to initialize client: %v", err) @@ -290,6 +288,10 @@ 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 } diff --git a/client/client_test.go b/client/client_test.go index 30baff15b57..95fc0e9cc4b 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -22,6 +22,7 @@ import ( nconfig "github.com/hashicorp/nomad/nomad/structs/config" "github.com/hashicorp/nomad/testutil" "github.com/mitchellh/hashstructure" + "github.com/stretchr/testify/assert" ctestutil "github.com/hashicorp/nomad/client/testutil" ) @@ -121,6 +122,25 @@ func TestClient_StartStop(t *testing.T) { } } +// Certain labels for metrics are dependant on client intial setup. This tests +// that the client has properly initialized before we assign values to labels +func TestClient_BaseLabels(t *testing.T) { + t.Parallel() + assert := assert.New(t) + + client := testClient(t, nil) + if err := client.Shutdown(); err != nil { + t.Fatalf("err: %v", err) + } + + nodeID := client.Node().ID + for _, e := range client.baseLabels { + if e.Name == "node_id" { + assert.Equal(nodeID, e.Value) + } + } +} + func TestClient_RPC(t *testing.T) { t.Parallel() s1, addr := testServer(t, nil)