Skip to content

Commit

Permalink
Add logRecordProcessor for attaching context and update test.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonWeber committed Aug 23, 2023
1 parent 3d6f6ee commit a0e5f67
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/shared/util/attributeLogRecordProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { BatchLogRecordProcessor, LogRecord, LogRecordExporter } from "@opentelemetry/sdk-logs";

export class AttributeLogProcessor extends BatchLogRecordProcessor {
private _attributes: { [key: string]: string };
constructor(exporter: LogRecordExporter, attributes: { [key: string]: string }) {
super(exporter);
this._attributes = attributes;
}
// Override onStart to apply span attributes before exporting
onEmit(record: LogRecord) {
record.setAttributes(this._attributes);
super.onEmit(record);
}
}
2 changes: 1 addition & 1 deletion src/shim/shim-applicationinsights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function start() {
const ignoreIncomingRequestHooks = httpOptions?.ignoreIncomingRequestHook?.toString();
const ignoreOutgoingRequestHooks = httpOptions?.ignoreOutgoingRequestHook?.toString();

const ignoreFunction = (request: any) => true;
const ignoreFunction = (request: http.IncomingMessage | http.RequestOptions) => true;
if (
ignoreIncomingRequestHooks === ignoreFunction.toString() &&
ignoreOutgoingRequestHooks === ignoreFunction.toString()
Expand Down
13 changes: 9 additions & 4 deletions src/shim/telemetryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import { dispose, Configuration, _setupCalled } from "./shim-applicationinsights
import { HttpInstrumentationConfig } from "@opentelemetry/instrumentation-http";
import { ShimJsonConfig } from "./shim-jsonConfig";
import ConfigHelper = require("../shared/util/configHelper");
import { AzureMonitorTraceExporter } from "@azure/monitor-opentelemetry-exporter";
import { AzureMonitorTraceExporter, AzureMonitorLogExporter } from "@azure/monitor-opentelemetry-exporter";
import { AttributeSpanProcessor } from "../shared/util/attributeSpanProcessor";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { AttributeLogProcessor } from "../shared/util/attributeLogRecordProcessor";

/**
* Application Insights telemetry client provides interface to track telemetry items, register telemetry initializers and
Expand All @@ -37,7 +38,8 @@ export class TelemetryClient {
private _console: AutoCollectConsole;
private _exceptions: AutoCollectExceptions;
private _idGenerator: IdGenerator;
private _attributeProcessor: AttributeSpanProcessor;
private _attributeSpanProcessor: AttributeSpanProcessor;
private _attributeLogProcessor: AttributeLogProcessor;
public context: Context;
public commonProperties: { [key: string]: string }; // TODO: Add setter so Resources are updated
public config: IConfig;
Expand Down Expand Up @@ -472,9 +474,12 @@ export class TelemetryClient {

// Only support attribute processing in the shim
if (_setupCalled) {
this._attributeProcessor = new AttributeSpanProcessor(new AzureMonitorTraceExporter(this._options.azureMonitorExporterConfig), this.context.tags);
this._attributeSpanProcessor = new AttributeSpanProcessor(new AzureMonitorTraceExporter(this._options.azureMonitorExporterConfig), this.context.tags);
const tracerProvider = this._client.getTracerProvider() as NodeTracerProvider;
tracerProvider.addSpanProcessor(this._attributeProcessor);
tracerProvider.addSpanProcessor(this._attributeSpanProcessor);
this._attributeLogProcessor = new AttributeLogProcessor(new AzureMonitorLogExporter(this._options.azureMonitorExporterConfig), this.context.tags);
const loggerProvider = this._client.getLoggerProvider();
loggerProvider.addLogRecordProcessor(this._attributeLogProcessor);
}
}

Expand Down
5 changes: 3 additions & 2 deletions test/unitTests/shim/config.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ describe("shim/configuration/config", () => {
assert.equal(JSON.stringify(telemetryClient["_options"].extendedMetrics), JSON.stringify({ gc: false, heap: false, loop: false }));
});

it("should set context tags", () => {
it("should set context tags on logs and spans", () => {
const telemetryClient = new TelemetryClient(connectionString);
telemetryClient.context.tags = { "ai.cloud.role": "testRole", "ai.cloud.roleInstance": "testRoleInstance" };
telemetryClient.start();
telemetryClient["_attributeProcessor"]["_attributes"] = { "ai.cloud.role": "testRole", "ai.cloud.roleInstance": "testRoleInstance" };
telemetryClient["_attributeSpanProcessor"]["_attributes"] = { "ai.cloud.role": "testRole", "ai.cloud.roleInstance": "testRoleInstance" };
telemetryClient["_attributeLogProcessor"]["_attributes"] = { "ai.cloud.role": "testRole", "ai.cloud.roleInstance": "testRoleInstance" };
});

it("should disableAppInsights", () => {
Expand Down

0 comments on commit a0e5f67

Please sign in to comment.