private RequestTelemetry InitializeRequestTelemetryFromActivity(HttpContext httpContext, Activity activity, bool isActivityCreatedFromRequestIdHeader, long timestamp)
        {
            var requestTelemetry = new RequestTelemetry();

            StringValues standardParentId;

            if (isActivityCreatedFromRequestIdHeader)
            {
                requestTelemetry.Context.Operation.ParentId = activity.ParentId;

                foreach (var prop in activity.Baggage)
                {
                    if (!requestTelemetry.Context.Properties.ContainsKey(prop.Key))
                    {
                        requestTelemetry.Context.Properties[prop.Key] = prop.Value;
                    }
                }
            }
            else if (httpContext.Request.Headers.TryGetValue(RequestResponseHeaders.StandardParentIdHeader, out standardParentId))
            {
                standardParentId = StringUtilities.EnforceMaxLength(standardParentId, InjectionGuardConstants.RequestHeaderMaxLength);
                requestTelemetry.Context.Operation.ParentId = standardParentId;
            }

            requestTelemetry.Id = activity.Id;
            requestTelemetry.Context.Operation.Id = activity.RootId;

            this.client.Initialize(requestTelemetry);
            requestTelemetry.Start(timestamp);
            httpContext.Features.Set(requestTelemetry);

            return(requestTelemetry);
        }
        private static RequestTelemetry SetUpRequestActivity(
            string requestId,
            string requestName,
            IEnumerable <KeyValuePair <string, string> > correlationContext,
            string activityName)
        {
            var activity = new Activity(activityName);

            activity.SetParentId(requestId);
            RequestTelemetry rt = new RequestTelemetry();

            rt.Context.Operation.ParentId = requestId;

            foreach (KeyValuePair <string, string> pair in correlationContext)
            {
                activity.AddBaggage(pair.Key, pair.Value);
            }

            activity.Start();

            rt.Id = activity.Id;
            rt.Context.Operation.Id = activity.RootId;
            rt.Name = requestName;

            telemetryClient.Initialize(rt);
            rt.Start(Stopwatch.GetTimestamp());
            return(rt);
        }
Exemplo n.º 3
0
        public async static Task DequeueOne([QueueTrigger("myqueue-items", Connection = "QueueConnectionString")]
                                            Message myQueueItem
                                            , [Queue("secondqueue-items", Connection = "QueueConnectionString")]
                                            IAsyncCollector <Message> payloads, ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
            var telemetry = new RequestTelemetry
            {
                Name = "Dequeue " + "myqueue-items"
            };

            telemetry.Start();
            telemetry.Context.Operation.Id       = myQueueItem.RootId;
            telemetry.Context.Operation.ParentId = myQueueItem.ParentId;
            telemetry.Stop();
            telemetryClient.Track(telemetry);

            var enqueueOperation =
                telemetryClient.StartOperation <DependencyTelemetry>("Enqueue " + "secondqueue-items");

            enqueueOperation.Telemetry.Type = "Queue";
            enqueueOperation.Telemetry.Data = "Enqueue " + "secondqueue-items";
            enqueueOperation.Telemetry.Context.Operation.Id       = telemetry.Context.Operation.Id;
            enqueueOperation.Telemetry.Context.Operation.ParentId = telemetry.Id;
            var message = new Message
            {
                RootId   = enqueueOperation.Telemetry.Context.Operation.Id,
                ParentId = enqueueOperation.Telemetry.Id,
                Payload  = "hello",
            };
            await payloads.AddAsync(message);

            telemetryClient.StopOperation(enqueueOperation);
        }
