Skip to content
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
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["telemetry.csproj", "./"]
RUN dotnet restore "telemetry.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "./telemetry.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./telemetry.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "telemetry.dll"]
14 changes: 12 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,22 @@ 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);
}
}
29 changes: 29 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +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.Services.AddSingleton<Metrics>();

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)));

var app = builder.Build();

Expand All @@ -13,6 +39,7 @@
app.UseHsts();
}


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

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

app.MapRazorPages();

app.UseOpenTelemetryPrometheusScrapingEndpoint();

app.Run();
5 changes: 5 additions & 0 deletions WeatherApp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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", "{467A809B-62BF-469C-8DC3-72E75DC53870}"
ProjectSection(SolutionItems) = preProject
compose.yaml = compose.yaml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
22 changes: 22 additions & 0 deletions cl.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);
}
}
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"
6 changes: 6 additions & 0 deletions prometheus.yml
Original file line number Diff line number Diff line change
@@ -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"]
11 changes: 11 additions & 0 deletions telemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

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

</Project>