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

Fix resource id in virtual machine scale sets with azure_monitor output #5821

Merged
merged 3 commits into from
May 20, 2019
Merged
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
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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if m.Compute.VMScaleSetName != ""

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