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

[Shim] Add Auto-Collection Methods, TelemetryClient, and Config #1175

Merged
merged 54 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
4786b2c
Add AutoCollectDependencies and Requests logic.
JacksonWeber Aug 2, 2023
e945d52
Merge branch 'beta' into jacksonweber/auto-collect
JacksonWeber Aug 2, 2023
159a40a
Add http.enable functionality and setUseDiskRetryCaching.
JacksonWeber Aug 2, 2023
2cb1bee
Add required no-op methods.
JacksonWeber Aug 2, 2023
4e47f07
Add methods to telemetryClient that were previously supported.
JacksonWeber Aug 2, 2023
822fdb7
Add global applicationinsights to
JacksonWeber Aug 3, 2023
055b1d5
Include possible way to handle config property on the TelemetryClient.
JacksonWeber Aug 3, 2023
e2d8af8
Set NodeHttp methods as no-ops.
JacksonWeber Aug 3, 2023
dae8c42
Framework for the config class to use with client.config.
JacksonWeber Aug 4, 2023
243c51c
Begin adding config properties to be modified on the client.
JacksonWeber Aug 4, 2023
20c4f77
Fix client.config initialization issue and implement config properties.
JacksonWeber Aug 5, 2023
0c77845
Add shim usage detection env var.
JacksonWeber Aug 5, 2023
053f3e7
Revert "Add shim usage detection env var."
JacksonWeber Aug 5, 2023
0ec4d4f
Apply env var updates.
JacksonWeber Aug 5, 2023
1536629
Fix env var check.
JacksonWeber Aug 5, 2023
a402c5b
Revert "Fix env var check."
JacksonWeber Aug 5, 2023
79bfa83
Env var.
JacksonWeber Aug 5, 2023
5832191
Update package-lock.
JacksonWeber Aug 5, 2023
c9d8fbc
Test if parseConfig is breaking functionaltests.
JacksonWeber Aug 5, 2023
cfe8a78
Update parseConfig to only run when in shim mode.
JacksonWeber Aug 5, 2023
481ea2e
Add more of the config parsing.
JacksonWeber Aug 6, 2023
c01fa91
Add final config values.
JacksonWeber Aug 6, 2023
45090e6
Begin tests for the client.confg.
JacksonWeber Aug 6, 2023
d028558
Fix distro tests and update httpInstrumentation configs.
JacksonWeber Aug 7, 2023
ac829e7
Update functionaltests.
JacksonWeber Aug 7, 2023
71ca8cc
Update package-lock.json
JacksonWeber Aug 7, 2023
dfedcd4
Test functional test update.
JacksonWeber Aug 7, 2023
1f4bda9
Is parseConfig breaking functionalTests?
JacksonWeber Aug 7, 2023
f20b688
Test parseConfig except enableAutoCollect methods.
JacksonWeber Aug 7, 2023
7ade280
Remove autoCollect logic.
JacksonWeber Aug 7, 2023
1a754c1
Test if we're wiping instrumentations configs when running autoCollec…
JacksonWeber Aug 7, 2023
36649b6
Update shim-applicationinsights.ts
JacksonWeber Aug 7, 2023
bc3a88c
Add flag to indicate shim is initialized so we know when to run
JacksonWeber Aug 8, 2023
d9198df
Implement noPatchModules and update noDiagnosticChannel.
JacksonWeber Aug 8, 2023
e0c754b
Add ContextTagKeys for implementing them on the context object.
JacksonWeber Aug 8, 2023
dfd3bf2
Fix import.
JacksonWeber Aug 8, 2023
c792250
Add support for further properties on the config and begin working on…
JacksonWeber Aug 9, 2023
f15676a
Write tests, and fix config parse methods.
JacksonWeber Aug 9, 2023
e995036
Add further tests, and update config methods that weren't working.
JacksonWeber Aug 9, 2023
488bce4
Add final config tests, and clean up duplicate code.
JacksonWeber Aug 9, 2023
7a7a3f4
Deliver warnings when trying to set unsupported values.
JacksonWeber Aug 9, 2023
2fcb4fd
Implement maxBatchInterval.
JacksonWeber Aug 9, 2023
3af3f1c
Remove tests and values not supported by client.config in AppInsights…
JacksonWeber Aug 11, 2023
e9db007
Test functionalTest.
JacksonWeber Aug 11, 2023
3e68e6d
Fix functional tests.
JacksonWeber Aug 11, 2023
2f7bc08
Test functionalTest.
JacksonWeber Aug 11, 2023
7a0025e
Update main.js
JacksonWeber Aug 11, 2023
4954405
Does parseConfig break functionalTests.
JacksonWeber Aug 11, 2023
4c56d69
Update client setup.
JacksonWeber Aug 11, 2023
11e586c
Remove unneded connStringParsing and enpointUrl contruction.
JacksonWeber Aug 11, 2023
18ad8ff
Convert config helper to a collection of functions.
JacksonWeber Aug 11, 2023
f78a9fe
Create NodeClient class.
JacksonWeber Aug 11, 2023
c9c3834
Clean up telemetryClient.
JacksonWeber Aug 14, 2023
3cd463c
Make track messages more explicit.
JacksonWeber Aug 14, 2023
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
14 changes: 14 additions & 0 deletions applicationinsights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export {
setup,
start,
getCorrelationContext,
startOperation,
wrapWithCorrelationContext,
Configuration,
defaultClient,
Contracts,
DistributedTracingModes,
HttpRequest,
TelemetryClient,
dispose,
} from "./src/shim/shim-applicationinsights";
568 changes: 342 additions & 226 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export { TelemetryClient } from "./shim/telemetryClient";
export { ApplicationInsightsOptions } from "./types";
export { KnownSeverityLevel } from "./declarations/generated";
Expand All @@ -15,4 +14,7 @@ export {

// To support previous versions of Beta, will be removed before GA release
export { ApplicationInsightsClient } from "./applicationInsightsClient";
export { ApplicationInsightsConfig } from "./applicationInsightsConfig";
export { ApplicationInsightsConfig } from "./applicationInsightsConfig";

// To support the shim
export * from "../applicationinsights";
34 changes: 34 additions & 0 deletions src/shim/NodeClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Logger } from "./logging";
import { Contracts, TelemetryClient } from "./shim-applicationinsights";

