public static void Run([QueueTrigger(QUEUE_NAME, Connection = "AzureWebJobsStorage")] MessagePayload myQueueItem, ExecutionContext context, TraceWriter log)
        {
            _telemetryClient.Context.Cloud.RoleName = context.FunctionName;

            RequestTelemetry requestTelemetry = new RequestTelemetry {
                Name = "Dequeue " + QUEUE_NAME
            };

            requestTelemetry.Context.Operation.Id       = CorrelationHelper.GetOperationId(myQueueItem.RequestId);
            requestTelemetry.Context.Operation.ParentId = myQueueItem.RequestId;

            var operation = _telemetryClient.StartOperation(requestTelemetry);

            try
            {
                // Only 10%
                if (_random.Next(1, 101) <= 10)
                {
                    throw new Exception("step 3 - bad things happen");
                }

                operation.Telemetry.Success = true;
                _telemetryClient.StopOperation(operation);
            }
            catch (Exception e)
            {
                _telemetryClient.TrackException(e);

                operation.Telemetry.Success      = false;
                operation.Telemetry.ResponseCode = "500";
                _telemetryClient.StopOperation(operation);

                throw;
            }
        }
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ExecutionContext context, TraceWriter log)
        {
            _telemetryClient.Context.Cloud.RoleName = context.FunctionName;

            var requestTelemetry = new RequestTelemetry
            {
                Name = $"{req.Method} {req.Path}",
                Url  = new Uri(req.GetDisplayUrl())
            };

            requestTelemetry.Properties["httpMethod"] = req.Method;

            if (req.Headers.ContainsKey("Request-Id"))
            {
                var requestId = req.Headers.GetCommaSeparatedValues("Request-Id").Single();
                requestTelemetry.Context.Operation.Id       = CorrelationHelper.GetOperationId(requestId);
                requestTelemetry.Context.Operation.ParentId = requestId;
            }

            var operation = _telemetryClient.StartOperation(requestTelemetry);

            ObjectResult response;

            try
            {
                // Only 10%
                if (_random.Next(1, 101) <= 10)
                {
                    throw new Exception("step 2 - bad things happen");
                }

                // Only 50%
                if (_random.Next(1, 101) <= 50)
                {
                    await CallQueueTriggerAsync();
                }

                response = new OkObjectResult($"All Done");

                operation.Telemetry.Success      = response.StatusCode >= 200 && response.StatusCode <= 299;
                operation.Telemetry.ResponseCode = response.StatusCode.ToString();
                _telemetryClient.StopOperation(operation);
            }
            catch (Exception e)
            {
                _telemetryClient.TrackException(e);

                operation.Telemetry.Success      = false;
                operation.Telemetry.ResponseCode = "500";
                _telemetryClient.StopOperation(operation);

                throw;
            }

            return(response);
        }