2022-03-02 22:41:07 +08:00
|
|
|
package metrics
|
2019-02-10 07:23:12 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2019-02-11 20:27:08 +08:00
|
|
|
// urlBuilder builds the URL for calling the Azure Monitor API
|
|
|
|
|
type urlBuilder struct {
|
2022-03-31 23:20:29 +08:00
|
|
|
ResourceURI string
|
|
|
|
|
|
2022-07-27 21:26:32 +08:00
|
|
|
// Following fields will be used to generate a ResourceURI
|
2019-05-21 18:28:30 +08:00
|
|
|
DefaultSubscription string
|
|
|
|
|
Subscription string
|
|
|
|
|
ResourceGroup string
|
2022-07-27 21:26:32 +08:00
|
|
|
MetricNamespace string
|
2019-05-21 18:28:30 +08:00
|
|
|
ResourceName string
|
2019-02-10 07:23:12 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-27 21:26:32 +08:00
|
|
|
func (params *urlBuilder) buildResourceURI() string {
|
|
|
|
|
if params.ResourceURI != "" {
|
|
|
|
|
return params.ResourceURI
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 23:49:46 +08:00
|
|
|
subscription := params.Subscription
|
|
|
|
|
|
|
|
|
|
if params.Subscription == "" {
|
|
|
|
|
subscription = params.DefaultSubscription
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 21:26:32 +08:00
|
|
|
metricNamespaceArray := strings.Split(params.MetricNamespace, "/")
|
2022-04-08 23:49:46 +08:00
|
|
|
resourceNameArray := strings.Split(params.ResourceName, "/")
|
2022-07-27 21:26:32 +08:00
|
|
|
provider := metricNamespaceArray[0]
|
|
|
|
|
metricNamespaceArray = metricNamespaceArray[1:]
|
2022-04-08 23:49:46 +08:00
|
|
|
|
2022-07-27 22:49:51 +08:00
|
|
|
if strings.HasPrefix(strings.ToLower(params.MetricNamespace), "microsoft.storage/storageaccounts/") &&
|
|
|
|
|
!strings.HasSuffix(params.ResourceName, "default") {
|
|
|
|
|
resourceNameArray = append(resourceNameArray, "default")
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 23:49:46 +08:00
|
|
|
urlArray := []string{
|
|
|
|
|
"/subscriptions",
|
|
|
|
|
subscription,
|
|
|
|
|
"resourceGroups",
|
|
|
|
|
params.ResourceGroup,
|
|
|
|
|
"providers",
|
|
|
|
|
provider,
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 21:26:32 +08:00
|
|
|
for i, namespace := range metricNamespaceArray {
|
|
|
|
|
urlArray = append(urlArray, namespace, resourceNameArray[i])
|
2022-04-08 23:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resourceURI := strings.Join(urlArray, "/")
|
|
|
|
|
return resourceURI
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 21:26:32 +08:00
|
|
|
// BuildMetricsURL checks the metric properties to see which form of the url
|
2019-02-10 07:23:12 +08:00
|
|
|
// should be returned
|
2022-03-31 23:20:29 +08:00
|
|
|
func (params *urlBuilder) BuildMetricsURL() string {
|
|
|
|
|
resourceURI := params.ResourceURI
|
|
|
|
|
|
|
|
|
|
// Prior to Grafana 9, we had a legacy query object rather than a resourceURI, so we manually create the resource URI
|
|
|
|
|
if resourceURI == "" {
|
2022-07-27 21:26:32 +08:00
|
|
|
resourceURI = params.buildResourceURI()
|
2019-02-10 07:23:12 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-31 23:20:29 +08:00
|
|
|
return fmt.Sprintf("%s/providers/microsoft.insights/metrics", resourceURI)
|
2019-02-10 07:23:12 +08:00
|
|
|
}
|
2022-10-07 20:57:01 +08:00
|
|
|
|
|
|
|
|
// BuildSubscriptionMetricsURL returns a URL for querying metrics for all resources in a subscription
|
|
|
|
|
// It requires to set a $filter and a region parameter
|
|
|
|
|
func BuildSubscriptionMetricsURL(subscription string) string {
|
|
|
|
|
return fmt.Sprintf("/subscriptions/%s/providers/microsoft.insights/metrics", subscription)
|
|
|
|
|
}
|