Skip to content

Commit

Permalink
[Shim] Add Support for Context.tags (#1193)
Browse files Browse the repository at this point in the history
* Add AutoCollectDependencies and Requests logic.

* Add http.enable functionality and setUseDiskRetryCaching.

* Add required no-op methods.

* Add methods to telemetryClient that were previously supported.

* Add global applicationinsights to

* Include possible way to handle config property on the TelemetryClient.

* Set NodeHttp methods as no-ops.

* Framework for the config class to use with client.config.

* Begin adding config properties to be modified on the client.

* Fix client.config initialization issue and implement config properties.

* Add shim usage detection env var.

* Revert "Add shim usage detection env var."

This reverts commit 0c77845.

* Apply env var updates.

* Fix env var check.

* Revert "Fix env var check."

This reverts commit 1536629.

* Env var.

* Update package-lock.

* Test if parseConfig is breaking functionaltests.

* Update parseConfig to only run when in shim mode.

* Add more of the config parsing.

* Add final config values.

* Begin tests for the client.confg.

* Fix distro tests and update httpInstrumentation configs.

* Update functionaltests.

* Update package-lock.json

* Test functional test update.

* Is parseConfig breaking functionalTests?

* Test parseConfig except enableAutoCollect methods.

* Remove autoCollect logic.

* Test if we're wiping instrumentations configs when running autoCollect methods.

* Update shim-applicationinsights.ts

* Add flag to indicate shim is initialized so we know when to run

* Implement noPatchModules and update noDiagnosticChannel.

* Add ContextTagKeys for implementing them on the context object.

* Fix import.

* Add support for further properties on the config and begin working on setting the context.tags.

* Write tests, and fix config parse methods.

* Add further tests, and update config methods that weren't working.

* Add final config tests, and clean up duplicate code.

* Deliver warnings when trying to set unsupported values.

* Implement maxBatchInterval.

* Remove tests and values not supported by client.config in AppInsights 2.x.

* Test functionalTest.

* Fix functional tests.

* Test functionalTest.

* Update main.js

* Does parseConfig break functionalTests.

* Update client setup.

* Remove unneded connStringParsing and enpointUrl contruction.

* Convert config helper to a collection of functions.

* Create NodeClient class.

* Add required JSON config values.

* Setup for supporting the first JSON config value.

* Clean up telemetryClient.

* Make track messages more explicit.

* Add initial JSON config parsing update, deprecate instrumentationKey/endpointUrl,.

* Update telemetryClient.ts

* Split JsonConfig into shim and non-shim files.

* Clean up config files and begin adding tests.

* Fix duplicate files.

* Clean up config files and add JSON tests.

* Reconfigure file paths.

* Add JSON config file tests.

* Add tests for the configuration string and environment variables.

* Update applicationinsights.json

* Update JSON config fields.

* Fix disabling http logic.

* Update and fix request tracking.

* Add attribute processor and tests.

* Only run attribute processing in the shim.

* Add logRecordProcessor for attaching context and update test.

* Update comment.

* Update package-lock.json

* Update package-lock.json

* Remove unused import.

* Use regular SpanProcessor.
  • Loading branch information
JacksonWeber authored Aug 30, 2023
1 parent 2366f0a commit de99a9f
Show file tree
Hide file tree
Showing 8 changed files with 9,679 additions and 191 deletions.
5,792 changes: 5,640 additions & 152 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/shared/util/attributeLogRecordProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { LogRecord, LogRecordProcessor } from "@opentelemetry/sdk-logs";

export class AttributeLogProcessor implements LogRecordProcessor {
private _attributes: { [key: string]: string };
constructor(attributes: { [key: string]: string }) {
this._attributes = attributes;
}

// Override onEmit to apply log record attributes before exporting
onEmit(record: LogRecord) {
record.setAttributes(this._attributes);
}

shutdown(): Promise<void> {
return Promise.resolve();
}

forceFlush(): Promise<void> {
return Promise.resolve();
}
}
25 changes: 25 additions & 0 deletions src/shared/util/attributeSpanProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { SpanProcessor, Span } from "@opentelemetry/sdk-trace-base";

export class AttributeSpanProcessor implements SpanProcessor {
private _attributes: { [key: string]: string };
constructor(attributes: { [key: string]: string }) {
this._attributes = attributes;
}

// Implement onStart to apply span attributes before exporting
onStart(span: Span): void {
span.setAttributes(this._attributes);
}

onEnd(): void {
return;
}

shutdown(): Promise<void> {
return Promise.resolve();
}

forceFlush(): Promise<void> {
return Promise.resolve();
}
}
4 changes: 2 additions & 2 deletions src/shared/util/configHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function setAutoCollectRequests(options: ApplicationInsightsOptions, valu
...options.instrumentationOptions?.http,
enabled: true,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ignoreIncomingRequestHook: (request: http.RequestOptions) => true,
ignoreIncomingRequestHook: (request: http.IncomingMessage) => true,
} as HttpInstrumentationConfig
};
} else {
Expand All @@ -35,7 +35,7 @@ export function setAutoCollectRequests(options: ApplicationInsightsOptions, valu
...options.instrumentationOptions?.http,
enabled: true,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ignoreIncomingRequestHook: (request: http.RequestOptions) => false,
ignoreIncomingRequestHook: (request: http.IncomingMessage) => false,
} as HttpInstrumentationConfig
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/shim/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class Config implements IConfig {

// NOT SUPPORTED CONFIGURATION OPTIONS
if (this.disableAppInsights) {
Logger.getInstance().warn("disableAppInsights cohnfiguration no longer supported.");
Logger.getInstance().warn("disableAppInsights configuration no longer supported.");
}
if (this.enableAutoCollectHeartbeat) {
Logger.getInstance().warn("Heartbeat metris are no longer supported.");
Expand Down
15 changes: 12 additions & 3 deletions src/shim/telemetryClient.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { Attributes, context, DiagLogLevel, SpanKind, SpanOptions, SpanStatusCode, trace } from "@opentelemetry/api";
import { Attributes, context, ProxyTracerProvider, SpanKind, SpanOptions, SpanStatusCode, trace } from "@opentelemetry/api";
import { logs } from "@opentelemetry/api-logs";
import { LoggerProvider } from "@opentelemetry/sdk-logs";
import { SemanticAttributes } from "@opentelemetry/semantic-conventions";

import * as Contracts from "../declarations/contracts";
import { TelemetryItem as Envelope } from "../declarations/generated";
import { Context } from "./context";
import { Logger } from "../shared/logging";
import { Util } from "../shared/util";
import Config = require("./config");
import { AttributeSpanProcessor } from "../shared/util/attributeSpanProcessor";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { AttributeLogProcessor } from "../shared/util/attributeLogRecordProcessor";
import { ApplicationInsightsClient } from "../applicationInsightsClient";
import { LogApi } from "../logs/api";


/**
* Application Insights telemetry client provides interface to track telemetry items, register telemetry initializers and
* and manually trigger immediate sending (flushing)
*/
export class TelemetryClient {
private _attributeSpanProcessor: AttributeSpanProcessor;
private _attributeLogProcessor: AttributeLogProcessor;
private _client: ApplicationInsightsClient;
private _logApi: LogApi;
public context: Context;
Expand All @@ -44,6 +48,11 @@ export class TelemetryClient {
// LoggerProvider would be initialized when client is instantiated
// Get Logger from global provider
this._logApi = new LogApi(logs.getLogger("ApplicationInsightsLogger"));
this._attributeSpanProcessor = new AttributeSpanProcessor(this.context.tags);
((trace.getTracerProvider() as ProxyTracerProvider).getDelegate() as NodeTracerProvider).addSpanProcessor(this._attributeSpanProcessor);

this._attributeLogProcessor = new AttributeLogProcessor(this.context.tags);
(logs.getLoggerProvider() as LoggerProvider).addLogRecordProcessor(this._attributeLogProcessor);
}

/**
Expand Down
Loading

0 comments on commit de99a9f

Please sign in to comment.