Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.

WIP: feat: adds some extension methods to telemetry #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/Liquid.OnAzure/Telemetry/AppInsightsTelemetryMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;

namespace Liquid.Microservices.Runtime.Telemetry
{
/// <summary>
/// Cria uma extensão para o middleware
/// </summary>
public static class TelemetryExtensions
{
/// <summary>
/// Ativa o middleware customizado AppInsightsTelemetry para capturar todos os eventos das API.
/// e registrar logg no AppInsights com os detalhes adequados
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseTelemetry(this IApplicationBuilder builder)
{
return builder.UseMiddleware<AppInsightsTelemetryMiddleware>();
}
}

/// <summary>
/// This telemetry don't will use the TelemetryClient because we need centrilaze all messages to AppInsights
/// </summary>
public class AppInsightsTelemetryMiddleware
{
private readonly LightTelemetry _telemetry = (LightTelemetry)WorkBench.Telemetry;

private readonly RequestDelegate _next;

/// <summary>
/// Middleware de Telemetria de Exceções 500
/// </summary>
/// <param name="next"></param>
public AppInsightsTelemetryMiddleware(RequestDelegate next)
{
_next = next;
}

/// <summary>
/// Intercepta a chamada da API
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task Invoke(HttpContext context)
{
try
{
//TODO: Write log's to AppInsights or another techonology.
//TODO: Why trackEvent for every rest call?!
//_telemetry.TrackEvent($"Telemetry from middleware.");

await _next(context);
}
catch (Exception ex)
{
_telemetry.TrackException(ex);
throw ex; //TODO: Check if its correcty
}
}
}
}