Skip to content

Instantly share code, notes, and snippets.

@JerryNixon
Last active September 25, 2024 20:12
Show Gist options
  • Save JerryNixon/2314602cdfc77d2d3d13bb6a4b33bc82 to your computer and use it in GitHub Desktop.
Save JerryNixon/2314602cdfc77d2d3d13bb6a4b33bc82 to your computer and use it in GitHub Desktop.
Open Telemetry 001
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using System.Diagnostics;
using System.Diagnostics.Metrics;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<EndpointMeters>();
builder.Services.AddOpenTelemetry()
.WithMetrics(options =>
{
options.AddMeter(EndpointMeters.Meter.Name);
options.AddConsoleExporter();
});
var app = builder.Build();
app.UseHttpsRedirection();
app.MapGet("/api1", GetData).WithName("GetApi1");
app.Run();
async Task<string> GetData(HttpContext context, EndpointMeters metrics)
{
var endpointTags = context.GetEndpointTags();
metrics.Requests.Add(1, endpointTags);
using (DurationMetric.Measure(metrics.Duration, endpointTags))
{
await Task.Delay(Random.Shared.Next(100, 1000));
return DateTime.Now.ToString();
}
}
public class EndpointMeters
{
public static readonly Meter Meter = new(nameof(EndpointMeters), "0.0.1");
public readonly Counter<int> Requests
= Meter.CreateCounter<int>("requests", description: "Number of requests");
public readonly Histogram<double> Duration
= Meter.CreateHistogram<double>("request_duration", "ms", description: "Duration in milliseconds");
}
public class DurationMetric(Histogram<double> metric, TagList endpointTags) : IDisposable
{
public static DurationMetric Measure(Histogram<double> metric, TagList endpointTags) => new(metric, endpointTags);
private readonly Stopwatch stopwatch = Stopwatch.StartNew();
public void Dispose() => metric.Record(stopwatch.Elapsed.TotalMilliseconds, endpointTags);
}
public static class Extensions
{
public static TagList GetEndpointTags(this HttpContext context)
{
return [new("endpoint", context.GetEndpointName())];
}
public static string GetEndpointName(this HttpContext context)
{
var endpoint = context.GetEndpoint() as RouteEndpoint;
var endpointNameMetadata = endpoint?.Metadata.GetMetadata<EndpointNameMetadata>();
return endpointNameMetadata?.EndpointName ?? "unknown";
}
}
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using System.Diagnostics.Metrics;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<EndpointMeters>();
builder.Services.AddOpenTelemetry()
.WithMetrics(options =>
{
options.AddMeter(EndpointMeters.Meter.Name);
options.AddConsoleExporter();
});
var app = builder.Build();
app.UseHttpsRedirection();
app.MapGet("/api1", GetData).WithName("GetApi1");
app.Run();
async Task<string> GetData(EndpointMeters metrics)
{
metrics.Requests.Add(1);
return DateTime.Now.ToString();
}
public class EndpointMeters
{
public static readonly Meter Meter = new("Endpoint.Meters", "0.0.1");
public readonly Counter<int> Requests = Meter.CreateCounter<int>("requests", description: "Number of requests");
}
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetry()
.WithTracing(options =>
{
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("TelemetryDemo"));
options.SetSampler(new AlwaysOnSampler());
options.AddConsoleExporter(exporterOptions =>
{
exporterOptions.Targets = OpenTelemetry.Exporter.ConsoleExporterOutputTargets.Console;
});
});
var app = builder.Build();
app.UseHttpsRedirection();
app.MapGet("/api1", GetData).WithName("GetApi1");
app.Run();
async Task<string> GetData(HttpContext context, TracerProvider tracerProvider)
{
var tracer = tracerProvider.GetTracer("EndpointTracer");
using (var span = tracer.StartActiveSpan("ProcessData"))
{
span.SetAttribute("endpoint", context.GetEndpointName());
await Task.Delay(Random.Shared.Next(100, 1000));
span.AddEvent("Processing complete");
return DateTime.Now.ToString();
}
}
public static class Extensions
{
public static string GetEndpointName(this HttpContext context)
{
var endpoint = context.GetEndpoint() as RouteEndpoint;
var endpointNameMetadata = endpoint?.Metadata.GetMetadata<EndpointNameMetadata>();
return endpointNameMetadata?.EndpointName ?? "unknown";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment