Skip to content

Commit

Permalink
【5章】Application Insights を追加 (#83)
Browse files Browse the repository at this point in the history
* Changed to add workspace-based application insights

* update document

* Change where monitor is defined

* fixed library version
  • Loading branch information
marumaru1019 authored Oct 26, 2023
1 parent 67e7dbf commit cc05bab
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 16 deletions.
2 changes: 2 additions & 0 deletions 5.internal-document-search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
|Azure Cosmos DB|プロビジョニング済みスループット||
|Azure Form Recgonizer|S0||
|Azure Blob Storage|汎用v2|ZRS|
|Azure Application Insights||ワークスペース ベース|
|Azure Log Analytics|||

#### ローカル開発環境
このデモをデプロイするためには、ローカルに以下の開発環境が必要です。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ IaCとして追加・更新したファイルは以下の通りです。
|Azure Cosmos DB|プロビジョニング済みスループット||
|Azure Form Recgonizer|S0||
|Azure Blob Storage|汎用v2|ZRS|
|Azure Application Insights||ワークスペース ベース|
|Azure Log Analytics|||

## デプロイ手順
閉域網でのデプロイを実行するには、以下の手順でデプロイします。
Expand Down
18 changes: 5 additions & 13 deletions 5.internal-document-search/infra/core/host/appservice.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' = {
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions 5.internal-document-search/infra/core/monitor/loganalytics.bicep
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions 5.internal-document-search/infra/core/monitor/monitoring.bicep
Original file line number Diff line number Diff line change
@@ -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

22 changes: 20 additions & 2 deletions 5.internal-document-search/infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions 5.internal-document-search/src/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -92,7 +96,10 @@
"r": ChatReadApproach()
}

configure_azure_monitor()

app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)

@app.route("/", defaults={"path": "index.html"})
@app.route("/<path:path>")
Expand Down
4 changes: 3 additions & 1 deletion 5.internal-document-search/src/backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
tiktoken==0.4.0
azure-monitor-opentelemetry==1.0.0b15
opentelemetry-instrumentation-flask==0.40b0

0 comments on commit cc05bab

Please sign in to comment.