From ab2f96ca0f935f8c7ac692a88b548e9207239e95 Mon Sep 17 00:00:00 2001 From: ZOXEXIVO Date: Tue, 13 Dec 2022 00:29:37 +0300 Subject: [PATCH] Add opentelemetry --- src/Backend/Geen.Data/Geen.Data.csproj | 2 +- src/Backend/Geen.Data/Registration.cs | 1 + .../Repositories/MentionRepository.cs | 1 + .../Geen.Data/Services/IdentityService.cs | 2 ++ .../Geen.Data/Settings/GeenSettings.cs | 23 +++++++++++----- .../Geen.Data/Storages/Mongo/MongoContext.cs | 7 +++-- .../Geen.Web/Application/Registration.cs | 4 ++- src/Backend/Geen.Web/Geen.Web.csproj | 6 +++++ src/Backend/Geen.Web/Startup.cs | 27 +++++++++++++++---- .../Geen.Web/appsettings.Development.json | 7 ++++- src/Backend/Geen.Web/appsettings.json | 9 +++++-- 11 files changed, 69 insertions(+), 20 deletions(-) diff --git a/src/Backend/Geen.Data/Geen.Data.csproj b/src/Backend/Geen.Data/Geen.Data.csproj index 96e5eda..2f469ee 100644 --- a/src/Backend/Geen.Data/Geen.Data.csproj +++ b/src/Backend/Geen.Data/Geen.Data.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/Backend/Geen.Data/Registration.cs b/src/Backend/Geen.Data/Registration.cs index 093076c..6337c29 100644 --- a/src/Backend/Geen.Data/Registration.cs +++ b/src/Backend/Geen.Data/Registration.cs @@ -4,6 +4,7 @@ using System.Reflection; using Geen.Core.Services.Interfaces; using Geen.Data.Caches; +using Geen.Data.Services; using Geen.Data.Settings; using Geen.Data.Storages.Mongo; using Geen.Data.Storages.Redis; diff --git a/src/Backend/Geen.Data/Repositories/MentionRepository.cs b/src/Backend/Geen.Data/Repositories/MentionRepository.cs index 2e48d4a..0be6f40 100644 --- a/src/Backend/Geen.Data/Repositories/MentionRepository.cs +++ b/src/Backend/Geen.Data/Repositories/MentionRepository.cs @@ -11,6 +11,7 @@ using Geen.Core.Models.Likes; using Geen.Data.Entities; using Geen.Data.Extensions; +using Geen.Data.Services; using Geen.Data.Storages.Mongo; using Geen.Data.Utils; using Mapster; diff --git a/src/Backend/Geen.Data/Services/IdentityService.cs b/src/Backend/Geen.Data/Services/IdentityService.cs index d7085b2..c771358 100644 --- a/src/Backend/Geen.Data/Services/IdentityService.cs +++ b/src/Backend/Geen.Data/Services/IdentityService.cs @@ -3,6 +3,8 @@ using Geen.Data.Storages.Mongo; using MongoDB.Driver; +namespace Geen.Data.Services; + public class IdentityService { private readonly MongoContext _context; diff --git a/src/Backend/Geen.Data/Settings/GeenSettings.cs b/src/Backend/Geen.Data/Settings/GeenSettings.cs index 02fc080..aca6b0b 100644 --- a/src/Backend/Geen.Data/Settings/GeenSettings.cs +++ b/src/Backend/Geen.Data/Settings/GeenSettings.cs @@ -2,25 +2,36 @@ { public class GeenSettings { - public virtual DatabaseSettings Database { get; set; } - public virtual Authentication Authentication { get; set; } - public virtual Prerender Prerender { get; set; } + public virtual DatabaseSettingsSettings Database { get; set; } + public virtual AuthenticationSettings Authentication { get; set; } + public virtual PrerenderSettings Prerender { get; set; } + public virtual TracingSettings Tracing { get; set; } } - public class DatabaseSettings + public class DatabaseSettingsSettings { public string MongoUrl { get; set; } public string RedisUrl { get; set; } } - public class Authentication + public class AuthenticationSettings { public string Login { get; set; } public string Password { get; set; } } - public class Prerender + public class PrerenderSettings { public string StaticPath { get; set; } } + + public class TracingSettings + { + public JaegerSettings Jaeger { get; set; } + + public class JaegerSettings + { + public string Endpoint { get; set; } + } + } } diff --git a/src/Backend/Geen.Data/Storages/Mongo/MongoContext.cs b/src/Backend/Geen.Data/Storages/Mongo/MongoContext.cs index 380517f..4dca531 100644 --- a/src/Backend/Geen.Data/Storages/Mongo/MongoContext.cs +++ b/src/Backend/Geen.Data/Storages/Mongo/MongoContext.cs @@ -4,7 +4,6 @@ using System.Reflection; using Geen.Data.Entities.Attributes; using Microsoft.Extensions.Logging; -using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Driver; using MongoDB.Driver.Core.Configuration; @@ -14,7 +13,7 @@ namespace Geen.Data.Storages.Mongo { public class MongoContext : IDisposable { - private readonly ConcurrentDictionary _collectionNameCache = new ConcurrentDictionary(); + private readonly ConcurrentDictionary _collectionNameCache = new(); private readonly MongoUrl _mongoUrl; private readonly Lazy _mongoClient; @@ -65,12 +64,12 @@ private IMongoCollection GetCollection() #region Logging - private static readonly JsonWriterSettings JsonWriterSettings = new JsonWriterSettings + private static readonly JsonWriterSettings JsonWriterSettings = new() { Indent = true }; - private readonly HashSet _ignoringCommands = new HashSet + private readonly HashSet _ignoringCommands = new() { "isMaster", "buildInfo", diff --git a/src/Backend/Geen.Web/Application/Registration.cs b/src/Backend/Geen.Web/Application/Registration.cs index 5b3c5d5..badb415 100644 --- a/src/Backend/Geen.Web/Application/Registration.cs +++ b/src/Backend/Geen.Web/Application/Registration.cs @@ -20,7 +20,7 @@ namespace Geen.Web.Application { public static class ServiceRegistration { - public static void RegisterInternalServices(this IServiceCollection services, IConfiguration configuration) + public static GeenSettings RegisterInternalServices(this IServiceCollection services, IConfiguration configuration) { var settings = services.RegisterSettings(configuration); @@ -35,6 +35,8 @@ public static void RegisterInternalServices(this IServiceCollection services, IC services.AddTransient(); services.AddSingleton(); + + return settings; } private static GeenSettings RegisterSettings(this IServiceCollection services, IConfiguration configuration) diff --git a/src/Backend/Geen.Web/Geen.Web.csproj b/src/Backend/Geen.Web/Geen.Web.csproj index 7a7ad9a..8610e0b 100644 --- a/src/Backend/Geen.Web/Geen.Web.csproj +++ b/src/Backend/Geen.Web/Geen.Web.csproj @@ -3,6 +3,12 @@ net7.0 + + + + + + diff --git a/src/Backend/Geen.Web/Startup.cs b/src/Backend/Geen.Web/Startup.cs index b5027df..e32b722 100644 --- a/src/Backend/Geen.Web/Startup.cs +++ b/src/Backend/Geen.Web/Startup.cs @@ -1,7 +1,5 @@ using System; using System.IO.Compression; -using Geen.Core.Domains.Mentions.Queries; -using Geen.Core.Domains.Replies.Queries; using Geen.Web.Application; using Geen.Web.Application.Prerender; using Microsoft.AspNetCore.Builder; @@ -10,6 +8,11 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; +using OpenTelemetry.Exporter; +using OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; namespace Geen.Web { @@ -24,7 +27,7 @@ public Startup(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { - services.RegisterInternalServices(Configuration); + var settings = services.RegisterInternalServices(Configuration); services.Configure( options => options.Level = CompressionLevel.Fastest); @@ -50,6 +53,21 @@ public void ConfigureServices(IServiceCollection services) }; }); + services.AddOpenTelemetryTracing(builder => + { + builder.SetSampler(new AlwaysOnSampler()) + .ConfigureResource(r => r.AddService( + serviceName: "Geen.Web", + serviceInstanceId: Environment.MachineName)) + .AddHttpClientInstrumentation() + .AddAspNetCoreInstrumentation(); + + builder.AddJaegerExporter(exporter => + { + exporter.Endpoint = new Uri(settings.Tracing.Jaeger.Endpoint); + }); + }); + services.AddCors(options => { options.AddPolicy("AllowAnyOrigin", @@ -60,7 +78,7 @@ public void ConfigureServices(IServiceCollection services) }); services.AddControllers(); -#if DEBUG + services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo @@ -68,7 +86,6 @@ public void ConfigureServices(IServiceCollection services) Title = "GeenApi", Version = "v1" }); }); -#endif } public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) diff --git a/src/Backend/Geen.Web/appsettings.Development.json b/src/Backend/Geen.Web/appsettings.Development.json index 6f47ae6..453df2b 100644 --- a/src/Backend/Geen.Web/appsettings.Development.json +++ b/src/Backend/Geen.Web/appsettings.Development.json @@ -10,6 +10,11 @@ }, "Prerender": { "StaticPath": "E:\\test" + }, + "Tracing": { + "Jaeger": { + "Endpoint": "http://localhost:14268" + } } }, "Logging": { @@ -17,5 +22,5 @@ "Default": "Information", "Microsoft": "Error" } - } + } } diff --git a/src/Backend/Geen.Web/appsettings.json b/src/Backend/Geen.Web/appsettings.json index b58f725..cabc7ae 100644 --- a/src/Backend/Geen.Web/appsettings.json +++ b/src/Backend/Geen.Web/appsettings.json @@ -10,12 +10,17 @@ }, "Prerender": { "StaticPath": "/html/geen" - } + }, + "Tracing": { + "Jaeger": { + "Endpoint": "http://172.17.0.1:14268" + } + } }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Error" } - } + } }