9

在将 Application Insights 与我的 Web 应用程序部署到 Azure 时,我遇到了先有鸡还是先有蛋的问题。在 ARM 模板中,Application Insights 模块依赖于应用程序 ID 的网站(请参阅下面的 ARM 模板中的依赖项)。另一方面,为了全面检测 Web 应用程序,我需要来自 Application Insights 模块的检测密钥。如何解决这个问题?

门户中的 Application Insights 视图

门户中的 Application Insights 模块

Web 应用的 ARM 模板

Web 应用 ARM 模板

Application Insights 的 ARM 模板

应用洞察力臂模板

4

3 回答 3

21

解决方案是将连接字符串和应用程序设置创建为网站的嵌套子资源。通过使用子资源策略,可以使 appsettings 依赖于网站应用程序的洞察力。这允许按以下顺序进行配置:

  1. 网站
  2. 应用洞察
  3. 网站配置/应用程序设置

以下两个答案很有帮助。第一个说明了如何拉动仪表键。第二个说明了如何将应用程序设置和连接字符串嵌套为网站的子资源。

如何拔出仪表钥匙

如何将应用设置嵌套为子资源

这是我的最终模板:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "webSiteName": {
      "type": "string"
    },
    "aadTenant": {
      "type": "string"
    },
    "aadAudience": {
      "type": "string"
    },
    "endpoints": {
      "type": "string",
      "defaultValue": "n/a"
    },
    "apiEndpoint": {
      "type": "string",
      "defaultValue": "n/a"
    },
    "sqlConnStrName": {
      "type": "string"
    },
    "sqlConnStrValue": {
      "type": "string"
    },
    "skuName": {
      "type": "string",
      "defaultValue": "F1",
      "allowedValues": [
        "F1",
        "D1",
        "B1",
        "B2",
        "B3",
        "S1",
        "S2",
        "S3",
        "P1",
        "P2",
        "P3",
        "P4"
      ],
      "metadata": {
        "description": "Describes plan's pricing tier and instance size. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"
      }
    },
    "skuCapacity": {
      "type": "int",
      "defaultValue": 1,
      "minValue": 1,
      "metadata": {
        "description": "Describes plan's instance count"
      }
    }
  },
  "variables": {
    "hostingPlanName": "[concat(parameters('webSiteName'), '-hostingplan')]"
  },
  "resources": [
    {
      "apiVersion": "2015-08-01",
      "name": "[variables('hostingPlanName')]",
      "type": "Microsoft.Web/serverfarms",
      "location": "[resourceGroup().location]",
      "tags": {
        "displayName": "HostingPlan"
      },
      "sku": {
        "name": "[parameters('skuName')]",
        "capacity": "[parameters('skuCapacity')]"
      },
      "properties": {
        "name": "[variables('hostingPlanName')]"
      }
    },
    {
      "apiVersion": "2015-08-01",
      "name": "[parameters('webSiteName')]",
      "type": "Microsoft.Web/sites",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[variables('hostingPlanName')]"
      ],
      "tags": {
        "[concat('hidden-related:', resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName')))]": "empty",
        "displayName": "Website"
      },
      "properties": {
        "name": "[parameters('webSiteName')]",
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
      },
      "resources": [
        {
          "apiVersion": "2015-08-01",
          "name": "appsettings",
          "type": "config",
          "dependsOn": [
            "[parameters('webSiteName')]",
            "[concat('AppInsights', parameters('webSiteName'))]"
          ],
          "properties": {
            "ida:Tenant": "[parameters('aadTenant')]",
            "ida:Audience": "[parameters('aadAudience')]",
            "endpoints": "[parameters('endpoints')]",
            "apiEndpoint": "[parameters('apiEndpoint')]",
            "applicationInsightsInstrumentationKey": "[reference(resourceId('Microsoft.Insights/components', concat('AppInsights', parameters('webSiteName'))), '2014-04-01').InstrumentationKey]"
          }
        },
        {
          "apiVersion": "2015-08-01",
          "type": "config",
          "name": "connectionstrings",
          "dependsOn": [
            "[parameters('webSiteName')]"
          ],
          "properties": {
            "[parameters('sqlConnStrName')]": {
              "value": "[parameters('sqlConnStrValue')]",
              "type": "SQLServer"
            }
          }
        },
        {
          "apiVersion": "2015-08-01",
          "name": "logs",
          "type": "config",
          "dependsOn": [
            "[parameters('webSiteName')]"
          ],
          "properties": {
            "applicationLogs": {
              "fileSystem": {
                "level": "Off"
              },
              "azureTableStorage": {
                "level": "Off",
                "sasUrl": null
              },
              "azureBlobStorage": {
                "level": "Information",
                "sasUrl": "TO DO: pass in a SAS Url",
                "retentionInDays": null
              }
            },
            "httpLogs": {
              "fileSystem": {
                "retentionInMb": 40,
                "enabled": true
              }
            },
            "failedRequestsTracing": {
              "enabled": true
            },
            "detailedErrorMessages": {
              "enabled": true
            }
          }
        }
      ]
    },
    {
      "apiVersion": "2014-04-01",
      "name": "[concat(variables('hostingPlanName'), '-', resourceGroup().name)]",
      "type": "Microsoft.Insights/autoscalesettings",
      "location": "[resourceGroup().location]",
      "tags": {
        "[concat('hidden-link:', resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName')))]": "Resource",
        "displayName": "AutoScaleSettings"
      },
      "dependsOn": [
        "[variables('hostingPlanName')]"
      ],
      "properties": {
        "profiles": [
          {
            "name": "Default",
            "capacity": {
              "minimum": 1,
              "maximum": 2,
              "default": 1
            },
            "rules": [
              {
                "metricTrigger": {
                  "metricName": "CpuPercentage",
                  "metricResourceUri": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
                  "timeGrain": "PT1M",
                  "statistic": "Average",
                  "timeWindow": "PT10M",
                  "timeAggregation": "Average",
                  "operator": "GreaterThan",
                  "threshold": 80.0
                },
                "scaleAction": {
                  "direction": "Increase",
                  "type": "ChangeCount",
                  "value": 1,
                  "cooldown": "PT10M"
                }
              },
              {
                "metricTrigger": {
                  "metricName": "CpuPercentage",
                  "metricResourceUri": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
                  "timeGrain": "PT1M",
                  "statistic": "Average",
                  "timeWindow": "PT1H",
                  "timeAggregation": "Average",
                  "operator": "LessThan",
                  "threshold": 60.0
                },
                "scaleAction": {
                  "direction": "Decrease",
                  "type": "ChangeCount",
                  "value": 1,
                  "cooldown": "PT1H"
                }
              }
            ]
          }
        ],
        "enabled": false,
        "name": "[concat(variables('hostingPlanName'), '-', resourceGroup().name)]",
        "targetResourceUri": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
      }
    },
    {
      "apiVersion": "2014-04-01",
      "name": "[concat('ServerErrors ', parameters('webSiteName'))]",
      "type": "Microsoft.Insights/alertrules",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[parameters('webSiteName')]"
      ],
      "tags": {
        "[concat('hidden-link:', resourceId('Microsoft.Web/sites', parameters('webSiteName')))]": "Resource",
        "displayName": "ServerErrorsAlertRule"
      },
      "properties": {
        "name": "[concat('ServerErrors ', parameters('webSiteName'))]",
        "description": "[concat(parameters('webSiteName'), ' has some server errors, status code 5xx.')]",
        "isEnabled": true,
        "condition": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
          "dataSource": {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
            "resourceUri": "[resourceId('Microsoft.Web/sites', parameters('webSiteName'))]",
            "metricName": "Http5xx"
          },
          "operator": "GreaterThan",
          "threshold": 5.0,
          "windowSize": "PT5M"
        },
        "action": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
          "sendToServiceOwners": true,
          "customEmails": ["you@example.com"]
        }
      }
    },
    {
      "apiVersion": "2014-04-01",
      "name": "[concat('ForbiddenRequests ', parameters('webSiteName'))]",
      "type": "Microsoft.Insights/alertrules",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[parameters('webSiteName')]"
      ],
      "tags": {
        "[concat('hidden-link:', resourceId('Microsoft.Web/sites', parameters('webSiteName')))]": "Resource",
        "displayName": "ForbiddenRequestsAlertRule"
      },
      "properties": {
        "name": "[concat('ForbiddenRequests ', parameters('webSiteName'))]",
        "description": "[concat(parameters('webSiteName'), ' has some requests that are forbidden, status code 403.')]",
        "isEnabled": true,
        "condition": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
          "dataSource": {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
            "resourceUri": "[resourceId('Microsoft.Web/sites', parameters('webSiteName'))]",
            "metricName": "Http403"
          },
          "operator": "GreaterThan",
          "threshold": 5,
          "windowSize": "PT5M"
        },
        "action": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
          "sendToServiceOwners": true,
          "customEmails": [ ]
        }
      }
    },
    {
      "apiVersion": "2014-04-01",
      "name": "[concat('CPUHigh ', variables('hostingPlanName'))]",
      "type": "Microsoft.Insights/alertrules",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[variables('hostingPlanName')]"
      ],
      "tags": {
        "[concat('hidden-link:', resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName')))]": "Resource",
        "displayName": "CPUHighAlertRule"
      },
      "properties": {
        "name": "[concat('CPUHigh ', variables('hostingPlanName'))]",
        "description": "[concat('The average CPU is high across all the instances of ', variables('hostingPlanName'))]",
        "isEnabled": false,
        "condition": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
          "dataSource": {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
            "resourceUri": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
            "metricName": "CpuPercentage"
          },
          "operator": "GreaterThan",
          "threshold": 90,
          "windowSize": "PT15M"
        },
        "action": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
          "sendToServiceOwners": true,
          "customEmails": [ ]
        }
      }
    },
    {
      "apiVersion": "2014-04-01",
      "name": "[concat('LongHttpQueue ', variables('hostingPlanName'))]",
      "type": "Microsoft.Insights/alertrules",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[variables('hostingPlanName')]"
      ],
      "tags": {
        "[concat('hidden-link:', resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName')))]": "Resource",
        "displayName": "AutoScaleSettings"
      },
      "properties": {
        "name": "[concat('LongHttpQueue ', variables('hostingPlanName'))]",
        "description": "[concat('The HTTP queue for the instances of ', variables('hostingPlanName'), ' has a large number of pending requests.')]",
        "isEnabled": false,
        "condition": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
          "dataSource": {
            "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
            "resourceUri": "[concat(resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', variables('hostingPlanName'))]",
            "metricName": "HttpQueueLength"
          },
          "operator": "GreaterThan",
          "threshold": 100.0,
          "windowSize": "PT5M"
        },
        "action": {
          "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleEmailAction",
          "sendToServiceOwners": true,
          "customEmails": [ ]
        }
      }
    },
    {
      "apiVersion": "2014-04-01",
      "name": "[concat('AppInsights', parameters('webSiteName'))]",
      "type": "Microsoft.Insights/components",
      "location": "Central US",
      "dependsOn": [
        "[parameters('webSiteName')]"
      ],
      "tags": {
        "[concat('hidden-link:', resourceId('Microsoft.Web/sites', parameters('webSiteName')))]": "Resource",
        "displayName": "AppInsightsComponent"
      },
      "properties": {
        "ApplicationId": "[parameters('webSiteName')]"
      }
    }
  ],
  "outputs": {
    "siteUri": {
      "type": "string",
      "value": "[reference(concat('Microsoft.Web/sites/', parameters('webSiteName')), '2015-08-01').hostnames[0]]"
    }
  }

}
于 2016-06-01T20:11:33.400 回答
2

