Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/azuremonitorreceiver] Added new attributes for metrics #24774

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .chloggen/azuremonitorreceiver-new-attributes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: receiver/azuremonitor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added new attrbutes to the metrics like name, type and resource_group.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [24774]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
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