From a279b4f5116963bcf9497a08b2c47fbcc239d759 Mon Sep 17 00:00:00 2001 From: sh1ney Date: Tue, 19 Nov 2024 15:57:10 +0500 Subject: [PATCH 1/2] 1.5 --- Program.cs | 27 ++++++++++++++++++++++++++- compose.yaml | 12 ++++++++++++ prometheus.yml | 6 ++++++ telemetry.csproj | 10 ++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 compose.yaml create mode 100644 prometheus.yml diff --git a/Program.cs b/Program.cs index bc275e4..5683fa9 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,28 @@ +using OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; + 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()); + // Add services to the container. builder.Services.AddRazorPages(); @@ -22,4 +45,6 @@ app.MapRazorPages(); -app.Run(); +app.UseOpenTelemetryPrometheusScrapingEndpoint(); + +app.Run(); \ No newline at end of file diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..b8a11cb --- /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..2d20d56 --- /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 diff --git a/telemetry.csproj b/telemetry.csproj index 1b28a01..63e07f3 100644 --- a/telemetry.csproj +++ b/telemetry.csproj @@ -6,4 +6,14 @@ enable + + + + + + + + + + From 927ac1e6a92efe7e4f57a79278e2d7720b2b83ec Mon Sep 17 00:00:00 2001 From: ollxko Date: Tue, 19 Nov 2024 16:32:22 +0500 Subject: [PATCH 2/2] 'done' --- Metrics.cs | 22 ++++++++++++++++++++++ Pages/Index.cshtml.cs | 15 +++++++++++++-- Program.cs | 7 +++++-- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 Metrics.cs 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..f249600 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -1,19 +1,30 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using OpenTelemetry.Metrics; +using System.Diagnostics; +using System.Diagnostics.Metrics; namespace telemetry.Pages; public class IndexModel : PageModel { private readonly ILogger _logger; + private readonly Metrics _metrics; - public IndexModel(ILogger logger) + public IndexModel(ILogger logger, IMeterFactory meterFactory) { + _metrics = new Metrics(meterFactory); _logger = logger; } 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 5683fa9..a36d1f5 100644 --- a/Program.cs +++ b/Program.cs @@ -2,6 +2,7 @@ using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; +using telemetry; var builder = WebApplication.CreateBuilder(args); @@ -12,6 +13,7 @@ ResourceBuilder.CreateDefault() .AddService("TelemetryExample")) .AddConsoleExporter(); + }); builder.Services.AddOpenTelemetry() .ConfigureResource(resource => resource.AddService("TelemetryExample")) @@ -21,7 +23,8 @@ .WithMetrics(metrics => metrics .AddPrometheusExporter() .AddAspNetCoreInstrumentation() - .AddConsoleExporter()); + .AddConsoleExporter() + .AddMeter(nameof(Metrics))); // Add services to the container. builder.Services.AddRazorPages(); @@ -47,4 +50,4 @@ app.UseOpenTelemetryPrometheusScrapingEndpoint(); -app.Run(); \ No newline at end of file +app.Run();