Skip to content

Commit

Permalink
Add test cases and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sriniketh923 committed Jun 15, 2024
1 parent d5f655a commit 8677018
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
30 changes: 27 additions & 3 deletions receiver/azuremonitorreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The following settings are required:

The following settings are optional:

- `auth` (default = service_principal): Specifies the used authentication method. Supported values are `service_principal`, `workload_identity`.
- `auth` (default = service_principal): Specifies the used authentication method. Supported values are `service_principal`, `workload_identity`, `managed_identity`, `default_credentials`.
- `resource_groups` (default = none): Filter metrics for specific resource groups, not setting a value will scrape metrics for all resources in the subscription.
- `services` (default = none): Filter metrics for specific services, not setting a value will scrape metrics for all services integrated with Azure Monitor.
- `cache_resources` (default = 86400): List of resources will be cached for the provided amount of time in seconds.
Expand All @@ -43,9 +43,13 @@ Authenticating using workload identities requires following additional settings:
- `client_id`
- `federate_token_file`

Authenticating using managed identities has the following optional settings:

- `client_id`

### Example Configurations

Using Service Principal for authentication:
Using [Service Principal](https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication?tabs=bash#service-principal-with-a-secret) for authentication:

```yaml
receivers:
Expand All @@ -65,7 +69,7 @@ receivers:
initial_delay: 1s
```
Using Azure Workload Identity for authentication:
Using [Azure Workload Identity](https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication?tabs=bash#option-2-use-workload-identity) for authentication:
```yaml
receivers:
Expand All @@ -77,6 +81,26 @@ receivers:
federated_token_file: "${env:AZURE_FEDERATED_TOKEN_FILE}"
```
Using [Managed Identity](https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication?tabs=bash#option-3-use-a-managed-identity) for authentication:
```yaml
receivers:
azuremonitor:
subscription_id: "${subscription_id}"
auth: "managed_identity"
client_id: "${env:AZURE_CLIENT_ID}"
```
Using [Environment Variables](https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication?tabs=bash#option-1-define-environment-variables) for authentication:
```yaml
receivers:
azuremonitor:
subscription_id: "${subscription_id}"
auth: "default_credentials"
```
## Metrics
Details about the metrics scraped by this receiver can be found in [Supported metrics with Azure Monitor](https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/metrics-supported). This receiver adds the prefix "azure_" to all scraped metrics.
64 changes: 64 additions & 0 deletions receiver/azuremonitorreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func azIDWorkloadFuncMock(*azidentity.WorkloadIdentityCredentialOptions) (*azide
return &azidentity.WorkloadIdentityCredential{}, nil
}

func azManagedIdentityFuncMock(*azidentity.ManagedIdentityCredentialOptions) (*azidentity.ManagedIdentityCredential, error) {
return &azidentity.ManagedIdentityCredential{}, nil
}

func azDefaultCredentialsFuncMock(*azidentity.DefaultAzureCredentialOptions) (*azidentity.DefaultAzureCredential, error) {
return &azidentity.DefaultAzureCredential{}, nil
}

func armClientFuncMock(string, azcore.TokenCredential, *arm.ClientOptions) (*armresources.Client, error) {
return &armresources.Client{}, nil
}
Expand Down Expand Up @@ -137,6 +145,62 @@ func TestAzureScraperStart(t *testing.T) {
require.IsType(t, &azidentity.WorkloadIdentityCredential{}, s.cred)
},
},
{
name: "managed_identity",
testFunc: func(t *testing.T) {
customCfg := &Config{
ControllerConfig: cfg.ControllerConfig,
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
CacheResources: 24 * 60 * 60,
CacheResourcesDefinitions: 24 * 60 * 60,
MaximumNumberOfMetricsInACall: 20,
Services: monitorServices,
Authentication: managedIdentity,
}
s := &azureScraper{
cfg: customCfg,
azIDCredentialsFunc: azIDCredentialsFuncMock,
azManagedIdentityFunc: azManagedIdentityFuncMock,
armClientFunc: armClientFuncMock,
armMonitorDefinitionsClientFunc: armMonitorDefinitionsClientFuncMock,
armMonitorMetricsClientFunc: armMonitorMetricsClientFuncMock,
}

if err := s.start(context.Background(), componenttest.NewNopHost()); err != nil {
t.Errorf("azureScraper.start() error = %v", err)
}
require.NotNil(t, s.cred)
require.IsType(t, &azidentity.ManagedIdentityCredential{}, s.cred)
},
},
{
name: "default_credentials",
testFunc: func(t *testing.T) {
customCfg := &Config{
ControllerConfig: cfg.ControllerConfig,
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
CacheResources: 24 * 60 * 60,
CacheResourcesDefinitions: 24 * 60 * 60,
MaximumNumberOfMetricsInACall: 20,
Services: monitorServices,
Authentication: defaultCredentials,
}
s := &azureScraper{
cfg: customCfg,
azIDCredentialsFunc: azIDCredentialsFuncMock,
azDefaultCredentialsFunc: azDefaultCredentialsFuncMock,
armClientFunc: armClientFuncMock,
armMonitorDefinitionsClientFunc: armMonitorDefinitionsClientFuncMock,
armMonitorMetricsClientFunc: armMonitorMetricsClientFuncMock,
}

if err := s.start(context.Background(), componenttest.NewNopHost()); err != nil {
t.Errorf("azureScraper.start() error = %v", err)
}
require.NotNil(t, s.cred)
require.IsType(t, &azidentity.DefaultAzureCredential{}, s.cred)
},
},
}
for _, tt := range tests {
t.Run(tt.name, tt.testFunc)
Expand Down

0 comments on commit 8677018

Please sign in to comment.