. net program performance monitoring system elastic amp usage

Time:2022-5-21

What is elastic amp

Elastic APM is an application performance monitoring system. It can the detailed performance information of request response time, database query, call to cache, external HTTP request, etc., and can monitor software services and applications in real time. This can help us quickly identify and fix performance problems.

Elastic APM also automatically collects unhandled errors and exceptions. Therefore, we can identify new errors when they occur and pay close attention to the number of specific errors.

Server metrics are another important source of information. Elastic APM agent will automatically obtain basic host level indicators and agent specific indicators.

Elastic APM currently supports node JS, python, ruby, PHP, Java, go, rum (JS), and NET.

working principle

  • Elastic amp collects the indicator information of the application through the agent
  • The agent uploads the collected information to the amp server
  • The amp server aggregates the data and stores it in elasticsearch
  • View indicator information through kibana

Environmental installation

We build a stand-alone environment through docker to demonstrate the functions of elastic APM.

1. Install elasticsearch


docker network create elastic
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.15.2
docker run -d --name es01-test --net elastic -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.15.2

2. Install kibana

docker pull docker.elastic.co/kibana/kibana:7.15.2
docker run -d --name kib01-test --net elastic -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://es01-test:9200" docker.elastic.co/kibana/kibana:7.15.2

3. Install elasticapm


docker run -d  -p 8200:8200  --name=apm-server --net elastic  --user=apm-server  docker.elastic.co/apm/apm-server:7.15.2  --strict.perms=false -e  -E output.elasticsearch.hosts=["es01-test:9200"]

Code demonstration

Elastic amp support NET Framwork4. 6.1 + and NET Core2. 1 +, supported components are:

Grpc, httpclient, ef6, efcore, elasticsearch, mysql, Mongo, redis, Kafka, rabbitmq, etc. for details, see https://www.elastic.co/guide/en/apm/agent/dotnet/master/supported-technologies.html 。 We use it this time Net framework create a new web project to demonstrate

1. Add a new web project

2. Add nuget package


  <package version="1.12.1" targetFramework="net472" />
  <package version="1.12.1" targetFramework="net472" />
  <package version="1.12.1" targetFramework="net472" />
  <package version="1.12.1" targetFramework="net472" />
  <package version="2.2.88" targetFramework="net472" />

3. Configure HttpModule

web. System. In config The following nodes are added to webserver


      <modules>
          <add name="ElasticApmModule" type="Elastic.Apm.AspNetFullFramework.ElasticApmModule, Elastic.Apm.AspNetFullFramework" />
      </modules>

4. Configure agent

We can configure agent information through environment variables

protected void Application_Start()
        {
            Environment. SetEnvironmentVariable("ELASTIC_APM_SERVICE_NAME", "TestFromworkSite");  // service name
            Environment. SetEnvironmentVariable("ELASTIC_APM_ENVIRONMENT", "Dev");    // environment
            Environment.SetEnvironmentVariable("ELASTIC_APM_SERVER_URL", "http://localhost:8200"); // APM server
            Environment. SetEnvironmentVariable("ELASTIC_APM_FLUSH_INTERVAL", "5s");  // Period of uploading data
            Environment.SetEnvironmentVariable("ELASTIC_APM_LOG_LEVEL", "Trace");
            //..........
        }

5. Launch the website

You can directly start the corresponding website in kimwork: test

Elastic APM core module

1. Transaction: we can see the call information of API through transaction

2. Dependencies: see Service Dependencies through dependencies

3. Error: you can see the error information in the program through error

4. MATRICS: you can see the memory and CPU information of the service gas through MATRICS

Elastic monitors MSSQL, redis and other components

New mssqlhelper

