Skip to content

Instantly share code, notes, and snippets.

public class RequestsMeter
{
private static readonly Meter s_meter = new Meter("MyCompany.MyApp.Requests", "1.0.0");
private static readonly Counter<long> s_requestCounter =
s_meter.CreateCounter<long>("request-count", description: "Total number of requests");
private static readonly ObservableGauge<int> s_queueLengthGauge =
s_meter.CreateObservableGauge("queue-length",
() => GetQueueLength(),
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Metrics;
var builder= Host.CreateDefaultBuilder(args);
builder.ConfigureServices(s =>
{
s.AddOpenTelemetry()
.WithMetrics(metrics =>
_requestCounter.Add(1,
new KeyValuePair<string, object?>("endpoint", "/api/users"),
new KeyValuePair<string, object?>("method", "GET"),
new KeyValuePair<string, object?>("status", 200));
// Or use a taglist
var tags = new TagList
{
{ "endpoint", "/api/users" },
{ "method", "GET" },
public class RequestsMeter
{
private static readonly Meter _meter = new Meter("MyCompany.MyApp", "1.0.0");
private static readonly Counter<long> _requestCounter =
s_meter.CreateCounter<long>("request-count", description: "Total number of requests");
private static readonly ObservableGauge<int> _queueLengthGauge =
s_meter.CreateObservableGauge("queue-length",
() => GetQueueLength(),
// Before: EventSource
[EventSource(Name = "MyCompany.MyApp.Requests")]
public sealed class RequestEventSource : EventSource
{
// ...
}
// After: Meter
private static readonly Meter _meter = new (
"MyCompany.MyApp.Requests",
[EventSource(Name = "MyCompany.MyApp")]
public sealed class MyEventSource : EventSource
{
public static readonly MyEventSource Log = new MyEventSource();
private EventCounter _requestCounter;
private PollingCounter _queueLengthCounter;
private MyEventSource()
{
<ItemGroup>
<!-- Explicitly reference the fixed version -->
<PackageReference Include="VulnerablePackage" Version="2.0.0" />
</ItemGroup>
<!-- Only audit direct dependencies -->
<PropertyGroup>
<NuGetAuditMode>direct</NuGetAuditMode>
</PropertyGroup>
<!-- Disable auditing entirely (not recommended) -->
<PropertyGroup>
<NuGetAudit>false</NuGetAudit>
</PropertyGroup>
private readonly Lock _lock = new();
public override void Initialize(IServiceCollection services)
{
foreach (var database in _configuration.DatabaseConfigurations)
{
lock (_lock)
ConfigurationMappings.DbContextTypes[database.Key] = _configuration.DbContextTypes[database.Key];
}
}
foreach (var database in _configuration.DatabaseConfigurations)
{
database.Value(services);
// Register a mapping from the database key to the DbContext type
ConfigurationMappings.DbContextTypes[database.Key]= _configuration.DbContextTypes[database.Key];
}