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

[Modules] Add AppInsights Diagnostic Settings #3277

Merged
merged 1 commit into from
May 28, 2023
Merged
Show file tree
Hide file tree
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
11 changes: 0 additions & 11 deletions modules/insights/components/.test/common/dependencies.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,10 @@ param location string = resourceGroup().location
@description('Required. The name of the Managed Identity to create.')
param managedIdentityName string

@description('Required. The name of the Log Analytics Workspace to create.')
param logAnalyticsWorkspaceName string

resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
name: managedIdentityName
location: location
}

resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2021-06-01' = {
name: logAnalyticsWorkspaceName
location: location
}

@description('The principal ID of the created Managed Identity.')
output managedIdentityPrincipalId string = managedIdentity.properties.principalId

@description('The resource ID of the created Log Analytics Workspace.')
output logAnalyticsWorkspaceResourceId string = logAnalyticsWorkspace.id
20 changes: 19 additions & 1 deletion modules/insights/components/.test/common/main.test.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,20 @@ module nestedDependencies 'dependencies.bicep' = {
name: '${uniqueString(deployment().name, location)}-nestedDependencies'
params: {
managedIdentityName: 'dep-<<namePrefix>>-msi-${serviceShort}'
}
}

// Diagnostics
// ===========
module diagnosticDependencies '../../../../.shared/.templates/diagnostic.dependencies.bicep' = {
scope: resourceGroup
name: '${uniqueString(deployment().name, location)}-diagnosticDependencies'
params: {
storageAccountName: 'dep<<namePrefix>>diasa${serviceShort}01'
logAnalyticsWorkspaceName: 'dep-<<namePrefix>>-law-${serviceShort}'
eventHubNamespaceEventHubName: 'dep-<<namePrefix>>-evh-${serviceShort}'
eventHubNamespaceName: 'dep-<<namePrefix>>-evhns-${serviceShort}'
location: location
}
}