Exemplo n.º 4
0
        private RequestTelemetry InitializeRequestTelemetry(HttpContext httpContext, Activity activity, bool isActivityCreatedFromRequestIdHeader, long timestamp)
        {
            var requestTelemetry = new RequestTelemetry();

            if (!this.enableW3CHeaders)
            {
                requestTelemetry.Context.Operation.Id = activity.RootId;
                requestTelemetry.Id = activity.Id;
            }

            foreach (var prop in activity.Baggage)
            {
                if (!requestTelemetry.Properties.ContainsKey(prop.Key))
                {
                    requestTelemetry.Properties[prop.Key] = prop.Value;
                }
            }

            this.client.Initialize(requestTelemetry);

            requestTelemetry.Source = GetAppIdFromRequestHeader(httpContext.Request.Headers, requestTelemetry.Context.InstrumentationKey);

            requestTelemetry.Start(timestamp);
            httpContext.Features.Set(requestTelemetry);

            return(requestTelemetry);
        }
Exemplo n.º 5
0
        void IWcfTelemetryModule.OnBeginRequest(IOperationContext operation)
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }
            if (telemetryClient == null)
            {
                return;
            }

            RequestTelemetry telemetry = operation.Request;

            telemetry.Start();

            telemetry.Url  = operation.EndpointUri;
            telemetry.Name = operation.OperationName;

            var httpHeaders = operation.GetHttpRequestHeaders();

            if (httpHeaders != null)
            {
                telemetry.HttpMethod = httpHeaders.Method;
                if (operation.ToHeader != null)
                {
                    // overwrite it for WebHttpBinding requests
                    telemetry.Url = operation.ToHeader;
                }
            }
        }
        private static void StartTelemetryIfFunctionInvocation(IDictionary <string, object> stateValues)
        {
            if (stateValues == null)
            {
                return;
            }

            string functionName         = stateValues.GetValueOrDefault <string>(ScopeKeys.FunctionName);
            string functionInvocationId = stateValues.GetValueOrDefault <string>(ScopeKeys.FunctionInvocationId);
            string eventName            = stateValues.GetValueOrDefault <string>(ScopeKeys.Event);

            // If we have the invocation id, function name, and event, we know it's a new function. That means
            // that we want to start a new operation and let App Insights track it for us.
            if (!string.IsNullOrEmpty(functionName) &&
                !string.IsNullOrEmpty(functionInvocationId) &&
                eventName == LogConstants.FunctionStartEvent)
            {
                RequestTelemetry request = new RequestTelemetry()
                {
                    Id   = functionInvocationId,
                    Name = functionName
                };

                // We'll need to store this operation context so we can stop it when the function completes
                request.Start();
                stateValues[OperationContext] = request;
            }
        }
