PowerShell을 사용하여 Application Insights 리소스 관리

참고 항목

Azure Az PowerShell 모듈을 사용하여 Azure와 상호 작용하는 것이 좋습니다. 시작하려면 Azure PowerShell 설치를 참조하세요. Az PowerShell 모듈로 마이그레이션하는 방법에 대한 자세한 내용은 Azure PowerShell을 AzureRM에서 Azure로 마이그레이션을 참조하세요.

이 문서에서는 Azure Resource Manager를 사용하여 Application Insights 리소스의 생성 및 업데이트를 자동화하는 방법을 보여줍니다. 예를 들어 빌드 프로세스의 일부로 이 작업을 수행할 수 있습니다. 기본 Application Insights 리소스와 함께 가용성 웹 테스트를 만들고, 경고를 설정하고, 가격 책정 계층을 설정하고, 기타 Azure 리소스를 만들 수 있습니다.

이러한 리소스를 만드는 데 핵심 사항은 Resource Manager용 JSON 템플릿입니다. 기본 절차는 다음과 같습니다.

  • 기존 리소스의 JSON 정의를 다운로드합니다.
  • 이름과 같은 특정 값을 매개 변수화합니다.
  • 새 리소스를 만들 때마다 템플릿을 실행합니다.

여러 리소스를 함께 패키지하여 모든 리소스를 한곳에서 만들 수 있습니다. 예를 들어, 연속 내보내기를 위해 가용성 테스트, 경고 및 스토리지를 사용하여 앱 모니터를 만들 수 있습니다. 일부 매개 변수화에 있는 약간의 미묘한 사항은 여기서 설명합니다.

일 회 설정

이전에 Azure 구독에서 PowerShell을 사용하지 않은 경우 스크립트를 실행하려는 컴퓨터에 Azure PowerShell 모듈을 설치합니다.

  1. Microsoft 웹 플랫폼 설치 관리자(v5 이상)를 설치합니다.
  2. 이 템플릿을 사용하여 Azure PowerShell을 설치합니다.

ARM 템플릿(Azure Resource Manager 템플릿)을 사용하는 것 외에도 다양한 Application Insights PowerShell cmdlets 집합이 있습니다. 이러한 cmdlet을 사용하면 Application Insights 리소스를 프로그래밍 방식으로 쉽게 구성할 수 있습니다. cmdlet으로 설정할 수 있는 기능을 사용하여 다음을 수행할 수 있습니다.

  • Application Insights 리소스 생성 및 삭제.
  • Application Insights 리소스 및 해당 속성의 목록 가져오기.
  • 연속 내보내기 생성 및 관리.
  • 애플리케이션 키 생성 및 관리.
  • 일일 상한 설정.
  • 가격 책정 계획을 설정합니다.

PowerShell cmdlet을 사용하여 Application Insights 리소스 만들기

New-AzApplicationInsights cmdlet을 사용하여 Azure 미국 동부 데이터 센터에 새 Application Insights 리소스를 만드는 방법은 다음과 같습니다.

New-AzApplicationInsights -ResourceGroupName <resource group> -Name <resource name> -location eastus

ARM 템플릿을 사용하여 Application Insights 리소스 만들기

ARM 템플릿을 사용하여 새 Application Insights 리소스를 만드는 방법은 다음과 같습니다.

ARM 템플릿 만들기

