Exemplo n.º 1
0
        /// <summary>
        /// Computes the duration and tracks the respective telemetry item on dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !this.isDisposed)
            {
                // We need to compare the operation id and name of telemetry with operation id and name of current call context before tracking it
                // to make sure that the customer is tracking the right telemetry.
                lock (this)
                {
                    if (!this.isDisposed)
                    {
                        var operationTelemetry = this.Telemetry;

                        var currentOperationContext = CallContextHelpers.GetCurrentOperationContext();
                        if (currentOperationContext == null || operationTelemetry.Id != currentOperationContext.ParentOperationId ||
                            operationTelemetry.Context.Operation.Name != currentOperationContext.RootOperationName)
                        {
                            CoreEventSource.Log.InvalidOperationToStopError();
                            return;
                        }

                        operationTelemetry.Stop();
                        CallContextHelpers.RestoreOperationContext(this.ParentContext);
                        this.telemetryClient.Track(operationTelemetry);
                    }

                    this.isDisposed = true;
                }
            }
        }
        /// <summary>
        /// Computes the duration and tracks the respective telemetry item on dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !this.isDisposed)
            {
                // We need to compare the operation id and name of telemetry with operation id and name of current call context before tracking it
                // to make sure that the customer is tracking the right telemetry.
                lock (this)
                {
                    if (!this.isDisposed)
                    {
                        var operationTelemetry = this.Telemetry;
                        operationTelemetry.Stop();

                        bool isActivityAvailable = false;
                        isActivityAvailable = ActivityExtensions.TryRun(() =>
                        {
                            var currentActivity = Activity.Current;
                            if (currentActivity == null || operationTelemetry.Id != currentActivity.Id)
                            {
                                CoreEventSource.Log.InvalidOperationToStopError(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Telemetry Id '{0}' does not match current Activity '{1}'",
                                        operationTelemetry.Id,
                                        currentActivity?.Id));
                                return;
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            currentActivity.Stop();
                        });

                        if (!isActivityAvailable)
                        {
                            var currentOperationContext = CallContextHelpers.GetCurrentOperationContext();
                            if (currentOperationContext == null || operationTelemetry.Id != currentOperationContext.ParentOperationId)
                            {
                                CoreEventSource.Log.InvalidOperationToStopError(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Telemetry Id '{0}' does not match current context '{1}'",
                                        operationTelemetry.Id,
                                        currentOperationContext?.ParentOperationId));
                                return;
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            CallContextHelpers.RestoreOperationContext(this.ParentContext);
                        }
                    }

                    this.isDisposed = true;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Computes the duration and tracks the respective telemetry item on dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !this.isDisposed)
            {
                // We need to compare the operation id and name of telemetry with operation id and name of current call context before tracking it
                // to make sure that the customer is tracking the right telemetry.
                lock (this)
                {
                    if (!this.isDisposed)
                    {
                        var  operationTelemetry  = this.Telemetry;
                        bool isActivityAvailable = false;
#if !NET40
                        isActivityAvailable = ActivityExtensions.TryRun(() =>
                        {
                            var currentActivity = Activity.Current;
                            if (currentActivity == null || operationTelemetry.Id != currentActivity.Id ||
                                operationTelemetry.Context.Operation.Name != currentActivity.GetOperationName())
                            {
                                CoreEventSource.Log.InvalidOperationToStopError();
                                return;
                            }

                            currentActivity.Stop();
                        });
#endif
                        if (!isActivityAvailable)
                        {
                            var currentOperationContext = CallContextHelpers.GetCurrentOperationContext();
                            if (currentOperationContext == null || operationTelemetry.Id != currentOperationContext.ParentOperationId ||
                                operationTelemetry.Context.Operation.Name != currentOperationContext.RootOperationName)
                            {
                                CoreEventSource.Log.InvalidOperationToStopError();
                                return;
                            }

                            CallContextHelpers.RestoreOperationContext(this.ParentContext);
                        }

                        operationTelemetry.Stop();

                        this.telemetryClient.Track(operationTelemetry);
                    }

                    this.isDisposed = true;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Computes the duration and tracks the respective telemetry item on dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !this.isDisposed)
            {
                // We need to compare the operation id and name of telemetry with operation id and name of current call context before tracking it
                // to make sure that the customer is tracking the right telemetry.
                lock (this)
                {
                    if (!this.isDisposed)
                    {
                        var operationTelemetry = this.Telemetry;
                        operationTelemetry.Stop();

                        bool isActivityAvailable = false;
                        isActivityAvailable = ActivityExtensions.TryRun(() =>
                        {
                            var currentActivity = Activity.Current;
                            if (currentActivity == null ||
                                (Activity.DefaultIdFormat != ActivityIdFormat.W3C && operationTelemetry.Id != currentActivity.Id) ||
                                (Activity.DefaultIdFormat == ActivityIdFormat.W3C && operationTelemetry.Id != currentActivity.SpanId.ToHexString()))
                            {
                                // W3COperationCorrelationTelemetryInitializer changes Id
                                // but keeps an original one in 'ai_legacyRequestId' property

                                if (!operationTelemetry.Properties.TryGetValue("ai_legacyRequestId", out var legacyId) ||
                                    legacyId != currentActivity?.Id)
                                {
                                    // this is for internal error reporting
                                    CoreEventSource.Log.InvalidOperationToStopError();

                                    // this are details with unique ids for debugging
                                    CoreEventSource.Log.InvalidOperationToStopDetails(
                                        string.Format(
                                            CultureInfo.InvariantCulture,
                                            "Telemetry Id '{0}' does not match current Activity '{1}'",
                                            operationTelemetry.Id,
                                            currentActivity?.Id));

                                    return;
                                }
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            currentActivity?.Stop();

                            if (this.originalActivity != null &&
                                Activity.Current != this.originalActivity &&
                                this.originalActivity is Activity activity)
                            {
                                Activity.Current = activity;
                            }
                        });

                        if (!isActivityAvailable)
                        {
                            var currentOperationContext = CallContextHelpers.GetCurrentOperationContext();
                            if (currentOperationContext == null || operationTelemetry.Id != currentOperationContext.ParentOperationId)
                            {
                                // this is for internal error reporting
                                CoreEventSource.Log.InvalidOperationToStopError();

                                // this are details with unique ids for debugging
                                CoreEventSource.Log.InvalidOperationToStopDetails(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Telemetry Id '{0}' does not match current Activity '{1}'",
                                        operationTelemetry.Id,
                                        currentOperationContext?.ParentOperationId));
                                return;
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            CallContextHelpers.RestoreOperationContext(this.ParentContext);
                        }
                    }

                    this.isDisposed = true;
                }
            }
        }