Add Azure Functions Agent initialization (#1048)

This commit is contained in:
Hector Hernandez 2023-01-06 10:12:49 -08:00 коммит произвёл GitHub
Родитель a4b6f71dfc
Коммит b6dfcb8c70
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 28 добавлений и 4 удалений

Просмотреть файл

@ -10,7 +10,7 @@ import { DiagnosticLog, DiagnosticMessageId } from "./DataModel";
// Private configuration vars // Private configuration vars
let _appInsights: typeof types | null; let _appInsights: typeof types | null;
let _prefix = "ad_"; // App Services, Default let _prefix = "ud_"; // Unknown, Default
export const defaultConfig = new Config(); // Will read env variables, expose for Agent initialization export const defaultConfig = new Config(); // Will read env variables, expose for Agent initialization
const _instrumentationKey = defaultConfig.instrumentationKey; const _instrumentationKey = defaultConfig.instrumentationKey;
@ -31,7 +31,7 @@ export function setLogger(logger: DiagnosticLogger) {
/** /**
* Sets the string which is prefixed to the existing sdkVersion, e.g. `ad_`, `alr_` * Sets the string which is prefixed to the existing sdkVersion, e.g. `ad_`, `alr_`
* @param prefix string prefix, including underscore. Defaults to `ad_` * @param prefix string prefix, including underscore. Defaults to `ud_`
*/ */
export function setUsagePrefix(prefix: string) { export function setUsagePrefix(prefix: string) {
_prefix = prefix; _prefix = prefix;
@ -45,7 +45,7 @@ export function setStatusLogger(statusLogger: StatusLogger) {
* Try to setup and start this app insights instance if attach is enabled. * Try to setup and start this app insights instance if attach is enabled.
* @param aadTokenCredential Optional AAD credential * @param aadTokenCredential Optional AAD credential
*/ */
export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential): typeof types | null { export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential, isAzureFunction?: boolean): typeof types | null {
// If app already contains SDK, skip agent attach // If app already contains SDK, skip agent attach
if (!forceStart && Helpers.sdkAlreadyExists(_logger)) { if (!forceStart && Helpers.sdkAlreadyExists(_logger)) {
_statusLogger.logStatus({ _statusLogger.logStatus({
@ -115,7 +115,31 @@ export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential
} }
// Instrument the SDK // Instrument the SDK
_appInsights.setup().setSendLiveMetrics(true); // Azure Functions
if (isAzureFunction) {
_appInsights.setup().setSendLiveMetrics(false)
.setAutoCollectPerformance(false)
.setAutoCollectPreAggregatedMetrics(false)
.setAutoCollectAzureFunctions(true)
.setAutoCollectRequests(true)
.setAutoCollectDependencies(true)
.setAutoCollectExceptions(true)
.setAutoCollectHeartbeat(true)
.setUseDiskRetryCaching(true);
}
// App Services
else {
_appInsights.setup().setSendLiveMetrics(true)
.setAutoCollectPerformance(true)
.setAutoCollectPreAggregatedMetrics(true)
.setAutoCollectAzureFunctions(false)
.setAutoCollectRequests(true)
.setAutoCollectDependencies(true)
.setAutoCollectExceptions(true)
.setAutoCollectHeartbeat(true)
.setUseDiskRetryCaching(true);
}
_appInsights.defaultClient.setAutoPopulateAzureProperties(true); _appInsights.defaultClient.setAutoPopulateAzureProperties(true);
_appInsights.defaultClient.addTelemetryProcessor(prefixInternalSdkVersion); _appInsights.defaultClient.addTelemetryProcessor(prefixInternalSdkVersion);
_appInsights.defaultClient.addTelemetryProcessor(copyOverPrefixInternalSdkVersionToHeartBeatMetric); _appInsights.defaultClient.addTelemetryProcessor(copyOverPrefixInternalSdkVersionToHeartBeatMetric);