Skip to content

Commit 511f56f

Browse files
macel94fstab
authored andcommittedMay 22, 2024
started working on dotnet sample
1 parent 47ffa80 commit 511f56f

14 files changed

+762
-1
lines changed
 

‎.dockerignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.classpath
2+
**/.dockerignore
3+
**/.env
4+
**/.git
5+
**/.gitignore
6+
**/.project
7+
**/.settings
8+
**/.toolstarget
9+
**/.vs
10+
**/.vscode
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/bin
15+
**/charts
16+
**/docker-compose*
17+
**/compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
LICENSE
25+
README.md

‎.vscode/launch.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Docker .NET Launch",
5+
"type": "docker",
6+
"request": "launch",
7+
"preLaunchTask": "docker-run: debug",
8+
"netCore": {
9+
"appProject": "${workspaceFolder}/examples/dotnet/rolldice.csproj"
10+
}
11+
}
12+
]
13+
}

‎.vscode/tasks.json

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "echo",
6+
"type": "shell",
7+
"command": "echo Hello"
8+
},
9+
{
10+
"type": "docker-build",
11+
"label": "docker-build: debug",
12+
"dependsOn": [
13+
"build"
14+
],
15+
"dockerBuild": {
16+
"tag": "dockerotellgtm:dev",
17+
"target": "base",
18+
"dockerfile": "${workspaceFolder}/examples/dotnet/Dockerfile",
19+
"context": "${workspaceFolder}",
20+
"pull": true
21+
},
22+
"netCore": {
23+
"appProject": "${workspaceFolder}/examples/dotnet/rolldice.csproj"
24+
}
25+
},
26+
{
27+
"type": "docker-build",
28+
"label": "docker-build: release",
29+
"dependsOn": [
30+
"build"
31+
],
32+
"dockerBuild": {
33+
"tag": "dockerotellgtm:latest",
34+
"dockerfile": "${workspaceFolder}/examples/dotnet/Dockerfile",
35+
"context": "${workspaceFolder}",
36+
"platform": {
37+
"os": "linux",
38+
"architecture": "amd64"
39+
},
40+
"pull": true
41+
},
42+
"netCore": {
43+
"appProject": "${workspaceFolder}/examples/dotnet/rolldice.csproj"
44+
}
45+
},
46+
{
47+
"type": "docker-run",
48+
"label": "docker-run: debug",
49+
"dependsOn": [
50+
"docker-build: debug"
51+
],
52+
"dockerRun": {},
53+
"netCore": {
54+
"appProject": "${workspaceFolder}/examples/dotnet/rolldice.csproj",
55+
"enableDebugging": true
56+
}
57+
},
58+
{
59+
"type": "docker-run",
60+
"label": "docker-run: release",
61+
"dependsOn": [
62+
"docker-build: release"
63+
],
64+
"dockerRun": {},
65+
"netCore": {
66+
"appProject": "${workspaceFolder}/examples/dotnet/rolldice.csproj"
67+
}
68+
}
69+
]
70+
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Each example uses a different application port (to be able to run all applicatio
6969
| Java | `curl http://localhost:8080/rolldice` |
7070
| Go | `curl http://localhost:8081/rolldice` |
7171
| Python | `curl http://localhost:8082/rolldice` |
72+
| dotnet | `curl http://localhost:8083/rolldice` |
7273

7374
## Related Work
7475

‎examples/dotnet/.gitignore

+484
Large diffs are not rendered by default.

‎examples/dotnet/Dockerfile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
2+
WORKDIR /app
3+
EXPOSE 8083
4+
5+
ENV ASPNETCORE_URLS=http://+:8083
6+
7+
USER app
8+
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build
9+
ARG configuration=Release
10+
WORKDIR /src
11+
COPY ["rolldice.csproj", "/"]
12+
RUN dotnet restore "/rolldice.csproj"
13+
COPY . .
14+
WORKDIR "/src/"
15+
RUN dotnet build "rolldice.csproj" -c $configuration -o /app/build
16+
17+
FROM build AS publish
18+
ARG configuration=Release
19+
RUN dotnet publish "rolldice.csproj" -c $configuration -o /app/publish /p:UseAppHost=false
20+
21+
FROM base AS final
22+
WORKDIR /app
23+
COPY --from=publish /app/publish .
24+
ENTRYPOINT ["dotnet", "rolldice.dll"]

‎examples/dotnet/Program.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Globalization;
2+
using Microsoft.AspNetCore.Mvc;
3+
using OpenTelemetry.Logs;
4+
using OpenTelemetry.Metrics;
5+
using OpenTelemetry.Resources;
6+
using OpenTelemetry.Trace;
7+
8+
var builder = WebApplication.CreateBuilder(args);
9+
10+
const string serviceName = "roll-dice";
11+
12+
builder.Logging.AddOpenTelemetry(options =>
13+
{
14+
options
15+
.SetResourceBuilder(
16+
ResourceBuilder.CreateDefault()
17+
.AddService(serviceName))
18+
.AddConsoleExporter();
19+
});
20+
builder.Services.AddOpenTelemetry()
21+
.ConfigureResource(resource => resource.AddService(serviceName))
22+
.WithTracing(tracing => tracing
23+
.AddAspNetCoreInstrumentation()
24+
.AddConsoleExporter())
25+
.WithMetrics(metrics => metrics
26+
.AddAspNetCoreInstrumentation()
27+
.AddConsoleExporter());
28+
29+
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
30+
31+
if (useOtlpExporter)
32+
{
33+
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter());
34+
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter());
35+
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter());
36+
}
37+
38+
var app = builder.Build();
39+
40+
string HandleRollDice([FromServices]ILogger<Program> logger, string? player)
41+
{
42+
var result = RollDice();
43+
44+
if (string.IsNullOrEmpty(player))
45+
{
46+
logger.LogInformation("Anonymous player is rolling the dice: {result}", result);
47+
}
48+
else
49+
{
50+
logger.LogInformation("{player} is rolling the dice: {result}", player, result);
51+
}
52+
53+
return result.ToString(CultureInfo.InvariantCulture);
54+
}
55+
56+
int RollDice()
57+
{
58+
return Random.Shared.Next(1, 7);
59+
}
60+
61+
app.MapGet("/rolldice/{player?}", HandleRollDice);
62+
63+
app.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "http://localhost:8083",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}

