Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

AppMonitoring - POC

The purpose of this repository is to demonstrate an example of a backend application that implements some modern traceability and observability solutions.

Structured Logging - Serilog

To add Serilog to the solution:

  1. Add the following packages to the project:
    • Serilog.AspNetCore
    • Serilog.Enrichers.Environment
    • Serilog.Enrichers.Process
    • Serilog.Enrichers.Thread
    • Serilog.Settings.Configuration
    • Serilog.Sinks.File
  2. Serilog, thanks to Serilog.Settings.Configuration, can be configured via appsettings.json, e.g:
"Serilog": {
   "Using": [],
   "MinimumLevel": {
      "Default": "Information",
      "Override": {
         "Microsoft": "Warning",
         "System": "Warning"
      }
   },
   "WriteTo": [
      {
      "Name": "File",
      "Args": {
         "path": "logs/log.json",
         "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
         }
      }
   ],
   "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithProcessId",
      "WithThreadId"
   ],
   "Properties": {
      "ApplicationName": "AppName"
   }
}
  1. If you want to activate AspNetCore request tracking you just need to activate the middleware:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
 ...
 app.UseSerilogRequestLogging();
...
}
  1. Enable Serilog configuration:
public static IHostBuilder CreateHostBuilder(string[] args) =>
      Host.CreateDefaultBuilder(args)
          .ConfigureWebHostDefaults(webBuilder =>
          {
              webBuilder.UseStartup<Startup>();
          }).UseSerilog((hostingContext, services, configuration) => {
              configuration.ReadFrom.Configuration(hostingContext.Configuration);
          }, writeToProviders: true);
}
  1. At this point you just need to consume the ILogger<> interface of dotnet:
public class MyController(ILogger<MyController> logger) {
  logger.LogError("Everything is on fire");
}

AppMetrics

To add AppMetrics to the solution:

  1. Add the following packages to the project:
  • App.Metrics.AspNetCore.All
  • App.Metrics.Prometheus (or any other reporting library)
  1. Configure the library via appsettings.json:
{
 "MetricsOptions": {
    "DefaultContextLabel": "MyMvcApplication",
    "Enabled": true
  },
  "MetricEndpointsOptions": {
    "MetricsEndpointEnabled": true,
    "MetricsTextEndpointEnabled": true,
    "EnvironmentInfoEndpointEnabled": true
   }
}
  1. Enable the library:
// Startup.cs

...
services.AddMvcCore(options => options.EnableEndpointRouting = false).AddMetricsCore();
...
app.UseMvc(); 
  1. Create a MetricsRegistry to centralise the definition of the metrics to be tracked:
//MetricsRegistry.cs
using App.Metrics.ReservoirSampling.Uniform;
using App.Metrics.Timer;

namespace AppMonitoring.POC
{
    public static class MetricsRegistry
    {
        private const string Context = "Weather";

        public static readonly TimerOptions TimerWeatherForecastGeneration = new TimerOptions
        {
            Context = Context,
            Name = "weather-forecast-generation",
            Reservoir = () => new DefaultAlgorithmRReservoir(),
        };
    }
}
  1. At this point all you have to do is pass an instance of IMetrics to your class and consume the metrics you are interested in:
public class MyController 
{
     public void GetByCountryCity([FromService]IMetrics metrics)
     {
         using (metrics.Measure.Timer.Time(MetricsRegistry.TimerWeatherForecastGeneration))
         {
             await MyVeryLongMethod();
         }
     }    
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages