Skip to content

Commit d1ca9b7

Browse files
Dev-Kylegolgeek
authored andcommitted
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 068293d commit d1ca9b7

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
@@ -126,7 +126,7 @@ func (c *PromClient) setUrl(url string) {
126126

127127
// instanceConfigRequest is the HTTP request received for generating instance config
128128
type instanceConfigRequest struct {
129-
//Config is the content of the yaml file
129+
// Config is the content of the yaml file
130130
Config string `json:"config"`
131131
Insecure bool `json:"insecure"`
132132
}
@@ -136,15 +136,15 @@ func (c *PromClient) UpdatePrometheusTargets(
136136
ctx context.Context,
137137
clusterName string,
138138
forceFetchCreds bool,
139-
nodes map[int][]*NodeInfo,
139+
nodeTargets NodeTargets,
140140
insecure bool,
141141
l *logger.Logger,
142142
) error {
143143
if c.disabled {
144144
l.Printf("Prometheus registration is disabled")
145145
return nil
146146
}
147-
req, err := buildCreateRequest(nodes, insecure)
147+
req, err := buildCreateRequest(nodeTargets, insecure)
148148
if err != nil {
149149
return err
150150
}
@@ -167,7 +167,7 @@ func (c *PromClient) UpdatePrometheusTargets(
167167
defer func() { _ = response.Body.Close() }()
168168
if response.StatusCode == http.StatusUnauthorized && !forceFetchCreds {
169169
l.Printf("request failed - this may be due to a stale token. retrying with forceFetchCreds true ...")
170-
return c.UpdatePrometheusTargets(ctx, clusterName, true, nodes, insecure, l)
170+
return c.UpdatePrometheusTargets(ctx, clusterName, true, nodeTargets, insecure, l)
171171
}
172172
body, err := io.ReadAll(response.Body)
173173
if err != nil {
@@ -249,8 +249,23 @@ type NodeInfo struct {
249249
CustomLabels map[string]string // Custom labels to be added to the cluster config
250250
}
251251

252+
// NodeTargets contains prometheus scrape targets for each node.
253+
type NodeTargets map[int][]*NodeInfo
254+
255+
func (nt NodeTargets) String() string {
256+
var parts []string
257+
for port, infos := range nt {
258+
var targets []string
259+
for _, info := range infos {
260+
targets = append(targets, info.Target)
261+
}
262+
parts = append(parts, fmt.Sprintf("%d:[%s]", port, strings.Join(targets, ",")))
263+
}
264+
return strings.Join(parts, " ")
265+
}
266+
252267
// createClusterConfigFile creates the cluster config file per node
253-
func buildCreateRequest(nodes map[int][]*NodeInfo, insecure bool) (io.Reader, error) {
268+
func buildCreateRequest(nodes NodeTargets, insecure bool) (io.Reader, error) {
254269
configs := make([]*CCParams, 0)
255270
for _, n := range nodes {
256271
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
@@ -816,7 +816,7 @@ func updatePrometheusTargets(
816816
}
817817

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

0 commit comments

Comments
 (0)