2022-03-02 06:41:07 -08:00
package metrics
2019-02-08 17:20:31 +01:00
import (
2021-06-11 17:02:24 +02:00
"context"
2019-02-08 18:15:17 +01:00
"encoding/json"
2019-02-08 17:20:31 +01:00
"fmt"
2021-06-11 17:02:24 +02:00
"net/http"
2019-02-09 21:52:44 +01:00
"net/url"
2022-08-10 13:37:51 +00:00
"os"
2020-04-27 17:43:02 +02:00
"path/filepath"
2023-04-27 20:24:11 +01:00
"strings"
2019-02-08 17:20:31 +01:00
"testing"
"time"
2020-06-01 12:37:39 -04:00
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
2021-06-07 14:54:51 +02:00
"github.com/grafana/grafana-plugin-sdk-go/backend"
2020-06-01 12:37:39 -04:00
"github.com/grafana/grafana-plugin-sdk-go/data"
2022-12-02 13:17:11 +01:00
"github.com/stretchr/testify/assert"
2022-11-04 09:28:38 -04:00
"github.com/stretchr/testify/require"
2023-06-10 15:30:49 -05:00
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/kinds/dataquery"
2023-04-27 20:24:11 +01:00
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/testdata"
2022-03-02 06:41:07 -08:00
azTime "github.com/grafana/grafana/pkg/tsdb/azuremonitor/time"
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
2019-02-08 17:20:31 +01:00
)
2024-01-03 19:20:22 +00:00
func Pointer [ T any ] ( v T ) * T { return & v }
2020-06-01 12:37:39 -04:00
func TestAzureMonitorBuildQueries ( t * testing . T ) {
datasource := & AzureMonitorDatasource { }
2022-03-02 06:41:07 -08:00
dsInfo := types . DatasourceInfo {
Settings : types . AzureMonitorSettings {
2021-06-07 14:54:51 +02:00
SubscriptionId : "default-subscription" ,
} ,
}
2020-06-01 12:37:39 -04:00
fromStart := time . Date ( 2018 , 3 , 15 , 13 , 0 , 0 , 0 , time . UTC ) . In ( time . Local )
2021-06-07 14:54:51 +02:00
duration , _ := time . ParseDuration ( "400s" )
2022-05-10 15:05:48 +01:00
wildcardFilter := "*"
testFilter := "test"
2020-06-01 12:37:39 -04:00
tests := [ ] struct {
name string
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties map [ string ] any
2020-06-01 12:37:39 -04:00
azureMonitorQueryTarget string
expectedInterval string
2021-06-07 14:54:51 +02:00
queryInterval time . Duration
2022-10-07 14:57:01 +02:00
expectedURL string
2023-01-13 12:30:45 +01:00
expectedBodyFilter string
expectedParamFilter string
2022-12-21 16:30:17 +00:00
expectedPortalURL * string
2023-06-10 15:30:49 -05:00
resources map [ string ] dataquery . AzureMonitorResource
2020-06-01 12:37:39 -04:00
} {
{
name : "Parse queries from frontend and build AzureMonitor API queries" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-04-25 19:30:28 -07:00
"resourceURI" : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana" ,
"timeGrain" : "PT1M" ,
"top" : "10" ,
2020-06-01 12:37:39 -04:00
} ,
expectedInterval : "PT1M" ,
2023-05-18 10:50:48 -06:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=10" ,
2020-06-01 12:37:39 -04:00
} ,
{
2022-04-25 19:30:28 -07:00
name : "legacy query without resourceURI and time grain set to auto" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2020-06-01 12:37:39 -04:00
"timeGrain" : "auto" ,
"top" : "10" ,
} ,
2021-06-07 14:54:51 +02:00
queryInterval : duration ,
2020-06-01 12:37:39 -04:00
expectedInterval : "PT15M" ,
2023-05-18 10:50:48 -06:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT15M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=10" ,
2020-06-01 12:37:39 -04:00
} ,
{
2022-04-25 19:30:28 -07:00
name : "legacy query without resourceURI and time grain set to auto" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2020-06-01 12:37:39 -04:00
"timeGrain" : "auto" ,
"allowedTimeGrainsMs" : [ ] int64 { 60000 , 300000 } ,
"top" : "10" ,
} ,
2021-06-07 14:54:51 +02:00
queryInterval : duration ,
2020-06-01 12:37:39 -04:00
expectedInterval : "PT5M" ,
2023-05-18 10:50:48 -06:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT5M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=10" ,
2020-06-01 12:37:39 -04:00
} ,
{
2022-04-25 19:30:28 -07:00
name : "legacy query without resourceURI and has a dimension filter" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2020-06-01 12:37:39 -04:00
"timeGrain" : "PT1M" ,
"dimension" : "blob" ,
"dimensionFilter" : "*" ,
"top" : "30" ,
} ,
2021-06-07 14:54:51 +02:00
queryInterval : duration ,
2020-06-01 12:37:39 -04:00
expectedInterval : "PT1M" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=30" ,
2023-01-13 12:30:45 +01:00
expectedParamFilter : "blob eq '*'" ,
2020-06-01 12:37:39 -04:00
} ,
{
2022-04-25 19:30:28 -07:00
name : "legacy query without resourceURI and has a dimension filter and none Dimension" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2020-06-01 12:37:39 -04:00
"timeGrain" : "PT1M" ,
"dimension" : "None" ,
"dimensionFilter" : "*" ,
"top" : "10" ,
} ,
2021-06-07 14:54:51 +02:00
queryInterval : duration ,
2020-06-01 12:37:39 -04:00
expectedInterval : "PT1M" ,
2023-05-18 10:50:48 -06:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=10" ,
2020-06-01 12:37:39 -04:00
} ,
2020-06-30 16:26:46 -04:00
{
2022-04-25 19:30:28 -07:00
name : "legacy query without resourceURI and has dimensionFilter*s* property with one dimension" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2020-09-08 15:12:08 -04:00
"timeGrain" : "PT1M" ,
2023-06-10 15:30:49 -05:00
"dimensionFilters" : [ ] dataquery . AzureMetricDimension { { Dimension : strPtr ( "blob" ) , Operator : strPtr ( "eq" ) , Filter : & wildcardFilter } } ,
2020-09-08 15:12:08 -04:00
"top" : "30" ,
2020-06-30 16:26:46 -04:00
} ,
2021-06-07 14:54:51 +02:00
queryInterval : duration ,
2020-06-30 16:26:46 -04:00
expectedInterval : "PT1M" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=30" ,
2023-01-13 12:30:45 +01:00
expectedParamFilter : "blob eq '*'" ,
2024-01-03 19:20:22 +00:00
expectedPortalURL : Pointer ( "http://ds/#blade/Microsoft_Azure_MonitoringMetrics/Metrics.ReactView/Referer/MetricsExplorer/TimeContext/%7B%22absolute%22%3A%7B%22startTime%22%3A%222018-03-15T13%3A00%3A00Z%22%2C%22endTime%22%3A%222018-03-15T13%3A34%3A00Z%22%7D%7D/ChartDefinition/%7B%22v2charts%22%3A%5B%7B%22grouping%22%3A%7B%22dimension%22%3A%22blob%22%2C%22sort%22%3A2%2C%22top%22%3A10%7D%2C%22metrics%22%3A%5B%7B%22resourceMetadata%22%3A%7B%22id%22%3A%22%2Fsubscriptions%2F12345678-aaaa-bbbb-cccc-123456789abc%2FresourceGroups%2Fgrafanastaging%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Fgrafana%22%7D%2C%22name%22%3A%22Percentage%20CPU%22%2C%22aggregationType%22%3A4%2C%22namespace%22%3A%22Microsoft.Compute%2FvirtualMachines%22%2C%22metricVisualization%22%3A%7B%22displayName%22%3A%22Percentage%20CPU%22%2C%22resourceDisplayName%22%3A%22grafana%22%7D%7D%5D%7D%5D%7D" ) ,
2020-06-30 16:26:46 -04:00
} ,
{
2022-04-25 19:30:28 -07:00
name : "legacy query without resourceURI and has dimensionFilter*s* property with two dimensions" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2020-09-08 15:12:08 -04:00
"timeGrain" : "PT1M" ,
2023-06-10 15:30:49 -05:00
"dimensionFilters" : [ ] dataquery . AzureMetricDimension { { Dimension : strPtr ( "blob" ) , Operator : strPtr ( "eq" ) , Filter : & wildcardFilter } , { Dimension : strPtr ( "tier" ) , Operator : strPtr ( "eq" ) , Filter : & wildcardFilter } } ,
2020-09-08 15:12:08 -04:00
"top" : "30" ,
2020-06-30 16:26:46 -04:00
} ,
2021-06-07 14:54:51 +02:00
queryInterval : duration ,
2020-06-30 16:26:46 -04:00
expectedInterval : "PT1M" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=30" ,
2023-01-13 12:30:45 +01:00
expectedParamFilter : "blob eq '*' and tier eq '*'" ,
2024-01-03 19:20:22 +00:00
expectedPortalURL : Pointer ( "http://ds/#blade/Microsoft_Azure_MonitoringMetrics/Metrics.ReactView/Referer/MetricsExplorer/TimeContext/%7B%22absolute%22%3A%7B%22startTime%22%3A%222018-03-15T13%3A00%3A00Z%22%2C%22endTime%22%3A%222018-03-15T13%3A34%3A00Z%22%7D%7D/ChartDefinition/%7B%22v2charts%22%3A%5B%7B%22grouping%22%3A%7B%22dimension%22%3A%22blob%22%2C%22sort%22%3A2%2C%22top%22%3A10%7D%2C%22metrics%22%3A%5B%7B%22resourceMetadata%22%3A%7B%22id%22%3A%22%2Fsubscriptions%2F12345678-aaaa-bbbb-cccc-123456789abc%2FresourceGroups%2Fgrafanastaging%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Fgrafana%22%7D%2C%22name%22%3A%22Percentage%20CPU%22%2C%22aggregationType%22%3A4%2C%22namespace%22%3A%22Microsoft.Compute%2FvirtualMachines%22%2C%22metricVisualization%22%3A%7B%22displayName%22%3A%22Percentage%20CPU%22%2C%22resourceDisplayName%22%3A%22grafana%22%7D%7D%5D%7D%5D%7D" ) ,
2020-06-30 16:26:46 -04:00
} ,
2022-03-29 07:27:09 -07:00
{
2022-04-25 19:30:28 -07:00
name : "legacy query without resourceURI and has a dimension filter without specifying a top" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-03-29 07:27:09 -07:00
"timeGrain" : "PT1M" ,
"dimension" : "blob" ,
"dimensionFilter" : "*" ,
} ,
queryInterval : duration ,
expectedInterval : "PT1M" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z" ,
2023-01-13 12:30:45 +01:00
expectedParamFilter : "blob eq '*'" ,
2022-03-29 07:27:09 -07:00
} ,
2022-04-29 17:01:13 +01:00
{
name : "has dimensionFilter*s* property with not equals operator" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-04-29 17:01:13 +01:00
"timeGrain" : "PT1M" ,
2023-06-10 15:30:49 -05:00
"dimensionFilters" : [ ] dataquery . AzureMetricDimension { { Dimension : strPtr ( "blob" ) , Operator : strPtr ( "ne" ) , Filter : & wildcardFilter , Filters : [ ] string { "test" } } } ,
2022-04-29 17:01:13 +01:00
"top" : "30" ,
} ,
queryInterval : duration ,
expectedInterval : "PT1M" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=30" ,
2023-01-13 12:30:45 +01:00
expectedParamFilter : "blob ne 'test'" ,
2024-01-03 19:20:22 +00:00
expectedPortalURL : Pointer ( "http://ds/#blade/Microsoft_Azure_MonitoringMetrics/Metrics.ReactView/Referer/MetricsExplorer/TimeContext/%7B%22absolute%22%3A%7B%22startTime%22%3A%222018-03-15T13%3A00%3A00Z%22%2C%22endTime%22%3A%222018-03-15T13%3A34%3A00Z%22%7D%7D/ChartDefinition/%7B%22v2charts%22%3A%5B%7B%22filterCollection%22%3A%7B%22filters%22%3A%5B%7B%22key%22%3A%22blob%22%2C%22operator%22%3A1%2C%22values%22%3A%5B%22test%22%5D%7D%5D%7D%2C%22grouping%22%3A%7B%22dimension%22%3A%22blob%22%2C%22sort%22%3A2%2C%22top%22%3A10%7D%2C%22metrics%22%3A%5B%7B%22resourceMetadata%22%3A%7B%22id%22%3A%22%2Fsubscriptions%2F12345678-aaaa-bbbb-cccc-123456789abc%2FresourceGroups%2Fgrafanastaging%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Fgrafana%22%7D%2C%22name%22%3A%22Percentage%20CPU%22%2C%22aggregationType%22%3A4%2C%22namespace%22%3A%22Microsoft.Compute%2FvirtualMachines%22%2C%22metricVisualization%22%3A%7B%22displayName%22%3A%22Percentage%20CPU%22%2C%22resourceDisplayName%22%3A%22grafana%22%7D%7D%5D%7D%5D%7D" ) ,
2022-04-29 17:01:13 +01:00
} ,
{
name : "has dimensionFilter*s* property with startsWith operator" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-04-29 17:01:13 +01:00
"timeGrain" : "PT1M" ,
2023-06-10 15:30:49 -05:00
"dimensionFilters" : [ ] dataquery . AzureMetricDimension { { Dimension : strPtr ( "blob" ) , Operator : strPtr ( "sw" ) , Filter : & testFilter } } ,
2022-04-29 17:01:13 +01:00
"top" : "30" ,
} ,
queryInterval : duration ,
expectedInterval : "PT1M" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=30" ,
2023-01-13 12:30:45 +01:00
expectedParamFilter : "blob sw 'test'" ,
2024-01-03 19:20:22 +00:00
expectedPortalURL : Pointer ( "http://ds/#blade/Microsoft_Azure_MonitoringMetrics/Metrics.ReactView/Referer/MetricsExplorer/TimeContext/%7B%22absolute%22%3A%7B%22startTime%22%3A%222018-03-15T13%3A00%3A00Z%22%2C%22endTime%22%3A%222018-03-15T13%3A34%3A00Z%22%7D%7D/ChartDefinition/%7B%22v2charts%22%3A%5B%7B%22filterCollection%22%3A%7B%22filters%22%3A%5B%7B%22key%22%3A%22blob%22%2C%22operator%22%3A3%2C%22values%22%3A%5B%22test%22%5D%7D%5D%7D%2C%22grouping%22%3A%7B%22dimension%22%3A%22blob%22%2C%22sort%22%3A2%2C%22top%22%3A10%7D%2C%22metrics%22%3A%5B%7B%22resourceMetadata%22%3A%7B%22id%22%3A%22%2Fsubscriptions%2F12345678-aaaa-bbbb-cccc-123456789abc%2FresourceGroups%2Fgrafanastaging%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Fgrafana%22%7D%2C%22name%22%3A%22Percentage%20CPU%22%2C%22aggregationType%22%3A4%2C%22namespace%22%3A%22Microsoft.Compute%2FvirtualMachines%22%2C%22metricVisualization%22%3A%7B%22displayName%22%3A%22Percentage%20CPU%22%2C%22resourceDisplayName%22%3A%22grafana%22%7D%7D%5D%7D%5D%7D" ) ,
2022-04-29 17:01:13 +01:00
} ,
{
name : "correctly sets dimension operator to eq (irrespective of operator) when filter value is '*'" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-04-29 17:01:13 +01:00
"timeGrain" : "PT1M" ,
2023-06-10 15:30:49 -05:00
"dimensionFilters" : [ ] dataquery . AzureMetricDimension { { Dimension : strPtr ( "blob" ) , Operator : strPtr ( "sw" ) , Filter : & wildcardFilter } , { Dimension : strPtr ( "tier" ) , Operator : strPtr ( "ne" ) , Filter : & wildcardFilter } } ,
2022-04-29 17:01:13 +01:00
"top" : "30" ,
} ,
queryInterval : duration ,
expectedInterval : "PT1M" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=30" ,
2023-01-13 12:30:45 +01:00
expectedParamFilter : "blob eq '*' and tier eq '*'" ,
2024-01-03 19:20:22 +00:00
expectedPortalURL : Pointer ( "http://ds/#blade/Microsoft_Azure_MonitoringMetrics/Metrics.ReactView/Referer/MetricsExplorer/TimeContext/%7B%22absolute%22%3A%7B%22startTime%22%3A%222018-03-15T13%3A00%3A00Z%22%2C%22endTime%22%3A%222018-03-15T13%3A34%3A00Z%22%7D%7D/ChartDefinition/%7B%22v2charts%22%3A%5B%7B%22grouping%22%3A%7B%22dimension%22%3A%22blob%22%2C%22sort%22%3A2%2C%22top%22%3A10%7D%2C%22metrics%22%3A%5B%7B%22resourceMetadata%22%3A%7B%22id%22%3A%22%2Fsubscriptions%2F12345678-aaaa-bbbb-cccc-123456789abc%2FresourceGroups%2Fgrafanastaging%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Fgrafana%22%7D%2C%22name%22%3A%22Percentage%20CPU%22%2C%22aggregationType%22%3A4%2C%22namespace%22%3A%22Microsoft.Compute%2FvirtualMachines%22%2C%22metricVisualization%22%3A%7B%22displayName%22%3A%22Percentage%20CPU%22%2C%22resourceDisplayName%22%3A%22grafana%22%7D%7D%5D%7D%5D%7D" ) ,
2022-04-29 17:01:13 +01:00
} ,
2022-05-10 15:05:48 +01:00
{
name : "correctly constructs target when multiple filter values are provided for the 'eq' operator" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-05-10 15:05:48 +01:00
"timeGrain" : "PT1M" ,
2023-06-10 15:30:49 -05:00
"dimensionFilters" : [ ] dataquery . AzureMetricDimension { { Dimension : strPtr ( "blob" ) , Operator : strPtr ( "eq" ) , Filter : & wildcardFilter , Filters : [ ] string { "test" , "test2" } } } ,
2022-05-10 15:05:48 +01:00
"top" : "30" ,
} ,
queryInterval : duration ,
expectedInterval : "PT1M" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=30" ,
2023-01-13 12:30:45 +01:00
expectedParamFilter : "blob eq 'test' or blob eq 'test2'" ,
2024-01-03 19:20:22 +00:00
expectedPortalURL : Pointer ( "http://ds/#blade/Microsoft_Azure_MonitoringMetrics/Metrics.ReactView/Referer/MetricsExplorer/TimeContext/%7B%22absolute%22%3A%7B%22startTime%22%3A%222018-03-15T13%3A00%3A00Z%22%2C%22endTime%22%3A%222018-03-15T13%3A34%3A00Z%22%7D%7D/ChartDefinition/%7B%22v2charts%22%3A%5B%7B%22filterCollection%22%3A%7B%22filters%22%3A%5B%7B%22key%22%3A%22blob%22%2C%22operator%22%3A0%2C%22values%22%3A%5B%22test%22%2C%22test2%22%5D%7D%5D%7D%2C%22grouping%22%3A%7B%22dimension%22%3A%22blob%22%2C%22sort%22%3A2%2C%22top%22%3A10%7D%2C%22metrics%22%3A%5B%7B%22resourceMetadata%22%3A%7B%22id%22%3A%22%2Fsubscriptions%2F12345678-aaaa-bbbb-cccc-123456789abc%2FresourceGroups%2Fgrafanastaging%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Fgrafana%22%7D%2C%22name%22%3A%22Percentage%20CPU%22%2C%22aggregationType%22%3A4%2C%22namespace%22%3A%22Microsoft.Compute%2FvirtualMachines%22%2C%22metricVisualization%22%3A%7B%22displayName%22%3A%22Percentage%20CPU%22%2C%22resourceDisplayName%22%3A%22grafana%22%7D%7D%5D%7D%5D%7D" ) ,
2022-05-10 15:05:48 +01:00
} ,
{
name : "correctly constructs target when multiple filter values are provided for ne 'eq' operator" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-05-10 15:05:48 +01:00
"timeGrain" : "PT1M" ,
2023-06-10 15:30:49 -05:00
"dimensionFilters" : [ ] dataquery . AzureMetricDimension { { Dimension : strPtr ( "blob" ) , Operator : strPtr ( "ne" ) , Filter : & wildcardFilter , Filters : [ ] string { "test" , "test2" } } } ,
2022-05-10 15:05:48 +01:00
"top" : "30" ,
} ,
queryInterval : duration ,
expectedInterval : "PT1M" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=30" ,
2023-01-13 12:30:45 +01:00
expectedParamFilter : "blob ne 'test' and blob ne 'test2'" ,
2024-01-03 19:20:22 +00:00
expectedPortalURL : Pointer ( "http://ds/#blade/Microsoft_Azure_MonitoringMetrics/Metrics.ReactView/Referer/MetricsExplorer/TimeContext/%7B%22absolute%22%3A%7B%22startTime%22%3A%222018-03-15T13%3A00%3A00Z%22%2C%22endTime%22%3A%222018-03-15T13%3A34%3A00Z%22%7D%7D/ChartDefinition/%7B%22v2charts%22%3A%5B%7B%22filterCollection%22%3A%7B%22filters%22%3A%5B%7B%22key%22%3A%22blob%22%2C%22operator%22%3A1%2C%22values%22%3A%5B%22test%22%2C%22test2%22%5D%7D%5D%7D%2C%22grouping%22%3A%7B%22dimension%22%3A%22blob%22%2C%22sort%22%3A2%2C%22top%22%3A10%7D%2C%22metrics%22%3A%5B%7B%22resourceMetadata%22%3A%7B%22id%22%3A%22%2Fsubscriptions%2F12345678-aaaa-bbbb-cccc-123456789abc%2FresourceGroups%2Fgrafanastaging%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Fgrafana%22%7D%2C%22name%22%3A%22Percentage%20CPU%22%2C%22aggregationType%22%3A4%2C%22namespace%22%3A%22Microsoft.Compute%2FvirtualMachines%22%2C%22metricVisualization%22%3A%7B%22displayName%22%3A%22Percentage%20CPU%22%2C%22resourceDisplayName%22%3A%22grafana%22%7D%7D%5D%7D%5D%7D" ) ,
2022-10-07 14:57:01 +02:00
} ,
{
name : "Includes a region" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-10-07 14:57:01 +02:00
"timeGrain" : "PT1M" ,
"top" : "10" ,
"region" : "westus" ,
} ,
expectedInterval : "PT1M" ,
2023-05-18 10:50:48 -06:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines®ion=westus×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=10" ,
2023-01-13 12:30:45 +01:00
expectedURL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
2022-10-07 14:57:01 +02:00
} ,
2022-12-02 13:17:11 +01:00
{
2023-01-13 12:30:45 +01:00
name : "Includes a region and a filter in the body with multiple resources" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-12-02 13:17:11 +01:00
"timeGrain" : "PT1M" ,
"top" : "10" ,
"region" : "westus" ,
2023-06-10 15:30:49 -05:00
"resources" : [ ] dataquery . AzureMonitorResource { { ResourceGroup : strPtr ( "rg" ) , ResourceName : strPtr ( "vm" ) } , { ResourceGroup : strPtr ( "rg2" ) , ResourceName : strPtr ( "vm2" ) } } ,
2022-12-02 13:17:11 +01:00
} ,
expectedInterval : "PT1M" ,
2023-05-18 10:50:48 -06:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines®ion=westus×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=10" ,
2022-12-02 13:17:11 +01:00
expectedURL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/providers/microsoft.insights/metrics" ,
2023-01-13 12:30:45 +01:00
expectedBodyFilter : "Microsoft.ResourceId eq '/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm' or Microsoft.ResourceId eq '/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/rg2/providers/Microsoft.Compute/virtualMachines/vm2'" ,
2022-12-02 13:17:11 +01:00
} ,
2022-10-07 14:57:01 +02:00
{
2023-01-13 12:30:45 +01:00
name : "includes a single resource as a parameter filter" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-10-07 14:57:01 +02:00
"timeGrain" : "PT1M" ,
2023-06-10 15:30:49 -05:00
"resources" : [ ] dataquery . AzureMonitorResource { { ResourceGroup : strPtr ( "rg" ) , ResourceName : strPtr ( "vm" ) } } ,
2022-10-07 14:57:01 +02:00
} ,
queryInterval : duration ,
expectedInterval : "PT1M" ,
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z" ,
2023-01-13 12:30:45 +01:00
expectedURL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm/providers/microsoft.insights/metrics" ,
2022-10-07 14:57:01 +02:00
} ,
{
name : "includes a resource and a dimesion as filters" ,
2023-08-30 08:46:47 -07:00
azureMonitorVariedProperties : map [ string ] any {
2022-10-07 14:57:01 +02:00
"timeGrain" : "PT1M" ,
2023-06-10 15:30:49 -05:00
"resources" : [ ] dataquery . AzureMonitorResource { { ResourceGroup : strPtr ( "rg" ) , ResourceName : strPtr ( "vm" ) } , { ResourceGroup : strPtr ( "rg2" ) , ResourceName : strPtr ( "vm2" ) } } ,
"dimensionFilters" : [ ] dataquery . AzureMetricDimension { { Dimension : strPtr ( "blob" ) , Operator : strPtr ( "ne" ) , Filter : & wildcardFilter , Filters : [ ] string { "test" , "test2" } } } ,
2022-10-07 14:57:01 +02:00
"top" : "30" ,
} ,
queryInterval : duration ,
expectedInterval : "PT1M" ,
2023-01-13 12:30:45 +01:00
expectedURL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/providers/microsoft.insights/metrics" ,
2022-10-07 14:57:01 +02:00
azureMonitorQueryTarget : "aggregation=Average&api-version=2021-05-01&interval=PT1M&metricnames=Percentage+CPU&metricnamespace=Microsoft.Compute%2FvirtualMachines×pan=2018-03-15T13%3A00%3A00Z%2F2018-03-15T13%3A34%3A00Z&top=30" ,
2023-01-13 12:30:45 +01:00
expectedBodyFilter : "(Microsoft.ResourceId eq '/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm' or Microsoft.ResourceId eq '/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/rg2/providers/Microsoft.Compute/virtualMachines/vm2') and (blob ne 'test' and blob ne 'test2')" ,
2024-01-03 19:20:22 +00:00
expectedPortalURL : Pointer ( "http://ds/#blade/Microsoft_Azure_MonitoringMetrics/Metrics.ReactView/Referer/MetricsExplorer/TimeContext/%7B%22absolute%22%3A%7B%22startTime%22%3A%222018-03-15T13%3A00%3A00Z%22%2C%22endTime%22%3A%222018-03-15T13%3A34%3A00Z%22%7D%7D/ChartDefinition/%7B%22v2charts%22%3A%5B%7B%22filterCollection%22%3A%7B%22filters%22%3A%5B%7B%22key%22%3A%22blob%22%2C%22operator%22%3A1%2C%22values%22%3A%5B%22test%22%2C%22test2%22%5D%7D%5D%7D%2C%22grouping%22%3A%7B%22dimension%22%3A%22blob%22%2C%22sort%22%3A2%2C%22top%22%3A10%7D%2C%22metrics%22%3A%5B%7B%22resourceMetadata%22%3A%7B%22id%22%3A%22%2Fsubscriptions%2F12345678-aaaa-bbbb-cccc-123456789abc%2FresourceGroups%2Fgrafanastaging%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Fgrafana%22%7D%2C%22name%22%3A%22Percentage%20CPU%22%2C%22aggregationType%22%3A4%2C%22namespace%22%3A%22Microsoft.Compute%2FvirtualMachines%22%2C%22metricVisualization%22%3A%7B%22displayName%22%3A%22Percentage%20CPU%22%2C%22resourceDisplayName%22%3A%22grafana%22%7D%7D%5D%7D%5D%7D" ) ,
2022-05-10 15:05:48 +01:00
} ,
2020-06-01 12:37:39 -04:00
}
2023-08-30 08:46:47 -07:00
commonAzureModelProps := map [ string ] any {
2022-07-27 15:26:32 +02:00
"aggregation" : "Average" ,
"resourceGroup" : "grafanastaging" ,
"resourceName" : "grafana" ,
"metricNamespace" : "Microsoft.Compute/virtualMachines" ,
"metricName" : "Percentage CPU" ,
2019-02-08 17:20:31 +01:00
2020-06-01 12:37:39 -04:00
"alias" : "testalias" ,
"queryType" : "Azure Monitor" ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
for k , v := range commonAzureModelProps {
tt . azureMonitorVariedProperties [ k ] = v
}
2021-06-07 14:54:51 +02:00
azureMonitorJSON , _ := json . Marshal ( tt . azureMonitorVariedProperties )
tsdbQuery := [ ] backend . DataQuery {
{
JSON : [ ] byte ( fmt . Sprintf ( ` {
2019-05-21 12:28:30 +02:00
"subscription" : "12345678-aaaa-bbbb-cccc-123456789abc" ,
2021-06-07 14:54:51 +02:00
"azureMonitor" : % s
} ` , string ( azureMonitorJSON ) ) ) ,
RefID : "A" ,
Interval : tt . queryInterval ,
TimeRange : backend . TimeRange {
From : fromStart ,
To : fromStart . Add ( 34 * time . Minute ) ,
2019-02-08 17:20:31 +01:00
} ,
} ,
}
2019-02-10 01:47:38 +01:00
2023-08-30 08:39:31 -05:00
queries , err := datasource . buildQueries ( tsdbQuery , dsInfo )
2022-12-02 13:17:11 +01:00
require . NoError ( t , err )
2023-06-10 15:30:49 -05:00
resources := map [ string ] dataquery . AzureMonitorResource { }
2023-05-22 11:03:43 +01:00
if tt . azureMonitorVariedProperties [ "resources" ] != nil {
2023-06-10 15:30:49 -05:00
resourceSlice := tt . azureMonitorVariedProperties [ "resources" ] . ( [ ] dataquery . AzureMonitorResource )
2023-05-22 11:03:43 +01:00
for _ , resource := range resourceSlice {
2023-06-10 15:30:49 -05:00
resources [ fmt . Sprintf ( "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s" , * resource . ResourceGroup , * resource . ResourceName ) ] = resource
2023-05-22 11:03:43 +01:00
}
} else {
2023-06-10 15:30:49 -05:00
resources [ "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana" ] = dataquery . AzureMonitorResource { ResourceGroup : strPtr ( "grafanastaging" ) , ResourceName : strPtr ( "grafana" ) }
2023-05-22 11:03:43 +01:00
}
2022-03-02 06:41:07 -08:00
azureMonitorQuery := & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : tt . expectedURL ,
Target : tt . azureMonitorQueryTarget ,
RefID : "A" ,
Alias : "testalias" ,
2021-06-07 14:54:51 +02:00
TimeRange : backend . TimeRange {
From : fromStart ,
To : fromStart . Add ( 34 * time . Minute ) ,
} ,
2023-05-22 11:03:43 +01:00
BodyFilter : tt . expectedBodyFilter ,
Subscription : "12345678-aaaa-bbbb-cccc-123456789abc" ,
Resources : resources ,
2022-12-02 13:17:11 +01:00
}
2022-12-21 16:30:17 +00:00
2023-01-13 12:30:45 +01:00
assert . Equal ( t , tt . expectedParamFilter , queries [ 0 ] . Params . Get ( "$filter" ) )
2022-10-07 14:57:01 +02:00
if azureMonitorQuery . URL == "" {
azureMonitorQuery . URL = "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics"
2020-06-01 12:37:39 -04:00
}
2019-07-04 22:47:24 +02:00
2023-05-23 14:01:49 +03:00
if diff := cmp . Diff ( azureMonitorQuery , queries [ 0 ] , cmpopts . IgnoreUnexported ( struct { } { } ) , cmpopts . IgnoreFields ( types . AzureMonitorQuery { } , "Params" , "Dimensions" ) ) ; diff != "" {
2020-06-01 12:37:39 -04:00
t . Errorf ( "Result mismatch (-want +got):\n%s" , diff )
}
2021-09-28 09:24:18 -04:00
2022-12-21 16:30:17 +00:00
expectedPortalURL := ` http://ds/#blade/Microsoft_Azure_MonitoringMetrics/Metrics.ReactView/Referer/MetricsExplorer/ ` +
2021-09-28 09:24:18 -04:00
` TimeContext/%7B%22absolute%22%3A%7B%22startTime%22%3A%222018-03-15T13%3A00%3A00Z%22%2C%22endTime%22%3A%222018-03-15T13%3A34%3A00Z%22%7D%7D/ ` +
` ChartDefinition/%7B%22v2charts%22%3A%5B%7B%22metrics%22%3A%5B%7B%22resourceMetadata%22%3A%7B%22id%22%3A%22%2Fsubscriptions%2F12345678-aaaa-bbbb-cccc-123456789abc%2FresourceGroups%2Fgrafanastaging%2Fproviders%2FMicrosoft.Compute%2FvirtualMachines%2Fgrafana%22%7D%2C ` +
2022-07-27 15:26:32 +02:00
` %22name%22%3A%22Percentage%20CPU%22%2C%22aggregationType%22%3A4%2C%22namespace%22%3A%22Microsoft.Compute%2FvirtualMachines%22%2C%22metricVisualization%22%3A%7B%22displayName%22%3A%22Percentage%20CPU%22%2C%22resourceDisplayName%22%3A%22grafana%22%7D%7D%5D%7D%5D%7D `
2022-12-21 16:30:17 +00:00
if tt . expectedPortalURL != nil {
expectedPortalURL = * tt . expectedPortalURL
}
2022-10-07 14:57:01 +02:00
actual , err := getQueryUrl ( queries [ 0 ] , "http://ds" , "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana" , "grafana" )
2021-09-28 09:24:18 -04:00
require . NoError ( t , err )
2022-12-21 16:30:17 +00:00
require . Equal ( t , expectedPortalURL , actual )
2019-02-08 17:20:31 +01:00
} )
2020-06-01 12:37:39 -04:00
}
}
2019-02-08 18:15:17 +01:00
2022-09-12 06:10:51 -04:00
func TestCustomNamespace ( t * testing . T ) {
datasource := & AzureMonitorDatasource { }
t . Run ( "it should set the metricNamespace to a customNamespace value if customNamespace is present as a parameter" , func ( t * testing . T ) {
q := [ ] backend . DataQuery {
{
JSON : [ ] byte ( ` {
"azureMonitor" : {
2023-05-23 14:01:49 +03:00
"customNamespace" : "custom/namespace"
2022-09-12 06:10:51 -04:00
}
} ` ) ,
} ,
}
2023-08-30 08:39:31 -05:00
result , err := datasource . buildQueries ( q , types . DatasourceInfo { } )
2022-09-12 06:10:51 -04:00
require . NoError ( t , err )
expected := "custom/namespace"
require . Equal ( t , expected , result [ 0 ] . Params . Get ( "metricnamespace" ) )
} )
}
2020-06-01 12:37:39 -04:00
func TestAzureMonitorParseResponse ( t * testing . T ) {
2023-06-10 15:30:49 -05:00
resources := map [ string ] dataquery . AzureMonitorResource { }
resources [ "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana" ] =
dataquery . AzureMonitorResource { ResourceGroup : strPtr ( "grafanastaging" ) , ResourceName : strPtr ( "grafana" ) }
2023-05-22 11:03:43 +01:00
subscription := "12345678-aaaa-bbbb-cccc-123456789abc"
2020-06-01 12:37:39 -04:00
tests := [ ] struct {
name string
responseFile string
2022-03-02 06:41:07 -08:00
mockQuery * types . AzureMonitorQuery
2020-06-01 12:37:39 -04:00
expectedFrames data . Frames
queryIntervalMS int64
} {
{
name : "average aggregate time series response" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/1-azure-monitor-response-avg.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
2020-06-01 12:37:39 -04:00
Params : url . Values {
"aggregation" : { "Average" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2020-06-01 12:37:39 -04:00
} ,
} ,
{
name : "total aggregate time series response" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/2-azure-monitor-response-total.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
2020-06-01 12:37:39 -04:00
Params : url . Values {
"aggregation" : { "Total" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2020-06-01 12:37:39 -04:00
} ,
} ,
{
name : "maximum aggregate time series response" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/3-azure-monitor-response-maximum.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
2020-06-01 12:37:39 -04:00
Params : url . Values {
"aggregation" : { "Maximum" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2020-06-01 12:37:39 -04:00
} ,
} ,
{
name : "minimum aggregate time series response" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/4-azure-monitor-response-minimum.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
2020-06-01 12:37:39 -04:00
Params : url . Values {
"aggregation" : { "Minimum" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2020-06-01 12:37:39 -04:00
} ,
} ,
{
name : "count aggregate time series response" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/5-azure-monitor-response-count.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
2020-06-01 12:37:39 -04:00
Params : url . Values {
"aggregation" : { "Count" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2020-06-01 12:37:39 -04:00
} ,
} ,
{
2020-06-30 16:26:46 -04:00
name : "single dimension time series response" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/6-azure-monitor-response-single-dimension.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
2020-06-01 12:37:39 -04:00
Params : url . Values {
"aggregation" : { "Average" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2020-06-01 12:37:39 -04:00
} ,
} ,
{
name : "with alias patterns in the query" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/2-azure-monitor-response-total.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
Alias : "custom {{resourcegroup}} {{namespace}} {{resourceName}} {{metric}}" ,
2020-06-01 12:37:39 -04:00
Params : url . Values {
"aggregation" : { "Total" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2020-06-01 12:37:39 -04:00
} ,
} ,
{
2020-06-30 16:26:46 -04:00
name : "single dimension with alias" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/6-azure-monitor-response-single-dimension.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
Alias : "{{dimensionname}}={{DimensionValue}}" ,
2020-06-01 12:37:39 -04:00
Params : url . Values {
"aggregation" : { "Average" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2020-06-01 12:37:39 -04:00
} ,
2020-06-30 16:26:46 -04:00
} ,
{
name : "multiple dimension time series response with label alias" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/7-azure-monitor-response-multi-dimension.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2023-05-22 11:03:43 +01:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanatest/providers/Microsoft.Storage/storageAccounts/testblobaccount/blobServices/default/providers/Microsoft.Insights/metrics" ,
2022-10-07 14:57:01 +02:00
Alias : "{{resourcegroup}} {Blob Type={{blobtype}}, Tier={{Tier}}}" ,
2020-06-30 16:26:46 -04:00
Params : url . Values {
"aggregation" : { "Average" } ,
} ,
2023-06-10 15:30:49 -05:00
Resources : map [ string ] dataquery . AzureMonitorResource { "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanatest/providers/Microsoft.Storage/storageAccounts/testblobaccount/blobServices/default/providers/Microsoft.Insights/metrics" : { ResourceGroup : strPtr ( "grafanatest" ) , ResourceName : strPtr ( "testblobaccount" ) } } ,
2023-05-22 11:03:43 +01:00
Subscription : subscription ,
2020-06-30 16:26:46 -04:00
} ,
2020-06-01 12:37:39 -04:00
} ,
2020-08-21 11:37:30 -04:00
{
name : "unspecified unit with alias should not panic" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/8-azure-monitor-response-unspecified-unit.json" ,
2022-03-02 06:41:07 -08:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
Alias : "custom" ,
2020-08-21 11:37:30 -04:00
Params : url . Values {
"aggregation" : { "Average" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2020-08-21 11:37:30 -04:00
} ,
} ,
2022-04-25 19:30:28 -07:00
{
name : "with legacy azure monitor query properties and without a resource uri" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/2-azure-monitor-response-total.json" ,
2022-04-25 19:30:28 -07:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
Alias : "custom {{resourcegroup}} {{namespace}} {{resourceName}} {{metric}}" ,
2022-04-25 19:30:28 -07:00
Params : url . Values {
"aggregation" : { "Total" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2022-04-25 19:30:28 -07:00
} ,
} ,
{
name : "with legacy azure monitor query properties and with a resource uri it should use the resource uri" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/2-azure-monitor-response-total.json" ,
2022-04-25 19:30:28 -07:00
mockQuery : & types . AzureMonitorQuery {
2022-10-07 14:57:01 +02:00
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/grafana/providers/microsoft.insights/metrics" ,
Alias : "custom {{resourcegroup}} {{namespace}} {{resourceName}} {{metric}}" ,
2022-04-25 19:30:28 -07:00
Params : url . Values {
"aggregation" : { "Total" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2022-04-25 19:30:28 -07:00
} ,
} ,
2022-10-07 14:57:01 +02:00
{
name : "multiple time series response" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/9-azure-monitor-response-multi.json" ,
2022-10-07 14:57:01 +02:00
mockQuery : & types . AzureMonitorQuery {
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/providers/microsoft.insights/metrics" ,
Params : url . Values {
"aggregation" : { "Average" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2022-10-07 14:57:01 +02:00
} ,
2023-04-21 19:24:23 +01:00
} ,
{
name : "multiple time series response with multiple dimensions" ,
2023-04-27 20:24:11 +01:00
responseFile : "azuremonitor/10-azure-monitor-response-multi-with-dimensions.json" ,
2023-04-21 19:24:23 +01:00
mockQuery : & types . AzureMonitorQuery {
URL : "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/providers/microsoft.insights/metrics" ,
Params : url . Values {
"aggregation" : { "Average" } ,
} ,
2023-05-22 11:03:43 +01:00
Resources : resources ,
Subscription : subscription ,
2023-04-21 19:24:23 +01:00
} ,
2022-10-07 14:57:01 +02:00
} ,
2020-06-01 12:37:39 -04:00
}
2019-07-04 22:47:24 +02:00
2020-06-01 12:37:39 -04:00
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-04-27 20:24:11 +01:00
azData := loadTestFile ( t , tt . responseFile )
2023-11-08 15:02:46 +00:00
datasource := & AzureMonitorDatasource { }
dframes , err := datasource . parseResponse ( azData , tt . mockQuery , "http://ds" , "" )
2020-06-01 12:37:39 -04:00
require . NoError ( t , err )
2021-03-08 07:02:49 +01:00
require . NotNil ( t , dframes )
2019-02-11 01:17:37 +01:00
2023-04-27 20:24:11 +01:00
testdata . CheckGoldenFrames ( t , "../testdata" , fmt . Sprintf ( "%s.%s" , tt . responseFile , strings . ReplaceAll ( tt . name , " " , "-" ) ) , dframes )
2020-06-01 12:37:39 -04:00
} )
}
}
2019-02-11 01:17:37 +01:00
2020-06-01 12:37:39 -04:00
func TestFindClosestAllowIntervalMS ( t * testing . T ) {
humanIntervalToMS := map [ string ] int64 {
"3m" : 180000 ,
"5m" : 300000 ,
"10m" : 600000 ,
"15m" : 900000 ,
"1d" : 86400000 ,
"2d" : 172800000 ,
}
tests := [ ] struct {
name string
allowedTimeGrains [ ] int64 // Note: Uses defaults when empty list
inputInterval int64
expectedInterval int64
} {
{
name : "closest to 3m is 5m" ,
allowedTimeGrains : [ ] int64 { } ,
inputInterval : humanIntervalToMS [ "3m" ] ,
expectedInterval : humanIntervalToMS [ "5m" ] ,
} ,
{
name : "closest to 10m is 15m" ,
allowedTimeGrains : [ ] int64 { } ,
inputInterval : humanIntervalToMS [ "10m" ] ,
expectedInterval : humanIntervalToMS [ "15m" ] ,
} ,
{
name : "closest to 2d is 1d" ,
allowedTimeGrains : [ ] int64 { } ,
inputInterval : humanIntervalToMS [ "2d" ] ,
expectedInterval : humanIntervalToMS [ "1d" ] ,
} ,
{
name : "closest to 3m is 1d when 1d is only allowed interval" ,
allowedTimeGrains : [ ] int64 { humanIntervalToMS [ "1d" ] } ,
inputInterval : humanIntervalToMS [ "2d" ] ,
expectedInterval : humanIntervalToMS [ "1d" ] ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2022-03-02 06:41:07 -08:00
interval := azTime . FindClosestAllowedIntervalMS ( tt . inputInterval , tt . allowedTimeGrains )
2020-06-01 12:37:39 -04:00
require . Equal ( t , tt . expectedInterval , interval )
2019-02-11 01:17:37 +01:00
} )
2020-06-01 12:37:39 -04:00
}
2019-02-08 17:20:31 +01:00
}
2019-02-08 18:15:17 +01:00
2022-03-02 06:41:07 -08:00
func loadTestFile ( t * testing . T , name string ) types . AzureMonitorResponse {
2020-12-15 09:32:06 +01:00
t . Helper ( )
2019-02-08 18:15:17 +01:00
2022-03-02 06:41:07 -08:00
path := filepath . Join ( "../testdata" , name )
2020-12-15 09:32:06 +01:00
// Ignore gosec warning G304 since it's a test
// nolint:gosec
2022-08-10 13:37:51 +00:00
jsonBody , err := os . ReadFile ( path )
2020-12-15 09:32:06 +01:00
require . NoError ( t , err )
2022-03-02 06:41:07 -08:00
var azData types . AzureMonitorResponse
2020-06-01 12:37:39 -04:00
err = json . Unmarshal ( jsonBody , & azData )
2020-12-15 09:32:06 +01:00
require . NoError ( t , err )
return azData
2019-02-08 18:15:17 +01:00
}
2021-06-11 17:02:24 +02:00
func TestAzureMonitorCreateRequest ( t * testing . T ) {
ctx := context . Background ( )
2021-07-16 12:47:26 +02:00
url := "http://ds/"
2021-06-11 17:02:24 +02:00
tests := [ ] struct {
name string
expectedURL string
expectedHeaders http . Header
Err require . ErrorAssertionFunc
} {
{
name : "creates a request" ,
2022-04-08 08:49:46 -07:00
expectedURL : "http://ds/" ,
2021-06-11 17:02:24 +02:00
expectedHeaders : http . Header {
"Content-Type" : [ ] string { "application/json" } ,
} ,
Err : require . NoError ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
ds := AzureMonitorDatasource { }
2023-08-30 08:39:31 -05:00
req , err := ds . createRequest ( ctx , url )
2021-06-11 17:02:24 +02:00
tt . Err ( t , err )
if req . URL . String ( ) != tt . expectedURL {
t . Errorf ( "Expecting %s, got %s" , tt . expectedURL , req . URL . String ( ) )
}
if ! cmp . Equal ( req . Header , tt . expectedHeaders ) {
t . Errorf ( "Unexpected HTTP headers: %v" , cmp . Diff ( req . Header , tt . expectedHeaders ) )
}
} )
}
}
2022-04-25 19:30:28 -07:00
func TestExtractResourceNameFromMetricsURL ( t * testing . T ) {
t . Run ( "it should extract the resourceName from a well-formed Metrics URL" , func ( t * testing . T ) {
url := "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/Grafana-Test.VM/providers/microsoft.insights/metrics"
expected := "Grafana-Test.VM"
require . Equal ( t , expected , extractResourceNameFromMetricsURL ( ( url ) ) )
} )
t . Run ( "it should extract the resourceName from a well-formed Metrics URL in a case insensitive manner" , func ( t * testing . T ) {
url := "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/Grafana-Test.VM/pRoViDeRs/MiCrOsOfT.iNsIgHtS/mEtRiCs"
expected := "Grafana-Test.VM"
require . Equal ( t , expected , extractResourceNameFromMetricsURL ( ( url ) ) )
} )
t . Run ( "it should return an empty string if no match is found" , func ( t * testing . T ) {
url := "/subscriptions/12345678-aaaa-bbbb-cccc-123456789abc/resourceGroups/grafanastaging/providers/Microsoft.Compute/virtualMachines/Grafana-Test.VM/providers/microsoft.insights/nope-this-part-does-not-match"
expected := ""
require . Equal ( t , expected , extractResourceNameFromMetricsURL ( ( url ) ) )
} )
}
2023-06-10 15:30:49 -05:00
func strPtr ( s string ) * string {
return & s
}