コード例 #1
0
        internal Activity(
            string activityName,
            ActivityLogLevel logLevel,
            string activityId,
            Activity root,
            Activity parent,
            string distributedTracingParentOperationId,
            string distributedTracingGlobalOperationId,
            LogicalExecutionStack logicalExecutionStack)
        {
            this.LogicalExecutionStack = Util.EnsureNotNull(logicalExecutionStack, nameof(logicalExecutionStack));

            this.Name     = Util.EnsureNotNullOrEmpty(activityName, nameof(activityName));
            this.LogLevel = logLevel;
            this.Status   = ActivityStatus.Running;

            this.ActivityId     = Util.EnsureNotNullOrEmpty(activityId, nameof(activityId));
            this.RootActivity   = root ?? this;
            this.ParentActivity = parent;

            this.DistributedTracingParentOperationId = distributedTracingParentOperationId;  // may be null
            this.DistributedTracingGlobalOperationId = distributedTracingGlobalOperationId;  // may be null

            this.StartTime = DateTimeOffset.Now;
            this.EndTime   = default(DateTimeOffset);

            this.FaultException       = null;
            this.FaultMessage         = null;
            this.InitialFaultActivity = null;
            this.FaultId = null;
        }
コード例 #2
0
        public Activity StartNewActivity(string activityName, ActivityLogLevel logLevel)
        {
            activityName = ValidateActivityName(activityName);

            string parentOperationId, globalOperationId;

            TryGetDistributedTracingContext(out parentOperationId, out globalOperationId);

            LogicalExecutionStack currentLogicalStack = GetOrCreateCurrentLogicalStack();

            lock (currentLogicalStack)
            {
                Activity parent = currentLogicalStack.Peek(); // may be null

                var activity = new Activity(
                    activityName,
                    logLevel,
                    Util.CreateRandomId(),
                    parent?.RootActivity,
                    parent,
                    parentOperationId,
                    globalOperationId,
                    currentLogicalStack);

                currentLogicalStack.Push(activity);

                return(activity);
            }
        }
コード例 #3
0
        private void FailActivity(Activity activity, Exception exception, string failureMessage)
        {
            DateTimeOffset now = DateTimeOffset.Now;

            LogicalExecutionStack logicalStack = _logicalExecutionThread.Value;

            if (logicalStack == null)
            {
                throw new InvalidOperationException(ExceptionMsg_ActivityStartEndMismatch);
            }

            if (logicalStack != activity.LogicalExecutionStack)
            {
                throw new InvalidOperationException(ExceptionMsg_ActivityStacksMismatch);
            }

            var faultedActivities = new List <Activity>();

            lock (logicalStack)
            {
                Activity poppedActivity;
                do
                {
                    poppedActivity = logicalStack.Pop();
                    faultedActivities.Add(poppedActivity);
                }while (poppedActivity != activity);

                if (logicalStack.Count == 0)
                {
                    _logicalExecutionThread.Value = logicalStack.ParentStack;
                }
            }

            string faultId = Util.CreateRandomId();

            for (int i = 0; i < faultedActivities.Count; i++)
            {
                Activity    faultedActivity     = faultedActivities[i];
                IDisposable associatedOperation = faultedActivity.AssociatedOperation;

                faultedActivity.TransitionToFaulted(exception, now, failureMessage, activity, faultId);
                _pipeline.ProcessAndSend(faultedActivity);

                if (associatedOperation != null)
                {
                    var telemetry = associatedOperation as OperationTelemetry;
                    if (telemetry != null)
                    {
                        telemetry.Success = true;
                    }

                    associatedOperation.Dispose();
                }
            }
        }
コード例 #4
0
        private Activity PeekCurrentActivityToUseProperties()
        {
            LogicalExecutionStack logicalStack = _logicalExecutionThread.Value;

            if (logicalStack == null)
            {
                throw new InvalidOperationException(ExceptionMsg_CannotAccessActivityToUseProperties);
            }

            lock (logicalStack)
            {
                Activity activity = logicalStack.Peek();
                return(activity);
            }
        }
