Fellows, Using otel in Api application works and the setup is easy.
But i would like to use otel in console application and get metrics from the application like CPU usage, memory, heap and etc..
There is any way to use like this?
You do it the same way you would for the Web API application. Web API applications are console applications that start the ASP.NET Core pipeline on top of the DI, Configuration and Logging middleware.
You could copy the OTEL setup code from the OTEL Getting Started tutorial into your Worker project almost as-is and add the instrumentation packages you want :
using myappnamespace;
//OTEL namespaces
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
//************
// OTEL configuration
const string serviceName = "roll-dice";
builder.Logging.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddConsoleExporter();
});
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(serviceName))
.WithTracing(tracing => tracing
.AddSqlClientInstrumentation()
.AddHttpClientInstrumentation()
.AddConsoleExporter())
.WithMetrics(metrics => metrics
.AddRuntimeInstrumentation()
.AddProcessInstrumentation()
.AddHttpClientInstrumentation()
.AddConsoleExporter());
//******************
var host = builder.Build();
host.Run();
It's better to extract the OTEL code into a separate method though:
...
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
SetupOtel(builder);
var host = builder.Build();
host.Run();
void SetupOtel(HostApplicationBuilder builder)
{
const string serviceName = "roll-dice";
builder.Logging.AddOpenTelemetry(options =>
{
options
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName))
.AddConsoleExporter();
});
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(serviceName))
.WithTracing(tracing => tracing
.AddSqlClientInstrumentation()
.AddHttpClientInstrumentation()
.AddConsoleExporter())
.WithMetrics(metrics => metrics
.AddRuntimeInstrumentation()
.AddProcessInstrumentation()
.AddHttpClientInstrumentation()
.AddConsoleExporter());
}
In this example the following instrumentation packages were added:
After that, it's easy to export eg to .NET Asprire using the OTLP collector exporter and get traces, metrics and logs.
using OpenTelemetry;
...
void SetupOtel(HostApplicationBuilder builder)
{
const string serviceName = "roll-dice";
builder.Logging.AddOpenTelemetry(options =>
{
...
});
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource.AddService(serviceName))
.WithTracing(tracing => tracing
...)
.WithMetrics(metrics => metrics
...)
.UseOtlpExporter();
}
And get metrics like this
To get this chart the Worker code was modified to call Google every second.
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var client = new HttpClient();
while (!stoppingToken.IsCancellationRequested)
{
if (_logger.IsEnabled(LogLevel.Information))
{
var html = await client.GetStringAsync("https://www.google.com");
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
}
}