Skip to content

Commit

Permalink
Merge pull request glennc#1 from glennc/shirhatti/counters
Browse files Browse the repository at this point in the history
Add total-orders EventCounter
  • Loading branch information
glennc authored Nov 18, 2019
2 parents c8301c7 + 994f50d commit 1aa669e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/BlazingPizza.Orders/OrdersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public async Task<ActionResult<Guid>> PlaceOrder(Order order)
order.OrderId = Guid.NewGuid();
order.TotalPrice = order.GetFormattedTotalPrice();
await _db.SaveOrder(order);
OrdersEventSource.Log.OrderCreated();
return order.OrderId;
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/BlazingPizza.Orders/OrdersEventSource.cs
Original file line number Diff line number Diff line change
@@ -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"
};
}
}
}
}
6 changes: 6 additions & 0 deletions src/BlazingPizza.Orders/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.ApplicationInsights.Extensibility.EventCounterCollector;

namespace BlazingPizza.Orders
{
Expand All @@ -31,6 +32,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllers();
services.AddGrpc();
services.AddApplicationInsightsTelemetry(Configuration);

services.ConfigureTelemetryModule<EventCounterCollectionModule>((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.
Expand Down

0 comments on commit 1aa669e

Please sign in to comment.