Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Beta] Detect Distro Already Loaded in Agent #1268

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/agent/agentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,24 +237,36 @@ export class AgentLoader {
private _sdkAlreadyExists(): boolean {
try {
// appInstance should either resolve to user SDK or crash. If it resolves to attach SDK, user probably modified their NODE_PATH
let appInstance: string;
let shimInstance: string;
let distroInstance: string;
try {
// Node 8.9+ Windows
if (this._isWindows) {
appInstance = (require.resolve as any)("applicationinsights", { paths: [process.cwd()] });
shimInstance = (require.resolve as any)("applicationinsights", { paths: [process.cwd()] });
distroInstance = (require.resolve as any)("@azure/monitor-opentelemetry", { paths: [process.cwd()] });
}
// Node 8.9+ Linux
else if (this._isLinux) {
appInstance = `${process.cwd()}${(require.resolve as any)("applicationinsights", { paths: [process.cwd()] })}`;
shimInstance = `${process.cwd()}${(require.resolve as any)("applicationinsights", { paths: [process.cwd()] })}`;
distroInstance = `${process.cwd()}${(require.resolve as any)("@azure/monitor-opentelemetry", { paths: [process.cwd()] })}`;
}
} catch (e) {
// Node <8.9
appInstance = require.resolve(`${process.cwd()}/node_modules/applicationinsights`);
shimInstance = require.resolve(`${process.cwd()}/node_modules/applicationinsights`);
distroInstance = require.resolve(`${process.cwd()}/node_modules/@azure/monitor-opentelemetry`);
}
// If loaded instance is in Azure machine home path do not attach the SDK, this means customer already instrumented their app
if (appInstance.indexOf("home") > -1) {
if (shimInstance.indexOf("home") > -1) {
const diagnosticLog: IDiagnosticLog = {
message: `Azure Monitor Application Insights Distro already exists. Module is already installed in this application; not re-attaching. Location: ${appInstance}`,
message: `Azure Monitor Application Insights Distro already exists as a part of the shim. Module is already installed in this application; not re-attaching. Location: ${shimInstance}`,
messageId: DiagnosticMessageId.sdkExists
};
this._diagnosticLogger.logMessage(diagnosticLog);
return true;
}
if (distroInstance.indexOf("home") > -1) {
const diagnosticLog: IDiagnosticLog = {
message: `Azure Monitor Application Insights Distro already exists. Module is already installed in this application; not re-attaching. Location: ${shimInstance}`,
messageId: DiagnosticMessageId.sdkExists
};
this._diagnosticLogger.logMessage(diagnosticLog);
Expand Down
Loading