Skip to content

Commit 9e8c88c

Browse files
committed
roachprod: create type alias to hold NodeInfo map
Previously the NodeInfo to Node was stored in an ambiguous map structure, leading to an improper printing of the ip ports stored in it, this patch creates a type alias with a String() method to replace all instances of this map structure. Release note: none Fixes: none Informs: none
1 parent 985dffd commit 9e8c88c

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

pkg/roachprod/promhelperclient/client.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (c *PromClient) setUrl(url string) {
130130

131131
// instanceConfigRequest is the HTTP request received for generating instance config
132132
type instanceConfigRequest struct {
133-
//Config is the content of the yaml file
133+
// Config is the content of the yaml file
134134
Config string `json:"config"`
135135
Insecure bool `json:"insecure"`
136136
}
@@ -140,15 +140,15 @@ func (c *PromClient) UpdatePrometheusTargets(
140140
ctx context.Context,
141141
clusterName string,
142142
forceFetchCreds bool,
143-
nodes map[int][]*NodeInfo,
143+
nodeTargets NodeTargets,
144144
insecure bool,
145145
l *logger.Logger,
146146
) error {
147147
if c.disabled {
148148
l.Printf("Prometheus registration is disabled")
149149
return nil
150150
}
151-
req, err := buildCreateRequest(nodes, insecure)
151+
req, err := buildCreateRequest(nodeTargets, insecure)
152152
if err != nil {
153153
return err
154154
}
@@ -171,7 +171,7 @@ func (c *PromClient) UpdatePrometheusTargets(
171171
defer func() { _ = response.Body.Close() }()
172172
if response.StatusCode == http.StatusUnauthorized && !forceFetchCreds {
173173
l.Printf("request failed - this may be due to a stale token. retrying with forceFetchCreds true ...")
174-
return c.UpdatePrometheusTargets(ctx, clusterName, true, nodes, insecure, l)
174+
return c.UpdatePrometheusTargets(ctx, clusterName, true, nodeTargets, insecure, l)
175175
}
176176
body, err := io.ReadAll(response.Body)
177177
if err != nil {
@@ -253,8 +253,23 @@ type NodeInfo struct {
253253
CustomLabels map[string]string // Custom labels to be added to the cluster config
254254
}
255255

256+
// NodeTargets contains prometheus scrape targets for each node.
257+
type NodeTargets map[int][]*NodeInfo
258+
259+
func (nt NodeTargets) String() string {
260+
var parts []string
261+
for port, infos := range nt {
262+
var targets []string
263+
for _, info := range infos {
264+
targets = append(targets, info.Target)
265+
}
266+
parts = append(parts, fmt.Sprintf("%d:[%s]", port, strings.Join(targets, ",")))
267+
}
268+
return strings.Join(parts, " ")
269+
}
270+
256271
// createClusterConfigFile creates the cluster config file per node
257-
func buildCreateRequest(nodes map[int][]*NodeInfo, insecure bool) (io.Reader, error) {
272+
func buildCreateRequest(nodes NodeTargets, insecure bool) (io.Reader, error) {
258273
configs := make([]*CCParams, 0)
259274
for _, n := range nodes {
260275
for _, node := range n {

pkg/roachprod/promhelperclient/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ func TestUpdatePrometheusTargets(t *testing.T) {
4646
}, nil
4747
}
4848
err := c.UpdatePrometheusTargets(ctx, "c1", false,
49-
map[int][]*NodeInfo{1: {{Target: "n1"}}}, true, l)
49+
NodeTargets{1: {{Target: "n1"}}}, true, l)
5050
require.NotNil(t, err)
5151
require.Equal(t, fmt.Sprintf(ErrorMessage, 400, getUrl(promUrl, "c1"), "failed"), err.Error())
5252
})
5353
t.Run("UpdatePrometheusTargets succeeds", func(t *testing.T) {
54-
nodeInfos := map[int][]*NodeInfo{
54+
nodeInfos := NodeTargets{
5555
1: {{
5656
Target: "n1",
5757
}},

pkg/roachprod/roachprod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ func updatePrometheusTargets(
818818
}
819819

820820
cl := promhelperclient.NewPromClient()
821-
nodeIPPorts := make(map[int][]*promhelperclient.NodeInfo)
821+
nodeIPPorts := promhelperclient.NodeTargets{}
822822
nodeIPPortsMutex := syncutil.RWMutex{}
823823
var wg sync.WaitGroup
824824
for _, node := range c.Nodes {

0 commit comments

Comments
 (0)