From cc05babf205c45e73fc37d25a7a1d3e20a7e84b8 Mon Sep 17 00:00:00 2001 From: marumaru1019 <70362624+marumaru1019@users.noreply.github.com> Date: Thu, 26 Oct 2023 17:07:34 +0900 Subject: [PATCH] =?UTF-8?q?=E3=80=905=E7=AB=A0=E3=80=91Application=20Insig?= =?UTF-8?q?hts=20=E3=82=92=E8=BF=BD=E5=8A=A0=20(#83)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Changed to add workspace-based application insights * update document * Change where monitor is defined * fixed library version --- 5.internal-document-search/README.md | 2 ++ .../deploy_private_endpoint_ennabled.md | 2 ++ .../infra/core/host/appservice.bicep | 18 ++++-------- .../core/monitor/applicationinsights.bicep | 19 +++++++++++++ .../infra/core/monitor/loganalytics.bicep | 12 ++++++++ .../infra/core/monitor/monitoring.bicep | 28 +++++++++++++++++++ 5.internal-document-search/infra/main.bicep | 22 +++++++++++++-- 5.internal-document-search/src/backend/app.py | 7 +++++ .../src/backend/requirements.txt | 4 ++- 9 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 5.internal-document-search/infra/core/monitor/applicationinsights.bicep create mode 100644 5.internal-document-search/infra/core/monitor/loganalytics.bicep create mode 100644 5.internal-document-search/infra/core/monitor/monitoring.bicep diff --git a/5.internal-document-search/README.md b/5.internal-document-search/README.md index 5649516..7ba6d36 100644 --- a/5.internal-document-search/README.md +++ b/5.internal-document-search/README.md @@ -29,6 +29,8 @@ |Azure Cosmos DB|プロビジョニング済みスループット|| |Azure Form Recgonizer|S0|| |Azure Blob Storage|汎用v2|ZRS| +|Azure Application Insights||ワークスペース ベース| +|Azure Log Analytics||| #### ローカル開発環境 このデモをデプロイするためには、ローカルに以下の開発環境が必要です。 diff --git a/5.internal-document-search/deploy_private_endpoint_ennabled.md b/5.internal-document-search/deploy_private_endpoint_ennabled.md index 670e8ee..1d26a2a 100644 --- a/5.internal-document-search/deploy_private_endpoint_ennabled.md +++ b/5.internal-document-search/deploy_private_endpoint_ennabled.md @@ -46,6 +46,8 @@ IaCとして追加・更新したファイルは以下の通りです。 |Azure Cosmos DB|プロビジョニング済みスループット|| |Azure Form Recgonizer|S0|| |Azure Blob Storage|汎用v2|ZRS| +|Azure Application Insights||ワークスペース ベース| +|Azure Log Analytics||| ## デプロイ手順 閉域網でのデプロイを実行するには、以下の手順でデプロイします。 diff --git a/5.internal-document-search/infra/core/host/appservice.bicep b/5.internal-document-search/infra/core/host/appservice.bicep index 15cdbc2..def7dfb 100644 --- a/5.internal-document-search/infra/core/host/appservice.bicep +++ b/5.internal-document-search/infra/core/host/appservice.bicep @@ -39,15 +39,6 @@ param ftpsState string = 'FtpsOnly' param healthCheckPath string = '' param virtualNetworkSubnetId string -resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = { - name: applicationInsightsName - location: location - kind: 'web' - properties: { - Application_Type: 'web' - } -} - resource appService 'Microsoft.Web/sites@2022-03-01' = { name: name location: location @@ -87,9 +78,6 @@ resource appService 'Microsoft.Web/sites@2022-03-01' = { !empty(applicationInsightsName) ? { APPLICATIONINSIGHTS_CONNECTION_STRING: applicationInsights.properties.ConnectionString } : {}, !empty(keyVaultName) ? { AZURE_KEY_VAULT_ENDPOINT: keyVault.properties.vaultUri } : {} ) - dependsOn: [ - applicationInsights - ] } resource configLogs 'config' = { @@ -110,8 +98,12 @@ resource keyVault 'Microsoft.KeyVault/vaults@2023-02-01' existing = if (!(empty( name: keyVaultName } +resource applicationInsights 'Microsoft.Insights/components@2020-02-02' existing = if (!empty(applicationInsightsName)) { + name: applicationInsightsName +} + + output identityPrincipalId string = managedIdentity ? appService.identity.principalId : '' output name string = appService.name output id string = appService.id output uri string = 'https://${appService.properties.defaultHostName}' -output applicationInsightsConnectionString string = applicationInsights.properties.ConnectionString diff --git a/5.internal-document-search/infra/core/monitor/applicationinsights.bicep b/5.internal-document-search/infra/core/monitor/applicationinsights.bicep new file mode 100644 index 0000000..f939542 --- /dev/null +++ b/5.internal-document-search/infra/core/monitor/applicationinsights.bicep @@ -0,0 +1,19 @@ +param name string +param location string = resourceGroup().location +param tags object = {} +param workspaceId string + +resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = { + name: name + location: location + tags: tags + kind: 'web' + properties: { + Application_Type: 'web' + WorkspaceResourceId: workspaceId + } +} + +output connectionString string = applicationInsights.properties.ConnectionString +output instrumentationKey string = applicationInsights.properties.InstrumentationKey +output name string = applicationInsights.name diff --git a/5.internal-document-search/infra/core/monitor/loganalytics.bicep b/5.internal-document-search/infra/core/monitor/loganalytics.bicep new file mode 100644 index 0000000..6c3f548 --- /dev/null +++ b/5.internal-document-search/infra/core/monitor/loganalytics.bicep @@ -0,0 +1,12 @@ +param workspaceName string = '' +param location string = resourceGroup().location +param tags object = {} + +resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2020-03-01-preview' = { + name: workspaceName + location: location + tags: tags +} + + +output wokspaceId string = logAnalyticsWorkspace.id diff --git a/5.internal-document-search/infra/core/monitor/monitoring.bicep b/5.internal-document-search/infra/core/monitor/monitoring.bicep new file mode 100644 index 0000000..ae43a7b --- /dev/null +++ b/5.internal-document-search/infra/core/monitor/monitoring.bicep @@ -0,0 +1,28 @@ +param applicationInsightsName string +param workspaceName string +param location string = resourceGroup().location +param tags object = {} + +module locanalytics 'loganalytics.bicep' = { + name: 'loganalytics' + params: { + workspaceName: workspaceName + location: location + tags: tags + } +} + +module applicationInsights 'applicationinsights.bicep' = { + name: 'applicationinsights' + params: { + name: applicationInsightsName + location: location + tags: tags + workspaceId: locanalytics.outputs.wokspaceId + } +} + +output applicationInsightsConnectionString string = applicationInsights.outputs.connectionString +output applicationInsightsInstrumentationKey string = applicationInsights.outputs.instrumentationKey +output applicationInsightsName string = applicationInsights.outputs.name + diff --git a/5.internal-document-search/infra/main.bicep b/5.internal-document-search/infra/main.bicep index 0f5e7e7..44e2994 100644 --- a/5.internal-document-search/infra/main.bicep +++ b/5.internal-document-search/infra/main.bicep @@ -16,6 +16,9 @@ param appServicePlanName string = '' param backendServiceName string = '' param resourceGroupName string = '' +param applicationInsightsName string = '' +param workspaceName string = '' + param searchServiceName string = '' param searchServiceResourceGroupName string = '' param searchServiceResourceGroupLocation string = location @@ -67,6 +70,9 @@ param vmLoginPassword string @description('Id of the user or app to assign application roles') param principalId string = '' +@description('Use Application Insights for monitoring and performance tracing') +param useApplicationInsights bool = true + var abbrs = loadJsonContent('abbreviations.json') var resourceToken = toLower(uniqueString(subscription().id, environmentName, location)) var tags = { 'azd-env-name': environmentName } @@ -123,6 +129,18 @@ module appServicePlan 'core/host/appserviceplan.bicep' = { } } +// Monitor application with Azure Monitor +module monitoring './core/monitor/monitoring.bicep' = if (useApplicationInsights) { + name: 'monitoring' + scope: resourceGroup + params: { + workspaceName: !empty(workspaceName) ? workspaceName : '${abbrs.insightsComponents}${resourceToken}-workspace' + location: location + tags: tags + applicationInsightsName: !empty(applicationInsightsName) ? applicationInsightsName : '${abbrs.insightsComponents}${resourceToken}' + } +} + // The application frontend module backend 'core/host/appservice.bicep' = { name: 'web' @@ -136,9 +154,10 @@ module backend 'core/host/appservice.bicep' = { runtimeVersion: '3.10' scmDoBuildDuringDeployment: true managedIdentity: true - applicationInsightsName: !empty(backendServiceName) ? backendServiceName : '${abbrs.webSitesAppService}backend-${resourceToken}' + applicationInsightsName: useApplicationInsights ? monitoring.outputs.applicationInsightsName : '' virtualNetworkSubnetId: isPrivateNetworkEnabled ? appServiceSubnet.outputs.id : '' appSettings: { + APPLICATIONINSIGHTS_CONNECTION_STRING: useApplicationInsights ? monitoring.outputs.applicationInsightsConnectionString : '' AZURE_STORAGE_ACCOUNT: storage.outputs.name AZURE_STORAGE_CONTAINER: storageContainerName AZURE_OPENAI_SERVICE: openAi.outputs.name @@ -628,4 +647,3 @@ output AZURE_COSMOSDB_RESOURCE_GROUP string = resourceGroup.name output BACKEND_IDENTITY_PRINCIPAL_ID string = backend.outputs.identityPrincipalId output BACKEND_URI string = backend.outputs.uri -output APPLICATIONINSIGHTS_CONNECTION_STRING string = backend.outputs.applicationInsightsConnectionString diff --git a/5.internal-document-search/src/backend/app.py b/5.internal-document-search/src/backend/app.py index 3af1266..2ff1deb 100644 --- a/5.internal-document-search/src/backend/app.py +++ b/5.internal-document-search/src/backend/app.py @@ -14,6 +14,10 @@ from approaches.chatreadretrieveread import ChatReadRetrieveReadApproach from approaches.chatread import ChatReadApproach +from azure.monitor.opentelemetry import configure_azure_monitor +from opentelemetry.instrumentation.flask import FlaskInstrumentor + + # Replace these with your own values, either in environment variables or directly here AZURE_STORAGE_ACCOUNT = os.environ.get("AZURE_STORAGE_ACCOUNT") AZURE_STORAGE_CONTAINER = os.environ.get("AZURE_STORAGE_CONTAINER") @@ -92,7 +96,10 @@ "r": ChatReadApproach() } +configure_azure_monitor() + app = Flask(__name__) +FlaskInstrumentor().instrument_app(app) @app.route("/", defaults={"path": "index.html"}) @app.route("/") diff --git a/5.internal-document-search/src/backend/requirements.txt b/5.internal-document-search/src/backend/requirements.txt index e8acb38..5a599a4 100644 --- a/5.internal-document-search/src/backend/requirements.txt +++ b/5.internal-document-search/src/backend/requirements.txt @@ -5,4 +5,6 @@ azure-search-documents==11.4.0b6 azure-storage-blob==12.14.1 azure-cosmos==4.5.0 opencensus-ext-azure==1.1.9 -tiktoken==0.4.0 \ No newline at end of file +tiktoken==0.4.0 +azure-monitor-opentelemetry==1.0.0b15 +opentelemetry-instrumentation-flask==0.40b0 \ No newline at end of file