void IWcfTelemetryModule.OnEndRequest(IOperationContext operation, Message reply)
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }
            if (telemetryClient == null)
            {
                return;
            }

            if (reply != null && reply.IsClosed())
            {
                WcfEventSource.Log.ResponseMessageClosed(nameof(RequestTrackingTelemetryModule), "OnEndRequest");
            }

            RequestTelemetry telemetry = operation.Request;

            // make some assumptions
            bool           isFault      = false;
            HttpStatusCode responseCode = HttpStatusCode.OK;

            if (reply != null && !reply.IsClosed())
            {
                isFault = reply.IsFault;
            }

            HttpResponseMessageProperty httpHeaders = operation.GetHttpResponseHeaders();

            if (httpHeaders != null)
            {
                responseCode = httpHeaders.StatusCode;
                if ((int)responseCode >= 400)
                {
                    isFault = true;
                }
            }
            else if (isFault)
            {
                responseCode = HttpStatusCode.InternalServerError;
            }

            // if the operation code has already marked the request as failed
            // don't overwrite the value if we think it was successful
            if (isFault || !telemetry.Success.HasValue)
            {
                telemetry.Success = !isFault;
            }
            telemetry.ResponseCode = responseCode.ToString("d");
            if (telemetry.Url != null)
            {
                telemetry.Properties.Add("Protocol", telemetry.Url.Scheme);
            }
            // if the Microsoft.ApplicationInsights.Web package started
            // tracking this request before WCF handled it, we
            // don't want to track it because it would duplicate the event.
            // Let the HttpModule instead write it later on.
            if (operation.OwnsRequest)
            {
                telemetry.Stop();
                telemetryClient.TrackRequest(telemetry);
            }
        }
Exemplo n.º 2
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.
            }
        }