Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Турмухамбетов Амир, Иванов Стас #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Metrics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Diagnostics.Metrics;

namespace telemetry;

public class Metrics
{
private readonly Counter<int> indexRequestsCount;
private readonly Histogram<double> indexRequestsTime;

public Metrics(IMeterFactory meterFactory)
{
var meter = meterFactory.Create(nameof(Metrics));
indexRequestsCount = meter.CreateCounter<int>("requests.index.count", "pcs", "Количество запросов");
indexRequestsTime = meter.CreateHistogram<double>("requests.index.time", "ms", "Время запроса к index");
}

public void RequestToIndex(TimeSpan elapsed)
{
indexRequestsCount.Add(1);
indexRequestsTime.Record(elapsed.TotalMilliseconds);
}
}
15 changes: 13 additions & 2 deletions Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

Expand All @@ -6,14 +8,23 @@ namespace telemetry.Pages;
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly Metrics _metrics;


public IndexModel(ILogger<IndexModel> logger)
public IndexModel(ILogger<IndexModel> 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);
}
}
35 changes: 34 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
@@ -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<Metrics>();

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.
Expand All @@ -13,6 +43,7 @@
app.UseHsts();
}


app.UseHttpsRedirection();
app.UseStaticFiles();

Expand All @@ -22,4 +53,6 @@

app.MapRazorPages();

app.Run();
app.UseOpenTelemetryPrometheusScrapingEndpoint();

app.Run();
12 changes: 12 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 5 additions & 0 deletions prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
scrape_configs:
- job_name: "prometheus"
scrape_interval: 5s
static_configs:
- targets: ["host.docker.internal:5149"]
10 changes: 10 additions & 0 deletions telemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.10.0-beta.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
</ItemGroup>

</Project>