Skip to content

Commit

Permalink
Upgrade to .NET 6 and use OTEL
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmanuelthiel committed Nov 27, 2023
1 parent 5ee243e commit a0aa103
Show file tree
Hide file tree
Showing 23 changed files with 177 additions and 253 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"preLaunchTask": "build-api",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/api/MicroCommunication.Api/bin/Debug/netcoreapp3.1/MicroCommunication.Api.dll",
"program": "${workspaceFolder}/src/api/MicroCommunication.Api/bin/Debug/net6.0/MicroCommunication.Api.dll",
"args": [],
"cwd": "${workspaceFolder}/src/api/MicroCommunication.Api",
"stopAtEntry": false,
Expand Down Expand Up @@ -39,7 +39,7 @@
"request": "launch",
"preLaunchTask": "build-random",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/random/MicroCommunication.Random/bin/Debug/netcoreapp3.1/MicroCommunication.Random.dll",
"program": "${workspaceFolder}/src/random/MicroCommunication.Random/bin/Debug/net6.0/MicroCommunication.Random.dll",
"args": [],
"cwd": "${workspaceFolder}/src/random/MicroCommunication.Random",
"stopAtEntry": false,
Expand Down
28 changes: 19 additions & 9 deletions MicroCommunication.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7D064DB9-6A6E-4313-81F0-1FFE0615252A}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D7FE2636-9C88-4352-A20B-A3E453ED4B7B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "api", "api", "{400B24CC-3A26-4E7A-BC17-EA227698DB80}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "api", "api", "{B8E23601-389C-4123-A07C-260AFA25D2FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroCommunication.Api", "src\api\MicroCommunication.Api\MicroCommunication.Api.csproj", "{DA4B2ACC-4BA2-42F7-A631-61551B927263}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroCommunication.Api.Tests", "src\api\MicroCommunication.Api.Tests\MicroCommunication.Api.Tests.csproj", "{6DD3A4E7-FF6D-46EA-9379-E8B60800E229}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "random", "random", "{54D10750-06DD-4CA5-911E-B25D0D4C96FE}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "random", "random", "{341CF29A-2125-465F-A82C-E3B05EBB95D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroCommunication.Random", "src\random\MicroCommunication.Random\MicroCommunication.Random.csproj", "{DA4B2ACC-4BA2-42F7-A631-61551B927263}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroCommunication.Random", "src\random\MicroCommunication.Random\MicroCommunication.Random.csproj", "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroCommunication.Api.Tests", "src\random\MicroCommunication.Random.Tests\MicroCommunication.Api.Tests.csproj", "{6DD3A4E7-FF6D-46EA-9379-E8B60800E229}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroCommunication.Random.Tests", "src\random\MicroCommunication.Random.Tests\MicroCommunication.Random.Tests.csproj", "{6DD3A4E7-FF6D-46EA-9379-E8B70800E221}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -34,11 +34,21 @@ Global
{6DD3A4E7-FF6D-46EA-9379-E8B60800E229}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DD3A4E7-FF6D-46EA-9379-E8B60800E229}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DD3A4E7-FF6D-46EA-9379-E8B60800E229}.Release|Any CPU.Build.0 = Release|Any CPU
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBE}.Release|Any CPU.Build.0 = Release|Any CPU
{6DD3A4E7-FF6D-46EA-9379-E8B70800E221}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6DD3A4E7-FF6D-46EA-9379-E8B70800E221}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DD3A4E7-FF6D-46EA-9379-E8B70800E221}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DD3A4E7-FF6D-46EA-9379-E8B70800E221}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{400B24CC-3A26-4E7A-BC17-EA227698DB80} = {7D064DB9-6A6E-4313-81F0-1FFE0615252A}
{DA4B2ACC-4BA2-42F7-A631-61551B927263} = {54D10750-06DD-4CA5-911E-B25D0D4C96FE}
{6DD3A4E7-FF6D-46EA-9379-E8B60800E229} = {54D10750-06DD-4CA5-911E-B25D0D4C96FE}
{54D10750-06DD-4CA5-911E-B25D0D4C96FE} = {7D064DB9-6A6E-4313-81F0-1FFE0615252A}
{B8E23601-389C-4123-A07C-260AFA25D2FB} = {D7FE2636-9C88-4352-A20B-A3E453ED4B7B}
{DA4B2ACC-4BA2-42F7-A631-61551B927263} = {B8E23601-389C-4123-A07C-260AFA25D2FB}
{6DD3A4E7-FF6D-46EA-9379-E8B60800E229} = {B8E23601-389C-4123-A07C-260AFA25D2FB}
{341CF29A-2125-465F-A82C-E3B05EBB95D6} = {D7FE2636-9C88-4352-A20B-A3E453ED4B7B}
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBE} = {341CF29A-2125-465F-A82C-E3B05EBB95D6}
{6DD3A4E7-FF6D-46EA-9379-E8B70800E221} = {341CF29A-2125-465F-A82C-E3B05EBB95D6}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ You need to set some environment variables to configure the services and their d

