oneuptime/Examples/otel-dotnet/Program.cs

144 lines
5.1 KiB
C#
Raw Normal View History

2023-10-23 14:17:25 +00:00
using System.Diagnostics;
using System.Diagnostics.Metrics;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
const string endpoint = "http://localhost:4317";
2023-11-12 15:12:49 +00:00
Console.WriteLine($"Env var: {Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS")?.ToString()}");
2023-10-23 14:17:25 +00:00
// Logging.
builder.Logging.ClearProviders();
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeScopes = true;
var resourceBuilder = ResourceBuilder
.CreateDefault()
.AddService(builder.Environment.ApplicationName);
logging.SetResourceBuilder(resourceBuilder)
// ConsoleExporter is used for demo purpose only.
// In production environment, ConsoleExporter should be replaced with other exporters (e.g. OTLP Exporter).
.AddConsoleExporter()
.AddOtlpExporter(opt =>
2023-11-09 21:37:23 +00:00
{
// If endpoint was not specified, the proper one will be selected according to the protocol.
if (!string.IsNullOrEmpty(endpoint))
{
opt.Endpoint = new Uri(endpoint);
2023-10-23 14:17:25 +00:00
2023-11-09 21:37:23 +00:00
// Set headers in OTLP exporter
2023-11-12 15:12:49 +00:00
// opt.Headers = "oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224";
2023-11-09 21:37:23 +00:00
}
System.Console.WriteLine($"OTLP Exporter is using {opt.Protocol} protocol and endpoint {opt.Endpoint}");
});
2023-10-23 14:17:25 +00:00
});
// Traces.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource
.AddService(serviceName: builder.Environment.ApplicationName))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddConsoleExporter()
.AddOtlpExporter(opt =>
{
// If endpoint was not specified, the proper one will be selected according to the protocol.
if (!string.IsNullOrEmpty(endpoint))
{
opt.Endpoint = new Uri(endpoint);
2023-11-09 21:37:23 +00:00
// Set headers in OTLP exporter
2023-11-12 15:12:49 +00:00
// opt.Headers = "oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224";
2023-11-12 14:24:27 +00:00
2023-10-23 14:17:25 +00:00
}
System.Console.WriteLine($"OTLP Exporter is using {opt.Protocol} protocol and endpoint {opt.Endpoint}");
}));
2023-10-24 12:35:26 +00:00
// Custom metrics for the application
var greeterMeter = new Meter("OtPrGrYa.Example");
2023-10-23 14:17:25 +00:00
// Metrics.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource
.AddService(serviceName: builder.Environment.ApplicationName))
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
2023-10-24 12:35:26 +00:00
.AddMeter("OtPrGrYa.Example")
2023-10-23 14:17:25 +00:00
.AddConsoleExporter((exporterOptions, metricReaderOptions) =>
{
metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000;
})
.AddOtlpExporter(opt =>
{
// If endpoint was not specified, the proper one will be selected according to the protocol.
if (!string.IsNullOrEmpty(endpoint))
{
opt.Endpoint = new Uri(endpoint);
2023-11-09 21:37:23 +00:00
// Set headers in OTLP exporter
2023-11-12 15:12:49 +00:00
// opt.Headers = "oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224";
2023-10-23 14:17:25 +00:00
}
System.Console.WriteLine($"OTLP Exporter is using {opt.Protocol} protocol and endpoint {opt.Endpoint}");
2023-11-09 21:37:23 +00:00
2023-10-23 14:17:25 +00:00
}));
2023-10-24 12:35:26 +00:00
2023-10-23 14:17:25 +00:00
var app = builder.Build();
2023-10-24 12:35:26 +00:00
greeterMeter.CreateObservableGauge("ThreadCount", () => new[] { new Measurement<int>(ThreadPool.ThreadCount) });
2023-10-24 19:49:10 +00:00
var histogram = greeterMeter.CreateHistogram<int>("greetings.size", "Size of greetings", "desc");
2023-10-23 14:17:25 +00:00
var countGreetings = greeterMeter.CreateCounter<int>("greetings.count", description: "Counts the number of greetings");
2023-10-24 12:35:26 +00:00
2023-10-24 19:49:10 +00:00
2023-10-23 14:17:25 +00:00
// Custom ActivitySource for the application
var greeterActivitySource = new ActivitySource("OtPrGrJa.Example");
async Task<String> SendGreeting(ILogger<Program> logger)
{
// Create a new Activity scoped to the method
using var activity = greeterActivitySource.StartActivity("GreeterActivity");
// Log a message
logger.LogInformation("Sending greeting");
2023-11-16 17:16:37 +00:00
logger.LogError("Error sending greeting");
logger.LogWarning("Warning sending greeting");
// very big log message
logger.LogInformation("LONG LOG: sdsfdg dfgdfgdfg dfgdfgfdgdfg dfgdfgdfg fdgfdgdf fdjgk gkdfjgf dfkgjdfkgjdfkjgkdfjgk gdkfjgkdfjgkdfj gjdfkjgkdfjgkdfjgk fdjgkdfjgkdfjgkjdfkgj fdkgjfdkgjdfkgjkdfg dfkgjdfkjgkfdjgkfjkgdfjkg fdkgjkfdgjkdfjgkjdkg fdkgjdfkjgk");
2023-10-23 14:17:25 +00:00
// Increment the custom counter
countGreetings.Add(1);
// Add a tag to the Activity
activity?.SetTag("greeting", "Hello World!");
2023-11-12 15:12:49 +00:00
//log out env var
2023-10-24 19:49:10 +00:00
histogram.Record("Hello World!".Length);
2023-10-23 14:17:25 +00:00
return $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}";
}
app.MapGet("/", SendGreeting);
app.Run();