From e439183645c393e5ba330c54d9debc960b6f6da2 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Tue, 5 Aug 2025 16:57:45 +0300 Subject: [PATCH] feat: add OpenTelemetry support for monitoring --- Endpoint/Endpoint.csproj | 5 +++++ Endpoint/Program.cs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/Endpoint/Endpoint.csproj b/Endpoint/Endpoint.csproj index f5bea67..89be01c 100644 --- a/Endpoint/Endpoint.csproj +++ b/Endpoint/Endpoint.csproj @@ -45,6 +45,11 @@ + + + + + diff --git a/Endpoint/Program.cs b/Endpoint/Program.cs index 643e494..7edc3b9 100644 --- a/Endpoint/Program.cs +++ b/Endpoint/Program.cs @@ -20,7 +20,11 @@ using Mirea.Api.Endpoint.Configuration.Validation; using Mirea.Api.Endpoint.Configuration.Validation.Validators; using Mirea.Api.Security.Services; using OfficeOpenXml; +using OpenTelemetry.Exporter; +using OpenTelemetry.Metrics; +using OpenTelemetry.Resources; using System; +using System.Collections.Generic; using System.IO; namespace Mirea.Api.Endpoint; @@ -118,6 +122,34 @@ public class Program builder.Services.AddDataProtection() .PersistKeysToFileSystem(new DirectoryInfo(PathBuilder.Combine("DataProtection"))); + builder.Host.ConfigureServices((context, services) => + { + var config = context.Configuration.Get()?.LogSettings; + + if (string.IsNullOrEmpty(config?.OpenTelemetryEndpoint) + || string.IsNullOrEmpty(config.OpenTelemetryServiceName)) + return; + + services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics + .AddAspNetCoreInstrumentation() + .AddRuntimeInstrumentation() + .SetResourceBuilder(ResourceBuilder.CreateDefault() + .AddService(serviceName: config.OpenTelemetryServiceName) + .AddAttributes([ + new KeyValuePair("deployment.environment", context.HostingEnvironment.EnvironmentName), + new KeyValuePair("host.name", Environment.MachineName) + ])) + .AddOtlpExporter(options => + { + options.Endpoint = new Uri(config.OpenTelemetryEndpoint); + options.Protocol = OtlpExportProtocol.Grpc; + }); + }); + }); + var app = builder.Build(); app.UseForwardedHeaders();