/// <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; } } } }
public void StartDependencyTrackingAddsOperationContextStoreToCallContext() { Assert.IsNull(AsyncLocalHelpers.GetCurrentOperationContext()); var operation = this.telemetryClient.StartOperation <DependencyTelemetry>(null); Assert.IsNotNull(AsyncLocalHelpers.GetCurrentOperationContext()); 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); }
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); }
/// <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 StartDependencyTrackingStoresTheArgumentOperationNameInContext() { var operation = this.telemetryClient.StartOperation <DependencyTelemetry>("TestOperationName"); Assert.AreEqual("TestOperationName", AsyncLocalHelpers.GetCurrentOperationContext().RootOperationName); }