새 .json 파일을 만듭니다. 이 예제에서는 template1.json이라고 하겠습니다. 아래 내용을 이 파일에 복사합니다.

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "appName": {
                "type": "string",
                "metadata": {
                    "description": "Enter the name of your Application Insights resource."
                }
            },
            "appType": {
                "type": "string",
                "defaultValue": "web",
                "allowedValues": [
                    "web",
                    "java",
                    "other"
                ],
                "metadata": {
                    "description": "Enter the type of the monitored application."
                }
            },
            "appLocation": {
                "type": "string",
                "defaultValue": "eastus",
                "metadata": {
                    "description": "Enter the location of your Application Insights resource."
                }
            },
            "retentionInDays": {
                "type": "int",
                "defaultValue": 90,
                "allowedValues": [
                    30,
                    60,
                    90,
                    120,
                    180,
                    270,
                    365,
                    550,
                    730
                ],
                "metadata": {
                    "description": "Data retention in days"
                }
            },
            "ImmediatePurgeDataOn30Days": {
                "type": "bool",
                "defaultValue": false,
                "metadata": {
                    "description": "If set to true when changing retention to 30 days, older data will be immediately deleted. Use this with extreme caution. This only applies when retention is being set to 30 days."
                }
            },
            "priceCode": {
                "type": "int",
                "defaultValue": 1,
                "allowedValues": [
                    1,
                    2
                ],
                "metadata": {
                    "description": "Pricing plan: 1 = Per GB (or legacy Basic plan), 2 = Per Node (legacy Enterprise plan)"
                }
            },
            "dailyQuota": {
                "type": "int",
                "defaultValue": 100,
                "minValue": 1,
                "metadata": {
                    "description": "Enter daily quota in GB."
                }
            },
            "dailyQuotaResetTime": {
                "type": "int",
                "defaultValue": 0,
                "metadata": {
                    "description": "Enter daily quota reset hour in UTC (0 to 23). Values outside the range will get a random reset hour."
                }
            },
            "warningThreshold": {
                "type": "int",
                "defaultValue": 90,
                "minValue": 1,
                "maxValue": 100,
                "metadata": {
                    "description": "Enter the % value of daily quota after which warning mail to be sent. "
                }
            }
        },
        "variables": {
            "priceArray": [
                "Basic",
                "Application Insights Enterprise"
            ],
            "pricePlan": "[take(variables('priceArray'),parameters('priceCode'))]",
            "billingplan": "[concat(parameters('appName'),'/', variables('pricePlan')[0])]"
        },
        "resources": [
            {
                "type": "microsoft.insights/components",
                "kind": "[parameters('appType')]",
                "name": "[parameters('appName')]",
                "apiVersion": "2014-04-01",
                "location": "[parameters('appLocation')]",
                "tags": {},
                "properties": {
                    "ApplicationId": "[parameters('appName')]",
                    "retentionInDays": "[parameters('retentionInDays')]",
                    "ImmediatePurgeDataOn30Days": "[parameters('ImmediatePurgeDataOn30Days')]"
                },
                "dependsOn": []
            },
            {
                "name": "[variables('billingplan')]",
                "type": "microsoft.insights/components/CurrentBillingFeatures",
                "location": "[parameters('appLocation')]",
                "apiVersion": "2015-05-01",
                "dependsOn": [
                    "[resourceId('microsoft.insights/components', parameters('appName'))]"
                ],
                "properties": {
                    "CurrentBillingFeatures": "[variables('pricePlan')]",
                    "DataVolumeCap": {
                        "Cap": "[parameters('dailyQuota')]",
                        "WarningThreshold": "[parameters('warningThreshold')]",
                        "ResetTime": "[parameters('dailyQuotaResetTime')]"
                    }
                }
            }
        ]
    }

ARM 템플릿을 사용하여 새로운 Application Insights 리소스 만들기

  1. PowerShell에서 $Connect-AzAccount를 사용하여 Azure에 로그인합니다.

  2. Set-AzContext "<subscription ID>"를 사용하여 컨텍스트를 구독으로 설정합니다.

  3. 새 배포를 실행하여 새 Application Insights 리소스 만들기:

        New-AzResourceGroupDeployment -ResourceGroupName Fabrikam `
               -TemplateFile .\template1.json `
               -appName myNewApp
    
    
    • -ResourceGroupName은 새 리소스를 만들려는 그룹입니다.
    • -TemplateFile은 사용자 지정 매개 변수 앞에 있어야 합니다.
    • -appName은 만들려는 리소스의 이름입니다.

다른 매개 변수를 추가할 수 있습니다. 템플릿의 매개 변수 섹션에서 해당 설명을 찾을 수 있습니다.

계측 키 가져오기

애플리케이션 리소스를 만든 후 계측 키가 필요할 수 있습니다.

  1. $Connect-AzAccount를 사용하여 Azure에 로그인합니다.
  2. Set-AzContext "<subscription ID>"를 사용하여 컨텍스트를 구독으로 설정합니다.
  3. 그런 다음, 다음을 사용합니다.
    1. $resource = Get-AzResource -Name "<resource name>" -ResourceType "Microsoft.Insights/components"
    2. $details = Get-AzResource -ResourceId $resource.ResourceId
    3. $details.Properties.InstrumentationKey

