Skip to content

Commit

Permalink
[Azure] [Billing] Add Azure resource tags from Usage Details API (ela…
Browse files Browse the repository at this point in the history
…stic#36428)

* Add resource tags from Usage Details API

Both legacy and modern data formats support resource tags.

* Store tags in the azure.resource.tags field

The Azure module already has an existing mapping for the
`azure.resource.tags` field.

* Add a unit test to check the tags field mapping

* Update changelog

---------

Co-authored-by: muthu-mps <101238137+muthu-mps@users.noreply.github.com>
  • Loading branch information
2 people authored and Scholar-Li committed Feb 5, 2024
1 parent 317d1e5 commit 28c220d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Migrate Azure Billing, Monitor, and Storage metricsets to the newer SDK. {pull}33585[33585]
- Add support for float64 values parsing for statsd metrics of counter type. {pull}35099[35099]
- Add kubernetes.deployment.status.* fields for Kubernetes module {pull}35999[35999]
- Add Azure resource tags support to Azure Billing module {pull}36428[36428]


*Osquerybeat*
Expand Down
8 changes: 8 additions & 0 deletions x-pack/metricbeat/module/azure/billing/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func EventsMapping(
"group": legacy.Properties.ResourceGroup,
},
}
if len(legacy.Tags) > 0 {
_, _ = event.ModuleFields.Put("resource.tags", legacy.Tags)
}

event.MetricSetFields = mapstr.M{
// original fields
"billing_period_id": legacy.ID,
Expand Down Expand Up @@ -96,6 +100,10 @@ func EventsMapping(
"group": strings.ToLower(*modern.Properties.ResourceGroup),
},
}
if len(modern.Tags) > 0 {
_, _ = event.ModuleFields.Put("resource.tags", modern.Tags)
}

event.MetricSetFields = mapstr.M{
// original fields
"billing_period_id": modern.ID,
Expand Down
15 changes: 14 additions & 1 deletion x-pack/metricbeat/module/azure/billing/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func TestEventMapping(t *testing.T) {
name := "test"
billingAccountId := "123"
startDate := time.Time{}
tagName := "team"
tagValue := "obs-cloud-monitoring"

//
// Usage Details
Expand All @@ -49,6 +51,9 @@ func TestEventMapping(t *testing.T) {
ID: &ID,
Kind: &kind,
Properties: &props,
Tags: map[string]*string{
tagName: &tagValue,
},
}

//
Expand Down Expand Up @@ -102,15 +107,23 @@ func TestEventMapping(t *testing.T) {
// Check the results
//
for _, event := range events {
// if is an usage event
if ok, _ := event.MetricSetFields.HasKey("department_name"); ok {
//
// The event is a usage detail
//
val1, _ := event.MetricSetFields.GetValue("account_name")
assert.Equal(t, val1, &name)
val2, _ := event.MetricSetFields.GetValue("product")
assert.Equal(t, val2, &name)
val3, _ := event.MetricSetFields.GetValue("department_name")
assert.Equal(t, val3, &name)
tags, _ := event.ModuleFields.GetValue("resource.tags")
assert.Equal(t, tags, map[string]*string{tagName: &tagValue})

} else {
//
// The event is a forecast
//

// Check the actual cost
isActual, _ := event.MetricSetFields.HasKey("actual_cost")
Expand Down

0 comments on commit 28c220d

Please sign in to comment.