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] Update Prefix Version in Auto-Attach Scenarios #1271

Merged
merged 10 commits into from
Feb 15, 2024
26 changes: 3 additions & 23 deletions src/agent/agentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Util } from "../shared/util";
import { ConsoleWriter } from "./diagnostics/writers/consoleWriter";
import { DiagnosticLogger } from "./diagnostics/diagnosticLogger";
import { StatusLogger } from "./diagnostics/statusLogger";
import { AgentResourceProviderType, DiagnosticMessageId, IDiagnosticLog, IDiagnosticLogger, NODE_JS_RUNTIME_MAJOR_VERSION } from "./types";
import { AZURE_MONITOR_AUTO_ATTACH, DiagnosticMessageId, IDiagnosticLog, IDiagnosticLogger, NODE_JS_RUNTIME_MAJOR_VERSION } from "./types";
import { AzureMonitorOpenTelemetryOptions } from "../types";
import { useAzureMonitor } from "../main";

Expand Down Expand Up @@ -120,6 +120,8 @@ export class AgentLoader {
}
if (this._validate()) {
try {
// Set environment variable to auto attach so the distro is aware of the attach state
process.env[AZURE_MONITOR_AUTO_ATTACH] = "true";
// Initialize Distro
this._options.azureMonitorExporterOptions.credential = this._aadCredential;
useAzureMonitor(this._options);
Expand Down Expand Up @@ -281,26 +283,4 @@ export class AgentLoader {
return false;
}
}

protected _getVersionPrefix(rpType: AgentResourceProviderType): string {
let rp = "u"; // Default unknown
let os = "u"; // Default unknown
if (rpType === AgentResourceProviderType.aks) {
rp = "k";
}
else if (rpType === AgentResourceProviderType.appServices) {
rp = "a";
}
else if (rpType === AgentResourceProviderType.azureFunctions) {
rp = "f";
}
if (process.platform === 'win32') {
os = "w";
}
else if (process.platform === 'linux') {
os = "l";
}
return `${rp}${os}_`;
}

}
3 changes: 0 additions & 3 deletions src/agent/aksLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { DiagnosticLogger } from './diagnostics/diagnosticLogger';
import { FileWriter } from "./diagnostics/writers/fileWriter";
import { StatusLogger } from "./diagnostics/statusLogger";
import { AgentLoader } from "./agentLoader";
import { AgentResourceProviderType, AZURE_MONITOR_AGENT_PREFIX } from './types';


export class AKSLoader extends AgentLoader {

Expand Down Expand Up @@ -50,7 +48,6 @@ export class AKSLoader extends AgentLoader {
}
)
);
process.env[AZURE_MONITOR_AGENT_PREFIX] = this._getVersionPrefix(AgentResourceProviderType.aks);
}
}
}
3 changes: 0 additions & 3 deletions src/agent/appServicesLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { DiagnosticLogger } from './diagnostics/diagnosticLogger';
import { FileWriter } from "./diagnostics/writers/fileWriter";
import { StatusLogger } from "./diagnostics/statusLogger";
import { AgentLoader } from "./agentLoader";
import { AgentResourceProviderType, AZURE_MONITOR_AGENT_PREFIX } from './types';


export class AppServicesLoader extends AgentLoader {

Expand Down Expand Up @@ -60,7 +58,6 @@ export class AppServicesLoader extends AgentLoader {
}
)
);
process.env[AZURE_MONITOR_AGENT_PREFIX] = this._getVersionPrefix(AgentResourceProviderType.appServices);
}
}
}
4 changes: 0 additions & 4 deletions src/agent/azureFunctionsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { AgentLoader } from "./agentLoader";
import { DiagnosticLogger } from "./diagnostics/diagnosticLogger";
import { StatusLogger } from "./diagnostics/statusLogger";
import { AzureFunctionsWriter } from "./diagnostics/writers/azureFunctionsWriter";
import { AgentResourceProviderType, AZURE_MONITOR_AGENT_PREFIX } from "./types";
import { Attributes } from "@opentelemetry/api";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";

