Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great - didn't know this is possible

max_line_length = 180

[*.md]
max_line_length = 120

Expand Down
5 changes: 5 additions & 0 deletions .github/super-linter.env
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ 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
FIX_GO=true
Expand Down
32 changes: 14 additions & 18 deletions examples/dotnet/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
117 changes: 57 additions & 60 deletions examples/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -14,84 +13,82 @@
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<AspNetCoreTraceInstrumentationOptions>(
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<AspNetCoreTraceInstrumentationOptions>(
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.
appBuilder.Logging.ClearProviders();

// 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<Program> 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<Program> 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();
2 changes: 1 addition & 1 deletion examples/dotnet/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:8083"
"applicationUrl": "http://localhost:8083/rolldice"
}
}
}
5 changes: 5 additions & 0 deletions examples/dotnet/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"allowPrerelease": false
}
}
10 changes: 5 additions & 5 deletions examples/dotnet/rolldice.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
</ItemGroup>

</Project>