Skip to content

Commit

Permalink
Update App Insights, track custom events (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Oct 7, 2022
1 parent 5b400c7 commit 3777659
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 282 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ If running in an Azure Web App, all of these values can be injected as applicati
| PORT | 3000 | Port the server will listen on |
| TODO_MONGO_CONNSTR | _none_ | Connect to specified MongoDB instance, when set the todo feature will be enabled |
| TODO_MONGO_DB | todoDb | Name of the database in MongoDB to use (optional) |
| APPLICATIONINSIGHTS_CONNECTION_STRING | _none_ | Enable Application Insights monitoring |
| APPINSIGHTS_CONNECTION_STRING | _none_ | Enable AZure Application Insights monitoring |
| WEATHER_API_KEY | _none_ | OpenWeather API key. [Info here](https://openweathermap.org/api) |
| AAD_APP_ID | _none_ | Client ID of app registered in Azure AD |
| DISABLE_METRICS | _none_ | Set to truthy value if you want to switch off Prometheus metrics |
Expand All @@ -179,6 +179,7 @@ See [deployment folder](./deploy) for deploying into Kubernetes with Helm or int

# Updates

- Oct 2022 - Update App Insights, track custom events
- Sept 2022 - Add Prometheus metrics
- Aug 2022 - Switch to PKCE for auth & login flow
- Nov 2021 - Replace DarkSky API with OpenWeather
Expand Down
20 changes: 7 additions & 13 deletions deploy/container-app.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ param image string = 'ghcr.io/benc-uk/nodejs-demoapp:latest'
param weatherApiKey string = ''

@description('Optional feature: Enable Azure App Insights')
param appInsightsInstrumentationKey string = ''
param appInsightsConnString string = ''

@description('Optional feature: Enable todo app with MongoDB')
param todoMongoConnstr string = ''

@description('Optional feature: Enable auth with Azure AD, client id')
param aadAppId string = ''
@description('Optional feature: Enable auth with Azure AD, client secret')
param aadAppSecret string = ''

// ===== Variables ============================================================

Expand All @@ -35,8 +33,8 @@ var environmentName = '${resourceGroup().name}-environment'
resource logWorkspace 'Microsoft.OperationalInsights/workspaces@2020-08-01' = {
location: location
name: logWorkspaceName
properties:{
sku:{
properties: {
sku: {
name: 'Free'
}
}
Expand All @@ -46,13 +44,13 @@ resource kubeEnv 'Microsoft.Web/kubeEnvironments@2021-02-01' = {
location: location
name: environmentName
kind: 'containerenvironment'

properties: {
type: 'Managed'
appLogsConfiguration: {
destination: 'log-analytics'
logAnalyticsConfiguration: {
customerId: logWorkspace.properties.customerId
customerId: logWorkspace.properties.customerId
sharedKey: logWorkspace.listKeys().primarySharedKey
}
}
Expand Down Expand Up @@ -80,8 +78,8 @@ resource containerApp 'Microsoft.Web/containerApps@2021-03-01' = {
value: weatherApiKey
}
{
name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
value: appInsightsInstrumentationKey
name: 'APPINSIGHTS_CONNECTION_STRING'
value: appInsightsConnString
}
{
name: 'TODO_MONGO_CONNSTR'
Expand All @@ -91,10 +89,6 @@ resource containerApp 'Microsoft.Web/containerApps@2021-03-01' = {
name: 'AAD_APP_ID'
value: aadAppId
}
{
name: 'AAD_APP_SECRET'
value: aadAppSecret
}
]
}
]
Expand Down
6 changes: 5 additions & 1 deletion deploy/kubernetes/aks-live.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ secretEnv:

env:
AAD_APP_ID: 129e29d2-6c0a-4dbf-849e-320a3aa93d52
APPLICATIONINSIGHTS_CONNECTION_STRING: "InstrumentationKey=76ae0e4d-5b1f-4081-915a-34cd49c8c4c2;IngestionEndpoint=https://uksouth-1.in.applicationinsights.azure.com/;LiveEndpoint=https://uksouth.livediagnostics.monitor.azure.com/"
APPINSIGHTS_CONNECTION_STRING: 'InstrumentationKey=76ae0e4d-5b1f-4081-915a-34cd49c8c4c2;IngestionEndpoint=https://uksouth-1.in.applicationinsights.azure.com/;LiveEndpoint=https://uksouth.livediagnostics.monitor.azure.com/'
REDIS_SESSION_HOST: redis

resources:
Expand All @@ -35,3 +35,7 @@ ingress:
tls:
enabled: true
secretName: benco-io-cert

podAnnotations:
prometheus.io/scrape: 'true'
prometheus.io/port: '3000'
2 changes: 1 addition & 1 deletion deploy/kubernetes/app.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ service:
# secretName: nodejs-secrets
# secretKey: mongoConnString
# env:
# APPINSIGHTS_INSTRUMENTATIONKEY: __CHANGE_ME__
# APPINSIGHTS_CONNECTION_STRING: __CHANGE_ME__
# AAD_APP_ID: __CHANGE_ME__
# TODO_MONGO_DB: __CHANGE_ME__

Expand Down
3 changes: 1 addition & 2 deletions deploy/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ az deployment group create --template-file container-app.bicep --resource-group
Optional deployment parameters, each one maps to an environment variable (see [main docs](../#configuration) for details):

- **weatherApiKey**
- **appInsightsInstrumentationKey**
- **appInsightsConnString**
- **todoMongoConnstr**
- **aadAppId**
- **aadAppSecret**
36 changes: 36 additions & 0 deletions src/graph.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Graph helper module, far more lightweight than the massive Graph SDK
// -------------------------------------------------------------------------
// Ben C - Sept 2022
//

import axios from 'axios'

export async function getUserDetails(accessToken) {
try {
const graphReq = {
url: 'https://graph.microsoft.com/v1.0/me',
headers: { Authorization: accessToken },
}

const resp = await axios(graphReq)
return resp.data
} catch (err) {
console.log(`### 💥 ERROR! Failed to get user details ${err.toString()}`)
}
}

export async function getUserPhoto(accessToken) {
try {
const graphReq = {
url: 'https://graph.microsoft.com/v1.0/me/photo/$value',
responseType: 'arraybuffer',
headers: { Authorization: accessToken },
}

const resp = await axios(graphReq)
return new Buffer.from(resp.data, 'binary').toString('base64')
} catch (err) {
console.log(`### 💥 ERROR! Failed to get user photo ${err.toString()}`)
}
}
Loading

0 comments on commit 3777659

Please sign in to comment.