Skip to content

Commit 3af117a

Browse files
committed
fix label handling for tag manager
Signed-off-by: Markus Blaschke <[email protected]>
1 parent 147e2e5 commit 3af117a

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

azuresdk/armclient/client.tags.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,6 @@ func (tagmgr *ArmClientTagManager) GetResourceTag(ctx context.Context, resourceI
7373

7474
ret := make([]ResourceTagResult, len(config.Tags))
7575

76-
// prefill tag config, this should not be empty in case of error
77-
i := -1
78-
for _, tagConfig := range config.Tags {
79-
i++
80-
81-
// default
82-
ret[i] = ResourceTagResult{
83-
TagName: tagConfig.Name,
84-
TagValue: "",
85-
TargetName: tagConfig.TargetName,
86-
}
87-
}
88-
8976
// parse resource id
9077
resourceID = strings.ToLower(resourceID)
9178
resourceInfo, err := ParseResourceId(resourceID)
@@ -163,7 +150,7 @@ func (tagmgr *ArmClientTagManager) GetResourceTag(ctx context.Context, resourceI
163150
return tagValue, nil
164151
}
165152

166-
i = -1
153+
i := -1
167154
for _, tagConfig := range config.Tags {
168155
i++
169156

@@ -393,13 +380,21 @@ func (c *ResourceTagManager) AddToPrometheusLabels(labels []string) []string {
393380

394381
// AddResourceTagsToPrometheusLabels adds resource tags to prometheus labels
395382
func (c *ResourceTagManager) AddResourceTagsToPrometheusLabels(ctx context.Context, labels prometheus.Labels, resourceID string) prometheus.Labels {
396-
resourceTags, err := c.client.TagManager.GetResourceTag(ctx, resourceID, c)
397-
if err != nil {
398-
c.client.TagManager.logger.Warnf(`unable to fetch resource tags for resource "%s": %v`, resourceID, err.Error())
383+
// init label value, do not miss a label or we run into problems later
384+
for _, tag := range c.Tags {
385+
labels[tag.TargetName] = ""
399386
}
400387

401-
for _, tag := range resourceTags {
402-
labels[tag.TargetName] = tag.TagValue
388+
// only run processing if we have a resourceID
389+
if resourceID != "" {
390+
resourceTags, err := c.client.TagManager.GetResourceTag(ctx, resourceID, c)
391+
if err != nil {
392+
c.client.TagManager.logger.Warnf(`unable to fetch resource tags for resource "%s": %v`, resourceID, err.Error())
393+
}
394+
395+
for _, tag := range resourceTags {
396+
labels[tag.TargetName] = tag.TagValue
397+
}
403398
}
404399

405400
return labels

0 commit comments

Comments
 (0)