Skip to content

Commit

Permalink
Fix scale set resource id with azure_monitor output (#5821)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored May 20, 2019
1 parent 9cdf1ea commit ad877fd
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions plugins/outputs/azure_monitor/azure_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ type AzureMonitor struct {
MetricOutsideWindow selfstat.Stat
}

// VirtualMachineMetadata contains information about a VM from the metadata service
type virtualMachineMetadata struct {
Compute struct {
Location string `json:"location"`
Name string `json:"name"`
ResourceGroupName string `json:"resourceGroupName"`
SubscriptionID string `json:"subscriptionId"`
VMScaleSetName string `json:"vmScaleSetName"`
} `json:"compute"`
}

func (m *virtualMachineMetadata) ResourceID() string {
if m.Compute.VMScaleSetName != "" {
return fmt.Sprintf(
resourceIDScaleSetTemplate,
m.Compute.SubscriptionID,
m.Compute.ResourceGroupName,
m.Compute.VMScaleSetName,
)
} else {
return fmt.Sprintf(
resourceIDTemplate,
m.Compute.SubscriptionID,
m.Compute.ResourceGroupName,
m.Compute.Name,
)
}
}

type dimension struct {
name string
value string
Expand All @@ -63,11 +92,12 @@ const (
defaultNamespacePrefix = "Telegraf/"
defaultAuthResource = "https://monitoring.azure.com/"

vmInstanceMetadataURL = "http://169.254.169.254/metadata/instance?api-version=2017-12-01"
resourceIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s"
urlTemplate = "https://%s.monitoring.azure.com%s/metrics"
urlOverrideTemplate = "%s%s/metrics"
maxRequestBodySize = 4000000
vmInstanceMetadataURL = "http://169.254.169.254/metadata/instance?api-version=2017-12-01"
resourceIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s"
resourceIDScaleSetTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets/%s"
urlTemplate = "https://%s.monitoring.azure.com%s/metrics"
urlOverrideTemplate = "%s%s/metrics"
maxRequestBodySize = 4000000
)

var sampleConfig = `
Expand Down Expand Up @@ -200,31 +230,17 @@ func vmInstanceMetadata(c *http.Client) (string, string, error) {
return "", "", err
}
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
return "", "", fmt.Errorf("unable to fetch instance metadata: [%v] %s", resp.StatusCode, body)
}

// VirtualMachineMetadata contains information about a VM from the metadata service
type VirtualMachineMetadata struct {
Compute struct {
Location string `json:"location"`
Name string `json:"name"`
ResourceGroupName string `json:"resourceGroupName"`
SubscriptionID string `json:"subscriptionId"`
} `json:"compute"`
return "", "", fmt.Errorf("unable to fetch instance metadata: [%s] %d",
vmInstanceMetadataURL, resp.StatusCode)
}

var metadata VirtualMachineMetadata
var metadata virtualMachineMetadata
if err := json.Unmarshal(body, &metadata); err != nil {
return "", "", err
}

region := metadata.Compute.Location
resourceID := fmt.Sprintf(
resourceIDTemplate,
metadata.Compute.SubscriptionID,
metadata.Compute.ResourceGroupName,
metadata.Compute.Name,
)
resourceID := metadata.ResourceID()

return region, resourceID, nil
}
Expand Down

0 comments on commit ad877fd

Please sign in to comment.