{"id":297919,"date":"2020-01-29T21:00:23","date_gmt":"2020-01-29T21:00:23","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=297919"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=297919","title":{"rendered":"Elastic APM \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/485996\/\">\n<p>\u041c\u043d\u043e\u0439 \u0434\u0430\u0432\u043d\u043e \u043d\u0435 \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0441\u0442\u0430\u0442\u044c\u0438 \u0438 \u0432\u043e\u0442 \u043e\u043f\u044f\u0442\u044c\u2026 \u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0430\u0441\u044c \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u043d\u043e, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439. \u041a\u043e\u0433\u0434\u0430-\u0442\u043e \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 \u043c\u0435\u0442\u0440\u0438\u043a Prometheus, \u043d\u043e\u2026 \u0421\u043f\u0443\u0441\u0442\u044f \u0432\u0440\u0435\u043c\u044f, \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 Elastic APM, \u0442. \u043a. \u0432\u0435\u0441\u044c \u0441\u0442\u0435\u043a \u0434\u043b\u044f Elastic \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0431\u044b\u043b \u0438 \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u0418\u0442\u0430\u043a, Elastic APM \u2014 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u0435\u0442\u0440\u0438\u043a\u0430\u043c\u0438, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Elastic APM Agent \u2014 \u0430\u0433\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 \u043c\u0435\u0442\u0440\u0438\u043a \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432. \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 Elastic APM .NET Agent. \u041f\u0430\u043a\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f .NET Framework, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 4.6.2.<\/p>\n<p>  <\/p>\n<p>\u0410\u0433\u0435\u043d\u0442\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 (Elastic APM Server). \u0412\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 web.config \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u043c\u0438 \u043a\u043b\u044e\u0447\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u0442 Elastic APM Agent.<br \/>  \u041d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c, \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c, url \u0434\u043b\u044f Elastic APM Server. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0431\u0438\u0442\u044c, \u043f\u0440\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u0432 \u041b\u041a, \u043f\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c \u0438 \u0441\u0440\u0435\u0434\u0430\u043c, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:<\/p>\n<p>  <\/p>\n<ul>\n<li>ElasticApm:ServiceName \u2014 \u0438\u043c\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u0434\u043e\u043b\u0436\u043d\u043e \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c: ^[a-zA-Z0-9 _-]+$ .<\/li>\n<li>ElasticApm:Environment \u2014 \u0438\u043c\u044f \u0441\u0440\u0435\u0434\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 Kibana, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 7.2.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u0440\u0438\u043a\u0438, \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440:<\/p>\n<p>  <\/p>\n<ul>\n<li>ElasticApm:MetricsInterval \u2014 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f, \u0447\u0435\u0440\u0435\u0437 \u043a\u0430\u043a\u043e\u0435 \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u044b \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u2014 5 \u0441. \u0415\u0441\u043b\u0438 \u0432\u0440\u0435\u043c\u044f \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0432 0, \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440. \u0412\u0441\u0435 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0432\u0435\u0434\u0443\u0442\u0441\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u043b\u043e\u0433\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: ElasticApm:LogLevel. <\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f web.config:<\/p>\n<p>  <\/p>\n<pre><code class=\"xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;!-- ... --&gt; &lt;configuration&gt;     &lt;!-- ... --&gt;     &lt;appSettings&gt;         &lt;!-- ... --&gt;         &lt;add key=&quot;ElasticApm:ServerUrls&quot; value=&quot;https:\/\/my-apm-server:8200&quot; \/&gt;         &lt;add key=&quot;ElasticApm:MetricsInterval&quot; value=&quot;10&quot; \/&gt;         &lt;add key=&quot;ElasticApm:Environment&quot; value=&quot;Stage&quot; \/&gt;         &lt;add key=&quot;ElasticApm:ServiceName&quot; value=&quot;Web.Api&quot; \/&gt;         &lt;!-- ... --&gt;     &lt;\/appSettings&gt;     &lt;!-- ... --&gt; &lt;\/configuration&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0434\u0438\u043d\u0438\u0446\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0430\u043a\u0435\u0442\u0430 Elastic APM Agent \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u2014 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0442\u0438\u043f\u0430 ITransaction. \u0414\u0430\u043d\u043d\u044b\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Reporter \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0440\u0430\u0437 \u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u0432\u044b\u0448\u0435.<\/p>\n<p>  <\/p>\n<p>\u0421\u0442\u0430\u0440\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 StartTransaction, \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430 End(). \u041f\u043e\u043c\u0438\u043c\u043e \u043c\u0435\u0442\u043e\u0434\u0434\u0430 StartTransaction \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 CaptureTransaction, \u043d\u043e \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 End(), \u0432\u0441\u0451 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 \u043c\u0435\u0442\u043e\u0434. \u0415\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u0442\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434 CaptureException.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0437\u0430\u043f\u0438\u0448\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0435 \u0435\u0439 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435. \u0412\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u2014 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u2014 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 Labels \u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. <\/p>\n<p>  <\/p>\n<p>\u0418\u0442\u0430\u043a, \u0447\u0442\u043e \u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c? \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u043d\u0430\u0441 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 ITransaction:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">public class MeasurementData {     public static string ApmServerUrl =&gt; ConfigurationManager.AppSettings[&quot;ElasticApm:ServerUrls&quot;] ?? &quot;http:\/\/localhost:8200&quot;;      public static string MetricsInterval =&gt; ConfigurationManager.AppSettings[&quot;ElasticApm:MetricsInterval&quot;] ?? &quot;10&quot;;      public static string ApmEnvironment =&gt; ConfigurationManager.AppSettings[&quot;ElasticApm:Environment&quot;] ?? &quot;local&quot;;      public static string ServiceName =&gt; ConfigurationManager.AppSettings[&quot;ElasticApm:ServiceName&quot;] ?? &quot;Api&quot;;      public ITransaction MetricsObject { get; set; }      \/\/ \u0421\u043e\u0437\u0434\u0430\u0451\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438     public static ITransaction Create(string metricsName)     {         Environment.SetEnvironmentVariable(ConfigConsts.EnvVarNames.ServerUrls, ApmServerUrl);         Environment.SetEnvironmentVariable(ConfigConsts.EnvVarNames.MetricsInterval, MetricsInterval);         Environment.SetEnvironmentVariable(ConfigConsts.EnvVarNames.Environment, ApmEnvironment);         Environment.SetEnvironmentVariable(ConfigConsts.EnvVarNames.ServiceName, ServiceName);         return Agent.Tracer.StartTransaction(metricsName, ApiConstants.TypeRequest);     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435, \u0441\u043e\u0437\u0434\u0430\u0451\u043c builder, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0435\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">public class MetricsBuilder {     private readonly MeasurementData _measurementData = new MeasurementData();     private string _metricName;      public void BuildMetrics(string metricName)     {         \/\/ \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043c\u0435\u0442\u0440\u0438\u043a\u0438, \u043e\u0431\u043e\u0431\u0449\u0430\u0435\u043c \u0435\u0433\u043e         _metricName = string.IsNullOrEmpty(metricName) ? &quot;api_payment_request_duration&quot; : metricName;         CheckAndCreateMetricsObjects();     }      \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0439 \u043c\u0435\u0442\u0440\u0438\u043a\u0438     public void AddMetricsLabels(string key, string value)     {         CheckAndCreateMetricsObjects();          if (!_measurementData.MetricsObject.Labels.ContainsKey(key))         {             _measurementData.MetricsObject.Labels.Add(key, value);             return;         }          _measurementData.MetricsObject.Labels[key] = value;     }      \/\/ \u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435     public void CaptureMetricException(Exception exception)     {         CheckAndCreateMetricsObjects();         _measurementData.MetricsObject.CaptureException(exception);     }      public void Dispose()     {         CheckAndCreateMetricsObjects();         _measurementData.MetricsObject.End();     }      \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0431\u044b\u043b\u0430 \u043b\u0438 \u0443\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u043d \u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u0435\u0442\u0440\u0438\u043a\u0438,     \/\/ \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 - \u0441\u043e\u0437\u0434\u0430\u0435\u043c     private void CheckAndCreateMetricsObjects()     {         if (_measurementData.MetricsObject == null)         {             _measurementData.MetricsObject = MeasurementData.Create(_metricName);              Logger.Info($&quot;{nameof(MetricsBuilder)}: CurrentTransaction: {JsonConvert.SerializeObject(Agent.Tracer.CurrentTransaction)} &quot; +                         $&quot;ServerUrls: {JsonConvert.SerializeObject(Agent.Config.ServerUrls)} &quot; +                         $&quot;MetricsIntervalInMilliseconds: {JsonConvert.SerializeObject(Agent.Config.MetricsIntervalInMilliseconds)}&quot;);         }     } }  public interface IMetricsService : IDisposable {     void AddMetricsLabels(string key, string value);      void CaptureMetricException(string message, Exception exception); }  public class MetricsService : IMetricsService {     private readonly MetricsBuilder _builder;      public MetricsService(string metricName)     {         _builder = new MetricsBuilder();         Build(metricName);     }      public void AddMetricsLabels(string key, string value)     {         try         {             _builder.AddMetricsLabels(key, value);         }         catch (Exception exception)         {             CaptureMetricException(&quot;Can't write metrics labels&quot;, exception);         }     }      public void CaptureMetricException(string message, Exception exception)     {         Logger.Error(message, exception);          try         {             _builder.CaptureMetricException(exception);         }         catch (Exception exec)         {             Logger.Error(&quot;Can't write capture exception of metrics&quot;, exec);         }     }      public void Dispose()     {         try         {             _builder.Dispose();         }         catch (Exception exception)         {             CaptureMetricException($&quot;Can't to do correct dispose of object: {typeof(MetricsService)}&quot;, exception);         }     }      private void Build(string metricName)     {         try         {             _builder.BuildMetrics(metricName);         }         catch (Exception exception)         {             CaptureMetricException(&quot;Can't create metrics object&quot;, exception);         }     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043c\u0435\u0442\u0440\u0438\u043a\u0438, \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441 MetricsService. \u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430 \u0434\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">public class TestClientMetricsService : ITestObject {     private readonly ITestObject _testObject;      public GatewayClientMetricsService(ITestObject testObject)     {         _testObject = testObject;     }      public ProcessingResult TestMethod()     {         return WriteMetrics(&quot;Test_Method&quot;, () =&gt; _testObject.Test());     }      private ProcessingResult WriteMetrics(string methodName, Func&lt;Result&gt; testMethod)     {         using (var metricsService = new MetricsService(methodName))         {             try             {                 metricsService.AddMetricsLabels(&quot;assemblyName&quot;, Assembly.GetCallingAssembly().FullName);                  var requestResult = testMethod();                  metricsService.AddMetricsLabels(&quot;success&quot;, requestResult?.Success.ToString() ?? &quot;false&quot;);                 metricsService.AddMetricsLabels(&quot;resultCode&quot;, requestResult?.GetResultCode().ToString());                  return requestResult;             }             catch (Exception exception)             {                 metricsService.CaptureMetricException(&quot;Can't write metrics&quot;, exception);                 throw;             }         }     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043c\u043e\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0434\u043b\u044f \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Elastic APM \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 \u043c\u0435\u0442\u0440\u0438\u043a.<cut><\/cut><\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/485996\/\"> https:\/\/habr.com\/ru\/post\/485996\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/485996\/\">\n<p>\u041c\u043d\u043e\u0439 \u0434\u0430\u0432\u043d\u043e \u043d\u0435 \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0441\u0442\u0430\u0442\u044c\u0438 \u0438 \u0432\u043e\u0442 \u043e\u043f\u044f\u0442\u044c\u2026 \u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0430\u0441\u044c \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u043d\u043e, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439. \u041a\u043e\u0433\u0434\u0430-\u0442\u043e \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 \u043c\u0435\u0442\u0440\u0438\u043a Prometheus, \u043d\u043e\u2026 \u0421\u043f\u0443\u0441\u0442\u044f \u0432\u0440\u0435\u043c\u044f, \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 Elastic APM, \u0442. \u043a. \u0432\u0435\u0441\u044c \u0441\u0442\u0435\u043a \u0434\u043b\u044f Elastic \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0431\u044b\u043b \u0438 \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-297919","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=297919"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297919\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}