Example #1
0
        protected void StopSendTelemetry(DependencyTelemetry telemetry, Message response, Exception error, string method)
        {
            if (telemetry == null)
            {
                return;
            }

            try
            {
                if (error != null)
                {
                    telemetry.Success    = false;
                    telemetry.ResultCode = error.ToResultCode();
                }

                if (response != null && response.IsFault)
                {
                    telemetry.Success    = false;
                    telemetry.ResultCode = "SoapFault";
                }

                telemetry.Stop();
                this.ChannelManager.TelemetryClient.TrackDependency(telemetry);
            }
            catch (Exception ex)
            {
                WcfClientEventSource.Log.ClientTelemetryError(method, ex.ToString());
            }
        }
        public void OperationTelemetryCanRecordPreciseDurations()
        {
            var telemetry = new DependencyTelemetry();

            long startTime = Stopwatch.GetTimestamp();

            telemetry.Start(timestamp: startTime);

            // Note: Do not use TimeSpan.FromSeconds because it rounds to the nearest millisecond.
            var expectedDuration = TimeSpan.Parse("00:00:00.1234560");

            // Ensure we choose a time that has a fractional (non-integral) number of milliseconds
            Assert.NotEqual(Math.Round(expectedDuration.TotalMilliseconds), expectedDuration.TotalMilliseconds);

            double durationInStopwatchTicks = Stopwatch.Frequency * expectedDuration.TotalSeconds;

            long stopTime = (long)Math.Round(startTime + durationInStopwatchTicks);

            telemetry.Stop(timestamp: stopTime);

            if (Stopwatch.Frequency == TimeSpan.TicksPerSecond)
            {
                // In this case, the times should match exactly.
                Assert.Equal(expectedDuration, telemetry.Duration);
            }
            else
            {
                // There will be a difference, but it should be less than
                // 1 microsecond (10 ticks)
                var difference = (telemetry.Duration - expectedDuration).Duration();
                Assert.True(difference.Ticks < 10);
            }
        }
        public void OperationTelemetryStopWithTimestampAssignsDurationZeroWithoutStart()
        {
            var telemetry = new DependencyTelemetry();

            telemetry.Stop(timestamp: 123456789012345L); // timestamp is ignored because Start was not called

            Assert.Equal(TimeSpan.Zero, telemetry.Duration);
        }
        public void OperationTelemetryStopAssignsCurrentTimeAsStartTimeAndTimeStampWithoutStart()
        {
            var telemetry = new DependencyTelemetry();

            telemetry.Stop();
            Assert.NotEqual(DateTimeOffset.MinValue, telemetry.Timestamp);
            Assert.Equal(telemetry.Duration, TimeSpan.Zero);
        }
        public void OperationTelemetryStopDoesNotAffectTimeStampAndStartTimeAfterStart()
        {
            var telemetry = new DependencyTelemetry();

            telemetry.Start();
            DateTimeOffset actualTime = telemetry.Timestamp;

            telemetry.Stop();
            Assert.Equal(telemetry.Timestamp, actualTime);
        }
Example #6
0
        public void OperationTelemetryStopComputesDurationAfterStart()
        {
            var telemetry = new DependencyTelemetry();

            telemetry.Start();
            Thread.Sleep(2000);
            Assert.Equal(TimeSpan.Zero, telemetry.Duration);
            telemetry.Stop();
            Assert.True(telemetry.Duration.TotalMilliseconds > 0);
        }
        public void OperationTelemetryStopWithTimestampComputesDurationAfterStartWithTimestamp()
        {
            var telemetry = new DependencyTelemetry();

            long startTime        = 123456789012345L;
            long ticksInOneSecond = Stopwatch.Frequency;
            long stopTime         = startTime + ticksInOneSecond;

            telemetry.Start(timestamp: startTime);
            telemetry.Stop(timestamp: stopTime);

            Assert.Equal(TimeSpan.FromSeconds(1), telemetry.Duration);
        }
Example #8
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."));
        }
Example #9
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."));
        }
 private void StopOpenTelemetry(DependencyTelemetry telemetry, Exception error, String method)
 {
     if (telemetry == null)
     {
         return;
     }
     try
     {
         telemetry.Success = error == null;
         telemetry.Stop();
         ChannelManager.TelemetryClient.TrackDependency(telemetry);
     } catch (Exception ex)
     {
         WcfClientEventSource.Log.ClientTelemetryError(method, ex.ToString());
     }
 }
Example #11
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);
        }
Example #12
0
        public async Task Run([TimerTrigger("*/15 * * * * *")] TimerInfo myTimer)
        {
            var dep = new DependencyTelemetry()
            {
                Name = "Dependency Test",
                Type = "Test",
                Data = "C# Timer trigger function executed"
            };

            dep.Start();

            await Task.Delay(3000);

            dep.Stop();
            _telemetryClient.TrackDependency(dep);

            _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        }
Example #13
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);
        }
Example #14
0
 /// <summary>
 /// Stops telemetry operation. Doesn't track the telemetry item.
 /// </summary>
 /// <param name="telemetry">Telemetry item to stop.</param>
 internal static void EndOperation(DependencyTelemetry telemetry)
 {
     telemetry.Stop();
 }
Example #15
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.
            }
        }
Example #16
0
 /// <summary>
 /// Function that needs to be invoked after the request call to the sever. Computes the duration of the request and tracks the dependency telemetry
 /// item.
 /// </summary>
 /// <param name="telemetryClient">Telemetry client object to track the telemetry item.</param>
 /// <param name="telemetry">Telemetry item to compute the duration and track.</param>
 internal static void EndTracking(TelemetryClient telemetryClient, DependencyTelemetry telemetry)
 {
     telemetry.Stop();
     telemetryClient.TrackDependency(telemetry);
 }
Example #17
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);
            }
        }