Skip to content

Commit

Permalink
Register with extension
Browse files Browse the repository at this point in the history
  • Loading branch information
askrinnik committed Nov 26, 2023
1 parent 747fb6c commit 13bcd48
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
11 changes: 10 additions & 1 deletion WebPullApp/MyInstruments.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.Metrics;
using OpenTelemetry.Metrics;
using System.Diagnostics.Metrics;

namespace WebPullApp;

Expand Down Expand Up @@ -33,4 +34,12 @@ public void Dispose()
_meter.Dispose();
GC.SuppressFinalize(this);
}
}

public static class MeterProviderBuilderExtensions
{
public static MeterProviderBuilder AddMyInstruments(this MeterProviderBuilder builder) =>
builder
.AddMeter(MyInstruments.MeterName)
.AddInstrumentation<MyInstruments>();
}
3 changes: 1 addition & 2 deletions WebPullApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<MyInstruments>();
builder.Services.AddHostedService<WeatherClient>();

builder.Services.AddOpenTelemetry() //Add OpenTelemetry.Extensions.Hosting nuget package
.ConfigureResource(r => r.AddService(MyInstruments.MeterName, serviceInstanceId: Environment.MachineName))
.WithMetrics(b => b
.AddMeter(MyInstruments.MeterName)
.AddMyInstruments()
.AddAspNetCoreInstrumentation() // Add OpenTelemetry.Instrumentation.AspNetCore nuget package
.AddHttpClientInstrumentation() // Add OpenTelemetry.Instrumentation.Http nuget package
.AddRuntimeInstrumentation() // Add OpenTelemetry.Instrumentation.Runtime nuget package
Expand Down
10 changes: 10 additions & 0 deletions WebPushApp/MyInstruments.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Diagnostics.Metrics;
using OpenTelemetry.Metrics;

namespace WebPushApp;

public class MyInstruments : IDisposable
{
internal const string GlobalSystemName = "MeterUsing.Demo";
internal const string MeterName = "MeterUsing.AspNetCore";
private readonly Meter _meter;
private int _todayTemperature;
Expand Down Expand Up @@ -33,4 +35,12 @@ public void Dispose()
_meter.Dispose();
GC.SuppressFinalize(this);
}
}

public static class MeterProviderBuilderExtensions
{
public static MeterProviderBuilder AddMyInstruments(this MeterProviderBuilder builder) =>
builder
.AddMeter(MyInstruments.MeterName)
.AddInstrumentation<MyInstruments>();
}
7 changes: 4 additions & 3 deletions WebPushApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<MyInstruments>();
builder.Services.AddHostedService<WeatherClient>();

builder.Services.AddOpenTelemetry() //Add OpenTelemetry.Extensions.Hosting nuget package
.ConfigureResource(r => r.AddService(MyInstruments.MeterName, serviceInstanceId: Environment.MachineName))
.ConfigureResource(r => r
.AddService(MyInstruments.MeterName, serviceInstanceId: Environment.MachineName)
.AddAttributes(new Dictionary<string, object> { ["SystemName"] = MyInstruments.GlobalSystemName })) // That is visible in the target_info separate metric.
.WithMetrics(meterBuilder => meterBuilder
.AddMeter(MyInstruments.MeterName)
.AddMyInstruments()
.AddAspNetCoreInstrumentation() // Add OpenTelemetry.Instrumentation.AspNetCore nuget package
.AddHttpClientInstrumentation() // Add OpenTelemetry.Instrumentation.Http nuget package
.AddRuntimeInstrumentation() // Add OpenTelemetry.Instrumentation.Runtime nuget package
Expand Down

0 comments on commit 13bcd48

Please sign in to comment.