Skip to content

Commit

Permalink
Merge branch 'beta' into hectorhdzg/updateddistro
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorhdzg authored Aug 23, 2023
2 parents d210e4a + 95f0ec0 commit f52bca6
Show file tree
Hide file tree
Showing 12 changed files with 786 additions and 48 deletions.
1 change: 0 additions & 1 deletion src/applicationInsightsClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.


import { shutdownAzureMonitor, useAzureMonitor } from "@azure/monitor-opentelemetry";
import { metrics, trace } from "@opentelemetry/api";
import { MeterProvider } from "@opentelemetry/sdk-metrics";
Expand Down
1 change: 1 addition & 0 deletions src/logs/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class AutoCollectExceptions {
) {
if (this._client) {
this._client.trackException({ exception: error });
this._client.flush();
// only rethrow when we are the only listener
if (reThrow && name && process.listeners(name as any).length === 1) {
// eslint-disable-next-line no-console
Expand Down
103 changes: 103 additions & 0 deletions src/shared/configuration/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { JsonConfig } from "./jsonConfig";
import { Logger } from "../logging";
import { ApplicationInsightsOptions, ExtendedMetricType, LogInstrumentationsConfig } from "../../types";


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

constructor(options?: ApplicationInsightsOptions) {
this.extendedMetrics = {};
// Load config values from env variables and JSON if available
this._loadDefaultValues();
this._mergeConfig();
// This will take precedence over other settings
if (options) {
this.enableAutoCollectExceptions =
options.enableAutoCollectExceptions || this.enableAutoCollectExceptions;
this.logInstrumentations = options.logInstrumentations || this.logInstrumentations;
}
}

public set logInstrumentations(value: LogInstrumentationsConfig) {
this._logInstrumentations = Object.assign(this._logInstrumentations, value);
}

public get logInstrumentations(): LogInstrumentationsConfig {
return this._logInstrumentations;
}

/**
* Get Instrumentation Key
* @deprecated This method should not be used
*/
public getDisableStatsbeat(): boolean {
return false;
}

private _loadDefaultValues() {
this.enableAutoCollectExceptions =
this.enableAutoCollectExceptions !== undefined
? this.enableAutoCollectExceptions
: true;
this._logInstrumentations = {
console: { enabled: false },
bunyan: { enabled: false },
winston: { enabled: false },
};
this.extendedMetrics[ExtendedMetricType.gc] = false;
this.extendedMetrics[ExtendedMetricType.heap] = false;
this.extendedMetrics[ExtendedMetricType.loop] = false;
}

private _mergeConfig() {
try {
const jsonConfig = JsonConfig.getInstance();
this.enableAutoCollectExceptions =
jsonConfig.enableAutoCollectExceptions !== undefined
? jsonConfig.enableAutoCollectExceptions
: this.enableAutoCollectExceptions;
if (jsonConfig.logInstrumentations) {
if (
jsonConfig.logInstrumentations.console &&
jsonConfig.logInstrumentations.console.enabled !== undefined
) {
this.logInstrumentations.console.enabled =
jsonConfig.logInstrumentations.console.enabled;
}
if (
jsonConfig.logInstrumentations.bunyan &&
jsonConfig.logInstrumentations.bunyan.enabled !== undefined
) {
this.logInstrumentations.bunyan.enabled =
jsonConfig.logInstrumentations.bunyan.enabled;
}
if (
jsonConfig.logInstrumentations.winston &&
jsonConfig.logInstrumentations.winston.enabled !== undefined
) {
this.logInstrumentations.winston.enabled =
jsonConfig.logInstrumentations.winston.enabled;
}
}
if (jsonConfig.extendedMetrics) {
if (jsonConfig.extendedMetrics[ExtendedMetricType.gc] !== undefined) {
this.extendedMetrics[ExtendedMetricType.gc] =
jsonConfig.extendedMetrics[ExtendedMetricType.gc];
}
if (jsonConfig.extendedMetrics[ExtendedMetricType.heap] !== undefined) {
this.extendedMetrics[ExtendedMetricType.heap] =
jsonConfig.extendedMetrics[ExtendedMetricType.heap];
}
if (jsonConfig.extendedMetrics[ExtendedMetricType.loop] !== undefined) {
this.extendedMetrics[ExtendedMetricType.loop] =
jsonConfig.extendedMetrics[ExtendedMetricType.loop];
}
}
} catch (error) {
Logger.getInstance().error("Failed to load JSON config file values.", error);
}
}
}
61 changes: 61 additions & 0 deletions src/shared/util/configHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,64 @@ export function setAutoCollectConsole(options: ApplicationInsightsOptions, value
}
}
}