‎examples/dotnet/appsettings.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

‎examples/dotnet/docker-compose.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
rolldice:
3+
image: rolldice
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
# args:
8+
# - configuration=Debug
9+
ports:
10+
- 8083:8083
11+
environment:
12+
- ASPNETCORE_ENVIRONMENT=Development
13+
- OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4317
14+
- OTEL_EXPORTER_OTLP_PROTOCOL=grpc
15+
16+
otel-lgtm:
17+
image: grafana/otel-lgtm
18+
19+
ports:
20+
- 3000:3000
21+
- 4317:4317
22+
- 4318:4318

‎examples/dotnet/rolldice.csproj

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
11+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
12+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
13+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
14+
</ItemGroup>
15+
16+
</Project>

‎examples/dotnet/run.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# TODO
4+
5+
# set -euo pipefail
6+
7+
# export OTEL_METRIC_EXPORT_INTERVAL="5000" # so we don't have to wait 60s for metrics
8+
# export OTEL_RESOURCE_ATTRIBUTES="service.name=rolldice,service.instance.id=localhost:8081"
9+
10+
# # Run the application
11+
# export OTEL_EXPORTER_OTLP_INSECURE="true" # use http instead of https (needed because of https://github.com/open-telemetry/opentelemetry-go/issues/4834)
12+
# go run .

‎generate-traffic.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22

3-
watch 'curl -s http://localhost:8080/rolldice; curl -s http://localhost:8081/rolldice; curl -s http://localhost:8082/rolldice'
3+
watch 'curl -s http://localhost:8080/rolldice; curl -s http://localhost:8081/rolldice; curl -s http://localhost:8082/rolldice; curl -s http://localhost:8083/rolldice'

0 commit comments

Comments
 (0)
Please sign in to comment.