Skip to content

Commit bae186c

Browse files
committed
[receiver/azuremonitorreceiver] feat: add subscription name resource attribute
Signed-off-by: Célian Garcia <[email protected]>
1 parent 49ccc5d commit bae186c

File tree

12 files changed

+626
-42
lines changed

12 files changed

+626
-42
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: azuremonitorreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add subscription name resource attribute
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [39029]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/azuremonitorreceiver/documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
| Name | Description | Values | Enabled |
88
| ---- | ----------- | ------ | ------- |
9+
| azuremonitor.subscription | Azure subscription name | Any Str | false |
910
| azuremonitor.subscription_id | Azure subscription ID | Any Str | false |
1011
| azuremonitor.tenant_id | Azure tenant ID | Any Str | false |

receiver/azuremonitorreceiver/internal/metadata/generated_config.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/azuremonitorreceiver/internal/metadata/generated_config_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/azuremonitorreceiver/internal/metadata/generated_resource.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/azuremonitorreceiver/internal/metadata/generated_resource_test.go

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/azuremonitorreceiver/internal/metadata/testdata/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
default:
22
all_set:
33
resource_attributes:
4+
azuremonitor.subscription:
5+
enabled: true
46
azuremonitor.subscription_id:
57
enabled: true
68
azuremonitor.tenant_id:
79
enabled: true
810
none_set:
911
resource_attributes:
12+
azuremonitor.subscription:
13+
enabled: false
1014
azuremonitor.subscription_id:
1115
enabled: false
1216
azuremonitor.tenant_id:

receiver/azuremonitorreceiver/metadata.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ resource_attributes:
1515
azuremonitor.subscription_id:
1616
description: Azure subscription ID
1717
type: string
18+
azuremonitor.subscription:
19+
description: Azure subscription name
20+
type: string
1821

1922
tests:
2023
config:

receiver/azuremonitorreceiver/options_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ type mockClientOptionsResolver struct {
8787
// Indeed, the way to mock Azure API is to provide a fake server that will return the expected data.
8888
// The fake server is built with "fake" package from Azure SDK for Go, and is set in the client options, via the transport.
8989
// This ctor takes the mock data in that order:
90-
// - subscriptions
90+
// - subscriptions get responses stored by ID
91+
// - subscriptions list response
9192
// - resources stored by subscription ID
9293
// - metrics definitions stored by resource URI
9394
// - metrics values stored by resource URI and metric name
9495
func newMockClientOptionsResolver(
95-
subscriptions []armsubscriptions.ClientListResponse,
96+
subscriptionsGetResponsesByID map[string]armsubscriptions.ClientGetResponse,
97+
subscriptionsListResponse []armsubscriptions.ClientListResponse,
9698
resources map[string][]armresources.ClientListResponse,
9799
metricsDefinitions map[string][]armmonitor.MetricDefinitionsClientListResponse,
98100
metrics map[string]map[string]armmonitor.MetricsClientListResponse,
@@ -112,7 +114,8 @@ func newMockClientOptionsResolver(
112114

113115
// Init subscriptions client options from subscriptions mock data
114116
subscriptionsServer := armsubscriptionsfake.Server{
115-
NewListPager: newMockSubscriptionsListPager(subscriptions),
117+
NewListPager: newMockSubscriptionsListPager(subscriptionsListResponse),
118+
Get: newMockSubscriptionGet(subscriptionsGetResponsesByID),
116119
}
117120
armSubscriptionsClientOptions := &arm.ClientOptions{
118121
ClientOptions: azcore.ClientOptions{

receiver/azuremonitorreceiver/scraper.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const (
6363
// and simple subscriptions ids that you can find in config.
6464
type azureSubscription struct {
6565
SubscriptionID string
66-
DisplayName *string
66+
DisplayName string
6767
resourcesUpdated time.Time
6868
}
6969

@@ -142,18 +142,14 @@ func (s *azureScraper) start(_ context.Context, _ component.Host) (err error) {
142142
s.subscriptions = map[string]*azureSubscription{}
143143
s.resources = map[string]map[string]*azureResource{}
144144

145-
// Initialize subscription ids from the config. Will be overridden if discovery is enabled anyway.
146-
for _, id := range s.cfg.SubscriptionIDs {
147-
s.loadSubscription(id)
148-
}
149-
150145
return
151146
}
152147

153-
func (s *azureScraper) loadSubscription(id string) {
154-
s.resources[id] = make(map[string]*azureResource)
155-
s.subscriptions[id] = &azureSubscription{
156-
SubscriptionID: id,
148+
func (s *azureScraper) loadSubscription(sub armsubscriptions.Subscription) {
149+
s.resources[*sub.SubscriptionID] = make(map[string]*azureResource)
150+
s.subscriptions[*sub.SubscriptionID] = &azureSubscription{
151+
SubscriptionID: *sub.SubscriptionID,
152+
DisplayName: *sub.DisplayName,
157153
}
158154
}
159155

@@ -195,7 +191,7 @@ func (s *azureScraper) loadCredentials() (err error) {
195191
func (s *azureScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
196192
s.getSubscriptions(ctx)
197193

198-
for subscriptionID := range s.subscriptions {
194+
for subscriptionID, subscription := range s.subscriptions {
199195
s.getResources(ctx, subscriptionID)
200196

201197
resourcesIDsWithDefinitions := make(chan string)
@@ -224,13 +220,14 @@ func (s *azureScraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
224220
rb := s.mb.NewResourceBuilder()
225221
rb.SetAzuremonitorTenantID(s.cfg.TenantID)
226222
rb.SetAzuremonitorSubscriptionID(subscriptionID)
223+
rb.SetAzuremonitorSubscription(subscription.DisplayName)
227224
s.mb.EmitForResource(metadata.WithResource(rb.Emit()))
228225
}
229226
return s.mb.Emit(), nil
230227
}
231228

232229
func (s *azureScraper) getSubscriptions(ctx context.Context) {
233-
if !s.cfg.DiscoverSubscriptions || !(time.Since(s.subscriptionsUpdated).Seconds() < s.cfg.CacheResources) {
230+
if time.Since(s.subscriptionsUpdated).Seconds() < s.cfg.CacheResources {
234231
return
235232
}
236233

@@ -241,6 +238,20 @@ func (s *azureScraper) getSubscriptions(ctx context.Context) {
241238
return
242239
}
243240

241+
if !s.cfg.DiscoverSubscriptions {
242+
// if there's only subscription ids configured, we need to get additional info
243+
for _, subID := range s.cfg.SubscriptionIDs {
244+
resp, err := armSubscriptionClient.Get(ctx, subID, &armsubscriptions.ClientGetOptions{})
245+
if err != nil {
246+
s.settings.Logger.Error("failed to get Azure Subscription", zap.String("subscription_id", subID), zap.Error(err))
247+
return
248+
}
249+
s.loadSubscription(resp.Subscription)
250+
}
251+
s.subscriptionsUpdated = time.Now()
252+
return
253+
}
254+
244255
opts := &armsubscriptions.ClientListOptions{}
245256
pager := armSubscriptionClient.NewListPager(opts)
246257

@@ -257,7 +268,7 @@ func (s *azureScraper) getSubscriptions(ctx context.Context) {
257268
}
258269

259270
for _, subscription := range nextResult.Value {
260-
s.loadSubscription(*subscription.SubscriptionID)
271+
s.loadSubscription(*subscription)
261272
delete(existingSubscriptions, *subscription.SubscriptionID)
262273
}
263274
}

0 commit comments

Comments
 (0)