0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

feat: setting cloud role name for AppInsights in React (#3721)

* feat: setting cloud role name for AppInsights in React

* refactor: simplify service role names
This commit is contained in:
Gao Sun 2023-04-19 20:39:37 +08:00 committed by GitHub
parent e97fe2e1f3
commit 352807b16a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 9 deletions

View file

@ -0,0 +1,7 @@
---
"@logto/app-insights": minor
"@logto/console": patch
"@logto/ui": patch
---
support setting cloud role name for AppInsights in React

View file

@ -16,12 +16,11 @@ class AppInsightsReact {
return this.appInsights?.trackPageView.bind(this.appInsights);
}
async setup(): Promise<boolean> {
async setup(cloudRole: string, connectionString_?: string): Promise<boolean> {
// The string needs to be normalized since it may contain '"'
const connectionString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING?.replace(
/^"?(.*)"?$/g,
'$1'
);
const connectionString = (
connectionString_ ?? process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
)?.replace(/^"?(.*)"?$/g, '$1');
if (!connectionString) {
return false;
@ -47,6 +46,14 @@ class AppInsightsReact {
},
});
this.appInsights.addTelemetryInitializer((item) => {
// The key 'ai.cloud.role' is extracted from Node SDK
// @see https://learn.microsoft.com/en-us/azure/azure-monitor/app/nodejs#multiple-roles-for-multi-component-applications
// @see https://github.com/microsoft/ApplicationInsights-node.js/blob/a573e40fc66981c6a3106bdc5b783d1d94f64231/Schema/PublicSchema/ContextTagKeys.bond#L83
// eslint-disable-next-line @silverhand/fp/no-mutation
item.tags = [...(item.tags ?? []), { 'ai.cloud.role': cloudRole }];
});
this.appInsights.loadAppInsights();
} catch (error: unknown) {
console.error('Unable to init ApplicationInsights:');

View file

@ -8,7 +8,7 @@ dotenv.config({ path: await findUp('.env', {}) });
const { appInsights } = await import('@logto/app-insights/node');
if (await appInsights.setup('logto-cloud')) {
if (await appInsights.setup('cloud')) {
console.debug('Initialized ApplicationInsights');
}

View file

@ -28,7 +28,7 @@ import TenantsProvider, { TenantsContext } from './contexts/TenantsProvider';
// Use `.then()` for better compatibility, update to top-level await some day
// eslint-disable-next-line unicorn/prefer-top-level-await
void appInsightsReact.setup().then((success) => {
void appInsightsReact.setup('console').then((success) => {
if (success) {
console.debug('Initialized ApplicationInsights');
}

View file

@ -11,7 +11,7 @@ dotenv.config({ path: await findUp('.env', {}) });
const { appInsights } = await import('@logto/app-insights/node');
if (await appInsights.setup('logto')) {
if (await appInsights.setup('core')) {
consoleLog.info('Initialized ApplicationInsights');
}

View file

@ -29,7 +29,7 @@ import './scss/normalized.scss';
if (shouldTrack) {
// Use `.then()` for better compatibility, update to top-level await some day
// eslint-disable-next-line unicorn/prefer-top-level-await, promise/prefer-await-to-then
void appInsightsReact.setup().then((success) => {
void appInsightsReact.setup('ui').then((success) => {
if (success) {
console.debug('Initialized ApplicationInsights');
}