Skip to content

Commit

Permalink
Refactor to use AggregatingOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnaraasen committed May 7, 2018
1 parent 93a579d commit 40c37aa
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 342 deletions.
27 changes: 5 additions & 22 deletions plugins/outputs/azuremonitor/azuremetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ import (
"net/url"
"strconv"
"time"

"github.com/prometheus/common/log"
)

// AzureInstanceMetadata is the proxy for accessing the instance metadata service on an Azure VM
type AzureInstanceMetadata struct {
}

// VirtualMachineMetadata contains information about a VM from the metadata service
type VirtualMachineMetadata struct {
Raw string
Expand Down Expand Up @@ -103,7 +97,7 @@ func (m *msiToken) NotBeforeTime() time.Time {
}

// GetMsiToken retrieves a managed service identity token from the specified port on the local VM
func (s *AzureInstanceMetadata) getMsiToken(clientID string, resourceID string) (*msiToken, error) {
func (a *AzureMonitor) getMsiToken(clientID string, resourceID string) (*msiToken, error) {
// Acquire an MSI token. Documented at:
// https://docs.microsoft.com/en-us/azure/active-directory/managed-service-identity/how-to-use-vm-token
//
Expand Down Expand Up @@ -137,16 +131,10 @@ func (s *AzureInstanceMetadata) getMsiToken(clientID string, resourceID string)
}
req.Header.Add("Metadata", "true")

// Create the HTTP client and call the token service
client := http.Client{
Timeout: 15 * time.Second,
}
resp, err := client.Do(req)
resp, err := a.client.Do(req)
if err != nil {
return nil, err
}

// Complete reading the body
defer resp.Body.Close()

reply, err := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -174,22 +162,17 @@ const (
)

// GetInstanceMetadata retrieves metadata about the current Azure VM
func (s *AzureInstanceMetadata) GetInstanceMetadata() (*VirtualMachineMetadata, error) {
func (a *AzureMonitor) GetInstanceMetadata() (*VirtualMachineMetadata, error) {
req, err := http.NewRequest("GET", vmInstanceMetadataURL, nil)
if err != nil {
log.Errorf("Error creating HTTP request")
return nil, err
return nil, fmt.Errorf("Error creating HTTP request")
}
req.Header.Set("Metadata", "true")
client := http.Client{
Timeout: 15 * time.Second,
}

resp, err := client.Do(req)
resp, err := a.client.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()

reply, err := ioutil.ReadAll(resp.Body)
Expand Down
4 changes: 2 additions & 2 deletions plugins/outputs/azuremonitor/azuremetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestGetMetadata(t *testing.T) {
azureMetadata := &AzureInstanceMetadata{}
azureMetadata := &AzureMonitor{}
metadata, err := azureMetadata.GetInstanceMetadata()

require.NoError(t, err)
Expand All @@ -27,7 +27,7 @@ func TestGetMetadata(t *testing.T) {
}

func TestGetTOKEN(t *testing.T) {
azureMetadata := &AzureInstanceMetadata{}
azureMetadata := &AzureMonitor{}

resourceID := "https://ingestion.monitor.azure.com/"
token, err := azureMetadata.getMsiToken("", resourceID)
Expand Down
Loading

0 comments on commit 40c37aa

Please sign in to comment.