export function enableAutoCollectExternalLoggers(options: ApplicationInsightsOptions, value: boolean) {
options.logInstrumentations = {
...options.logInstrumentations,
winston: { enabled: value },
bunyan: { enabled: value },
}
}

export function enableAutoCollectConsole(options: ApplicationInsightsOptions, value: boolean) {
options.logInstrumentations = {
...options.logInstrumentations,
console: { enabled: value },
}
}

export function enableAutoCollectExtendedMetrics(options: ApplicationInsightsOptions, value: boolean) {
options.extendedMetrics = {
[ExtendedMetricType.gc]: value,
[ExtendedMetricType.heap]: value,
[ExtendedMetricType.loop]: value,
}
}

export function setMaxBatchIntervalMs(options: ApplicationInsightsOptions, value: number) {
options.otlpTraceExporterConfig = { ...options.otlpTraceExporterConfig, timeoutMillis: value };
options.otlpMetricExporterConfig = { ...options.otlpMetricExporterConfig, timeoutMillis: value };
options.otlpLogExporterConfig = { ...options.otlpLogExporterConfig, timeoutMillis: value };
}

export function setProxyUrl(options: ApplicationInsightsOptions, proxyUrlString: string) {
const proxyUrl = new URL(proxyUrlString);
options.azureMonitorExporterConfig.proxyOptions = {
host: proxyUrl.hostname,
port: Number(proxyUrl.port),
}
}

export function setExtendedMetricDisablers(options: ApplicationInsightsOptions, disablers: string) {
const extendedMetricDisablers: string[] = disablers.split(",");
for (const extendedMetricDisabler of extendedMetricDisablers) {
if (extendedMetricDisabler === "gc") {
options.extendedMetrics = {
...options.extendedMetrics,
[ExtendedMetricType.gc]: false
};
}
if (extendedMetricDisabler === "heap") {
options.extendedMetrics = {
...options.extendedMetrics,
[ExtendedMetricType.heap]: false
};
}
if (extendedMetricDisabler === "loop") {
options.extendedMetrics = {
...options.extendedMetrics,
[ExtendedMetricType.loop]: false
};
}
}
}
7 changes: 7 additions & 0 deletions src/shim/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import azureCoreAuth = require("@azure/core-auth");
import { Logger } from "../shared/logging";
import constants = require("../declarations/constants");


class config implements IConfig {

public static ENV_azurePrefix = "APPSETTING_"; // Azure adds this prefix to all environment variables
Expand Down Expand Up @@ -83,6 +84,7 @@ class config implements IConfig {
// this.enableAutoWebSnippetInjection = this.enableWebInstrumentation;
this.correlationHeaderExcludedDomains =
this.correlationHeaderExcludedDomains ||
ShimJsonConfig.getInstance().correlationHeaderExcludedDomains ||
[
"*.core.windows.net",
"*.core.chinacloudapi.cn",
Expand Down Expand Up @@ -139,6 +141,11 @@ class config implements IConfig {
* Fifth section has 12 characters
*/
private static _validateInstrumentationKey(iKey: string): boolean {
if (iKey.startsWith("InstrumentationKey=")) {
const startIndex = iKey.indexOf("InstrumentationKey=") + "InstrumentationKey=".length;
const endIndex = iKey.indexOf(";", startIndex);
iKey = iKey.substring(startIndex, endIndex);
}
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);
Expand Down
2 changes: 1 addition & 1 deletion src/shim/shim-applicationinsights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import * as azureFunctionsTypes from "@azure/functions";
import { DiagLogLevel, SpanContext } from "@opentelemetry/api";
import { Span } from "@opentelemetry/sdk-trace-base";
import { HttpInstrumentationConfig } from "@opentelemetry/instrumentation-http";

import { CorrelationContextManager } from "./correlationContextManager";
import { Logger } from "../shared/logging";
import { ICorrelationContext, HttpRequest, DistributedTracingModes } from "./types";
import { TelemetryClient } from "./telemetryClient";
import * as Contracts from "../declarations/contracts";
import { ApplicationInsightsOptions } from "../types";
import { HttpInstrumentationConfig } from "@opentelemetry/instrumentation-http";
import ConfigHelper = require("../shared/util/configHelper");

// We export these imports so that SDK users may use these classes directly.
Expand Down
Loading

0 comments on commit f52bca6

Please sign in to comment.