Application Insights 리소스의 여러 다른 속성 목록을 보려면 다음을 사용합니다.

Get-AzApplicationInsights -ResourceGroupName Fabrikam -Name FabrikamProd | Format-List

cmdlet을 통해 추가 속성을 사용할 수 있습니다.

  • Set-AzApplicationInsightsDailyCap
  • Set-AzApplicationInsightsPricingPlan
  • Get-AzApplicationInsightsApiKey
  • Get-AzApplicationInsightsContinuousExport

이러한 cmdlet의 매개 변수에 대한 자세한 설명서를 참조하세요.

참고 항목

2025년 3월 31일에 계측 키 수집에 대한 지원이 종료됩니다. 계측 키 수집은 계속 작동하지만 더 이상 기능에 대한 업데이트 또는 지원을 제공하지 않습니다. 연결 문자열로 전환하여 새로운 기능을 활용합니다.

데이터 보존 기간 설정

다음 세 가지 방법을 사용하여 Application Insights 리소스의 데이터 보존을 프로그래밍 방식으로 설정할 수 있습니다.

PowerShell 명령을 사용하여 데이터 보존 설정

다음은 Application Insights 리소스에 대한 데이터 보존 기간을 설정하는 간단한 PowerShell 명령 집합입니다.

$Resource = Get-AzResource -ResourceType Microsoft.Insights/components -ResourceGroupName MyResourceGroupName -ResourceName MyResourceName
$Resource.Properties.RetentionInDays = 365
$Resource | Set-AzResource -Force

REST를 사용하여 데이터 보존 설정

Application Insights 리소스의 현재 데이터 보존 기간을 확인하려면 OSS 도구 ARMClient를 사용하면 됩니다. David Ebbo 및 Daniel Bowbyes가 작성한 문서에서 ARMClient에 대해 자세히 알아보세요. ARMClient를 사용하여 현재 보존을 확인하는 예는 다음과 같습니다.

armclient GET /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName?api-version=2018-05-01-preview

보존을 설정하는 명령은 PUT과 유사합니다.

armclient PUT /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName?api-version=2018-05-01-preview "{location: 'eastus', properties: {'retentionInDays': 365}}"

선행 템플릿을 사용하여 데이터 보존 기간을 365일로 설정하려면 다음을 실행합니다.

