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..cef3d60 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; +using System.Diagnostics.Metrics; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; @@ -6,14 +8,23 @@ 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..d9df369 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,38 @@ +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.AddScoped(); + +builder.Services.AddOpenTelemetry() + .ConfigureResource(resource => resource.AddService("TelemetryExample")) + .WithTracing(tracing => tracing + .AddAspNetCoreInstrumentation() + .AddConsoleExporter()) + .WithMetrics(metrics => metrics + .AddPrometheusExporter() + .AddAspNetCoreInstrumentation() + .AddConsoleExporter() + .AddMeter(nameof(Metrics))); + + + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -13,6 +43,7 @@ app.UseHsts(); } + app.UseHttpsRedirection(); app.UseStaticFiles(); @@ -22,4 +53,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..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..9ad5785 --- /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 + + + + + + + + + +