- `ApiKey=test123` _Optional: The key, that the API allows for authorization_
- `RedisCacheConnectionString=...` _Optional: The connection string for a Redis Cache to sync SignalR Hubs_
- `ApplicationInsightsInstrumentationKey=...` _Optional: The Azure Application Insights Instrumentation Key_
- `ApplicationInsightsConnectionString=...` _Optional: The Azure Application Insights Instrumentation Key_
- `Cors` _Optional: The domain of your web app to add to the CORS_
- `RandomApiUrl` _The URL to connect to the Random Service_

`MicroCommunication.Random`:

- `MongoDbConnectionString=mongo://...` _Optional: The connection string for a Mongo DB to store the history in_
- `ApplicationInsightsInstrumentationKey=...` _Optional: The Azure Application Insights Instrumentation Key_
- `ApplicationInsightsConnectionString=...` _Optional: The Azure Application Insights Instrumentation Key_
- `Cors` _Optional: The domain of your web app to add to the CORS_

`MicroCommunication.Web`:
Expand Down
4 changes: 2 additions & 2 deletions src/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#######################################################
# Download the official ASP.NET Core SDK image
# to build the project while creating the docker image
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build
FROM mcr.microsoft.com/dotnet/sdk:6 as build
WORKDIR /app

# Restore NuGet packages
Expand All @@ -27,7 +27,7 @@ RUN dotnet test --configuration Release MicroCommunication.Api.Tests/MicroCommun
#######################################################
# Download the official ASP.NET Core Runtime image
# to run the compiled application
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
FROM mcr.microsoft.com/dotnet/aspnet:6
WORKDIR /app

# Open port
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ProjectGuid>{6DD3A4E7-FF6D-46EA-9379-E8B60800E229}</ProjectGuid>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.8.1">
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Moq" Version="4.14.5" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 13 additions & 7 deletions src/api/MicroCommunication.Api/MicroCommunication.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ProjectGuid>{DA4B2ACC-4BA2-42F7-A631-61551B927263}</ProjectGuid>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="3.1.2" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.13.1" />
<PackageReference Include="prometheus-net.AspNetCore" Version="3.5.0" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.0.0-beta.6" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.6.0-rc.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.6.0-beta.3" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.7" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.5.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="6.0.25" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.1.0" />
<PackageReference Include="RandomNameGeneratorLibrary" Version="1.2.2.0" />
<PackageReference Include="Refit.HttpClientFactory" Version="5.2.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
<PackageReference Include="Refit" Version="5.2.4" />
<PackageReference Include="Refit.HttpClientFactory" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Refit" Version="7.0.0" />
</ItemGroup>

<PropertyGroup>
Expand Down
49 changes: 38 additions & 11 deletions src/api/MicroCommunication.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Prometheus;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using RandomNameGeneratorLibrary;
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Refit;

namespace MicroCommunication.Api
Expand Down Expand Up @@ -45,13 +48,6 @@ public void ConfigureServices(IServiceCollection services)
});
}

