Exemple #1
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."));
        }
        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."));
        }
Exemple #3
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);
        }
        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);
        }
Exemple #5
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);
            }
        }
        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.
            }
        }