Skip to content

Commit

Permalink
Add .Net Aspire.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarciarubio committed Oct 13, 2024
1 parent 69259af commit 1ed0dac
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.8.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AspNetCore.Examples.OpenTelemetry.ServiceDefaults\AspNetCore.Examples.OpenTelemetry.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
21 changes: 0 additions & 21 deletions AspNetCore.Examples.OpenTelemetry.Api/Dockerfile

This file was deleted.

This file was deleted.

25 changes: 19 additions & 6 deletions AspNetCore.Examples.OpenTelemetry.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
using AspNetCore.Examples.OpenTelemetry.Api.Extensions;
using AspNetCore.Examples.OpenTelemetry.Api.SampleTelemetry;

var builder = WebApplication.CreateBuilder(args);

builder.Logging
.AddCustomOpenTelemetry(builder.Configuration);
builder.AddServiceDefaults();

builder.Services.AddCustomOpenApi();

if (builder.Environment.IsDevelopment())
{
builder.Services.AddCors(cors => cors.AddDefaultPolicy(policy =>
{
policy
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}));
}

builder.Services.AddSampleTelemetry();

builder.Services
.AddCustomOpenApi()
.AddCustomOpenTelemetry(builder.Configuration);

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseCors();
app.MapCustomOpenApi();
app.UseCustomOpenApiUI();
}

app.MapTelemetryEndpoints();
app.MapSampleTelemetryEndpoints();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;

namespace AspNetCore.Examples.OpenTelemetry.Api.SampleTelemetry;

public static class SampleTelemetryEndpoints
{
public static IServiceCollection AddSampleTelemetry(this IServiceCollection services)
{
services
.AddSingleton<Telemetry>()
.AddOpenTelemetry()
.WithTracing(t => t.AddSource(Telemetry.ActivitySourceName))
.WithMetrics(m => m.AddMeter(Telemetry.MeterName));
return services;
}

public static IEndpointRouteBuilder MapSampleTelemetryEndpoints(this IEndpointRouteBuilder builder)
{
builder.MapGet("telemetry/traces", async (Telemetry telemetry, int durationMs = 0, string? tag = null) =>
{
var tags = new Dictionary<string, object?>() { { "tag", tag } };
using var trace = telemetry.ActivitySource.StartActivity(name: "sample_activity", kind: ActivityKind.Internal, tags: tags);

await Task.Delay(durationMs);

return Results.Ok();
});

builder.MapGet("telemetry/metrics", (Telemetry telemetry, int value = 0, string? tag = null) =>
{
var tags = new Dictionary<string, object?>() { { "tag", tag } }.ToArray();

telemetry.Histogram.Record(value, tags);
telemetry.Counter.Add(value, tags);
telemetry.Gauge.Record(value, tags);
telemetry.UpDownCounter.Add(value, tags);

return Results.Ok();
});


builder.MapGet("telemetry/logs", (Telemetry telemetry, LogLevel level = LogLevel.Information, string? attributeValue = null) =>
{
telemetry.Logger.Log(level, "Sample log message with attribute value: \"{attribute}\"", attributeValue);

return Results.Ok();
});

return builder;
}

private sealed class Telemetry : IDisposable
{
public const string LoggerName = "sample_logger";
public const string MeterName = "sample_meter";
public const string ActivitySourceName = "sample_source";
public readonly ILogger Logger;
public readonly ActivitySource ActivitySource;
public readonly Meter Meter;
public readonly Histogram<int> Histogram;
public readonly Counter<int> Counter;
public readonly Gauge<int> Gauge;
public readonly UpDownCounter<int> UpDownCounter;

public Telemetry(ILoggerFactory loggerFactory, IMeterFactory meterFactory)
{
Logger = loggerFactory.CreateLogger(LoggerName);
ActivitySource = new ActivitySource(ActivitySourceName, version: "1.0");
Meter = meterFactory.Create(MeterName);
Histogram = Meter.CreateHistogram<int>(name: "sample_histogram", unit: "Units", description: "Sample Histogram description");
Counter = Meter.CreateCounter<int>(name: "sample_counter", unit: "Units", description: "Sample Counter description");
Gauge = Meter.CreateGauge<int>(name: "sample_gauge", unit: "Units", description: "Sample Gauge description");
UpDownCounter = Meter.CreateUpDownCounter<int>(name: "sample_up_down_counter", unit: "Units", description: "Sample Up Down Counter description");
}

public void Dispose()
{
ActivitySource.Dispose();
Meter.Dispose();
}
}
}

This file was deleted.

Loading

0 comments on commit 1ed0dac

Please sign in to comment.