mirror of
https://github.com/logto-io/logto.git
synced 2025-03-10 22:22:45 -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:
parent
e97fe2e1f3
commit
352807b16a
6 changed files with 23 additions and 9 deletions
7
.changeset/real-falcons-greet.md
Normal file
7
.changeset/real-falcons-greet.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
"@logto/app-insights": minor
|
||||||
|
"@logto/console": patch
|
||||||
|
"@logto/ui": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
support setting cloud role name for AppInsights in React
|
|
@ -16,12 +16,11 @@ class AppInsightsReact {
|
||||||
return this.appInsights?.trackPageView.bind(this.appInsights);
|
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 '"'
|
// The string needs to be normalized since it may contain '"'
|
||||||
const connectionString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING?.replace(
|
const connectionString = (
|
||||||
/^"?(.*)"?$/g,
|
connectionString_ ?? process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
||||||
'$1'
|
)?.replace(/^"?(.*)"?$/g, '$1');
|
||||||
);
|
|
||||||
|
|
||||||
if (!connectionString) {
|
if (!connectionString) {
|
||||||
return false;
|
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();
|
this.appInsights.loadAppInsights();
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
console.error('Unable to init ApplicationInsights:');
|
console.error('Unable to init ApplicationInsights:');
|
||||||
|
|
|
@ -8,7 +8,7 @@ dotenv.config({ path: await findUp('.env', {}) });
|
||||||
|
|
||||||
const { appInsights } = await import('@logto/app-insights/node');
|
const { appInsights } = await import('@logto/app-insights/node');
|
||||||
|
|
||||||
if (await appInsights.setup('logto-cloud')) {
|
if (await appInsights.setup('cloud')) {
|
||||||
console.debug('Initialized ApplicationInsights');
|
console.debug('Initialized ApplicationInsights');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ import TenantsProvider, { TenantsContext } from './contexts/TenantsProvider';
|
||||||
|
|
||||||
// Use `.then()` for better compatibility, update to top-level await some day
|
// Use `.then()` for better compatibility, update to top-level await some day
|
||||||
// eslint-disable-next-line unicorn/prefer-top-level-await
|
// eslint-disable-next-line unicorn/prefer-top-level-await
|
||||||
void appInsightsReact.setup().then((success) => {
|
void appInsightsReact.setup('console').then((success) => {
|
||||||
if (success) {
|
if (success) {
|
||||||
console.debug('Initialized ApplicationInsights');
|
console.debug('Initialized ApplicationInsights');
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ dotenv.config({ path: await findUp('.env', {}) });
|
||||||
|
|
||||||
const { appInsights } = await import('@logto/app-insights/node');
|
const { appInsights } = await import('@logto/app-insights/node');
|
||||||
|
|
||||||
if (await appInsights.setup('logto')) {
|
if (await appInsights.setup('core')) {
|
||||||
consoleLog.info('Initialized ApplicationInsights');
|
consoleLog.info('Initialized ApplicationInsights');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ import './scss/normalized.scss';
|
||||||
if (shouldTrack) {
|
if (shouldTrack) {
|
||||||
// Use `.then()` for better compatibility, update to top-level await some day
|
// 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
|
// 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) {
|
if (success) {
|
||||||
console.debug('Initialized ApplicationInsights');
|
console.debug('Initialized ApplicationInsights');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue