diff --git a/src/BlazingPizza.Orders/OrdersController.cs b/src/BlazingPizza.Orders/OrdersController.cs index c32f554..5658e6c 100644 --- a/src/BlazingPizza.Orders/OrdersController.cs +++ b/src/BlazingPizza.Orders/OrdersController.cs @@ -49,6 +49,7 @@ public async Task> PlaceOrder(Order order) order.OrderId = Guid.NewGuid(); order.TotalPrice = order.GetFormattedTotalPrice(); await _db.SaveOrder(order); + OrdersEventSource.Log.OrderCreated(); return order.OrderId; } } diff --git a/src/BlazingPizza.Orders/OrdersEventSource.cs b/src/BlazingPizza.Orders/OrdersEventSource.cs new file mode 100644 index 0000000..85b0fed --- /dev/null +++ b/src/BlazingPizza.Orders/OrdersEventSource.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Tracing; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace BlazingPizza.Orders +{ + internal sealed class OrdersEventSource : EventSource + { + public static readonly OrdersEventSource Log = new OrdersEventSource(); + + private PollingCounter _totalOrdersCounter; + + private long _totalOrders; + + internal OrdersEventSource() + : base("BlazingOrders.Pizza") + { + + } + + internal void OrderCreated() + { + Interlocked.Increment(ref _totalOrders); + } + + protected override void OnEventCommand(EventCommandEventArgs command) + { + if (command.Command == EventCommand.Enable) + { + _totalOrdersCounter ??= new PollingCounter("total-orders", this, () => _totalOrders) + { + DisplayName = "Total Orders" + }; + } + } + } +} diff --git a/src/BlazingPizza.Orders/Startup.cs b/src/BlazingPizza.Orders/Startup.cs index 1c44ae8..b970e17 100644 --- a/src/BlazingPizza.Orders/Startup.cs +++ b/src/BlazingPizza.Orders/Startup.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.Logging; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector; namespace BlazingPizza.Orders { @@ -31,6 +32,11 @@ public void ConfigureServices(IServiceCollection services) services.AddControllers(); services.AddGrpc(); services.AddApplicationInsightsTelemetry(Configuration); + + services.ConfigureTelemetryModule((module, options) => + { + module.Counters.Add(new EventCounterCollectionRequest("BlazingOrders.Pizza", "total-orders")); + }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.