Exemplo n.º 7
0
        private RequestTelemetry InitializeRequestTelemetry(HttpContext httpContext, Activity activity, long timestamp, string legacyRootId = null)
        {
            var requestTelemetry = new RequestTelemetry();

            if (activity.IdFormat == ActivityIdFormat.W3C)
            {
                var traceId = activity.TraceId.ToHexString();
                requestTelemetry.Id = activity.SpanId.ToHexString();
                requestTelemetry.Context.Operation.Id = traceId;
                AspNetCoreEventSource.Instance.RequestTelemetryCreated("W3C", requestTelemetry.Id, traceId);
            }
            else
            {
                requestTelemetry.Context.Operation.Id = activity.RootId;
                requestTelemetry.Id = activity.Id;
                AspNetCoreEventSource.Instance.RequestTelemetryCreated("Hierarchical", requestTelemetry.Id, requestTelemetry.Context.Operation.Id);
            }

            if (this.proactiveSamplingEnabled &&
                !activity.Recorded &&
                this.configuration != null &&
                !string.IsNullOrEmpty(requestTelemetry.Context.Operation.Id) &&
                SamplingScoreGenerator.GetSamplingScore(requestTelemetry.Context.Operation.Id) >= this.configuration.GetLastObservedSamplingPercentage(requestTelemetry.ItemTypeFlag))
            {
                requestTelemetry.ProactiveSamplingDecision = SamplingDecision.SampledOut;
                AspNetCoreEventSource.Instance.TelemetryItemWasSampledOutAtHead(requestTelemetry.Context.Operation.Id);
            }

            //// When the item is proactively sampled out, we can avoid heavy operations that do not have known dependency later in the pipeline.
            //// We mostly exclude operations that were deemed heavy as per the corresponding profiler trace of this code path.

            if (requestTelemetry.ProactiveSamplingDecision != SamplingDecision.SampledOut)
            {
                foreach (var prop in activity.Baggage)
                {
                    if (!requestTelemetry.Properties.ContainsKey(prop.Key))
                    {
                        requestTelemetry.Properties[prop.Key] = prop.Value;
                    }
                }

                if (!string.IsNullOrEmpty(legacyRootId))
                {
                    requestTelemetry.Properties[LegacyRootIdProperty] = legacyRootId;
                }
            }

            this.client.InitializeInstrumentationKey(requestTelemetry);
            requestTelemetry.Source = this.GetAppIdFromRequestHeader(httpContext.Request.Headers, requestTelemetry.Context.InstrumentationKey);

            requestTelemetry.Start(timestamp);
            httpContext.Features.Set(requestTelemetry);

            return(requestTelemetry);
        }
        private RequestTelemetry InitializeRequestTelemetry(HttpContext httpContext, Activity activity, bool isActivityCreatedFromRequestIdHeader, long timestamp)
        {
            var requestTelemetry = new RequestTelemetry();

            StringValues standardParentId;

            if (isActivityCreatedFromRequestIdHeader)
            {
                requestTelemetry.Context.Operation.ParentId = activity.ParentId;

                foreach (var prop in activity.Baggage)
                {
                    if (!requestTelemetry.Context.Properties.ContainsKey(prop.Key))
                    {
                        requestTelemetry.Context.Properties[prop.Key] = prop.Value;
                    }
                }
            }
            else if (httpContext.Request.Headers.TryGetValue(RequestResponseHeaders.StandardParentIdHeader, out standardParentId))
            {
                standardParentId = StringUtilities.EnforceMaxLength(standardParentId, InjectionGuardConstants.RequestHeaderMaxLength);
                requestTelemetry.Context.Operation.ParentId = standardParentId;
            }

            requestTelemetry.Id = activity.Id;
            requestTelemetry.Context.Operation.Id = activity.RootId;

            this.client.Initialize(requestTelemetry);

            // set Source
            string headerCorrelationId = HttpHeadersUtilities.GetRequestContextKeyValue(httpContext.Request.Headers, RequestResponseHeaders.RequestContextSourceKey);

            string applicationId = null;

            // If the source header is present on the incoming request, and it is an external component (not the same ikey as the one used by the current component), populate the source field.
            if (!string.IsNullOrEmpty(headerCorrelationId))
            {
                headerCorrelationId = StringUtilities.EnforceMaxLength(headerCorrelationId, InjectionGuardConstants.AppIdMaxLengeth);
                if (string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey))
                {
                    requestTelemetry.Source = headerCorrelationId;
                }

                else if ((this.applicationIdProvider?.TryGetApplicationId(requestTelemetry.Context.InstrumentationKey, out applicationId) ?? false) &&
                         applicationId != headerCorrelationId)
                {
                    requestTelemetry.Source = headerCorrelationId;
                }
            }

            requestTelemetry.Start(timestamp);
            httpContext.Features.Set(requestTelemetry);

            return(requestTelemetry);
        }
Exemplo n.º 9
0
        public static RequestTelemetry CreateAndStartRequestTelemetry(this Activity activity)
        {
            var requestTelemetry = new RequestTelemetry {
                Name = activity.OperationName
            };

            requestTelemetry.Id = activity.Id;
            requestTelemetry.Context.Operation.Id       = activity.RootId;
            requestTelemetry.Context.Operation.ParentId = activity.ParentId;
            requestTelemetry.Start();
            return(requestTelemetry);
        }
