Skip to content

Commit e4f0f68

Browse files
committed
Output: Azure Monitor: Cleanup and add README
1 parent 341d467 commit e4f0f68

File tree

4 files changed

+268
-211
lines changed

4 files changed

+268
-211
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## Azure Monitor Custom Metrics Output for Telegraf
2+
3+
This plugin will send custom metrics to Azure Monitor.
4+
5+
All metrics are written as summarized values: min, max, sum, count. The Telegraf field name is appended to the metric name. All Telegraf tags are set as the metric dimensions.
6+
7+
## Azure Authentication
8+
9+
This plugin can use one of several different types of credentials to authenticate
10+
with the Azure Monitor Custom Metrics ingestion API endpoint. In the following
11+
order the plugin will attempt to authenticate.
12+
1. Managed Service Identity (MSI) token
13+
- This is the prefered authentication method.
14+
- Note: MSI is only available to ARM-based resources.
15+
2. AAD Application Tokens (Service Principals)
16+
- Primarily useful if Telegraf is writing metrics for other resources. [More information](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-application-objects).
17+
- A Service Principal or User Principal needs to be assigned the `Monitoring Contributor` or `Metric Publisher` roles.
18+
3. AAD User Tokens (User Principals)
19+
- Allows Telegraf to authenticate like a user. It is best to use this method for development.
20+
21+
## Config
22+
23+
For this output plugin to function correctly the following variables
24+
must be configured.
25+
26+
* resourceId
27+
* region
28+
29+
### region
30+
31+
The region is the Azure region that you wish to connect to.
32+
Examples include but are not limited to:
33+
* useast
34+
* centralus
35+
* westcentralus
36+
* westeurope
37+
* southeastasia
38+
39+
### resourceId
40+
41+
The resourceId used for AWS CloudWatch metrics.
42+
43+
### Configuration:
44+
45+
```
46+
# Configuration for sending aggregate metrics to Azure Monitor
47+
[[outputs.azuremonitor]]
48+
## The resource ID against which metric will be logged. If not
49+
## specified, the plugin will attempt to retrieve the resource ID
50+
## of the VM via the instance metadata service (optional if running
51+
## on an Azure VM with MSI)
52+
#resourceId = "/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.Compute/virtualMachines/<vm-name>"
53+
## Azure region to publish metrics against. Defaults to eastus.
54+
## Leave blank to automatically query the region via MSI.
55+
#region = "useast"
56+
57+
## Write HTTP timeout, formatted as a string. If not provided, will default
58+
## to 5s. 0s means no timeout (not recommended).
59+
# timeout = "5s"
60+
61+
## Whether or not to use managed service identity.
62+
#useManagedServiceIdentity = true
63+
64+
## Fill in the following values if using Active Directory Service
65+
## Principal or User Principal for authentication.
66+
## Subscription ID
67+
#azureSubscription = ""
68+
## Tenant ID
69+
#azureTenant = ""
70+
## Client ID
71+
#azureClientId = ""
72+
## Client secrete
73+
#azureClientSecret = ""
74+
```

plugins/outputs/azuremonitor/azuremetadata.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ type VirtualMachineMetadata struct {
5959
} `json:"network"`
6060
}
6161

62-
// MsiToken is the managed service identity token
63-
type MsiToken struct {
62+
// msiToken is the managed service identity token
63+
type msiToken struct {
6464
AccessToken string `json:"access_token"`
6565
RefreshToken string `json:"refresh_token"`
6666
ExpiresIn string `json:"expires_in"`
@@ -74,7 +74,7 @@ type MsiToken struct {
7474
raw string
7575
}
7676

77-
func (m *MsiToken) parseTimes() {
77+
func (m *msiToken) parseTimes() {
7878
val, err := strconv.ParseInt(m.ExpiresOn, 10, 64)
7979
if err == nil {
8080
m.expiresAt = time.Unix(val, 0)
@@ -87,23 +87,23 @@ func (m *MsiToken) parseTimes() {
8787
}
8888

8989
// ExpiresAt is the time at which the token expires
90-
func (m *MsiToken) ExpiresAt() time.Time {
90+
func (m *msiToken) ExpiresAt() time.Time {
9191
return m.expiresAt
9292
}
9393

9494
// ExpiresInDuration returns the duration until the token expires
95-
func (m *MsiToken) ExpiresInDuration() time.Duration {
95+
func (m *msiToken) ExpiresInDuration() time.Duration {
9696
expiresDuration := m.expiresAt.Sub(time.Now().UTC())
9797
return expiresDuration
9898
}
9999

100100
// NotBeforeTime returns the time at which the token becomes valid
101-
func (m *MsiToken) NotBeforeTime() time.Time {
101+
func (m *msiToken) NotBeforeTime() time.Time {
102102
return m.notBefore
103103
}
104104

105105
// GetMsiToken retrieves a managed service identity token from the specified port on the local VM
106-
func (s *AzureInstanceMetadata) GetMsiToken(clientID string, resourceID string) (*MsiToken, error) {
106+
func (s *AzureInstanceMetadata) getMsiToken(clientID string, resourceID string) (*msiToken, error) {
107107
// Acquire an MSI token. Documented at:
108108
// https://docs.microsoft.com/en-us/azure/active-directory/managed-service-identity/how-to-use-vm-token
109109
//
@@ -159,7 +159,7 @@ func (s *AzureInstanceMetadata) GetMsiToken(clientID string, resourceID string)
159159
resp.StatusCode, resp.Status, reply)
160160
}
161161

162-
var token MsiToken
162+
var token msiToken
163163
if err := json.Unmarshal(reply, &token); err != nil {
164164
return nil, err
165165
}

plugins/outputs/azuremonitor/azuremetadata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestGetTOKEN(t *testing.T) {
3030
azureMetadata := &AzureInstanceMetadata{}
3131

3232
resourceID := "https://ingestion.monitor.azure.com/"
33-
token, err := azureMetadata.GetMsiToken("", resourceID)
33+
token, err := azureMetadata.getMsiToken("", resourceID)
3434

3535
require.NoError(t, err)
3636
require.NotEmpty(t, token.AccessToken)

0 commit comments

Comments
 (0)