From ef11af82b03e0a1a5adf825eec14cbc607a5a341 Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 13:54:08 +0100 Subject: [PATCH 01/10] Idiomatic C# formatting - Use idiomatic formatting for C# (4 spaces, CRLF). - Avoid repetition with constants for OTLP endpoint. - Make endpoint handler methods `static`. --- .editorconfig | 6 ++ examples/dotnet/Program.cs | 117 ++++++++++++++++++------------------- 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/.editorconfig b/.editorconfig index 7b9556a1..34d8e4d0 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,6 +4,12 @@ root = true max_line_length = 100 indent_size = 2 +[*.cs] +indent_size = 4 +insert_final_newline = true +end_of_line = crlf +max_line_length = unset + [*.md] max_line_length = 120 diff --git a/examples/dotnet/Program.cs b/examples/dotnet/Program.cs index ffb24f32..08b04ce4 100644 --- a/examples/dotnet/Program.cs +++ b/examples/dotnet/Program.cs @@ -3,7 +3,6 @@ using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; -using Microsoft.AspNetCore.Mvc; using System.Globalization; var appBuilder = WebApplication.CreateBuilder(args); @@ -14,39 +13,41 @@ serviceVersion: typeof(Program).Assembly.GetName().Version?.ToString() ?? "unknown", serviceInstanceId: Environment.MachineName); -// Configure OpenTelemetry tracing & metrics with auto-start using the -// AddOpenTelemetry extension from OpenTelemetry.Extensions.Hosting. +const string DefaultEndpoint = "http://localhost:4317"; + +// Configure OpenTelemetry tracing and metrics with auto-start using the +// AddOpenTelemetry() extension method from the OpenTelemetry.Extensions.Hosting package. appBuilder.Services.AddOpenTelemetry() .ConfigureResource(configureResource) .WithTracing(builder => - { - builder - .AddHttpClientInstrumentation() - .AddAspNetCoreInstrumentation(); - - // Use IConfiguration binding for AspNetCore instrumentation options. - appBuilder.Services.Configure( - appBuilder.Configuration.GetSection("AspNetCoreInstrumentation")); - - builder.AddOtlpExporter(otlpOptions => - { - // Use IConfiguration directly for Otlp exporter endpoint option. - otlpOptions.Endpoint = new Uri(appBuilder.Configuration.GetValue( - "Otlp:Endpoint", defaultValue: "http://localhost:4317")!); - }); + { + builder + .AddHttpClientInstrumentation() + .AddAspNetCoreInstrumentation(); + + // Use IConfiguration binding for AspNetCore instrumentation options. + appBuilder.Services.Configure( + appBuilder.Configuration.GetSection("AspNetCoreInstrumentation")); + + builder.AddOtlpExporter(otlpOptions => + { + // Use IConfiguration directly for OTLP exporter endpoint option. + otlpOptions.Endpoint = new Uri( + appBuilder.Configuration.GetValue("Otlp:Endpoint", defaultValue: DefaultEndpoint)!); + }); }) .WithMetrics(builder => - { - builder - .AddHttpClientInstrumentation() - .AddAspNetCoreInstrumentation(); - - builder.AddOtlpExporter(otlpOptions => - { - // Use IConfiguration directly for Otlp exporter endpoint option. - otlpOptions.Endpoint = new Uri(appBuilder.Configuration.GetValue( - "Otlp:Endpoint", defaultValue: "http://localhost:4317")!); - }); + { + builder + .AddHttpClientInstrumentation() + .AddAspNetCoreInstrumentation(); + + builder.AddOtlpExporter(otlpOptions => + { + // Use IConfiguration directly for OTLP exporter endpoint option. + otlpOptions.Endpoint = new Uri( + appBuilder.Configuration.GetValue("Otlp:Endpoint", defaultValue: DefaultEndpoint)!); + }); }); // Clear default logging providers used by WebApplication host. @@ -54,44 +55,40 @@ // Configure OpenTelemetry Logging. appBuilder.Logging.AddOpenTelemetry(options => -{ - // Note: See appsettings.json Logging:OpenTelemetry section for configuration. - - var resourceBuilder = ResourceBuilder.CreateDefault(); - configureResource(resourceBuilder); - options.SetResourceBuilder(resourceBuilder); - - options.AddOtlpExporter(otlpOptions => - { - // Use IConfiguration directly for Otlp exporter endpoint option. - otlpOptions.Endpoint = new Uri(appBuilder.Configuration.GetValue( - "Otlp:Endpoint", defaultValue: "http://localhost:4317")!); - }); +{ + // See appsettings.json "Logging:OpenTelemetry" section for configuration. + var resourceBuilder = ResourceBuilder.CreateDefault(); + configureResource(resourceBuilder); + options.SetResourceBuilder(resourceBuilder); + + options.AddOtlpExporter(otlpOptions => + { + // Use IConfiguration directly for OTLP exporter endpoint option. + otlpOptions.Endpoint = new Uri( + appBuilder.Configuration.GetValue( "Otlp:Endpoint", defaultValue: DefaultEndpoint)!); + }); }); var app = appBuilder.Build(); -string HandleRollDice([FromServices] ILogger logger, string? player) -{ - var result = RollDice(); - - if (string.IsNullOrEmpty(player)) - { - logger.LogInformation("Anonymous player is rolling the dice: {result}", result); - } - else - { - logger.LogInformation("{player} is rolling the dice: {result}", player, result); - } - - return result.ToString(CultureInfo.InvariantCulture); -} +static string HandleRollDice(string? player, ILogger logger) +{ + var result = RollDice(); -int RollDice() -{ - return Random.Shared.Next(1, 7); + if (string.IsNullOrEmpty(player)) + { + logger.LogInformation("Anonymous player is rolling the dice: {result}", result); + } + else + { + logger.LogInformation("{player} is rolling the dice: {result}", player, result); + } + + return result.ToString(CultureInfo.InvariantCulture); } +static int RollDice() => Random.Shared.Next(1, 7); + app.MapGet("/rolldice/{player?}", HandleRollDice); app.Run(); From 26e4dbabdceaf7a7805cd3135082c95c5013eb78 Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 13:54:28 +0100 Subject: [PATCH 02/10] Avoid preview SDKs Avoid using .NET preview SDKs if installed. --- examples/dotnet/global.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 examples/dotnet/global.json diff --git a/examples/dotnet/global.json b/examples/dotnet/global.json new file mode 100644 index 00000000..47a7fa60 --- /dev/null +++ b/examples/dotnet/global.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "allowPrerelease": false + } +} From 0522ac29da1d8fc97eb9622e5afd97b3f3bc0258 Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 13:54:57 +0100 Subject: [PATCH 03/10] Launch to endpoint Launch the app to the `/rolldice` endpoint to avoid an HTTP 404 when navigating to the root URL. --- examples/dotnet/Properties/launchSettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dotnet/Properties/launchSettings.json b/examples/dotnet/Properties/launchSettings.json index c2c48db0..77dd35ee 100644 --- a/examples/dotnet/Properties/launchSettings.json +++ b/examples/dotnet/Properties/launchSettings.json @@ -5,7 +5,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:8083" + "applicationUrl": "http://localhost:8083/rolldice" } } } From 76c77af3eb901e9ae757c9b601cdfda5b70ff75b Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 13:55:15 +0100 Subject: [PATCH 04/10] Update OpenTelemetry packages Update the OpenTelemetry NuGet packages to the latest stable release. --- examples/dotnet/rolldice.csproj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/dotnet/rolldice.csproj b/examples/dotnet/rolldice.csproj index c0923df3..f32e31ba 100644 --- a/examples/dotnet/rolldice.csproj +++ b/examples/dotnet/rolldice.csproj @@ -7,11 +7,11 @@ - - - - - + + + + + From cfc0c2b8623a933d26f1e3c7659b9655fbc2ddef Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 13:56:16 +0100 Subject: [PATCH 05/10] Simplify .NET Dockerfile - Simplify by just publishing the app. - Add cache for NuGet packages. - Use `ASPNETCORE_HTTP_PORTS`. --- examples/dotnet/Dockerfile | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/examples/dotnet/Dockerfile b/examples/dotnet/Dockerfile index 2a70e1cd..f396e42e 100644 --- a/examples/dotnet/Dockerfile +++ b/examples/dotnet/Dockerfile @@ -1,24 +1,20 @@ -FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base -WORKDIR /app -EXPOSE 8083 +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:9.0 AS build +ARG TARGETARCH +ARG CONFIGURATION=Release -ENV ASPNETCORE_URLS=http://+:8083 +COPY . /source +WORKDIR /source -USER app -FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:9.0 AS build -ARG configuration=Release -WORKDIR /src -COPY ["rolldice.csproj", "/"] -RUN dotnet restore "/rolldice.csproj" -COPY . . -WORKDIR "/src/" -RUN dotnet build "rolldice.csproj" -c $configuration -o /app/build +SHELL ["/bin/bash", "-o", "pipefail", "-c"] -FROM build AS publish -ARG configuration=Release -RUN dotnet publish "rolldice.csproj" -c $configuration -o /app/publish /p:UseAppHost=false +RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \ + dotnet publish "rolldice.csproj" --arch "${TARGETARCH}" --configuration "${CONFIGURATION}" --output /app /p:UseAppHost=false -FROM base AS final +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS final WORKDIR /app -COPY --from=publish /app/publish . +EXPOSE 8083 + +ENV ASPNETCORE_HTTP_PORTS=8083 + +COPY --from=build /app . ENTRYPOINT ["dotnet", "rolldice.dll"] From 026ce69a19bebde7faf1ac647ce96c6d75c1df44 Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 14:04:10 +0100 Subject: [PATCH 06/10] Fix formatting Remove rogue space. --- examples/dotnet/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dotnet/Program.cs b/examples/dotnet/Program.cs index 08b04ce4..d084a76a 100644 --- a/examples/dotnet/Program.cs +++ b/examples/dotnet/Program.cs @@ -65,7 +65,7 @@ { // Use IConfiguration directly for OTLP exporter endpoint option. otlpOptions.Endpoint = new Uri( - appBuilder.Configuration.GetValue( "Otlp:Endpoint", defaultValue: DefaultEndpoint)!); + appBuilder.Configuration.GetValue("Otlp:Endpoint", defaultValue: DefaultEndpoint)!); }); }); From b8578c594d8948495c39a99b3455870ef90fa3ba Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 14:05:36 +0100 Subject: [PATCH 07/10] Avoid redundant work Disable .NET SDK features we don't need to speed things up. --- .github/workflows/super-linter.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/super-linter.yml b/.github/workflows/super-linter.yml index 6ac4f9bb..3fc241c4 100644 --- a/.github/workflows/super-linter.yml +++ b/.github/workflows/super-linter.yml @@ -27,5 +27,8 @@ jobs: - name: Super-linter uses: super-linter/super-linter@12150456a73e248bdc94d0794898f94e23127c88 # v7.4.0 env: + DOTNET_GENERATE_ASPNET_CERTIFICATE: false + DOTNET_NOLOGO: true + NUGET_XMLDOC_MODE: skip # To report GitHub Actions status checks GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 749a1462fec945319b8f3f337f03548606362fe8 Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 14:18:18 +0100 Subject: [PATCH 08/10] Increase line length Arbitrarily set 180. --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 34d8e4d0..aecff513 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,7 +8,7 @@ indent_size = 2 indent_size = 4 insert_final_newline = true end_of_line = crlf -max_line_length = unset +max_line_length = 180 [*.md] max_line_length = 120 From fafbda7afee156a19f4e435e381b8025744ed058 Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 14:19:42 +0100 Subject: [PATCH 09/10] Move environment variables Move into `super-linter.env`. --- .github/super-linter.env | 4 ++++ .github/workflows/super-linter.yml | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/super-linter.env b/.github/super-linter.env index c31c3f62..2cb9912a 100644 --- a/.github/super-linter.env +++ b/.github/super-linter.env @@ -11,6 +11,10 @@ VALIDATE_GO_MODULES=false VALIDATE_JAVASCRIPT_STANDARD=false # we have many duplicate code in our codebase for demo purposes VALIDATE_JSCPD=false +# Avoid redundant work linting C# files +DOTNET_GENERATE_ASPNET_CERTIFICATE=false +DOTNET_NOLOGO=true +NUGET_XMLDOC_MODE=skip FIX_CSHARP=true FIX_ENV=true diff --git a/.github/workflows/super-linter.yml b/.github/workflows/super-linter.yml index 3fc241c4..6ac4f9bb 100644 --- a/.github/workflows/super-linter.yml +++ b/.github/workflows/super-linter.yml @@ -27,8 +27,5 @@ jobs: - name: Super-linter uses: super-linter/super-linter@12150456a73e248bdc94d0794898f94e23127c88 # v7.4.0 env: - DOTNET_GENERATE_ASPNET_CERTIFICATE: false - DOTNET_NOLOGO: true - NUGET_XMLDOC_MODE: skip # To report GitHub Actions status checks GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From b216db141f76c2115db5e09d3c8f3810217d6d98 Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 19 May 2025 14:28:21 +0100 Subject: [PATCH 10/10] Update super-linter.env Fix sorting(?) --- .github/super-linter.env | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/super-linter.env b/.github/super-linter.env index 2cb9912a..0d2b8e97 100644 --- a/.github/super-linter.env +++ b/.github/super-linter.env @@ -11,6 +11,7 @@ VALIDATE_GO_MODULES=false VALIDATE_JAVASCRIPT_STANDARD=false # we have many duplicate code in our codebase for demo purposes VALIDATE_JSCPD=false + # Avoid redundant work linting C# files DOTNET_GENERATE_ASPNET_CERTIFICATE=false DOTNET_NOLOGO=true