Skip to content

Commit

Permalink
[Beta] Removing OTLP Exporters (#1172)
Browse files Browse the repository at this point in the history
* Removing OTLP Exporters

* WIP

* Use latest @azure/monitor-opentelemetry
  • Loading branch information
hectorhdzg committed Jul 14, 2023
1 parent aa2c706 commit f2979ab
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 326 deletions.
562 changes: 329 additions & 233 deletions package-lock.json

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,26 @@
"@azure/core-client": "^1.0.0",
"@azure/core-rest-pipeline": "^1.9.2",
"@azure/identity": "^3.1.3",
"@azure/monitor-opentelemetry": "^1.0.0-beta.0",
"@azure/monitor-opentelemetry": "^1.0.0-beta.1",
"@azure/opentelemetry-instrumentation-azure-sdk": "^1.0.0-beta.1",
"@opentelemetry/api": "^1.4.1",
"@opentelemetry/api-logs": "0.40.0",
"@opentelemetry/core": "^1.13.0",
"@opentelemetry/exporter-metrics-otlp-http": "^0.40.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.40.0",
"@opentelemetry/instrumentation": "^0.39.1",
"@opentelemetry/instrumentation-http": "^0.40.0",
"@opentelemetry/api-logs": "^0.41.0",
"@opentelemetry/core": "^1.15.0",
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation-http": "^0.41.0",
"@opentelemetry/instrumentation-mongodb": "^0.34.0",
"@opentelemetry/instrumentation-mysql": "^0.32.0",
"@opentelemetry/instrumentation-pg": "^0.34.0",
"@opentelemetry/instrumentation-mysql": "^0.33.0",
"@opentelemetry/instrumentation-pg": "^0.35.0",
"@opentelemetry/instrumentation-redis": "^0.34.1",
"@opentelemetry/instrumentation-redis-4": "^0.34.1",
"@opentelemetry/otlp-exporter-base": "^0.39.1",
"@opentelemetry/resources": "^1.14.0",
"@opentelemetry/sdk-logs": "0.40.0",
"@opentelemetry/sdk-metrics": "^1.14.0",
"@opentelemetry/sdk-trace-base": "^1.14.0",
"@opentelemetry/sdk-trace-node": "^1.14.0",
"@opentelemetry/semantic-conventions": "^1.14.0",
"@opentelemetry/resources": "^1.15.0",
"@opentelemetry/sdk-logs": "^0.41.0",
"@opentelemetry/sdk-metrics": "^1.15.0",
"@opentelemetry/sdk-trace-base": "^1.15.0",
"@opentelemetry/sdk-trace-node": "^1.15.0",
"@opentelemetry/semantic-conventions": "^1.15.0",
"diagnostic-channel": "1.1.0",
"diagnostic-channel-publishers": "1.0.4",
"semver": "^7.3.5"
"diagnostic-channel-publishers": "1.0.4"
},
"peerDependencies": {
"applicationinsights-native-metrics": "*"
Expand Down
9 changes: 4 additions & 5 deletions src/agent/agentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import { ManagedIdentityCredential } from "@azure/identity";
import { TelemetryClient } from "../shim/telemetryClient";
import { AzureMonitorOpenTelemetryClient } from "@azure/monitor-opentelemetry";
import { Util } from "../shim/util";
import { ConsoleWriter } from "./diagnostics/writers/consoleWriter";
import { DiagnosticLogger } from "./diagnostics/diagnosticLogger";
Expand Down Expand Up @@ -110,19 +110,17 @@ export class AgentLoader {
this._diagnosticLogger = logger;
}

public initialize(): void {
public initialize(): AzureMonitorOpenTelemetryClient {
if (!this._canLoad) {
const msg = `Cannot load Azure Monitor Application Insights Distro because of unsupported Node.js runtime, currently running in version ${NODE_JS_RUNTIME_MAJOR_VERSION}`;
console.log(msg);
return;
}
if (this._validate()) {
try {
// TODO: Set Prefix

// Initialize Distro
this._options.azureMonitorExporterConfig.aadTokenCredential = this._aadCredential;
const appInsightsClient = new TelemetryClient(this._options);
const appInsightsClient = new AzureMonitorOpenTelemetryClient(this._options);
// Agent successfully initialized
const diagnosticLog: IDiagnosticLog = {
message: "Azure Monitor Application Insights Distro was started succesfully.",
Expand All @@ -132,6 +130,7 @@ export class AgentLoader {
this._statusLogger.logStatus({
AgentInitializedSuccessfully: true
});
return appInsightsClient;

}
catch (error) {
Expand Down
6 changes: 6 additions & 0 deletions src/agent/aksLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import { AgentResourceProviderType, AZURE_MONITOR_AGENT_PREFIX } from './types';


export class AKSLoader extends AgentLoader {

constructor() {
super();
if (this._canLoad) {
// AKS specific configuration
this._options.otlpMetricExporterConfig = {
enabled: true
};

let statusLogDir = '/var/log/applicationinsights/';
if (this._isWindows) {
if (process.env.HOME) {
Expand Down
1 change: 1 addition & 0 deletions src/agent/azureFunctionsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Attributes } from "@opentelemetry/api";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";

export class AzureFunctionsLoader extends AgentLoader {

constructor() {
super();
if (this._canLoad) {
Expand Down
4 changes: 1 addition & 3 deletions src/applicationInsightsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { TokenCredential } from "@azure/core-auth";
import { Resource } from "@opentelemetry/resources";
import { ApplicationInsightsOptions, LogInstrumentationsConfig, OTLPExporterConfig } from "./types";
import { ApplicationInsightsOptions, LogInstrumentationsConfig } from "./types";
import { AzureMonitorExporterOptions } from "@azure/monitor-opentelemetry-exporter";
import { InstrumentationOptions } from "@azure/monitor-opentelemetry";

Expand All @@ -12,8 +12,6 @@ import { InstrumentationOptions } from "@azure/monitor-opentelemetry";
*/
export class ApplicationInsightsConfig implements ApplicationInsightsOptions {
// ApplicationInsightsOptions
public otlpTraceExporterConfig?: OTLPExporterConfig;
public otlpMetricExporterConfig?: OTLPExporterConfig;
public enableAutoCollectExceptions?: boolean;
public extendedMetrics: { [type: string]: boolean };
public logInstrumentations?: LogInstrumentationsConfig;
Expand Down
33 changes: 1 addition & 32 deletions src/shim/configuration/internal.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { JsonConfig } from "./jsonConfig";
import { Logger } from "../logging";
import { ApplicationInsightsOptions, ExtendedMetricType, LogInstrumentationsConfig, OTLPExporterConfig } from "../../types";
import { ApplicationInsightsOptions, ExtendedMetricType, LogInstrumentationsConfig } from "../../types";


export class InternalConfig implements ApplicationInsightsOptions {
private _otlpTraceExporterConfig: OTLPExporterConfig;
private _otlpMetricExporterConfig: OTLPExporterConfig;
private _logInstrumentations: LogInstrumentationsConfig;
public enableAutoCollectExceptions: boolean;
public extendedMetrics: { [type: string]: boolean };

constructor(options?: ApplicationInsightsOptions) {
this._otlpMetricExporterConfig = {};
this._otlpTraceExporterConfig = {};
this.extendedMetrics = {};
// Load config values from env variables and JSON if available
this._loadDefaultValues();
Expand All @@ -21,29 +17,10 @@ export class InternalConfig implements ApplicationInsightsOptions {
if (options) {
this.enableAutoCollectExceptions =
options.enableAutoCollectExceptions || this.enableAutoCollectExceptions;
this.otlpMetricExporterConfig =
options.otlpMetricExporterConfig || this.otlpMetricExporterConfig;
this.otlpTraceExporterConfig = options.otlpTraceExporterConfig || this.otlpTraceExporterConfig;
this.logInstrumentations = options.logInstrumentations || this.logInstrumentations;
}
}

public set otlpTraceExporterConfig(value: OTLPExporterConfig) {
this._otlpTraceExporterConfig = Object.assign(this._otlpTraceExporterConfig, value);
}

public get otlpTraceExporterConfig(): OTLPExporterConfig {
return this._otlpTraceExporterConfig;
}

public set otlpMetricExporterConfig(value: OTLPExporterConfig) {
this._otlpMetricExporterConfig = Object.assign(this._otlpMetricExporterConfig, value);
}

public get otlpMetricExporterConfig(): OTLPExporterConfig {
return this._otlpMetricExporterConfig;
}

public set logInstrumentations(value: LogInstrumentationsConfig) {
this._logInstrumentations = Object.assign(this._logInstrumentations, value);
}
Expand Down Expand Up @@ -78,14 +55,6 @@ export class InternalConfig implements ApplicationInsightsOptions {
private _mergeConfig() {
try {
const jsonConfig = JsonConfig.getInstance();
this.otlpMetricExporterConfig =
jsonConfig.otlpMetricExporterConfig !== undefined
? jsonConfig.otlpMetricExporterConfig
: this.otlpMetricExporterConfig;
this.otlpTraceExporterConfig =
jsonConfig.otlpTraceExporterConfig !== undefined
? jsonConfig.otlpTraceExporterConfig
: this.otlpTraceExporterConfig;
this.enableAutoCollectExceptions =
jsonConfig.enableAutoCollectExceptions !== undefined
? jsonConfig.enableAutoCollectExceptions
Expand Down
6 changes: 1 addition & 5 deletions src/shim/configuration/jsonConfig.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import * as fs from "fs";
import * as path from "path";
import { Logger } from "../logging";
import { ApplicationInsightsOptions, LogInstrumentationsConfig, OTLPExporterConfig } from "../../types";
import { ApplicationInsightsOptions, LogInstrumentationsConfig } from "../../types";

const ENV_CONFIGURATION_FILE = "APPLICATIONINSIGHTS_CONFIGURATION_FILE";
const ENV_CONTENT = "APPLICATIONINSIGHTS_CONFIGURATION_CONTENT";

export class JsonConfig implements ApplicationInsightsOptions {
private static _instance: JsonConfig;
public otlpTraceExporterConfig?: OTLPExporterConfig;
public otlpMetricExporterConfig?: OTLPExporterConfig;
public enableAutoCollectExceptions: boolean;
public logInstrumentations: LogInstrumentationsConfig;
public extendedMetrics: { [type: string]: boolean };
Expand Down Expand Up @@ -53,8 +51,6 @@ export class JsonConfig implements ApplicationInsightsOptions {
}
try {
const jsonConfig: ApplicationInsightsOptions = JSON.parse(jsonString);
this.otlpMetricExporterConfig = jsonConfig.otlpMetricExporterConfig;
this.otlpTraceExporterConfig = jsonConfig.otlpTraceExporterConfig;
this.enableAutoCollectExceptions = jsonConfig.enableAutoCollectExceptions;
this.logInstrumentations = jsonConfig.logInstrumentations;
this.extendedMetrics = jsonConfig.extendedMetrics;
Expand Down
10 changes: 0 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@
// Licensed under the MIT license.

import { AzureMonitorOpenTelemetryOptions } from "@azure/monitor-opentelemetry";
import { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';

export interface OTLPExporterConfig {
baseConfig?: OTLPExporterNodeConfigBase,
enabled?: boolean
}

/**
* Azure Monitor OpenTelemetry Options
*/
export interface ApplicationInsightsOptions extends AzureMonitorOpenTelemetryOptions {
/** OTLP Trace Exporter Configuration */
otlpTraceExporterConfig?: OTLPExporterConfig;
/** OTLP Metric Exporter Configuration */
otlpMetricExporterConfig?: OTLPExporterConfig;
/**
* Sets the state of exception tracking (enabled by default)
* if true uncaught exceptions will be sent to Application Insights
Expand Down
35 changes: 15 additions & 20 deletions test/unitTests/shim/telemetryClient.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("shim/TelemetryClient", () => {
client = new TelemetryClient(
"InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3333"
);
traceExportStub = sinon.stub(client["_client"]["_traceHandler"]["_azureExporter"], "export").callsFake(
traceExportStub = sinon.stub(client.getAzureMonitorOpenTelemetryClient()["_traceHandler"]["_azureExporter"], "export").callsFake(
(data: any, resultCallback: any) =>
new Promise((resolve) => {
resultCallback({
Expand All @@ -51,7 +51,7 @@ describe("shim/TelemetryClient", () => {
resolve(data);
})
);
logExportStub = sinon.stub(client["_client"]["_logHandler"]["_azureExporter"], "export").callsFake(
logExportStub = sinon.stub(client.getAzureMonitorOpenTelemetryClient()["_logHandler"]["_azureExporter"], "export").callsFake(
(data: any, resultCallback: any) =>
new Promise((resolve) => {
resultCallback({
Expand Down Expand Up @@ -90,7 +90,7 @@ describe("shim/TelemetryClient", () => {
assert.equal(spans[0].attributes["peer.service"], "TestTarget");
done();
})
.catch((error) => {
.catch((error: Error) => {
done(error);
});
});
Expand Down Expand Up @@ -119,7 +119,7 @@ describe("shim/TelemetryClient", () => {
assert.equal(spans[0].attributes["db.statement"], "SELECT * FROM test");
done();
})
.catch((error) => {
.catch((error: Error) => {
done(error);
});
});
Expand Down Expand Up @@ -149,7 +149,7 @@ describe("shim/TelemetryClient", () => {
assert.equal(spans[0].attributes["http.url"], "http://test.com");
done();
})
.catch((error) => {
.catch((error: Error) => {
done(error);
});
});
Expand Down Expand Up @@ -181,8 +181,7 @@ describe("shim/TelemetryClient", () => {
success: false,
};
client.trackAvailability(telemetry);
client
.flush()
client.getAzureMonitorOpenTelemetryClient()["_logHandler"].flush()
.then(() => {
assert.ok(logExportStub.calledOnce, "Export called");
const logs = logExportStub.args[0][0];
Expand All @@ -199,7 +198,7 @@ describe("shim/TelemetryClient", () => {
assert.equal(logs[0].instrumentationScope.name, "AzureMonitorLogger");
done();
})
.catch((error) => {
.catch((error: Error) => {
done(error);
});
});
Expand All @@ -214,8 +213,7 @@ describe("shim/TelemetryClient", () => {
url: "testUrl",
};
client.trackPageView(telemetry);
client
.flush()
client.getAzureMonitorOpenTelemetryClient()["_logHandler"].flush()
.then(() => {
assert.ok(logExportStub.calledOnce, "Export called");
const logs = logExportStub.args[0][0];
Expand All @@ -231,7 +229,7 @@ describe("shim/TelemetryClient", () => {
assert.equal(logs[0].instrumentationScope.name, "AzureMonitorLogger");
done();
})
.catch((error) => {
.catch((error: Error) => {
done(error);
});
});
Expand All @@ -243,8 +241,7 @@ describe("shim/TelemetryClient", () => {
severity: "Information",
};
client.trackTrace(telemetry);
client
.flush()
client.getAzureMonitorOpenTelemetryClient()["_logHandler"].flush()
.then(() => {
assert.ok(logExportStub.calledOnce, "Export called");
const logs = logExportStub.args[0][0];
Expand All @@ -257,7 +254,7 @@ describe("shim/TelemetryClient", () => {
assert.equal(logs[0].instrumentationScope.name, "AzureMonitorLogger");
done();
})
.catch((error) => {
.catch((error: Error) => {
done(error);
});
});
Expand All @@ -272,8 +269,7 @@ describe("shim/TelemetryClient", () => {
measurements: measurements,
};
client.trackException(telemetry);
client
.flush()
client.getAzureMonitorOpenTelemetryClient()["_logHandler"].flush()
.then(() => {
assert.ok(logExportStub.calledOnce, "Export called");
const logs = logExportStub.args[0][0];
Expand All @@ -288,7 +284,7 @@ describe("shim/TelemetryClient", () => {
assert.equal(logs[0].instrumentationScope.name, "AzureMonitorLogger");
done();
})
.catch((error) => {
.catch((error: Error) => {
done(error);
});
});
Expand All @@ -302,8 +298,7 @@ describe("shim/TelemetryClient", () => {
measurements: measurements,
};
client.trackEvent(telemetry);
client
.flush()
client.getAzureMonitorOpenTelemetryClient()["_logHandler"].flush()
.then(() => {
assert.ok(logExportStub.calledOnce, "Export called");
const logs = logExportStub.args[0][0];
Expand All @@ -316,7 +311,7 @@ describe("shim/TelemetryClient", () => {
assert.equal(logs[0].instrumentationScope.name, "AzureMonitorLogger");
done();
})
.catch((error) => {
.catch((error: Error) => {
done(error);
});
});
Expand Down

0 comments on commit f2979ab

Please sign in to comment.