Exemplo n.º 10
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [Queue("control-queue-ai", Connection = "ConnectionString")] IAsyncCollector <Context> contexts,
            ILogger log)
        {
            log.LogInformation("Accept the request. Incoming request doesn't have correlation info.");
            var requestActivity = new Activity("ActivitySpike: HttpTrigger Request");
            var current         = Activity.Current; // You can refer the current Activity. That is automatically created by HttpTrigger.

            // No parent from the start
            requestActivity.Start();

            var requestTelemetry = new RequestTelemetry {
                Name = "ActivitySpike: HttpTrigger Request"
            };

            requestTelemetry.SetActivity(requestActivity);

            requestTelemetry.Start();

            requestTelemetry.Stop();
            client.Track(requestTelemetry);

            var dependencyActivity = new Activity("HttpTrigger Dependency Queue output");

            dependencyActivity.SetParentId(requestActivity.Id); // You can omit this code
            dependencyActivity.Start();
            var context = new Context()
            {
                ActivityId = dependencyActivity.Id,
                ParentId   = dependencyActivity.ParentId,
            };
            var dependencyTelemetry = new DependencyTelemetry {
                Name = "ActivitySpike:: Enqueue"
            };

            dependencyTelemetry.SetActivity(dependencyActivity);
            dependencyTelemetry.Start();
            client.StartOperation(requestTelemetry);

            await contexts.AddAsync(context);

            dependencyActivity.Stop();

            requestActivity.Stop();

            dependencyTelemetry.Stop();
            client.Track(dependencyTelemetry);

            return(new OkObjectResult($"Orchestration has been started."));
        }
Exemplo n.º 11
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [Queue("control-queue-ai", Connection = "ConnectionString")] IAsyncCollector <TraceContext> contexts,
            ILogger log)
        {
            log.LogInformation("Accept the request. Incoming request doesn't have correlation info.");
            var requestActivity = new Activity("W3CActivitySpike: HttpTrigger Request");

            requestActivity.GenerateW3CContext();
            // No parent from the start
            requestActivity.Start();
            var requestTelemetry = new RequestTelemetry {
                Name = "W3CActivitySpike: HttpTrigger Request"
            };

            requestTelemetry.SetActivity(requestActivity);

            requestTelemetry.Start();

            requestTelemetry.Stop();
            client.Track(requestTelemetry);

            var dependencyActivity = new Activity("HttpTrigger Dependency Queue output");

            dependencyActivity.SetTraceparent(requestActivity.GetTraceparent());
            dependencyActivity.SetTracestate(requestActivity.GetTracestate());
            // dependencyActivity.SetParentId(requestActivity.Id); // maybe not necessary
            dependencyActivity.Start();
            var context = dependencyActivity.CreateTraceContext();

            var dependencyTelemetry = new DependencyTelemetry {
                Name = "W3CActivitySpike:: Enqueue"
            };

            dependencyTelemetry.SetActivity(dependencyActivity);
            dependencyTelemetry.Start();


            await contexts.AddAsync(context);

            dependencyActivity.Stop();

            requestActivity.Stop();

            dependencyTelemetry.Stop();
            client.Track(dependencyTelemetry);

            return(new OkObjectResult($"Orchestration has been started."));
        }
Exemplo n.º 12
0
        public static void DequeueTwo([QueueTrigger("secondqueue-items", Connection = "QueueConnectionString")]
                                      Message myQueueItem, ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
            var telemetry = new RequestTelemetry
            {
                Name = "Dequeue " + "secondqueue-items"
            };

            telemetry.Start();
            telemetry.Context.Operation.Id       = myQueueItem.RootId;
            telemetry.Context.Operation.ParentId = myQueueItem.ParentId;

            telemetry.Stop();
            telemetryClient.Track(telemetry);
        }
