Skip to content

mindbox-cloud/Mindbox.DiagnosticContext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mindbox.DiagnosticContext

Adding diagnostic context to a Asp.Net Core app

Add a reference to Mindbox.DiagnosticContext.AspNetCore package. This will allow you to instrument your action methods with diagnostic context:

[UseDiagnosticContext("order")]
public ActionResult CreateOrder()
{ 
  var diagnosticContext = HttpContext.GetDiagnosticContext();
  // ...   
}

Setting up diagnostic context factory

You must setup dependency injection in order to use diagnostic context. IDiagnosticContextFactory implementation controls what type of diagnostic context gets created and how the collected metrics are exposed.

It is recommended to use one of the provided implementations (see below), but any IDiagnosticContextFactory service registration will suffice.

Using prometheus to expose the metrics

Add a reference to Mindbox.DiagnosticContext.Prometheus package. Then, add this code to your service configuration:

services
  .AddPrometheusDiagnosticContext("orders");

It is strongly recommended to use a unique prefix that includes the name of the application - this can guarantee that there is no intersection of metrics.

If your application doesn't yet expose prometheus metrics, add the following code to your Startup class or use the prometheus-net documentation to instrument your code:

app.UseMetricServer();

Using diagnostic context + prometheus in .Net Framework

  1. Register a factory with the desired settings.
  2. Create a DiagnosticContext instance
  3. Use the generated DiagnosticContext

Collect EntityFramework metrics

Mindbox.DiagnosticContext.EntityFramework provides mechanics to collect EF metrics:

  • number of executed sql commands (exposed by EfExecutedCommandsMetricsType metric type)

How to add this metrics to you application:

  • Register required DbContext interceptors, using AddEfCommandsMetrics extension
  • Add required metrics to you diagnostic context factory:

Sample

// Register interceptors
public class MyDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // ...
        optionsBuilder.AddEfCommandsMetrics();
        // ...
    }
}

// Adding metrics to DC factory
IDiagnosticContextFactory dcFactory = ...
dcFactory.CreateDiagnosticContext(
    metricName,
    metricsTypesOverride: new []
    {  
        new EfExecutedCommandsMetricsType()    
    })

Using the diagnostic context in DirectCRM

IDiagnosticContextFactory already registered in DirectCrmCoreModule, just use it.

Metrics are sent to special prometheus, here you can see the values of metrics and build queries.

Eexample of creating a DiagnosticContext for a ModelContext.

To use an external DiagnosticContext, you need to use IDiagnosticContextFactory and create an instance of IDiagnosticContext. Remember to dispose it.

Things to know when switching from Relic to Prometheus in DirectCRM

  • Prometheus uses metrics in conjunction with labels. By labels, using promql, you can conveniently group (sum(some_metric) by (lable)).

  • When creating a DiagnosticContext, you must specify the name of the metric. If it contains characters invalid for the metric, they will be removed. You can read about which characters are invalid here.

  • To split the dashboard by projects, the name of the project must be selected from the name of the metric using the query and regex in Grafana. An example of a dashboard using such metrics, broken down by project, here.

  • When creating a DiagnosticContext using IDiagnosticContextFactory, the name of the metric being written is diagnosticcontext_orders_{metricName}_metric_projectSystemName. In other words, several metrics are collected at once and metric can be: processingtime, cpuprocessingtime, counters, etc.

Example:

...
using var diagnosticContext = diagnosticContextFactory.CreateDiagnosticContext("metric_name");
using diagnosticContext.Measure("some_step");
...

The final metric name will look like: diagnosticcontext_orders_metric_name_[metric]_projectSystemName. For example, if we want to build a pie based on the time spent, then we need to use the metric: diagnosticcontext_orders_metric_name_processingtime_projectSystemName. The names of the steps will be recorded on the labels. The example shows one step: some_step - it will go to the label step.

  • Prometheus, unlike Relic, has a set of counters that differ in the name label. This metric is named: diagnosticcontext_orders_metricName_counters_projectSystemName. In other words, counters is appended to the metric name specified when the DiagnosticContext was created. If you need to find out the value of a specific counter, you need to make the following request: diagnosticcontext_orders_metricName_counters_projectSystemName{name=~"counter_name"}.

Example:

using var diagnosticContext = diagnosticContextFactory.CreateDiagnosticContext("metric_name");
using diagnosticContext.Increment("some_counter");

The final query for this counter will be as follows: diagnosticcontext_orders_metric_name_counters_projectSystemName{name=~"some_counter"}.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages