From 600e69f26493722f9a5ddf6daf6f5673532934d0 Mon Sep 17 00:00:00 2001 From: "n.aleksandrov" Date: Tue, 19 Nov 2024 15:48:21 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B1=D0=B5=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Metrics.cs | 22 ++++++++++++++++++++++ Pages/Index.cshtml.cs | 14 +++++++++++--- Program.cs | 29 +++++++++++++++++++++++++++-- compose.yaml | 12 ++++++++++++ prometheus.yml | 5 +++++ telemetry.csproj | 10 ++++++++++ 6 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 Metrics.cs create mode 100644 compose.yaml create mode 100644 prometheus.yml diff --git a/Metrics.cs b/Metrics.cs new file mode 100644 index 0000000..30e2524 --- /dev/null +++ b/Metrics.cs @@ -0,0 +1,22 @@ +using System.Diagnostics.Metrics; + +namespace telemetry; + +public class Metrics +{ + private readonly Counter indexRequestsCount; + private readonly Histogram indexRequestsTime; + + public Metrics(IMeterFactory meterFactory) + { + var meter = meterFactory.Create(nameof(Metrics)); + indexRequestsCount = meter.CreateCounter("requests.index.count", "pcs", "Количество запросов"); + indexRequestsTime = meter.CreateHistogram("requests.index.time", "ms", "Время запроса к index"); + } + + public void RequestToIndex(TimeSpan elapsed) + { + indexRequestsCount.Add(1); + indexRequestsTime.Record(elapsed.TotalMilliseconds); + } +} \ No newline at end of file diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 34a599f..f37dcd4 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Mvc; +using System.Diagnostics; using Microsoft.AspNetCore.Mvc.RazorPages; namespace telemetry.Pages; @@ -6,14 +6,22 @@ namespace telemetry.Pages; public class IndexModel : PageModel { private readonly ILogger _logger; + private readonly Metrics _metrics; - public IndexModel(ILogger logger) + public IndexModel(ILogger logger, Metrics metrics) { _logger = logger; + _metrics = metrics; } public void OnGet() { - + var sw = Stopwatch.StartNew(); + for (var i = 0; i < new Random().Next(0, 100); i++) + { + Console.Write("1"); + } + sw.Stop(); + _metrics.RequestToIndex(sw.Elapsed); } } diff --git a/Program.cs b/Program.cs index bc275e4..6c07e2c 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,33 @@ +using OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; +using telemetry; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); - +builder.Logging.AddOpenTelemetry(options => +{ + options + .SetResourceBuilder( + ResourceBuilder.CreateDefault() + .AddService("TelemetryExample")) + .AddConsoleExporter(); +}); +builder.Services.AddOpenTelemetry() + .ConfigureResource(resource => resource.AddService("TelemetryExample")) + .WithTracing(tracing => tracing + .AddAspNetCoreInstrumentation() + .AddConsoleExporter()) + .WithMetrics(metrics => metrics + .AddPrometheusExporter() + .AddAspNetCoreInstrumentation() + .AddConsoleExporter() + .AddMeter(nameof(Metrics))); + +builder.Services.AddSingleton(); var app = builder.Build(); // Configure the HTTP request pipeline. @@ -19,7 +44,7 @@ app.UseRouting(); app.UseAuthorization(); - +app.UseOpenTelemetryPrometheusScrapingEndpoint(); app.MapRazorPages(); app.Run(); diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..34b44cc --- /dev/null +++ b/compose.yaml @@ -0,0 +1,12 @@ +services: + prometheus: + image: "prom/prometheus" + ports: + - "9090:9090" + volumes: + - "./prometheus.yml:/etc/prometheus/prometheus.yml" + + grafana: + image: "grafana/grafana-oss" + ports: + - "3000:3000" \ No newline at end of file diff --git a/prometheus.yml b/prometheus.yml new file mode 100644 index 0000000..4a4026c --- /dev/null +++ b/prometheus.yml @@ -0,0 +1,5 @@ +scrape_configs: + - job_name: "prometheus" + scrape_interval: 5s + static_configs: + - targets: ["host.docker.internal:5149"] \ No newline at end of file diff --git a/telemetry.csproj b/telemetry.csproj index 1b28a01..63e07f3 100644 --- a/telemetry.csproj +++ b/telemetry.csproj @@ -6,4 +6,14 @@ enable + + + + + + + + + +