コード例 #5
0
        private void FailCurrentActivity(Exception exception, string failureMessage)
        {
            DateTimeOffset now = DateTimeOffset.Now;

            LogicalExecutionStack logicalStack = _logicalExecutionThread.Value;

            if (logicalStack == null)
            {
                throw new InvalidOperationException(ExceptionMsg_ActivityStartEndMismatch);
            }

            Activity activity;

            lock (logicalStack)
            {
                activity = logicalStack.Pop();

                if (logicalStack.Count == 0)
                {
                    _logicalExecutionThread.Value = logicalStack.ParentStack;
                }
            }

            IDisposable associatedOperation = activity.AssociatedOperation;

            activity.TransitionToFaulted(exception, now, failureMessage, activity, Util.CreateRandomId());
            _pipeline.ProcessAndSend(activity);

            if (associatedOperation != null)
            {
                var telemetry = associatedOperation as OperationTelemetry;
                if (telemetry != null)
                {
                    telemetry.Success = true;
                }

                associatedOperation.Dispose();
            }
        }
コード例 #6
0
        public void CompleteActivity()
        {
            DateTimeOffset now = DateTimeOffset.Now;

            LogicalExecutionStack logicalStack = _logicalExecutionThread.Value;

            if (logicalStack == null)
            {
                throw new InvalidOperationException(ExceptionMsg_ActivityStartEndMismatch);
            }

            Activity activity;

            lock (logicalStack)
            {
                activity = logicalStack.Pop();

                if (logicalStack.Count == 0)
                {
                    _logicalExecutionThread.Value = logicalStack.ParentStack;
                }
            }

            IDisposable associatedOperation = activity.AssociatedOperation;

            activity.TransitionToComplete(now);
            _pipeline.ProcessAndSend(activity);

            if (associatedOperation != null)
            {
                var telemetry = associatedOperation as OperationTelemetry;
                if (telemetry != null)
                {
                    telemetry.Success = true;
                }

                associatedOperation.Dispose();
            }
        }
コード例 #7
0
        private LogicalExecutionStack GetOrCreateCurrentLogicalStack()
        {
            LogicalExecutionStack currentLogicalStack = _logicalExecutionThread.Value;

            if (currentLogicalStack != null)
            {
                return(currentLogicalStack);
            }

            LogicalExecutionStack newLogicalStack = new LogicalExecutionStack(parentStack: null);

            lock (_logicalExecutionThread)
            {
                currentLogicalStack = _logicalExecutionThread.Value;
                if (currentLogicalStack != null)
                {
                    return(currentLogicalStack);
                }

                _logicalExecutionThread.Value = newLogicalStack;
                return(newLogicalStack);
            }
        }
コード例 #8
0
        public Activity StartNewLogicalActivityThread(string activityName, ActivityLogLevel logLevel)
        {
            activityName = ValidateActivityName(activityName);

            string parentOperationId, globalOperationId;

            TryGetDistributedTracingContext(out parentOperationId, out globalOperationId);

            LogicalExecutionStack currentLogicalStack = _logicalExecutionThread.Value;

            object lockScope = currentLogicalStack;

            lockScope = lockScope ?? _logicalExecutionThread;

            lock (lockScope)
            {
                Activity parent = currentLogicalStack?.Peek();  // may be null

                var newLogicalStack = new LogicalExecutionStack(currentLogicalStack);

                var activity = new Activity(
                    activityName,
                    logLevel,
                    Util.CreateRandomId(),
                    parent?.RootActivity,
                    parent,
                    parentOperationId,
                    globalOperationId,
                    newLogicalStack);

                newLogicalStack.Push(activity);

                _logicalExecutionThread.Value = newLogicalStack;

                return(activity);
            }
        }
コード例 #9
0
 public LogicalExecutionStack(LogicalExecutionStack parentStack)
 {
     this.ParentStack = parentStack;
 }