Skip to content

Commit b2627df

Browse files
Merge pull request #2 from celian-garcia/migrate-azquery-to-azmetrics
chore: Migrate azquery to azmetrics
2 parents 96472f5 + 6e0b9bd commit b2627df

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

receiver/azuremonitorreceiver/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.0
55
require (
66
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0
77
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0
8-
github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery v1.2.0-beta.1
8+
github.com/Azure/azure-sdk-for-go/sdk/monitor/query/azmetrics v1.1.0
99
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor v0.11.0
1010
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0
1111
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions v1.3.0

receiver/azuremonitorreceiver/go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/azuremonitorreceiver/scraper_batch.go

+27-26
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
2020
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
2121
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
22-
"github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery"
22+
"github.com/Azure/azure-sdk-for-go/sdk/monitor/query/azmetrics"
2323
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
2424
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
2525
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
@@ -35,7 +35,7 @@ import (
3535
type azureType struct {
3636
name *string
3737
attributes map[string]*string
38-
resourceIDs []*string
38+
resourceIDs []string
3939
metricsByCompositeKey map[metricsCompositeKey]*azureResourceMetrics
4040
metricsDefinitionsUpdated time.Time
4141
}
@@ -48,7 +48,6 @@ func newBatchScraper(conf *Config, settings receiver.Settings) *azureBatchScrape
4848
azIDCredentialsFunc: azidentity.NewClientSecretCredential,
4949
azIDWorkloadFunc: azidentity.NewWorkloadIdentityCredential,
5050
armMonitorDefinitionsClientFunc: armmonitor.NewMetricDefinitionsClient,
51-
azQueryMetricsBatchClientFunc: azquery.NewMetricsBatchClient,
5251
mutex: &sync.Mutex{},
5352
}
5453
}
@@ -73,8 +72,7 @@ type azureBatchScraper struct {
7372
armClientOptions *arm.ClientOptions
7473
armSubscriptionclient ArmsubscriptionClient
7574
armMonitorDefinitionsClientFunc func(string, azcore.TokenCredential, *arm.ClientOptions) (*armmonitor.MetricDefinitionsClient, error)
76-
azQueryMetricsBatchClientOptions *azquery.MetricsBatchClientOptions
77-
azQueryMetricsBatchClientFunc func(string, azcore.TokenCredential, *azquery.MetricsBatchClientOptions) (*azquery.MetricsBatchClient, error)
75+
azQueryMetricsBatchClientOptions *azmetrics.ClientOptions
7876
mutex *sync.Mutex
7977
}
8078

@@ -95,7 +93,7 @@ func (s *azureBatchScraper) getArmClientOptions() *arm.ClientOptions {
9593
return &options
9694
}
9795

