Skip to content

Commit

Permalink
labels depend on full setup of client beforehand
Browse files Browse the repository at this point in the history
  • Loading branch information
chelseakomlo committed Sep 1, 2017
1 parent 9b364b5 commit 7bacaff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7bacaff

Please sign in to comment.