Ejemplo n.º 1
0
        /// <summary>
        /// Initializes/Adds operation id to the existing telemetry item.
        /// </summary>
        /// <param name="telemetryItem">Target telemetry item to add operation id.</param>
        public void Initialize(ITelemetry telemetryItem)
        {
            var itemContext = telemetryItem.Context.Operation;

            if (string.IsNullOrEmpty(itemContext.ParentId) || string.IsNullOrEmpty(itemContext.Id) || string.IsNullOrEmpty(itemContext.Name))
            {
                var parentContext = AsyncLocalHelpers.GetCurrentOperationContext();
                if (parentContext != null)
                {
                    if (string.IsNullOrEmpty(itemContext.ParentId) &&
                        !string.IsNullOrEmpty(parentContext.ParentOperationId))
                    {
                        itemContext.ParentId = parentContext.ParentOperationId;
                    }

                    if (string.IsNullOrEmpty(itemContext.Id) &&
                        !string.IsNullOrEmpty(parentContext.RootOperationId))
                    {
                        itemContext.Id = parentContext.RootOperationId;
                    }

                    if (string.IsNullOrEmpty(itemContext.Name) &&
                        !string.IsNullOrEmpty(parentContext.RootOperationName))
                    {
                        itemContext.Name = parentContext.RootOperationName;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void StartDependencyTrackingCreatesADependencyTelemetryItemWithTimeStamp()
        {
            var operation = this.telemetryClient.StartOperation <DependencyTelemetry>(operationName: null);

            Assert.AreNotEqual(operation.Telemetry.Timestamp, DateTimeOffset.MinValue);

            AsyncLocalHelpers.SaveOperationContext(null);
        }
        public void InitializerDoesNotFailOnNullContextStore()
        {
            var telemetry = new DependencyTelemetry();

            AsyncLocalHelpers.SaveOperationContext(null);
            (new OperationCorrelationTelemetryInitializer()).Initialize(telemetry);
            Assert.IsNull(telemetry.Context.Operation.ParentId);
        }
        public void StartDependencyTrackingAddsOperationContextStoreToCallContext()
        {
            Assert.IsNull(AsyncLocalHelpers.GetCurrentOperationContext());
            var operation = this.telemetryClient.StartOperation <DependencyTelemetry>(null);

            Assert.IsNotNull(AsyncLocalHelpers.GetCurrentOperationContext());

            AsyncLocalHelpers.SaveOperationContext(null);
        }
        public void StartDependencyTrackingReturnsOperationWithSameTelemetryItem()
        {
            var operation = this.telemetryClient.StartOperation <DependencyTelemetry>(null);

            Assert.IsNotNull(operation);
            Assert.IsNotNull(operation.Telemetry);

            AsyncLocalHelpers.SaveOperationContext(null);
        }
        public void UsingSendsTelemetryAndDisposesOperationItem()
        {
            Assert.IsNull(AsyncLocalHelpers.GetCurrentOperationContext());
            using (var operation = this.telemetryClient.StartOperation <DependencyTelemetry>(null))
            {
            }

            Assert.IsNull(AsyncLocalHelpers.GetCurrentOperationContext());
            Assert.AreEqual(1, this.sendItems.Count);
            AsyncLocalHelpers.SaveOperationContext(null);
        }
Ejemplo n.º 7
0
        public void UsingWithStopOperationSendsTelemetryAndDisposesOperationItemOnlyOnce()
        {
            Assert.IsNull(AsyncLocalHelpers.GetCurrentOperationContext());
            using (var operation = this.telemetryClient.StartOperation <DependencyTelemetry>(operationName: null))
            {
                this.telemetryClient.StopOperation(operation);
            }

            Assert.IsNull(AsyncLocalHelpers.GetCurrentOperationContext());
            Assert.AreEqual(1, this.sendItems.Count);
        }
        public void TelemetryContextIsUpdatedWithOperationNameForDependencyTelemetry()
        {
            AsyncLocalHelpers.SaveOperationContext(new OperationContextForAsyncLocal {
                RootOperationName = "OperationName"
            });
            var telemetry = new DependencyTelemetry();

            (new OperationCorrelationTelemetryInitializer()).Initialize(telemetry);
            Assert.AreEqual(telemetry.Context.Operation.Name, "OperationName");
            AsyncLocalHelpers.SaveOperationContext(null);
        }
        public void TelemetryContextIsUpdatedWithOperationIdForDependencyTelemetry()
        {
            AsyncLocalHelpers.SaveOperationContext(new OperationContextForAsyncLocal {
                ParentOperationId = "ParentOperationId"
            });
            var telemetry = new DependencyTelemetry();

            (new OperationCorrelationTelemetryInitializer()).Initialize(telemetry);
            Assert.AreEqual("ParentOperationId", telemetry.Context.Operation.ParentId);
            AsyncLocalHelpers.SaveOperationContext(null);
        }
        public void TestInitialize()
        {
            var configuration = new TelemetryConfiguration();

            this.sendItems = new List <ITelemetry>();
            configuration.TelemetryChannel = new StubTelemetryChannel {
                OnSend = item => this.sendItems.Add(item)
            };
            configuration.InstrumentationKey = Guid.NewGuid().ToString();
            this.telemetryClient             = new TelemetryClient(configuration);
            AsyncLocalHelpers.SaveOperationContext(null);
        }
        public void InitializeDoesNotUpdateOperationNameIfItExists()
        {
            AsyncLocalHelpers.SaveOperationContext(new OperationContextForAsyncLocal {
                RootOperationName = "OperationName"
            });
            var telemetry = new DependencyTelemetry();

            telemetry.Context.Operation.Name = "OldOperationName";
            (new OperationCorrelationTelemetryInitializer()).Initialize(telemetry);
            Assert.AreEqual(telemetry.Context.Operation.Name, "OldOperationName");
            AsyncLocalHelpers.SaveOperationContext(null);
        }
        public void InitializeDoesNotUpdateOperationIdIfItExists()
        {
            AsyncLocalHelpers.SaveOperationContext(new OperationContextForAsyncLocal {
                ParentOperationId = "ParentOperationId"
            });
            var telemetry = new DependencyTelemetry();

            telemetry.Context.Operation.ParentId = "OldParentOperationId";
            (new OperationCorrelationTelemetryInitializer()).Initialize(telemetry);
            Assert.AreEqual("OldParentOperationId", telemetry.Context.Operation.ParentId);
            AsyncLocalHelpers.SaveOperationContext(null);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Creates an operation object with a given telemetry item.
        /// </summary>
        /// <typeparam name="T">Type of the telemetry item.</typeparam>
        /// <param name="telemetryClient">Telemetry client object.</param>
        /// <param name="operationTelemetry">Operation to start.</param>
        /// <returns>Operation item object with a new telemetry item having current start time and timestamp.</returns>
        public static IOperationHolder <T> StartOperation <T>(this TelemetryClient telemetryClient, T operationTelemetry) where T : OperationTelemetry
        {
            if (telemetryClient == null)
            {
                throw new ArgumentNullException("Telemetry client cannot be null.");
            }

            if (operationTelemetry == null)
            {
                throw new ArgumentNullException("operationTelemetry cannot be null.");
            }

            var operationHolder = new AsyncLocalBasedOperationHolder <T>(telemetryClient, operationTelemetry)
            {
                // Parent context store is assigned to operation that is used to restore call context.
                ParentContext = AsyncLocalHelpers.GetCurrentOperationContext()
            };

            telemetryClient.Initialize(operationTelemetry);

            // Initialize operation id if it wasn't initialized by telemetry initializers
            if (string.IsNullOrEmpty(operationTelemetry.Id))
            {
                operationTelemetry.GenerateOperationId();
            }

            // If the operation is not executing in the context of any other operation
            // set its name and id as a context (root) operation name and id
            if (string.IsNullOrEmpty(operationTelemetry.Context.Operation.Id))
            {
                operationTelemetry.Context.Operation.Id = operationTelemetry.Id;
            }

            if (string.IsNullOrEmpty(operationTelemetry.Context.Operation.Name))
            {
                operationTelemetry.Context.Operation.Name = operationTelemetry.Name;
            }

            operationTelemetry.Start();

            // Update the call context to store certain fields that can be used for subsequent operations.
            var operationContext = new OperationContextForAsyncLocal();

            operationContext.ParentOperationId = operationTelemetry.Id;
            operationContext.RootOperationId   = operationTelemetry.Context.Operation.Id;
            operationContext.RootOperationName = operationTelemetry.Context.Operation.Name;
            AsyncLocalHelpers.SaveOperationContext(operationContext);

            return(operationHolder);
        }
        public void StartDependencyTrackingHandlesMultipleContextStoresInCallContext()
        {
            var operation          = this.telemetryClient.StartOperation <DependencyTelemetry>("OperationName") as AsyncLocalBasedOperationHolder <DependencyTelemetry>;
            var parentContextStore = AsyncLocalHelpers.GetCurrentOperationContext();

            Assert.AreEqual(operation.Telemetry.Context.Operation.Id, parentContextStore.ParentOperationId);
            Assert.AreEqual(operation.Telemetry.Context.Operation.Name, parentContextStore.RootOperationName);

            var childOperation    = this.telemetryClient.StartOperation <DependencyTelemetry>("OperationName") as AsyncLocalBasedOperationHolder <DependencyTelemetry>;
            var childContextStore = AsyncLocalHelpers.GetCurrentOperationContext();

            Assert.AreEqual(childOperation.Telemetry.Context.Operation.Id, childContextStore.ParentOperationId);
            Assert.AreEqual(childOperation.Telemetry.Context.Operation.Name, childContextStore.RootOperationName);

            Assert.IsNull(operation.ParentContext);
            Assert.AreEqual(parentContextStore, childOperation.ParentContext);

            this.telemetryClient.StopOperation(childOperation);
            Assert.AreEqual(parentContextStore, AsyncLocalHelpers.GetCurrentOperationContext());
            this.telemetryClient.StopOperation(operation);
            Assert.IsNull(AsyncLocalHelpers.GetCurrentOperationContext());
        }
 public void TestCleanup()
 {
     AsyncLocalHelpers.SaveOperationContext(null);
 }
        public void StartDependencyTrackingStoresTheArgumentOperationNameInContext()
        {
            var operation = this.telemetryClient.StartOperation <DependencyTelemetry>("TestOperationName");

            Assert.AreEqual("TestOperationName", AsyncLocalHelpers.GetCurrentOperationContext().RootOperationName);
        }