private void ApplyFunctionResultActivityTags()

in src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLogger.cs [412:476]


        private void ApplyFunctionResultActivityTags(IEnumerable<KeyValuePair<string, object>> state, IReadOnlyDictionary<string, object> scope, LogLevel logLevel)
        {
            // Activity carries tracing context. It is managed by instrumented library (e.g. ServiceBus or Asp.Net Core)
            // and consumed by ApplicationInsights.
            // This function stamps all function-related tags on the Activity. Then WebJobsTelemetryInitializer sets them on the RequestTelemetry.
            // This way, requests reported by WebJobs (e.g. timer trigger) and requests reported by ApplicationInsights (Http, ServiceBus)
            // both have essential information about function execution
            Activity currentActivity = Activity.Current;

            // should always be true
            if (currentActivity != null)
            {
                // Build up the telemetry model. Some values special and go right on the telemetry object. All others
                // are added to the Properties bag.
                foreach (KeyValuePair<string, object> prop in state)
                {
                    switch (prop.Key)
                    {
                        case LogConstants.StartTimeKey:
                        case LogConstants.EndTimeKey:
                            // These values are set by the calls to Start/Stop the telemetry. Other
                            // Loggers may want them, but we'll ignore.
                            break;
                        case LogConstants.LogLevelKey:
                        case LogConstants.CategoryNameKey:
                        case LogConstants.EventIdKey:
                            // this is set in the WebJobs initializer,
                            // we will ignore it here
                            break;
                        case LogConstants.MessageEnqueuedTimeKey:
                            // this is populated when creating telemetry
                            // we will ignore it here
                            break;
                        case LogConstants.DurationKey:
                            if (prop.Value is TimeSpan duration)
                            {
                                currentActivity.AddTag(LogConstants.FunctionExecutionTimeKey, duration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture));
                            }
                            break;
                        default:
                            // There should be no custom properties here, so just copy
                            // the passed-in values without any 'prop__' prefix.
                            if (prop.Value != null)
                            {
                                currentActivity.AddTag(prop.Key, prop.Value.ToString());
                            }

                            break;
                    }
                }

                currentActivity.AddTag(LogConstants.CategoryNameKey, _categoryName);
                currentActivity.AddTag(LogConstants.LogLevelKey, logLevel.ToStringOptimized());

                if (scope != null)
                {
                    if (!_loggerOptions.HttpAutoCollectionOptions.EnableHttpTriggerExtendedInfoCollection &&
                        scope.TryGetValue(ApplicationInsightsScopeKeys.HttpRequest, out var request) &&
                        request is HttpRequest httpRequest)
                    {
                        currentActivity.AddTag(LoggingConstants.ClientIpKey, GetIpAddress(httpRequest));
                    }
                }
            }
        }