New-AzResourceGroupDeployment -ResourceGroupName "<resource group>" `
       -TemplateFile .\template1.json `
       -retentionInDays 365 `
       -appName myApp

PowerShell 스크립트를 사용하여 데이터 보존 설정

다음 스크립트를 사용하여 보존 기간을 변경할 수도 있습니다. 이 스크립트를 복사하여 Set-ApplicationInsightsRetention.ps1로 저장합니다.

Param(
    [Parameter(Mandatory = $True)]
    [string]$SubscriptionId,

    [Parameter(Mandatory = $True)]
    [string]$ResourceGroupName,

    [Parameter(Mandatory = $True)]
    [string]$Name,

    [Parameter(Mandatory = $True)]
    [string]$RetentionInDays
)
$ErrorActionPreference = 'Stop'
if (-not (Get-Module Az.Accounts)) {
    Import-Module Az.Accounts
}
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
if (-not $azProfile.Accounts.Count) {
    Write-Error "Ensure you have logged in before calling this function."    
}
$currentAzureContext = Get-AzContext
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azProfile)
$token = $profileClient.AcquireAccessToken($currentAzureContext.Tenant.TenantId)
$UserToken = $token.AccessToken
$RequestUri = "https://management.azure.com/subscriptions/$($SubscriptionId)/resourceGroups/$($ResourceGroupName)/providers/Microsoft.Insights/components/$($Name)?api-version=2015-05-01"
$Headers = @{
    "Authorization"         = "Bearer $UserToken"
    "x-ms-client-tenant-id" = $currentAzureContext.Tenant.TenantId
}
## Get Component object via ARM
$GetResponse = Invoke-RestMethod -Method "GET" -Uri $RequestUri -Headers $Headers 

## Update RetentionInDays property
if($($GetResponse.properties | Get-Member "RetentionInDays"))
{
    $GetResponse.properties.RetentionInDays = $RetentionInDays
}
else
{
    $GetResponse.properties | Add-Member -Type NoteProperty -Name "RetentionInDays" -Value $RetentionInDays
}
## Upsert Component object via ARM
$PutResponse = Invoke-RestMethod -Method "PUT" -Uri "$($RequestUri)" -Headers $Headers -Body $($GetResponse | ConvertTo-Json) -ContentType "application/json"
$PutResponse

그런 다음, 이 스크립트를 다음과 같이 사용할 수 있습니다.

Set-ApplicationInsightsRetention `
        [-SubscriptionId] <String> `
        [-ResourceGroupName] <String> `
        [-Name] <String> `
        [-RetentionInDays <Int>]

일일 상한 설정

일일 상한 속성을 가져오려면 Set-AzApplicationInsightsPricingPlan cmdlet을 사용합니다.

Set-AzApplicationInsightsDailyCap -ResourceGroupName <resource group> -Name <resource name> | Format-List

일일 상한 속성을 설정할 때도 동일한 cmdlet을 사용합니다. 예를 들어 상한을 하루 300GB로 설정하려면 다음을 수행합니다.

Set-AzApplicationInsightsDailyCap -ResourceGroupName <resource group> -Name <resource name> -DailyCapGB 300

ARMClient를 사용하여 일일 상한 매개 변수를 가져오고 설정할 수도 있습니다. 현재 값을 가져오려면 다음을 사용합니다.

armclient GET /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName/CurrentBillingFeatures?api-version=2018-05-01-preview

일일 상한 초기화 시간 설정

일일 상한 초기화 시간을 설정하려면 ARMClient를 사용하면 됩니다. ARMClient를 사용하여 초기화 시간을 새 시간으로 설정하는 예제는 다음과 같습니다. 이 예제에서는 12:00 UTC가 표시됩니다.

armclient PUT /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName/CurrentBillingFeatures?api-version=2018-05-01-preview "{'CurrentBillingFeatures':['Basic'],'DataVolumeCap':{'Cap':100,'WarningThreshold':80,'ResetTime':12}}"

요금제 설정

현재 요금제를 가져오려면 Set-AzApplicationInsightsPricingPlan cmdlet을 사용합니다.

Set-AzApplicationInsightsPricingPlan -ResourceGroupName <resource group> -Name <resource name> | Format-List

요금제를 설정하려면 동일한 cmdlet에 -PricingPlan을 지정합니다.

Set-AzApplicationInsightsPricingPlan -ResourceGroupName <resource group> -Name <resource name> -PricingPlan Basic

위의 ARM 템플릿을 사용하여 기존 Application Insights 리소스에 대한 요금제를 설정할 수도 있습니다. 이 경우에는 “microsoft Insights/components” 리소스와 청구 리소스의 dependsOn 노드를 생략합니다. 예를 들어 GB당 요금제(이전 명칭: 기본 요금제)로 설정하려면 다음을 실행합니다.

        New-AzResourceGroupDeployment -ResourceGroupName "<resource group>" `
               -TemplateFile .\template1.json `
               -priceCode 1 `
               -appName myApp

priceCode는 다음으로 정의됩니다.

priceCode 계획
1 GB당(이전 명칭: 기본 요금제)
2 노드당(이전 명칭: 엔터프라이즈 요금제)

마지막으로, ARMClient를 사용하여 요금제와 일일 상한 매개 변수를 가져오고 설정할 수 있습니다. 현재 값을 가져오려면 다음을 사용합니다.

armclient GET /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName/CurrentBillingFeatures?api-version=2018-05-01-preview

다음을 사용하여 이러한 모든 매개 변수를 설정할 수 있습니다.

armclient PUT /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName/CurrentBillingFeatures?api-version=2018-05-01-preview
"{'CurrentBillingFeatures':['Basic'],'DataVolumeCap':{'Cap':200,'ResetTime':12,'StopSendNotificationWhenHitCap':true,'WarningThreshold':90,'StopSendNotificationWhenHitThreshold':true}}"