Expand All @@ -33,9 +32,6 @@ export class AzureFunctionsLoader extends AgentLoader {
const writer = new AzureFunctionsWriter(this._instrumentationKey);
this._diagnosticLogger = new DiagnosticLogger(this._instrumentationKey, writer);
this._statusLogger = new StatusLogger(this._instrumentationKey, writer);
process.env[AZURE_MONITOR_AGENT_PREFIX] = this._getVersionPrefix(
AgentResourceProviderType.azureFunctions
);
}
}
}
8 changes: 1 addition & 7 deletions src/agent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ export const LOGGER_NAME = "applicationinsights.extension.diagnostics";
export const LOGGER_LANGUAGE = "nodejs";
export const NODE_JS_RUNTIME_MAJOR_VERSION = parseInt(process.versions.node.split('.')[0], 10);
export const AZURE_APP_NAME = process.env.WEBSITE_SITE_NAME || 'unknown';
export const AZURE_MONITOR_AGENT_PREFIX = "AZURE_MONITOR_AGENT_PREFIX";

export enum AgentResourceProviderType {
appServices,
azureFunctions,
aks
}
export const AZURE_MONITOR_AUTO_ATTACH = "AZURE_MONITOR_AUTO_ATTACH";

export interface IAgentLogger {
log(message: any, ...optional: any[]): void;
Expand Down
1 change: 1 addition & 0 deletions test/unitTests/agent/agentLoader.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe("agent/agentLoader", () => {
const diagnosticLoggerStub = sandbox.stub(agent["_diagnosticLogger"], "logMessage");

const initAgent = agent.initialize();
assert.equal(process.env["AZURE_MONITOR_AUTO_ATTACH"], "true");
assert.ok(diagnosticLoggerStub.calledOnce);
});

Expand Down
3 changes: 0 additions & 3 deletions test/unitTests/agent/aksLoader.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ describe("agent/AKSLoader", () => {
// Loader is using correct diagnostics
assert.equal(agent["_diagnosticLogger"], diagnosticLogger, "Wrong diagnosticLogger");
assert.equal(agent["_statusLogger"], statusLogger, "Wrong statusLogger");
// Prefix Env variable should be set
assert.equal(process.env["AZURE_MONITOR_AGENT_PREFIX"].length, 3, "Missing prefix");
assert.ok(process.env["AZURE_MONITOR_AGENT_PREFIX"].startsWith, "k");
});

it("initialize", () => {
Expand Down
5 changes: 0 additions & 5 deletions test/unitTests/agent/appServicesLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,13 @@ describe("agent/AppServicesLoader", () => {
assert.ok(statusLogger["_agentLogger"] instanceof FileWriter, "Wrong statusLogger agentLogger");
assert.equal(statusLogger["_agentLogger"]["_filename"], "status_nodejs.json");

// Prefix Env variable should be set
assert.equal(process.env["AZURE_MONITOR_AGENT_PREFIX"].length, 3, "Missing prefix");

if (isWindows) {
assert.equal(diagnosticLogger["_agentLogger"]["_filepath"], "c:\\LogFiles\\ApplicationInsights\\status");
assert.equal(statusLogger["_agentLogger"]["_filepath"], "c:\\LogFiles\\ApplicationInsights\\status");
assert.equal(process.env["AZURE_MONITOR_AGENT_PREFIX"], "aw_");
}
else {
assert.equal(diagnosticLogger["_agentLogger"]["_filepath"], "/var/log/applicationinsights/");
assert.equal(statusLogger["_agentLogger"]["_filepath"], "/var/log/applicationinsights/");
assert.equal(process.env["AZURE_MONITOR_AGENT_PREFIX"], "al_");
}
// Loader is using correct diagnostics
assert.equal(agent["_diagnosticLogger"], diagnosticLogger, "Wrong diagnosticLogger");
Expand Down
3 changes: 0 additions & 3 deletions test/unitTests/agent/azureFunctionsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ describe("agent/AzureFunctionsLoader", () => {
// Loader is using correct diagnostics
assert.equal(agent["_diagnosticLogger"], diagnosticLogger);
assert.equal(agent["_statusLogger"], statusLogger);
// Prefix Env variable should be set
assert.equal(process.env["AZURE_MONITOR_AGENT_PREFIX"].length, 3, "Missing prefix");
assert.ok(process.env["AZURE_MONITOR_AGENT_PREFIX"].startsWith, "f");
});

it("initialize", () => {
Expand Down
Loading