Code Monkey home page Code Monkey logo

mindbox.diagnosticcontext's Introduction

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"}.

mindbox.diagnosticcontext's People

Contributors

antontolmachev avatar chipodeil avatar daniilchervyakov avatar deadoc avatar dmitriiskvortsov avatar gizdatullin avatar gureevleonid avatar hazuwall avatar ilyaaxenov avatar itsyrulnikov avatar ivonin avatar kirill29 avatar letfdsada avatar listerenko avatar natalyturbina avatar renovate-bot avatar splatrika avatar tad-in-github avatar timramone avatar timurmannapov avatar vbakaryuk avatar vyafilatov avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mindbox.diagnosticcontext's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/publish.yml
.github/workflows/pull-request.yml
nuget
src/AspNetCore/AspNetCore.csproj
  • Mindbox.Analyzers.V3 5.1.14
src/AspNetCoreTestProject/AspNetCoreTestProject.csproj
  • Mindbox.Analyzers.V3 5.1.14
src/Core/Core.csproj
  • Mindbox.Analyzers.V3 5.1.14
src/Directory.Build.props
src/Prometheus.Tests/Prometheus.Tests.csproj
  • Mindbox.Analyzers.V3 5.1.14
src/Prometheus/Prometheus.csproj
  • Mindbox.Analyzers.V3 5.1.14

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (local>mindbox-cloud/renovate-config)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.