Skip to content

Commit

Permalink
Merge pull request open-telemetry#2 from celian-garcia/migrate-azquer…
Browse files Browse the repository at this point in the history
…y-to-azmetrics

chore: Migrate azquery to azmetrics
  • Loading branch information
celian-garcia authored Nov 5, 2024
2 parents 96472f5 + 6e0b9bd commit b2627df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion receiver/azuremonitorreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.0
require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0
github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery v1.2.0-beta.1
github.com/Azure/azure-sdk-for-go/sdk/monitor/query/azmetrics v1.1.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor v0.11.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions v1.3.0
Expand Down
4 changes: 2 additions & 2 deletions receiver/azuremonitorreceiver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 27 additions & 26 deletions receiver/azuremonitorreceiver/scraper_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery"
"github.com/Azure/azure-sdk-for-go/sdk/monitor/query/azmetrics"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
Expand All @@ -35,7 +35,7 @@ import (
type azureType struct {
name *string
attributes map[string]*string
resourceIDs []*string
resourceIDs []string
metricsByCompositeKey map[metricsCompositeKey]*azureResourceMetrics
metricsDefinitionsUpdated time.Time
}
Expand All @@ -48,7 +48,6 @@ func newBatchScraper(conf *Config, settings receiver.Settings) *azureBatchScrape
azIDCredentialsFunc: azidentity.NewClientSecretCredential,
azIDWorkloadFunc: azidentity.NewWorkloadIdentityCredential,
armMonitorDefinitionsClientFunc: armmonitor.NewMetricDefinitionsClient,
azQueryMetricsBatchClientFunc: azquery.NewMetricsBatchClient,
mutex: &sync.Mutex{},
}
}
Expand All @@ -73,8 +72,7 @@ type azureBatchScraper struct {
armClientOptions *arm.ClientOptions
armSubscriptionclient ArmsubscriptionClient
armMonitorDefinitionsClientFunc func(string, azcore.TokenCredential, *arm.ClientOptions) (*armmonitor.MetricDefinitionsClient, error)
azQueryMetricsBatchClientOptions *azquery.MetricsBatchClientOptions
azQueryMetricsBatchClientFunc func(string, azcore.TokenCredential, *azquery.MetricsBatchClientOptions) (*azquery.MetricsBatchClient, error)
azQueryMetricsBatchClientOptions *azmetrics.ClientOptions
mutex *sync.Mutex
}

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

func (s *azureBatchScraper) getAzQueryMetricsBatchClientOptions() *azquery.MetricsBatchClientOptions {
func (s *azureBatchScraper) getAzQueryMetricsBatchClientOptions() *azmetrics.ClientOptions {
var cloudToUse cloud.Configuration
switch s.cfg.Cloud {
case azureGovernmentCloud:
Expand All @@ -104,7 +102,7 @@ func (s *azureBatchScraper) getAzQueryMetricsBatchClientOptions() *azquery.Metri
cloudToUse = cloud.AzurePublic
}

options := azquery.MetricsBatchClientOptions{
options := azmetrics.ClientOptions{
ClientOptions: azcore.ClientOptions{
Cloud: cloudToUse,
},
Expand All @@ -129,15 +127,15 @@ func (s *azureBatchScraper) getMetricsDefinitionsClient(subscriptionID string) m
}

type MetricBatchValuesClient interface {
QueryBatch(ctx context.Context, subscriptionID string, metricNamespace string, metricNames []string, resourceIDs azquery.ResourceIDList, options *azquery.MetricsBatchClientQueryBatchOptions) (
azquery.MetricsBatchClientQueryBatchResponse, error,
QueryResources(ctx context.Context, subscriptionID string, metricNamespace string, metricNames []string, resourceIDs azmetrics.ResourceIDList, options *azmetrics.QueryResourcesOptions) (
azmetrics.QueryResourcesResponse, error,
)
}

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

Expand Down Expand Up @@ -292,10 +290,10 @@ func (s *azureBatchScraper) getResources(ctx context.Context, subscriptionID str
updatedTypes[*resource.Type] = &azureType{
name: resource.Type,
attributes: map[string]*string{},
resourceIDs: []*string{resource.ID},
resourceIDs: []string{*resource.ID},
}
} else {
updatedTypes[*resource.Type].resourceIDs = append(updatedTypes[*resource.Type].resourceIDs, resource.ID)
updatedTypes[*resource.Type].resourceIDs = append(updatedTypes[*resource.Type].resourceIDs, *resource.ID)
}
}
delete(existingResources, *resource.ID)
Expand Down Expand Up @@ -335,12 +333,12 @@ func (s *azureBatchScraper) getResourceMetricsDefinitionsByType(ctx context.Cont
s.resourceTypes[*subscription.SubscriptionID][resourceType].metricsByCompositeKey = map[metricsCompositeKey]*azureResourceMetrics{}

resourceIDs := s.resourceTypes[*subscription.SubscriptionID][resourceType].resourceIDs
if len(resourceIDs) == 0 && resourceIDs[0] != nil {
if len(resourceIDs) == 0 && len(resourceIDs[0]) > 0 {
return
}

clientMetricsDefinitions := s.getMetricsDefinitionsClient(*subscription.SubscriptionID)
pager := clientMetricsDefinitions.NewListPager(*resourceIDs[0], nil)
pager := clientMetricsDefinitions.NewListPager(resourceIDs[0], nil)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
if err != nil {
Expand Down Expand Up @@ -420,20 +418,23 @@ func (s *azureBatchScraper) getBatchMetricsValues(ctx context.Context, subscript
zap.String("interval", compositeKey.timeGrain),
)

response, err := clientMetrics.QueryBatch(
response, err := clientMetrics.QueryResources(
ctx,
*subscription.SubscriptionID,
resourceType,
metricsByGrain.metrics[start:end],
azquery.ResourceIDList{ResourceIDs: resType.resourceIDs[startResources:endResources]},
&azquery.MetricsBatchClientQueryBatchOptions{
Aggregation: to.SliceOfPtrs(
azquery.AggregationTypeAverage,
azquery.AggregationTypeMaximum,
azquery.AggregationTypeMinimum,
azquery.AggregationTypeTotal,
azquery.AggregationTypeCount,
),
azmetrics.ResourceIDList{ResourceIDs: resType.resourceIDs[startResources:endResources]},
&azmetrics.QueryResourcesOptions{
Aggregation: to.Ptr(strings.Join(
[]string{
string(armmonitor.AggregationTypeAverage),
string(armmonitor.AggregationTypeMaximum),
string(armmonitor.AggregationTypeMinimum),
string(armmonitor.AggregationTypeTotal),
string(armmonitor.AggregationTypeCount),
},
",",
)),
StartTime: to.Ptr(startTime.Format(time.RFC3339)),
EndTime: to.Ptr(now.Format(time.RFC3339)),
Interval: to.Ptr(compositeKey.timeGrain),
Expand Down Expand Up @@ -498,8 +499,8 @@ func (s *azureBatchScraper) getBatchMetricsValues(ctx context.Context, subscript

func (s *azureBatchScraper) processQueryTimeseriesData(
resourceID string,
metric *azquery.Metric,
metricValue *azquery.MetricValue,
metric azmetrics.Metric,
metricValue azmetrics.MetricValue,
attributes map[string]*string,
) {
s.mutex.Lock()
Expand Down

0 comments on commit b2627df

Please sign in to comment.