Expand All @@ -47,7 +60,12 @@ module testDeployment '../../main.bicep' = {
params: {
enableDefaultTelemetry: enableDefaultTelemetry
name: '<<namePrefix>>${serviceShort}001'
workspaceResourceId: nestedDependencies.outputs.logAnalyticsWorkspaceResourceId
workspaceResourceId: diagnosticDependencies.outputs.logAnalyticsWorkspaceResourceId
diagnosticStorageAccountId: diagnosticDependencies.outputs.storageAccountResourceId
diagnosticWorkspaceId: diagnosticDependencies.outputs.logAnalyticsWorkspaceResourceId
diagnosticEventHubAuthorizationRuleId: diagnosticDependencies.outputs.eventHubAuthorizationRuleId
diagnosticEventHubName: diagnosticDependencies.outputs.eventHubNamespaceEventHubName
diagnosticLogsRetentionInDays: 7
roleAssignments: [
{
roleDefinitionIdOrName: 'Reader'
Expand Down
29 changes: 29 additions & 0 deletions modules/insights/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
| :-- | :-- |
| `Microsoft.Authorization/roleAssignments` | [2022-04-01](https://learn.microsoft.com/en-us/azure/templates/Microsoft.Authorization/2022-04-01/roleAssignments) |
| `Microsoft.Insights/components` | [2020-02-02](https://learn.microsoft.com/en-us/azure/templates/Microsoft.Insights/2020-02-02/components) |
| `Microsoft.Insights/diagnosticSettings` | [2021-05-01-preview](https://learn.microsoft.com/en-us/azure/templates/Microsoft.Insights/2021-05-01-preview/diagnosticSettings) |

## Parameters

Expand All @@ -29,6 +30,14 @@
| Parameter Name | Type | Default Value | Allowed Values | Description |
| :-- | :-- | :-- | :-- | :-- |
| `applicationType` | string | `'web'` | `[other, web]` | Application type. |
| `diagnosticEventHubAuthorizationRuleId` | string | `''` | | Resource ID of the diagnostic event hub authorization rule for the Event Hubs namespace in which the event hub should be created or streamed to. |
| `diagnosticEventHubName` | string | `''` | | Name of the diagnostic event hub within the namespace to which logs are streamed. Without this, an event hub is created for each log category. |
| `diagnosticLogCategoriesToEnable` | array | `[allLogs]` | `[allLogs, AppAvailabilityResults, AppBrowserTimings, AppDependencies, AppEvents, AppExceptions, AppMetrics, AppPageViews, AppPerformanceCounters, AppRequests, AppSystemEvents, AppTraces]` | The name of logs that will be streamed. "allLogs" includes all possible logs for the resource. |
| `diagnosticLogsRetentionInDays` | int | `365` | | Specifies the number of days that logs will be kept for; a value of 0 will retain data indefinitely. |
| `diagnosticMetricsToEnable` | array | `[AllMetrics]` | `[AllMetrics]` | The name of metrics that will be streamed. |
| `diagnosticSettingsName` | string | `''` | | The name of the diagnostic setting, if deployed. If left empty, it defaults to "<resourceName>-diagnosticSettings". |
| `diagnosticStorageAccountId` | string | `''` | | Resource ID of the diagnostic storage account. |
| `diagnosticWorkspaceId` | string | `''` | | Resource ID of the diagnostic log analytics workspace. |
| `enableDefaultTelemetry` | bool | `True` | | Enable telemetry via a Globally Unique Identifier (GUID). |
| `kind` | string | `''` | | The kind of application that this component refers to, used to customize UI. This value is a freeform string, values should typically be one of the following: web, ios, other, store, java, phone. |
| `location` | string | `[resourceGroup().location]` | | Location for all Resources. |
Expand Down Expand Up @@ -176,6 +185,11 @@ module components './insights/components/main.bicep' = {
name: '<<namePrefix>>iccom001'
workspaceResourceId: '<workspaceResourceId>'
// Non-required parameters
diagnosticEventHubAuthorizationRuleId: '<diagnosticEventHubAuthorizationRuleId>'
diagnosticEventHubName: '<diagnosticEventHubName>'
diagnosticLogsRetentionInDays: 7
diagnosticStorageAccountId: '<diagnosticStorageAccountId>'
diagnosticWorkspaceId: '<diagnosticWorkspaceId>'
enableDefaultTelemetry: '<enableDefaultTelemetry>'
roleAssignments: [
{
Expand Down Expand Up @@ -214,6 +228,21 @@ module components './insights/components/main.bicep' = {
"value": "<workspaceResourceId>"
},
// Non-required parameters
"diagnosticEventHubAuthorizationRuleId": {
"value": "<diagnosticEventHubAuthorizationRuleId>"
},
"diagnosticEventHubName": {
"value": "<diagnosticEventHubName>"
},
"diagnosticLogsRetentionInDays": {
"value": 7
},
"diagnosticStorageAccountId": {
"value": "<diagnosticStorageAccountId>"
},
"diagnosticWorkspaceId": {
"value": "<diagnosticWorkspaceId>"
},
"enableDefaultTelemetry": {
"value": "<enableDefaultTelemetry>"
},
Expand Down
89 changes: 89 additions & 0 deletions modules/insights/components/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,82 @@ param tags object = {}
@description('Optional. Enable telemetry via a Globally Unique Identifier (GUID).')
param enableDefaultTelemetry bool = true

@description('Optional. Specifies the number of days that logs will be kept for; a value of 0 will retain data indefinitely.')
@minValue(0)
@maxValue(365)
param diagnosticLogsRetentionInDays int = 365

@description('Optional. Resource ID of the diagnostic storage account.')
param diagnosticStorageAccountId string = ''

@description('Optional. Resource ID of the diagnostic log analytics workspace.')
param diagnosticWorkspaceId string = ''

@description('Optional. Resource ID of the diagnostic event hub authorization rule for the Event Hubs namespace in which the event hub should be created or streamed to.')
param diagnosticEventHubAuthorizationRuleId string = ''

@description('Optional. Name of the diagnostic event hub within the namespace to which logs are streamed. Without this, an event hub is created for each log category.')
param diagnosticEventHubName string = ''

@description('Optional. The name of logs that will be streamed. "allLogs" includes all possible logs for the resource.')
@allowed([
'allLogs'
'AppAvailabilityResults'
'AppBrowserTimings'
'AppEvents'
'AppMetrics'
'AppDependencies'
'AppExceptions'
'AppPageViews'
'AppPerformanceCounters'
'AppRequests'
'AppSystemEvents'
'AppTraces'
])
param diagnosticLogCategoriesToEnable array = [
'allLogs'
]

@description('Optional. The name of metrics that will be streamed.')
@allowed([
'AllMetrics'
])
param diagnosticMetricsToEnable array = [
'AllMetrics'
]

@description('Optional. The name of the diagnostic setting, if deployed. If left empty, it defaults to "<resourceName>-diagnosticSettings".')
param diagnosticSettingsName string = ''

var diagnosticsLogsSpecified = [for category in filter(diagnosticLogCategoriesToEnable, item => item != 'allLogs'): {
category: category
enabled: true
retentionPolicy: {
enabled: true
days: diagnosticLogsRetentionInDays
}
}]

var diagnosticsLogs = contains(diagnosticLogCategoriesToEnable, 'allLogs') ? [
{
categoryGroup: 'allLogs'
enabled: true
retentionPolicy: {
enabled: true
days: diagnosticLogsRetentionInDays
}
}
] : diagnosticsLogsSpecified

var diagnosticsMetrics = [for metric in diagnosticMetricsToEnable: {
category: metric
timeGrain: null
enabled: true
retentionPolicy: {
enabled: true
days: diagnosticLogsRetentionInDays
}
}]
resource defaultTelemetry 'Microsoft.Resources/deployments@2021-04-01' = if (enableDefaultTelemetry) {
name: 'pid-47ed15a6-730a-4827-bcb4-0fd963ffbd82-${uniqueString(deployment().name, location)}'
properties: {
Expand Down Expand Up @@ -99,6 +175,19 @@ module appInsights_roleAssignments '.bicep/nested_roleAssignments.bicep' = [for
}
}]


resource appInsights_diagnosticSettings 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = if ((!empty(diagnosticStorageAccountId)) || (!empty(diagnosticWorkspaceId)) || (!empty(diagnosticEventHubAuthorizationRuleId)) || (!empty(diagnosticEventHubName))) {
name: !empty(diagnosticSettingsName) ? diagnosticSettingsName : '${name}-diagnosticSettings'
properties: {
storageAccountId: !empty(diagnosticStorageAccountId) ? diagnosticStorageAccountId : null
workspaceId: !empty(diagnosticWorkspaceId) ? diagnosticWorkspaceId : null
eventHubAuthorizationRuleId: !empty(diagnosticEventHubAuthorizationRuleId) ? diagnosticEventHubAuthorizationRuleId : null
eventHubName: !empty(diagnosticEventHubName) ? diagnosticEventHubName : null
metrics: diagnosticsMetrics
logs: diagnosticsLogs
}
scope: appInsights
}
@description('The name of the application insights component.')
output name string = appInsights.name

Expand Down