Skip to content

Commit

Permalink
Application insights for node maybe working
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Johnston committed Oct 8, 2023
1 parent 7470405 commit 70afc35
Show file tree
Hide file tree
Showing 8 changed files with 1,254 additions and 152 deletions.
1,351 changes: 1,215 additions & 136 deletions blog-app/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions blog-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@angular/platform-browser-dynamic": "^15.0.4",
"@angular/platform-server": "^15.0.4",
"@angular/router": "^15.0.4",
"@azure/monitor-opentelemetry": "^1.0.0",
"@brokerloop/ttlcache": "^3.2.3",
"@contentful/rich-text-html-renderer": "^15.13.1",
"@microsoft/applicationinsights-web": "^2.8.6",
Expand Down
20 changes: 20 additions & 0 deletions blog-app/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { existsSync } from 'fs';
import { join } from 'path';

import { AppServerModule } from './src/main.server';
import { AzureMonitorOpenTelemetryOptions, useAzureMonitor } from '@azure/monitor-opentelemetry';
const { Resource } = require("@opentelemetry/resources");
const { SemanticResourceAttributes } = require("@opentelemetry/semantic-conventions");


const domino = require('domino');
const fs = require('fs');
Expand All @@ -21,6 +25,22 @@ const version = fs
.readFileSync(path.join(join(process.cwd(), 'dist/blog-app/browser/assets'), 'version.txt'))
.toString().trim();

const customResource = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: "NodeJS",
[SemanticResourceAttributes.SERVICE_INSTANCE_ID]: os.hostname(),
});

const options: AzureMonitorOpenTelemetryOptions = {
resource: customResource
};

if (process.env['APPLICATIONINSIGHTS_CONNECTION_STRING']) {
useAzureMonitor(options);
console.log('Azure monitor configured');
} else {
console.log('Application insights not configured.');
};

global['window'] = domino.createWindow(template, 'SERVER');
global['document'] = window.document;

Expand Down
4 changes: 2 additions & 2 deletions cicd/Ansible-Playbook-Web-Deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
copy:
src: ../blog-app/dist
dest: /var/www/joelj.ca-{{ slot }}

- name: Start PM2 process
shell:
chdir: /var/www/joelj.ca-{{ slot }}
cmd: "(pm2 delete joeljca{{ slot }} || true) && PORT={{ slot_port }} pm2 start /var/www/joelj.ca-{{ slot }}/dist/blog-app/server/main.js --name joeljca{{ slot }} && pm2 save"
cmd: "(pm2 delete joeljca{{ slot }} || true) && APPLICATIONINSIGHTS_CONNECTION_STRING='{{ node_insights_connection_string }}' PORT={{ slot_port }} pm2 start /var/www/joelj.ca-{{ slot }}/dist/blog-app/server/main.js --name joeljca{{ slot }} && pm2 save"
21 changes: 9 additions & 12 deletions cicd/PS-Configure-And-Deploy-To-Slot.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ param(
[Parameter(Mandatory=$true)][string] $RepoRootPath,
[Parameter(Mandatory=$true)][string] $InventoryFile,
[Parameter(Mandatory=$true)][string] $Slot,
[Parameter(Mandatory=$true)][string] $RootPassword
[Parameter(Mandatory=$true)][string] $RootPassword,
[Parameter(Mandatory=$true)][string] $NodeInsightsConnectionString
)

. $RepoRootPath/cicd/PS-Lib.ps1

$ErrorActionPreference = "Stop"

Write-Host 'In Configure-And-Deploy-To-Slot'
Expand All @@ -22,21 +25,15 @@ else {
}

# Configure Hosts
$Cmd = "ansible-playbook -i $InventoryFile $RepoRootPath/cicd/Ansible-Playbook-Web-Configure.yml --extra-vars `"root_password=$RootPassword`"" + ';$?'
$Cmd = "ansible-playbook -i $InventoryFile $RepoRootPath/cicd/Ansible-Playbook-Web-Configure.yml --extra-vars `"root_password=$RootPassword`""

Write-Host $Cmd.Replace($RootPassword, '***')

$Success = Invoke-Expression $Cmd
if (-not $Success){
Write-Error "Command invocation failed"
}
InvokeAndCheck $Cmd

# Deploy to Hosts
$Cmd = "ansible-playbook -i $InventoryFile $RepoRootPath/cicd/Ansible-Playbook-Web-Deploy.yml --extra-vars `"root_password=$RootPassword slot_port=$Port slot=$Slot`"" + ';$?'
$Cmd = "ansible-playbook -i $InventoryFile $RepoRootPath/cicd/Ansible-Playbook-Web-Deploy.yml --extra-vars `"root_password=$RootPassword slot_port=$Port slot=$Slot node_insights_connection_string='$NodeInsightsConnectionString'`""

Write-Host $Cmd.Replace($RootPassword, '***')
Write-Host $Cmd.Replace($RootPassword, '***').Replace($NodeInsightsConnectionString, '***');

$Success = Invoke-Expression $Cmd
if (-not $Success){
Write-Error "Command invocation failed"
}
InvokeAndCheck $Cmd
3 changes: 2 additions & 1 deletion cicd/PS-Deploy-BLUE.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
$ErrorActionPreference = "Stop"
$RootPassword = Get-AzKeyVaultSecret -VaultName "joeljcakeys" -Name "LinodeRootPasswordWorld1" -AsPlainText
$NodeInsightsConnectionString = Get-AzKeyVaultSecret -VaultName "joeljcakeys" -Name "NodeInsightsConnectionString" -AsPlainText

Write-Host -ForegroundColor blue 'In Deploy-BLUE'

$CICDPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRootPath = Split-Path -Parent $CICDPath

& "${CICDPath}/PS-Configure-And-Deploy-To-Slot.ps1" $RepoRootPath "${CICDPath}/Ansible-Inventory-WORLD1.yml" 'blue' $RootPassword
& "${CICDPath}/PS-Configure-And-Deploy-To-Slot.ps1" $RepoRootPath "${CICDPath}/Ansible-Inventory-WORLD1.yml" 'blue' $RootPassword $NodeInsightsConnectionString
3 changes: 2 additions & 1 deletion cicd/PS-Deploy-GREEN.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
$ErrorActionPreference = "Stop"
$RootPassword = Get-AzKeyVaultSecret -VaultName "joeljcakeys" -Name "LinodeRootPasswordWorld1" -AsPlainText
$NodeInsightsConnectionString = Get-AzKeyVaultSecret -VaultName "joeljcakeys" -Name "NodeInsightsConnectionString" -AsPlainText

Write-Host -ForegroundColor green 'In Deploy-GREEN'

$CICDPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$RepoRootPath = Split-Path -Parent $CICDPath

& "${CICDPath}/PS-Configure-And-Deploy-To-Slot.ps1" $RepoRootPath "${CICDPath}/Ansible-Inventory-WORLD1.yml" 'green' $RootPassword
& "${CICDPath}/PS-Configure-And-Deploy-To-Slot.ps1" $RepoRootPath "${CICDPath}/Ansible-Inventory-WORLD1.yml" 'green' $RootPassword $NodeInsightsConnectionString
3 changes: 3 additions & 0 deletions cicd/PS-Lib.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function InvokeAndCheck($Cmd) {
Invoke-Expression $($Cmd + '; if(-not $?){ throw "Invocation Error" }')
}

0 comments on commit 70afc35

Please sign in to comment.