class NodeClient extends TelemetryClient {
/**
* Log RequestTelemetry from HTTP request and response. This method will log immediately without waiting for request completion
* and it requires duration parameter to be specified on NodeHttpRequestTelemetry object.
* Use trackNodeHttpRequest function to log the telemetry after request completion
* @param telemetry Object encapsulating incoming request, response and duration information
*/
public trackNodeHttpRequestSync(telemetry: Contracts.NodeHttpRequestTelemetry) {

Check warning on line 11 in src/shim/NodeClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 14.x)

'telemetry' is defined but never used

Check warning on line 11 in src/shim/NodeClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'telemetry' is defined but never used

Check warning on line 11 in src/shim/NodeClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'telemetry' is defined but never used
Logger.getInstance().warn("trackNodeHttpRequestSync is not implemented and is a no-op.");
}

/**
* Log RequestTelemetry from HTTP request and response. This method will `follow` the request to completion.
* Use trackNodeHttpRequestSync function to log telemetry immediately without waiting for request completion
* @param telemetry Object encapsulating incoming request and response information
*/
public trackNodeHttpRequest(telemetry: Contracts.NodeHttpRequestTelemetry) {

Check warning on line 20 in src/shim/NodeClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 14.x)

'telemetry' is defined but never used

Check warning on line 20 in src/shim/NodeClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'telemetry' is defined but never used

Check warning on line 20 in src/shim/NodeClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'telemetry' is defined but never used
Logger.getInstance().warn("trackNodeHttpRequest is not implemented and is a no-op.");
}

/**
* Log DependencyTelemetry from outgoing HTTP request. This method will instrument the outgoing request and append
* the specified headers and will log the telemetry when outgoing request is complete
* @param telemetry Object encapsulating outgoing request information
*/
public trackNodeHttpDependency(telemetry: Contracts.NodeHttpRequestTelemetry) {

Check warning on line 29 in src/shim/NodeClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 14.x)

'telemetry' is defined but never used

Check warning on line 29 in src/shim/NodeClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'telemetry' is defined but never used

Check warning on line 29 in src/shim/NodeClient.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'telemetry' is defined but never used
Logger.getInstance().warn("trackNodeHttpDependency is not implemented and is a no-op.");
}
}

export = NodeClient;
148 changes: 148 additions & 0 deletions src/shim/configuration/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { DistributedTracingModes, IConfig, IDisabledExtendedMetrics, IWebInstrumentationConfig } from "../types";
import http = require("http");
import https = require("https");
import azureCoreAuth = require("@azure/core-auth");
import { Logger } from "../logging";
import constants = require("../../declarations/constants");

