Skip to content

Commit

Permalink
azure monitor receiver added new attributes for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
altuner committed Aug 2, 2023
1 parent be93b0f commit 860729f
Show file tree
Hide file tree
Showing 4 changed files with 1,020 additions and 336 deletions.
49 changes: 35 additions & 14 deletions receiver/azuremonitorreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"fmt"
"regexp"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -49,16 +50,19 @@ var (
)

const (
tagPrefix = "tags_"
metadataPrefix = "metadata_"
location = "location"
attributeLocation = "location"
attributeName = "name"
attributeResourceGroup = "resource_group"
attributeResourceType = "type"
metadataPrefix = "metadata_"
tagPrefix = "tags_"
)

type azureResource struct {
attributes map[string]*string
metricsByCompositeKey map[metricsCompositeKey]*azureResourceMetrics
metricsDefinitionsUpdated time.Time
tags map[string]*string
location string
}

type metricsCompositeKey struct {
Expand Down Expand Up @@ -201,11 +205,19 @@ func (s *azureScraper) getResources(ctx context.Context) {
return
}
for _, resource := range nextResult.Value {

if _, ok := s.resources[*resource.ID]; !ok {
s.resources[*resource.ID] = &azureResource{tags: resource.Tags}
resourceGroup := getResourceGroupFromID(*resource.ID)
attributes := map[string]*string{
attributeName: resource.Name,
attributeResourceGroup: &resourceGroup,
attributeResourceType: resource.Type,
}
if resource.Location != nil {
s.resources[*resource.ID].location = *resource.Location
attributes[attributeLocation] = resource.Location
}
s.resources[*resource.ID] = &azureResource{
attributes: attributes,
tags: resource.Tags,
}
}
delete(existingResources, *resource.ID)
Expand All @@ -220,6 +232,16 @@ func (s *azureScraper) getResources(ctx context.Context) {
s.resourcesUpdated = time.Now()
}

func getResourceGroupFromID(id string) string {
var s = regexp.MustCompile(`\/resourcegroups/([^\/]+)\/`)
match := s.FindStringSubmatch(strings.ToLower(id))

if len(match) == 2 {
return match[1]
}
return ""
}

func (s *azureScraper) getResourcesFilter() string {
// TODO: switch to parsing services from
// https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/metrics-supported
Expand Down Expand Up @@ -264,11 +286,9 @@ func (s *azureScraper) getResourceMetricsDefinitions(ctx context.Context, resour
}
}
sort.Strings(dimensionsSlice)
dimensionsCompositeKey := metricsCompositeKey{timeGrain: timeGrain, dimensions: strings.Join(dimensionsSlice, ",")}
s.storeMetricsDefinition(resourceID, name, dimensionsCompositeKey)
} else {
s.storeMetricsDefinition(resourceID, name, compositeKey)
compositeKey.dimensions = strings.Join(dimensionsSlice, ",")
}
s.storeMetricsDefinition(resourceID, name, compositeKey)
}
}
s.resources[resourceID].metricsDefinitionsUpdated = time.Now()
Expand Down Expand Up @@ -325,15 +345,16 @@ func (s *azureScraper) getResourceMetricsValues(ctx context.Context, resourceID
for _, metric := range result.Value {

for _, timeseriesElement := range metric.Timeseries {

if timeseriesElement.Data != nil {
attributes := map[string]*string{}
for name, value := range res.attributes {
attributes[name] = value
}
for _, value := range timeseriesElement.Metadatavalues {
name := metadataPrefix + *value.Name.Value
attributes[name] = value.Value
}
if len(res.location) > 0 {
attributes[location] = &res.location
}
if s.cfg.AppendTagsAsAttributes {
for tagName, value := range res.tags {
name := tagPrefix + tagName
Expand Down
33 changes: 21 additions & 12 deletions receiver/azuremonitorreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,14 @@ func TestAzureScraperScrape(t *testing.T) {
}

func getResourcesMockData(tags bool) []armresources.ClientListResponse {
id1, id2, id3, location1 := "resourceId1", "resourceId2", "resourceId3", "location1"
id1, id2, id3, location1, name1, type1 := "/resourceGroups/group1/resourceId1",
"/resourceGroups/group1/resourceId2", "/resourceGroups/group1/resourceId3", "location1", "name1", "type1"

resourceID1 := armresources.GenericResourceExpanded{
ID: &id1,
Location: &location1,
Name: &name1,
Type: &type1,
}
if tags {
tagName1, tagValue1 := "tagName1", "tagValue1"
Expand All @@ -249,7 +252,10 @@ func getResourcesMockData(tags bool) []armresources.ClientListResponse {
Value: []*armresources.GenericResourceExpanded{
&resourceID1,
{
ID: &id2,
ID: &id2,
Location: &location1,
Name: &name1,
Type: &type1,
},
},
},
Expand All @@ -258,7 +264,10 @@ func getResourcesMockData(tags bool) []armresources.ClientListResponse {
ResourceListResult: armresources.ResourceListResult{
Value: []*armresources.GenericResourceExpanded{
{
ID: &id3,
ID: &id3,
Location: &location1,
Name: &name1,
Type: &type1,
},
},
},
Expand All @@ -271,13 +280,13 @@ func getMetricsDefinitionsMockData() (map[string]int, map[string][]armmonitor.Me
"metric2", "metric3", "metric4", "metric5", "metric6", "metric7", "PT1M", "PT1H", "dimension1", "dimension2"

counters := map[string]int{
"resourceId1": 0,
"resourceId2": 0,
"resourceId3": 0,
"/resourceGroups/group1/resourceId1": 0,
"/resourceGroups/group1/resourceId2": 0,
"/resourceGroups/group1/resourceId3": 0,
}

pages := map[string][]armmonitor.MetricDefinitionsClientListResponse{
"resourceId1": {
"/resourceGroups/group1/resourceId1": {
{
MetricDefinitionCollection: armmonitor.MetricDefinitionCollection{
Value: []*armmonitor.MetricDefinition{
Expand Down Expand Up @@ -315,7 +324,7 @@ func getMetricsDefinitionsMockData() (map[string]int, map[string][]armmonitor.Me
},
},
},
"resourceId2": {
"/resourceGroups/group1/resourceId2": {
{
MetricDefinitionCollection: armmonitor.MetricDefinitionCollection{
Value: []*armmonitor.MetricDefinition{
Expand Down Expand Up @@ -366,7 +375,7 @@ func getMetricsDefinitionsMockData() (map[string]int, map[string][]armmonitor.Me
},
},
},
"resourceId3": {
"/resourceGroups/group1/resourceId3": {
{
MetricDefinitionCollection: armmonitor.MetricDefinitionCollection{
Value: []*armmonitor.MetricDefinition{
Expand Down Expand Up @@ -400,7 +409,7 @@ func getMetricsValuesMockData() map[string]map[string]armmonitor.MetricsClientLi
var value1 float64 = 1

return map[string]map[string]armmonitor.MetricsClientListResponse{
"resourceId1": {
"/resourceGroups/group1/resourceId1": {
strings.Join([]string{name1, name2}, ","): {
Response: armmonitor.Response{
Value: []*armmonitor.Metric{
Expand Down Expand Up @@ -471,7 +480,7 @@ func getMetricsValuesMockData() map[string]map[string]armmonitor.MetricsClientLi
},
},
},
"resourceId2": {
"/resourceGroups/group1/resourceId2": {
name4: {
Response: armmonitor.Response{
Value: []*armmonitor.Metric{
Expand Down Expand Up @@ -570,7 +579,7 @@ func getMetricsValuesMockData() map[string]map[string]armmonitor.MetricsClientLi
},
},
},
"resourceId3": {
"/resourceGroups/group1/resourceId3": {
name7: {
Response: armmonitor.Response{
Value: []*armmonitor.Metric{
Expand Down
Loading

0 comments on commit 860729f

Please sign in to comment.