public class MSSqlHelper
    {
        public static DataSet SqlExecuteReader(string _sql, SqlParameter[] _parameters, CommandType _type = CommandType.Text, string _constring = @"Data Source=.\SQLEXPRESS;Initial Catalog=dev;Integrated Security=True;")
        {
            DataSet ds = new DataSet();
            try
            {
                using (SqlConnection conn = new SqlConnection(_constring))
                {
                    SqlCommand cmd = new SqlCommand(_sql, conn);
                    if (_parameters != null)
                    {
                        foreach (SqlParameter p in _parameters)
                        { cmd.Parameters.Add(p); }
                    }
                    cmd.CommandType = _type;
                    cmd. CommandTimeout = 10;// Timeout in S
                    conn.Open();
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        sda.SelectCommand = cmd;
                        sda. Fill(ds);// Populate dataset
                    }
                }
                return ds;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }

New redishelper


public class RedisHelper
    {
        private static IDatabase database;

        public static void UseApmForRedis()
        {
            var connection = ConnectionMultiplexer.Connect("127.0.0.1:6379");
            connection.UseElasticApm();
            database = connection.GetDatabase();
        }

        public static void StringSet(string key,string value)
        {
            database.StringSet(key,value);
        }
    }

In application_ Enable the monitoring of sqlserver and redis in start()


            Agent.Subscribe(new SqlClientDiagnosticSubscriber());
            RedisHelper.UseApmForRedis();

HomeController. Call of new sqlserver and redis in index interface


public ActionResult Index()
        {
            MSSqlHelper.SqlExecuteReader("select * from S_dev.UserObject", null);

            RedisHelper.StringSet("a", "a");
        }

Start the program to see the data related to MSSQL and redis

In depth use of elastic APM API

1. Starttransaction and startspan enable custom transaction and span

Some scheduled tasks can also use elastic APM’s agent Tracer. Starttransaction and can be monitored, and we can add a user-defined node through startspan. In this way, we need to handle the exception information ourselves. We add a new job

public class TestJob
    {
        public void Run()
        {
            while (true)
            {
                var trans = Agent.Tracer.StartTransaction("TestJob", ApiConstants.TypeRequest);
                try
                {
                    MSSqlHelper.SqlExecuteReader("select * from S_dev.UserObject", null);

                    RedisHelper.StringSet("a", "a");

                    trans.SetLabel("name", "chester");

                    var span = trans. Startspan ("custom span", apiconstants.typeexternal, apiconstants.subtypehttp, apiconstants. Actionquery);
                    try
                    {
                        //Http request
                    }
                    catch (Exception e)
                    {
                        span.CaptureException(e);
                    }
                    finally
                    {
                        span.End();
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    trans.CaptureException(ex);
                    throw;
                }
                finally
                {
                    trans.End();
                }
            }
        }
    }

Application_ Start testjob in start


Task.Run(() => new TestJob().Run());

Start the program to see the corresponding testjob monitoring

2. Capturetransaction and capturespan enable custom transaction and span

Compared with starttransaction and startspan, capturetransaction and capturespan can help us end the transaction and span, or automatically catch exceptions and add a testjob2

public class TestJob2
    {
        public void Run()
        {
            while (true)
            {
                Agent.Tracer.CaptureTransaction("TestJob2", ApiConstants.TypeRequest, (trans) =>
                {
                    MSSqlHelper.SqlExecuteReader("select * from S_dev.UserObject", null);

                    RedisHelper.StringSet("a", "a");

                    trans.SetLabel("name", "chester");

                    trans. Capturespan ("custom Span2", apiconstants. Typedb, (s) = >
                    {
                        //execute db query
                    }, ApiConstants.SubtypeMssql, ApiConstants.ActionQuery);

                    Thread.Sleep(1000);
                });
            }
        }
    }

Application_ Start testjob2 in start


 Task.Run(() => new TestJob2().Run());

Start the program to see the corresponding testjob2 monitoring

3. Agent global interception

We can intercept transaction and span through the filter and add additional content such as label to them


Agent.AddFilter((ITransaction t) =>
            {
                //t.SetLabel("foo", "bar");
                return t;
            });
            Agent.AddFilter((ISpan span) =>
            {
                // ..
                return span;
            });

This is the introduction Net program performance monitoring system elastic amp is introduced here. I hope it will be helpful to your study, and I hope you can support developpaer.