class config implements IConfig {

public static ENV_azurePrefix = "APPSETTING_"; // Azure adds this prefix to all environment variables
public static ENV_iKey = "APPINSIGHTS_INSTRUMENTATIONKEY"; // This key is provided in the readme
public static legacy_ENV_iKey = "APPINSIGHTS_INSTRUMENTATION_KEY";
public static ENV_profileQueryEndpoint = "APPINSIGHTS_PROFILE_QUERY_ENDPOINT";
public static ENV_quickPulseHost = "APPINSIGHTS_QUICKPULSE_HOST";

public endpointUrl: string;
public maxBatchSize: number;
public maxBatchIntervalMs: number;
public disableAppInsights: boolean;
public samplingPercentage: number;
public correlationIdRetryIntervalMs: number;
public correlationHeaderExcludedDomains: string[];
public proxyHttpUrl: string;
public proxyHttpsUrl: string;
public httpAgent: http.Agent;
public httpsAgent: https.Agent;
public ignoreLegacyHeaders: boolean;
public aadTokenCredential?: azureCoreAuth.TokenCredential;
public enableAutoCollectConsole: boolean;
public enableLoggerErrorToTrace: boolean;
public enableAutoCollectExceptions: boolean;
public enableAutoCollectPerformance: boolean;
public enableAutoCollectExternalLoggers: boolean;
public enableAutoCollectPreAggregatedMetrics: boolean;
public enableAutoCollectHeartbeat: boolean;
public enableAutoCollectRequests: boolean;
public enableAutoCollectDependencies: boolean;
public enableAutoDependencyCorrelation: boolean;
public enableAutoCollectIncomingRequestAzureFunctions: boolean;
public enableSendLiveMetrics: boolean;
public enableUseDiskRetryCaching: boolean;
public enableUseAsyncHooks: boolean;
public distributedTracingMode: DistributedTracingModes;

public enableAutoCollectExtendedMetrics: boolean | IDisabledExtendedMetrics;
public enableResendInterval: number;
public enableMaxBytesOnDisk: number;
public enableInternalDebugLogging: boolean;
public enableInternalWarningLogging: boolean;
public disableAllExtendedMetrics: boolean;
public disableStatsbeat: boolean; // TODO: Implement this as a way to shutoff statsbeat
public extendedMetricDisablers: string;
public quickPulseHost: string; // TODO: This is not noted in the README
public enableWebInstrumentation: boolean;
public webInstrumentationConfig: IWebInstrumentationConfig[];
public webInstrumentationSrc: string;
public noPatchModules: string;
public noHttpAgentKeepAlive: boolean;

// To Be deprecated.
public enableAutoWebSnippetInjection: boolean;

public correlationId: string; // TODO: Should be private NOTE: This is not noted in the README
private _connectionString: string;
private _endpointBase: string = constants.DEFAULT_BREEZE_ENDPOINT;

private _instrumentationKey: string;
public _webInstrumentationConnectionString: string;

// Added to maintain parity between JSON config and setting manually in the shim
public noDiagnosticChannel: boolean;

constructor(setupString?: string) {
this.instrumentationKey = setupString;

this.maxBatchSize = this.maxBatchSize || 250;
this.maxBatchIntervalMs = this.maxBatchIntervalMs || 15000;
this.disableAppInsights = this.disableAppInsights || false;
this.samplingPercentage = this.samplingPercentage || 100;
this.correlationIdRetryIntervalMs = this.correlationIdRetryIntervalMs || 30 * 1000;
// this.enableWebInstrumentation = this.enableWebInstrumentation || this.enableAutoWebSnippetInjection || false;
this.webInstrumentationConfig = this.webInstrumentationConfig || null;
// this.enableAutoWebSnippetInjection = this.enableWebInstrumentation;
this.correlationHeaderExcludedDomains =
this.correlationHeaderExcludedDomains ||
[
"*.core.windows.net",
"*.core.chinacloudapi.cn",
"*.core.cloudapi.de",
"*.core.usgovcloudapi.net",
"*.core.microsoft.scloud",
"*.core.eaglex.ic.gov"
];

this.ignoreLegacyHeaders = true;
this.webInstrumentationConnectionString = this.webInstrumentationConnectionString || this._webInstrumentationConnectionString || "";
this.webSnippetConnectionString = this.webInstrumentationConnectionString;
}

public set instrumentationKey(iKey: string) {
if (!config._validateInstrumentationKey(iKey)) {
Logger.getInstance().warn("An invalid instrumentation key was provided. There may be resulting telemetry loss", this.instrumentationKey);
}
this._instrumentationKey = iKey;
}

public get instrumentationKey(): string {
return this._instrumentationKey;
}

public set webSnippetConnectionString(connectionString: string) {
this._webInstrumentationConnectionString = connectionString;
}

public get webSnippetConnectionString(): string {
return this._webInstrumentationConnectionString;
}

public set webInstrumentationConnectionString(connectionString: string) {
this._webInstrumentationConnectionString = connectionString;
}

public get webInstrumentationConnectionString() {
return this._webInstrumentationConnectionString;
}

/**
* Validate UUID Format
* Specs taken from breeze repo
* The definition of a VALID instrumentation key is as follows:
* Not none
* Not empty
* Every character is a hex character [0-9a-f]
* 32 characters are separated into 5 sections via 4 dashes
* First section has 8 characters
* Second section has 4 characters
* Third section has 4 characters
* Fourth section has 4 characters
* Fifth section has 12 characters
*/
private static _validateInstrumentationKey(iKey: string): boolean {
const UUID_Regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
const regexp = new RegExp(UUID_Regex);
return regexp.test(iKey);
}
}

export = config;
8 changes: 5 additions & 3 deletions src/shim/context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
export class Context {
public keys: any;
import ContextTagKeys = require("./util/contextTagKeys");

export class Context {
public keys: ContextTagKeys;
public tags: { [key: string]: string };
public static DefaultRoleName = "Web";
public static appVersion: { [path: string]: string } = {};
public static sdkVersion: string = null;

constructor(packageJsonPath?: string) {
this.keys = {};
this.keys = new ContextTagKeys();
this.tags = <{ [key: string]: string }>{};
}
}
Loading
Loading