替代和更新的答案 (2020/03):执行 Azure Portal UX 所做的事情(可以在 webapp 创建的最后一步通过“下载模板”查看)。

对于应用程序洞察资源,您不一定需要等待创建网站资源。根据 applicationInsights ARM 的website.Name习惯applicaitonInsights.properties.ApplicationId。由于您同时创建两者,因此您可以将名称值从输入中传递给 ApplicationInsights 和网站资源:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    “参数”: {
        “订阅号”:{
            “类型”:“字符串”
        },
        “姓名”: {
            “类型”:“字符串”
        } ,
        “地点”: {
            “类型”:“字符串”
        },
        “托管环境”:{
            “类型”:“字符串”
        },
        “托管计划名称”:{
            “类型”:“字符串”
        },
        “服务器农场资源组”:{
            “类型”:“字符串”
        },
        “永远在线”:{
            “类型”:“布尔”
        },
        “sku”:{
            “类型”:“字符串”
        },
        “sku代码”:{
            “类型”:“字符串”
        },
        “工人尺寸”:{
            “类型”:“字符串”
        },
        “workerSizeId”:{
            “类型”:“字符串”
        },
        "numberOfWorkers": {
            “类型”:“字符串”
        },
        “当前堆栈”:{
            “类型”:“字符串”
        },
        “网络框架版本”:{
            “类型”:“字符串”
        }
    },
    “资源”: [
        {
            "apiVersion": "2018-11-01",
            “名称”:“[参数('名称')]”,
            "type": "Microsoft.Web/sites",
            “位置”:“[参数('位置')]”,
            “标签”:{},
            “取决于”: [
                “microsoft.insights/components/testapp01”,
                “[concat('Microsoft.Web/serverfarms/',参数('hostingPlanName'))]”
            ],
            “特性”: {
                “名称”:“[参数('名称')]”,
                “站点配置”:{
                    “应用程序设置”:[
                        {
                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                            “值”:“[参考('microsoft.insights/components/testapp01','2015-05-01').InstrumentationKey]”
                        },
                        {
                            "名称": "APPLICATIONINSIGHTS_CONNECTION_STRING",
                            “价值”:“[参考('microsoft.insights/components/testapp01','2015-05-01').ConnectionString]”
                        },
                        {
                            "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
                            “价值”:“~2”
                        }
                    ],
                    “元数据”:[
                        {
                            "名称": "CURRENT_STACK",
                            “值”:“[参数('currentStack')]”
                        }
                    ],
                    "netFrameworkVersion": "[参数('netFrameworkVersion')]",
                    “alwaysOn”:“[参数('alwaysOn')]”
                },
                "serverFarmId": "[concat('/subscriptions/', parameters('subscriptionId'),'/resourcegroups/', parameters('serverFarmResourceGroup'), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName) '))]",
                “hostingEnvironment”:“[参数('hostingEnvironment')]”,
                “clientAffinityEnabled”:真
            }
        },
        {
            "apiVersion": "2018-11-01",
            “名称”:“[参数('hostingPlanName')]”,
            "type": "Microsoft.Web/serverfarms",
            “位置”:“[参数('位置')]”,
            “种类”: ””,
            “标签”:{},
            “取决于”: [],
            “特性”: {
                “名称”:“[参数('hostingPlanName')]”,
                “workerSize”:“[参数('workerSize')]”,
                “workerSizeId”:“[参数('workerSizeId')]”,
                “numberOfWorkers”:“[参数('numberOfWorkers')]”,
                “hostingEnvironment”:“[参数('hostingEnvironment')]”
            },
            “sku”:{
                “层”:“[参数('sku')]”,
                “名称”:“[参数('skuCode')]”
            }
        },
        {
            "apiVersion": "2015-05-01",
            “名称”:“testapp01”,
            “类型”:“microsoft.insights/components”,
            “位置”:“中央”,
            “标签”:{},
            “特性”: {
                "ApplicationId": "[parameters('name')]" ,
                “Request_Source”:“IbizaWebAppExtensionCreate”
            }
        }
    ]
}

创建顺序如下:

  1. Application Insights 资源
  2. 应用服务 webapp
于 2020-03-02T17:25:34.467 回答
0

要详细说明 Mikl X 答案,您可以让网站依赖于应用程序洞察资源并在网站的应用程序设置上设置检测密钥。

跳过不相关的部分:

    "resources": [
        {
            "type": "Microsoft.Insights/components",
            "name": "[variables('appInsightsName')]"
            ... more config
        },
        {
            "type": "Microsoft.Web/sites",
            "name": "[variables('apiName')]",
            "dependsOn": [
                "[resourceId('Microsoft.Insights/components', variables('appInsightsName'))]" <-- ensure app insights is created first
            ],
            "properties": {
                "siteConfig": {
                    "appSettings": [
                        {
                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                            "value": "[reference(variables('appInsightsName')).InstrumentationKey]" <-- use key of previously created resource
                        }
                    ]
                }
            }
        }
        ... more config
    ]
于 2020-03-23T14:42:44.867 回答