From 5f79aaf960930d46ff985ef7bc01e07233c76e2b Mon Sep 17 00:00:00 2001 From: Ske Date: Mon, 12 Aug 2019 06:54:28 +0200 Subject: [PATCH] Fix API service injection --- PluralKit.API/Startup.cs | 5 ++++- PluralKit.Bot/Bot.cs | 9 +-------- PluralKit.Core/Utils.cs | 10 ++++++++++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/PluralKit.API/Startup.cs b/PluralKit.API/Startup.cs index 8437031f..17d9570a 100644 --- a/PluralKit.API/Startup.cs +++ b/PluralKit.API/Startup.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Builder; +using App.Metrics; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; @@ -28,6 +29,8 @@ namespace PluralKit.API .AddTransient() .AddTransient() .AddTransient() + + .AddSingleton(svc => InitUtils.InitMetrics(svc.GetRequiredService(), "API")) .AddScoped() diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index defcf281..67f58d4a 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -115,14 +115,7 @@ namespace PluralKit.Bot .AddTransient() .AddTransient() - .AddSingleton(svc => - { - var cfg = svc.GetRequiredService(); - var builder = AppMetrics.CreateDefaultBuilder(); - if (cfg.InfluxUrl != null && cfg.InfluxDb != null) - builder.Report.ToInfluxDb(cfg.InfluxUrl, cfg.InfluxDb); - return builder.Build(); - }) + .AddSingleton(svc => InitUtils.InitMetrics(svc.GetRequiredService())) .AddSingleton() .AddScoped(_ => new Sentry.Scope(null)) diff --git a/PluralKit.Core/Utils.cs b/PluralKit.Core/Utils.cs index fab1d874..615a7eeb 100644 --- a/PluralKit.Core/Utils.cs +++ b/PluralKit.Core/Utils.cs @@ -323,6 +323,16 @@ namespace PluralKit .CreateLogger(); } + public static IMetrics InitMetrics(CoreConfig config, string onlyContext = null) + { + var builder = AppMetrics.CreateDefaultBuilder(); + if (config.InfluxUrl != null && config.InfluxDb != null) + builder.Report.ToInfluxDb(config.InfluxUrl, config.InfluxDb); + if (onlyContext != null) + builder.Filter.ByIncludingOnlyContext(onlyContext); + return builder.Build(); + } + public static JsonSerializerSettings BuildSerializerSettings() => new JsonSerializerSettings().BuildSerializerSettings(); public static JsonSerializerSettings BuildSerializerSettings(this JsonSerializerSettings settings)