// Logging
if (!string.IsNullOrEmpty(Configuration["ApplicationInsightsInstrumentationKey"]))
{
services.AddApplicationInsightsTelemetry(Configuration["ApplicationInsightsInstrumentationKey"]);
Console.WriteLine("Application Insights configured.");
}

// Register Random API
services
.AddRefitClient<IRandomApi>()
Expand All @@ -67,11 +63,42 @@ public void ConfigureServices(IServiceCollection services)
var environment = string.IsNullOrEmpty(Configuration["EnvironmentName"]) ? "Default" : Configuration["EnvironmentName"];
Console.WriteLine("My environment is: " + environment);

// Monitoring
services
.AddOpenTelemetry()
.WithMetrics(builder =>
{
builder.AddRuntimeInstrumentation();
builder.AddHttpClientInstrumentation();
builder.AddAspNetCoreInstrumentation();
builder.AddPrometheusExporter();
})
.WithTracing(builder =>
{
builder.AddAspNetCoreInstrumentation();
builder.AddEntityFrameworkCoreInstrumentation();
builder.ConfigureResource((resource) =>
{
resource.AddService("API", "MicroCommunication", Assembly.GetExecutingAssembly().GetName().Version!.ToString(), false, name);
});
});

if (!string.IsNullOrEmpty(Configuration["ApplicationInsightsConnectionString"]))
{
services.AddOpenTelemetry().UseAzureMonitor(options =>
{
options.ConnectionString = Configuration["ApplicationInsightsConnectionString"];
});
Console.WriteLine("Using Azure Application Insights");
}

// Enforce lowercase routes
services.AddRouting(options => options.LowercaseUrls = true);

services.AddControllers();

services.AddHealthChecks();

// SignalR
var signalR = services.AddSignalR();
if (!string.IsNullOrEmpty(Configuration["RedisCacheConnectionString"]))
Expand Down Expand Up @@ -156,13 +183,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseEndpoints(endpoints =>
{
// Add Prometheus server
endpoints.MapMetrics();

endpoints.MapControllers();
endpoints.MapHealthChecks("/healthz");
endpoints.MapHub<ChatHub>("/chat");
});

app.UseOpenTelemetryPrometheusScrapingEndpoint();

app.UseSwagger();
app.UseSwaggerUI(c =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"Database": "MongoDB",
"RedisCacheConnectionString": "",
"Cors": "",
"ApplicationInsightsInstrumentationKey": "",
"ApplicationInsightsConnectionString": "InstrumentationKey=53927c90-74fa-4066-ab1a-e7d206abf5e9;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com/",
"EnvironmentName": "",
"RandomApiUrl": "http://localhost:8090"
}
2 changes: 1 addition & 1 deletion src/api/MicroCommunication.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"AllowedHosts": "*",
"ApiKey": "",
"Cors": "",
"ApplicationInsightsInstrumentationKey": "",
"ApplicationInsightsConnectionString": "",
"EnvironmentName": "",
"RandomApiUrl": ""
}
4 changes: 2 additions & 2 deletions src/random/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#######################################################
# Download the official ASP.NET Core SDK image
# to build the project while creating the docker image
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build
FROM mcr.microsoft.com/dotnet/sdk:6 as build
WORKDIR /app

# Restore NuGet packages
Expand All @@ -26,7 +26,7 @@ RUN dotnet test --configuration Release MicroCommunication.Random.Tests/MicroCom
#######################################################
# Download the official ASP.NET Core Runtime image
# to run the compiled application
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
FROM mcr.microsoft.com/dotnet/aspnet:6
WORKDIR /app

# Open port
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ProjectGuid>{6DD3A4E7-FF6D-46EA-9379-E8B60800E229}</ProjectGuid>
<TargetFramework>net6.0</TargetFramework>
<ProjectGuid>{6DD3A4E7-FF6D-46EA-9379-E8B70800E221}</ProjectGuid>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.8.1">
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Moq" Version="4.14.5" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit a0aa103

Please sign in to comment.