labels depend on full setup of client beforehand

This commit is contained in:
Chelsea Holland Komlo
2017-09-01 19:41:07 +00:00
parent b6e104fdf9
commit 3c0710074c
2 changed files with 24 additions and 2 deletions

View File

@@ -194,8 +194,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)
@@ -298,6 +296,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
}

View File

@@ -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"
)
@@ -136,6 +137,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)