From 8ba7ce29ab8a8204df8c334274ecd6a012c8470c Mon Sep 17 00:00:00 2001 From: Konstantin Kolesnikov Date: Mon, 10 Nov 2025 12:54:09 +0500 Subject: [PATCH 1/2] feat: prometheus and grafana --- Metrics.cs | 22 ++++++++++++++++++++++ docker-compose.yaml | 12 ++++++++++++ prometheus.yml | 6 ++++++ 3 files changed, 40 insertions(+) create mode 100644 Metrics.cs create mode 100644 docker-compose.yaml create mode 100644 prometheus.yml diff --git a/Metrics.cs b/Metrics.cs new file mode 100644 index 0000000..204d732 --- /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/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..34b44cc --- /dev/null +++ b/docker-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..0660196 --- /dev/null +++ b/prometheus.yml @@ -0,0 +1,6 @@ +scrape_configs: + - job_name: "prometheus" + # Override the global default and scrape targets from this job every 5 seconds. + scrape_interval: 5s + static_configs: + - targets: ["host.docker.internal:5149"] \ No newline at end of file From e89a4826202e442deea5d7d8308ceb3caab37e62 Mon Sep 17 00:00:00 2001 From: Konstantin Kolesnikov Date: Mon, 10 Nov 2025 14:12:03 +0500 Subject: [PATCH 2/2] wip --- Pages/Index.cshtml.cs | 17 +++++++++++++---- Program.cs | 30 ++++++++++++++++++++++++++++++ WeatherApp.sln | 5 +++++ telemetry.csproj | 11 +++++++++++ 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 34a599f..6834dc4 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; @@ -5,15 +6,23 @@ namespace telemetry.Pages; public class IndexModel : PageModel { - private readonly ILogger _logger; + private readonly ILogger logger; + private readonly Metrics metrics; - public IndexModel(ILogger logger) + public IndexModel(ILogger logger, Metrics metrics) { - _logger = logger; + this.logger = logger; + this.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..062ff68 100644 --- a/Program.cs +++ b/Program.cs @@ -1,10 +1,40 @@ +using OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; +using telemetry; + var builder = WebApplication.CreateBuilder(args); +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(); + // Add services to the container. builder.Services.AddRazorPages(); var app = builder.Build(); +app.UseOpenTelemetryPrometheusScrapingEndpoint(); + // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/WeatherApp.sln b/WeatherApp.sln index fea1c6b..238172a 100644 --- a/WeatherApp.sln +++ b/WeatherApp.sln @@ -5,6 +5,11 @@ VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "telemetry", "telemetry.csproj", "{318D0823-22FB-4FE3-94F2-7391C3B7CEEB}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6734A4AF-1058-4C5E-A10C-1CE50164AA04}" + ProjectSection(SolutionItems) = preProject + docker-compose.yaml = docker-compose.yaml + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/telemetry.csproj b/telemetry.csproj index 1b28a01..27e0ff0 100644 --- a/telemetry.csproj +++ b/telemetry.csproj @@ -4,6 +4,17 @@ net8.0 enable enable + Linux + + + + + + + + + +