Exemplo n.º 13
0
        public static async Task ActivityFunction([QueueTrigger("work-item-queue-ai", Connection = "ConnectionString")] Context context,
                                                  [Queue("control-queue-ai", Connection = "ConnectionString")] IAsyncCollector <Context> contexts,
                                                  ILogger log)
        {
            log.LogInformation($"Activity Functions Started.");
            var requestActivity = new Activity("Activity Spike: Activity Function Request");

            requestActivity.SetParentId(context.ActivityId);
            requestActivity.Start();

            var requestTelemetry = new RequestTelemetry {
                Name = "Activity Spike: Activity Function Request"
            };

            requestTelemetry.SetActivity(requestActivity);

            requestTelemetry.Start();

            var dependencyActivity = new Activity("Activity FUnction Dependency");

            dependencyActivity.SetParentId(requestActivity.Id);  // You can omit this.
            dependencyActivity.Start();

            var dependencyTelemetry = new DependencyTelemetry {
                Name = "Activity Spike: Activity Function Dependency"
            };

            dependencyTelemetry.SetActivity(dependencyActivity);
            dependencyTelemetry.Start();

            var c = new Context()
            {
                ActivityId            = dependencyActivity.Id,
                ParentId              = dependencyActivity.ParentId,
                OrchestrationActivity = context.OrchestrationActivity,    // I skip the code for stack for the activity.
                Completed             = true
            };
            await contexts.AddAsync(c);

            dependencyActivity.Stop();
            dependencyTelemetry.Stop();
            client.Track(dependencyTelemetry);
            requestActivity.Stop();
            requestTelemetry.Stop();
            client.Track(requestTelemetry);
        }
        private RequestTelemetry InitializeRequestTelemetry(HttpContext httpContext, Activity activity, long timestamp)
        {
            var requestTelemetry = new RequestTelemetry();

            if (!this.enableW3CHeaders)
            {
                requestTelemetry.Context.Operation.Id = activity.RootId;
                requestTelemetry.Id = activity.Id;
            }
            else
            {
                activity.UpdateTelemetry(requestTelemetry, false);
            }

            if (this.proactiveSamplingEnabled &&
                this.configuration != null &&
                !string.IsNullOrEmpty(requestTelemetry.Context.Operation.Id) &&
                SamplingScoreGenerator.GetSamplingScore(requestTelemetry.Context.Operation.Id) >= this.configuration.GetLastObservedSamplingPercentage(requestTelemetry.ItemTypeFlag))
            {
                requestTelemetry.IsSampledOutAtHead = true;
                AspNetCoreEventSource.Instance.TelemetryItemWasSampledOutAtHead(requestTelemetry.Context.Operation.Id);
            }

            //// When the item is proactively sampled out, we can avoid heavy operations that do not have known dependency later in the pipeline.
            //// We mostly exclude operations that were deemed heavy as per the corresponding profiler trace of this code path.

            if (!requestTelemetry.IsSampledOutAtHead)
            {
                foreach (var prop in activity.Baggage)
                {
                    if (!requestTelemetry.Properties.ContainsKey(prop.Key))
                    {
                        requestTelemetry.Properties[prop.Key] = prop.Value;
                    }
                }
            }

            this.client.InitializeInstrumentationKey(requestTelemetry);
            requestTelemetry.Source = GetAppIdFromRequestHeader(httpContext.Request.Headers, requestTelemetry.Context.InstrumentationKey);

            requestTelemetry.Start(timestamp);
            httpContext.Features.Set(requestTelemetry);

            return(requestTelemetry);
        }
Exemplo n.º 15
0
        public void OnBeginRequest(HttpContext httpContext, long timestamp)
        {
            if (this.client.IsEnabled())
            {
                var requestTelemetry = new RequestTelemetry
                {
                    Id = httpContext.TraceIdentifier
                };
                this.client.Initialize(requestTelemetry);
                requestTelemetry.Start(timestamp);
                httpContext.Features.Set(requestTelemetry);

                IHeaderDictionary responseHeaders = httpContext.Response?.Headers;
                if (responseHeaders != null && !string.IsNullOrEmpty(requestTelemetry.Context.InstrumentationKey) && !responseHeaders.ContainsKey(RequestResponseHeaders.TargetInstrumentationKeyHeader))
                {
                    responseHeaders.Add(RequestResponseHeaders.TargetInstrumentationKeyHeader, new StringValues(InstrumentationKeyHashLookupHelper.GetInstrumentationKeyHash(requestTelemetry.Context.InstrumentationKey)));
                }
            }
        }
