-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69259af
commit 1ed0dac
Showing
14 changed files
with
314 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
153 changes: 0 additions & 153 deletions
153
AspNetCore.Examples.OpenTelemetry.Api/Extensions/TelemetryExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
83 changes: 83 additions & 0 deletions
83
AspNetCore.Examples.OpenTelemetry.Api/SampleTelemetry/SampleTelemetryEndpoints.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
50 changes: 0 additions & 50 deletions
50
AspNetCore.Examples.OpenTelemetry.Api/Telemetry/TelemetryEndpoints.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.