98-
func (s *azureBatchScraper) getAzQueryMetricsBatchClientOptions() *azquery.MetricsBatchClientOptions {
96+
func (s *azureBatchScraper) getAzQueryMetricsBatchClientOptions() *azmetrics.ClientOptions {
9997
var cloudToUse cloud.Configuration
10098
switch s.cfg.Cloud {
10199
case azureGovernmentCloud:
@@ -104,7 +102,7 @@ func (s *azureBatchScraper) getAzQueryMetricsBatchClientOptions() *azquery.Metri
104102
cloudToUse = cloud.AzurePublic
105103
}
106104

107-
options := azquery.MetricsBatchClientOptions{
105+
options := azmetrics.ClientOptions{
108106
ClientOptions: azcore.ClientOptions{
109107
Cloud: cloudToUse,
110108
},
@@ -129,15 +127,15 @@ func (s *azureBatchScraper) getMetricsDefinitionsClient(subscriptionID string) m
129127
}
130128

131129
type MetricBatchValuesClient interface {
132-
QueryBatch(ctx context.Context, subscriptionID string, metricNamespace string, metricNames []string, resourceIDs azquery.ResourceIDList, options *azquery.MetricsBatchClientQueryBatchOptions) (
133-
azquery.MetricsBatchClientQueryBatchResponse, error,
130+
QueryResources(ctx context.Context, subscriptionID string, metricNamespace string, metricNames []string, resourceIDs azmetrics.ResourceIDList, options *azmetrics.QueryResourcesOptions) (
131+
azmetrics.QueryResourcesResponse, error,
134132
)
135133
}
136134

137135
func (s *azureBatchScraper) GetMetricsBatchValuesClient(region string) MetricBatchValuesClient {
138136
endpoint := "https://" + region + ".metrics.monitor.azure.com"
139137
s.settings.Logger.Info("Batch Endpoint", zap.String("endpoint", endpoint))
140-
client, _ := azquery.NewMetricsBatchClient(endpoint, s.cred, s.azQueryMetricsBatchClientOptions)
138+
client, _ := azmetrics.NewClient(endpoint, s.cred, s.azQueryMetricsBatchClientOptions)
141139
return client
142140
}
143141

@@ -292,10 +290,10 @@ func (s *azureBatchScraper) getResources(ctx context.Context, subscriptionID str
292290
updatedTypes[*resource.Type] = &azureType{
293291
name: resource.Type,
294292
attributes: map[string]*string{},
295-
resourceIDs: []*string{resource.ID},
293+
resourceIDs: []string{*resource.ID},
296294
}
297295
} else {
298-
updatedTypes[*resource.Type].resourceIDs = append(updatedTypes[*resource.Type].resourceIDs, resource.ID)
296+
updatedTypes[*resource.Type].resourceIDs = append(updatedTypes[*resource.Type].resourceIDs, *resource.ID)
299297
}
300298
}
301299
delete(existingResources, *resource.ID)
@@ -335,12 +333,12 @@ func (s *azureBatchScraper) getResourceMetricsDefinitionsByType(ctx context.Cont
335333
s.resourceTypes[*subscription.SubscriptionID][resourceType].metricsByCompositeKey = map[metricsCompositeKey]*azureResourceMetrics{}
336334

337335
resourceIDs := s.resourceTypes[*subscription.SubscriptionID][resourceType].resourceIDs
338-
if len(resourceIDs) == 0 && resourceIDs[0] != nil {
336+
if len(resourceIDs) == 0 && len(resourceIDs[0]) > 0 {
339337
return
340338
}
341339

342340
clientMetricsDefinitions := s.getMetricsDefinitionsClient(*subscription.SubscriptionID)
343-
pager := clientMetricsDefinitions.NewListPager(*resourceIDs[0], nil)
341+
pager := clientMetricsDefinitions.NewListPager(resourceIDs[0], nil)
344342
for pager.More() {
345343
nextResult, err := pager.NextPage(ctx)
346344
if err != nil {
@@ -420,20 +418,23 @@ func (s *azureBatchScraper) getBatchMetricsValues(ctx context.Context, subscript
420418
zap.String("interval", compositeKey.timeGrain),
421419
)
422420

423-
response, err := clientMetrics.QueryBatch(
421+
response, err := clientMetrics.QueryResources(
424422
ctx,
425423
*subscription.SubscriptionID,
426424
resourceType,
427425
metricsByGrain.metrics[start:end],
428-
azquery.ResourceIDList{ResourceIDs: resType.resourceIDs[startResources:endResources]},
429-
&azquery.MetricsBatchClientQueryBatchOptions{
430-
Aggregation: to.SliceOfPtrs(
431-
azquery.AggregationTypeAverage,
432-
azquery.AggregationTypeMaximum,
433-
azquery.AggregationTypeMinimum,
434-
azquery.AggregationTypeTotal,
435-
azquery.AggregationTypeCount,
436-
),
426+
azmetrics.ResourceIDList{ResourceIDs: resType.resourceIDs[startResources:endResources]},
427+
&azmetrics.QueryResourcesOptions{
428+
Aggregation: to.Ptr(strings.Join(
429+
[]string{
430+
string(armmonitor.AggregationTypeAverage),
431+
string(armmonitor.AggregationTypeMaximum),
432+
string(armmonitor.AggregationTypeMinimum),
433+
string(armmonitor.AggregationTypeTotal),
434+
string(armmonitor.AggregationTypeCount),
435+
},
436+
",",
437+
)),
437438
StartTime: to.Ptr(startTime.Format(time.RFC3339)),
438439
EndTime: to.Ptr(now.Format(time.RFC3339)),
439440
Interval: to.Ptr(compositeKey.timeGrain),
@@ -498,8 +499,8 @@ func (s *azureBatchScraper) getBatchMetricsValues(ctx context.Context, subscript
498499

499500
func (s *azureBatchScraper) processQueryTimeseriesData(
500501
resourceID string,
501-
metric *azquery.Metric,
502-
metricValue *azquery.MetricValue,
502+
metric azmetrics.Metric,
503+
metricValue azmetrics.MetricValue,
503504
attributes map[string]*string,
504505
) {
505506
s.mutex.Lock()

0 commit comments

Comments
 (0)