Exemplo n.º 16
0
        public static async Task ActivityFunction([QueueTrigger("work-item-queue-ai", Connection = "ConnectionString")] TraceContext traceContext,
                                                  [Queue("control-queue-ai", Connection = "ConnectionString")] IAsyncCollector <TraceContext> contexts,
                                                  ILogger log)
        {
            log.LogInformation($"W3CActivity Functions Started.");
            var requestActivity = new Activity("W3CActivity Spike: Activity Function Request");

            requestActivity.SetParentAndStart(traceContext);

            var requestTelemetry = new RequestTelemetry {
                Name = "W3CActivity Spike: Activity Function Request"
            };

            requestTelemetry.SetActivity(requestActivity);

            requestTelemetry.Start();

            var dependencyActivity = new Activity("W3CActivity FUnction Dependency");

            dependencyActivity.SetParentAndStart(requestActivity);

            var dependencyTelemetry = new DependencyTelemetry {
                Name = "W3CActivity Spike: Activity Function Dependency"
            };

            dependencyTelemetry.SetActivity(dependencyActivity);
            dependencyTelemetry.Start();
            var c = dependencyActivity.CreateTraceContext();

            c.OrchestrationContexts = traceContext.OrchestrationContexts;
            c.Completed             = true;

            await contexts.AddAsync(c);

            dependencyActivity.Stop();
            dependencyTelemetry.Stop();
            client.Track(dependencyTelemetry);
            requestActivity.Stop();
            requestTelemetry.Stop();
            client.Track(requestTelemetry);
        }
Exemplo n.º 17
0
        void IWcfTelemetryModule.OnBeginRequest(IOperationContext operation)
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }

            if (this.telemetryClient == null)
            {
                return;
            }

            RequestTelemetry telemetry = operation.Request;

            // if ASP.NET has already started the request, leave the start time alone.
            if (operation.OwnsRequest)
            {
                telemetry.Start();
            }

            telemetry.Url  = operation.EndpointUri;
            telemetry.Name = operation.OperationName;
            telemetry.Properties["soapAction"] = operation.SoapAction;

            var httpHeaders = operation.GetHttpRequestHeaders();

            if (httpHeaders != null)
            {
                telemetry.Properties["httpMethod"] = httpHeaders.Method;
                if (operation.ToHeader != null)
                {
                    // overwrite it for WebHttpBinding requests
                    telemetry.Url = operation.ToHeader;
                }
            }

            // run telemetry initializers here, while the request message is still open
            this.telemetryClient.Initialize(telemetry);
        }
        public static void TrackRequest(string name, Uri uri, string code, bool success, long start, long end)
        {
            if (context == null)
            {
                Initialize();
            }
            if (!enable)
            {
                return;
            }
            RequestTelemetry request = new RequestTelemetry();

            request.Name         = name;
            request.ResponseCode = code;
            request.Success      = success;
            request.Url          = uri;
            request.Timestamp    = DateTime.Now;
            request.Start(start);
            request.Context.Operation.Name = name;
            request.Duration = new TimeSpan(end - start);
            context.TrackRequest(request);
        }
