Skip to content

Commit

Permalink
Add Azure Functions Agent initialization (microsoft#1048)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorhdzg authored Jan 6, 2023
1 parent a4b6f71 commit b6dfcb8
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions Bootstrap/Default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DiagnosticLog, DiagnosticMessageId } from "./DataModel";

// Private configuration vars
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
const _instrumentationKey = defaultConfig.instrumentationKey;
Expand All @@ -31,7 +31,7 @@ export function setLogger(logger: DiagnosticLogger) {

/**
* 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) {
_prefix = prefix;
Expand All @@ -45,7 +45,7 @@ export function setStatusLogger(statusLogger: StatusLogger) {
* Try to setup and start this app insights instance if attach is enabled.
* @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 (!forceStart && Helpers.sdkAlreadyExists(_logger)) {
_statusLogger.logStatus({
Expand Down Expand Up @@ -115,7 +115,31 @@ export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential
}

// 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.addTelemetryProcessor(prefixInternalSdkVersion);
_appInsights.defaultClient.addTelemetryProcessor(copyOverPrefixInternalSdkVersionToHeartBeatMetric);
Expand Down

0 comments on commit b6dfcb8

Please sign in to comment.