이 코드는 일일 한도를 하루 200GB로 설정하고, 일일 상한 초기화 시간을 12:00 UTC로 구성하고, 상한에 도달하여 경고 수준이 충족되면 메일을 전송하고, 경고 임계값을 상한의 90%로 설정합니다.

메트릭 경고 추가

메트릭 경고 만들기를 자동화하려면 메트릭 경고 템플릿 문서를 참조하세요.

가용성 테스트 추가

가용성 테스트를 자동화하려면 메트릭 경고 템플릿 문서를 참조하세요.

리소스 추가

다양한 종류의 다른 리소스의 생성을 자동화하려면 수동으로 예제를 만든 다음 Azure Resource Manager에서 해당 코드를 복사하고 매개 변수화합니다.

  1. Azure 리소스 관리자를 엽니다. subscriptions/resourceGroups/<your resource group>/providers/Microsoft.Insights/components를 통해 애플리케이션 리소스로 이동합니다.

    Screenshot that shows navigation in Azure Resource Explorer.

    구성 요소는 애플리케이션을 표시하기 위한 기본 Application Insights 리소스입니다. 연결된 경고 규칙 및 가용성 웹 테스트에 대한 별도의 리소스가 있습니다.

  2. 구성 요소의 JSON을 template1.json의 적절한 위치에 복사합니다.

  3. 다음 속성을 삭제합니다.

    • id
    • InstrumentationKey
    • CreationDate
    • TenantId
  4. webtestsalertrules 섹션을 열고 개별 항목에 대한 JSON을 템플릿에 복사합니다. webtests 또는 alertrules 노드에서 복사하지 마세요. 그 아래에 있는 항목으로 이동합니다.

    각 웹 테스트에는 연결된 경고 규칙이 있으므로 둘 다 복사해야 합니다.

  5. 각 리소스에 다음 줄을 삽입합니다.

    "apiVersion": "2015-05-01",

템플릿 매개 변수화

이제 특정 이름을 매개 변수로 대체해야 합니다. 템플릿을 매개 변수화하려면 도우미 함수 집합을 사용하여 식을 작성합니다.

문자열의 일부만 매개 변수화할 수 없으므로 concat()을 사용하여 문자열을 빌드합니다.

다음은 만들 수 있는 대체 예제입니다. 각 대체가 여러 번 발생합니다. 템플릿에서 다른 사항이 필요할 수 있습니다. 이러한 예제에서는 템플릿의 위쪽에서 정의한 매개 변수 및 변수를 사용합니다.

찾기 Replace with
"hidden-link:/subscriptions/.../../components/MyAppName" "[concat('hidden-link:',
resourceId('microsoft.insights/components',
parameters('appName')))]"
"/subscriptions/.../../alertrules/myAlertName-myAppName-subsId", "[resourceId('Microsoft.Insights/alertrules', variables('alertRuleName'))]",
"/subscriptions/.../../webtests/myTestName-myAppName", "[resourceId('Microsoft.Insights/webtests', parameters('webTestName'))]",
"myWebTest-myAppName" "[variables(testName)]"'
"myTestName-myAppName-subsId" "[variables('alertRuleName')]"
"myAppName" "[parameters('appName')]"
"myappname" (소문자) "[toLower(parameters('appName'))]"
"<WebTest Name=\"myWebTest\" ...
Url=\"http://fabrikam.com/home\" ...>"
[concat('<WebTest Name=\"',
parameters('webTestName'),
'\" ... Url=\"', parameters('Url'),
'\"...>')]"

리소스 간의 종속성 설정

Azure에서는 엄격한 순서로 리소스를 설정해야 합니다. 다음 설정 시작 전에 하나의 설정이 완료되게 하려면 종속성 줄을 추가합니다.

  • 가용성 테스트 리소스:

    "dependsOn": ["[resourceId('Microsoft.Insights/components', parameters('appName'))]"],

  • 가용성 테스트에 대한 경고 리소스:

    "dependsOn": ["[resourceId('Microsoft.Insights/webtests', variables('testName'))]"],

다음 단계

다음과 같은 다른 자동화 문서를 참조하세요.