Exemplo n.º 19
0
        public static async Task Orchestrator([QueueTrigger("control-queue-ai", Connection = "ConnectionString")] Context context,
                                              [Queue("work-item-queue-ai", Connection = "ConnectionString")] IAsyncCollector <Context> contexts,
                                              ILogger log)
        {
            log.LogInformation($"Orchestration Started.");
            Activity requestActivity = null;

            if (context.OrchestrationActivity == null) // In case of the initial execution.
            {
                requestActivity = new Activity("Activity Spike: Orchestration Request");
                requestActivity.SetParentId(context.ActivityId);
            }
            else
            {
                requestActivity = new Activity("Activity Spike: Orchestration Request");
                // After Activity.SetParentId then Start the actvitiy, it will create a new Id. However, it is not Identical as the last execution.
                // This is necessary. Or directly set from SubsetActivity. This is not recommended. However, there is no way for this protocol.
                var property = typeof(Activity).GetProperty("Id", BindingFlags.Public | BindingFlags.Instance);
                property.SetValue(requestActivity, context.OrchestrationActivity.ActivityId);
                requestActivity.SetParentId(context.OrchestrationActivity.ParentId);
            }

            requestActivity.Start();
            var subsetActivity = new SubsetActivity()
            {
                ActivityId = requestActivity.Id,
                ParentId   = requestActivity.ParentId,
                RootId     = requestActivity.RootId
            };
            var requestTelemetry = new RequestTelemetry {
                Name = "Activity Spike: Ochestration Result"
            };

            if (context.OrchestrationActivity == null)
            {
                requestTelemetry.SetActivity(requestActivity);
            }
            else
            {
                requestTelemetry.Id = context.OrchestrationActivity.ActivityId;
                requestTelemetry.Context.Operation.Id       = context.OrchestrationActivity.RootId;
                requestTelemetry.Context.Operation.ParentId = context.OrchestrationActivity.ParentId;
            }

            requestTelemetry.Start();
            // Only the last execution, we track it.
            var dependencyActivity = new Activity("Activity Spike: Orchestration Dependency");

            dependencyActivity.SetParentId(requestActivity.Id); // You can omit this.
            dependencyActivity.Start();

            var dependencyTelemetry = new DependencyTelemetry {
                Name = "Activity Spike: Orchestration Dependency"
            };

            dependencyTelemetry.SetActivity(dependencyActivity);

            dependencyTelemetry.Start();

            var c = new Context()
            {
                ActivityId            = requestActivity.Id,
                ParentId              = requestActivity.ParentId,
                OrchestrationActivity = subsetActivity
            };


            dependencyActivity.Stop();
            dependencyTelemetry.Stop();

            if (context.Completed)
            {
                client.Track(dependencyTelemetry);
            }
            else
            {
                await contexts.AddAsync(c);

                // We don't need to emit telemetry for intermediate execution.
            }

            requestActivity.Stop();

            requestTelemetry.Stop();
            if (context.OrchestrationActivity == null) // In case of the initial execution.
            {
                client.Track(requestTelemetry);
            }
        }
Exemplo n.º 20
0
        public static async Task Orchestrator([QueueTrigger("control-queue-ai", Connection = "ConnectionString")]
                                              TraceContext traceContext,
                                              [Queue("work-item-queue-ai", Connection = "ConnectionString")]
                                              IAsyncCollector <TraceContext> contexts,
                                              ILogger log)
        {
            log.LogInformation($"Orchestration Started.");
            Activity requestActivity = null;
            var      isReplay        = traceContext.OrchestrationContexts.Count != 0;

            if (!isReplay)
            {
                requestActivity = new Activity("W3CActivity Spike: Orchestration Request");
                requestActivity.SetParentAndStart(traceContext);

                var requestTelemetry = new RequestTelemetry {
                    Name = "W3CActivity Spike: Orchestration Request"
                };
                requestTelemetry.SetActivity(requestActivity);

                requestTelemetry.Start();

                requestActivity.Stop();

                requestTelemetry.Stop();
                client.Track(requestTelemetry);
            }

            TraceContext c = null;

            if (!isReplay)
            {
                c = requestActivity.CreateTraceContext();
                c.OrchestrationContexts = traceContext.OrchestrationContexts;
                c.OrchestrationContexts.Push(requestActivity.CreateTraceContext());
            }
            else
            {
                c = traceContext.OrchestrationContexts.Peek(); // if necessary. This program doesn't need it.
            }



            if (traceContext.Completed)
            {
                var dependencyActivity = new Activity("W3CActivity Spike: Orchestration Dependency");
                dependencyActivity.SetParentAndStart(traceContext.OrchestrationContexts.Peek());
                var dependencyTelemetry = new DependencyTelemetry {
                    Name = "W3CActivity Spike: Orchestration Dependency"
                };
                dependencyTelemetry.SetActivity(dependencyActivity);

                dependencyTelemetry.Start();
                dependencyActivity.Stop();
                dependencyTelemetry.Stop();

                client.Track(dependencyTelemetry);
            }
            else
            {
                await contexts.AddAsync(c);

                // We don't need to emit telemetry for intermediate execution.
            }
        }