Fix API service injection

This commit is contained in:
Ske 2019-08-12 06:54:28 +02:00
parent 1604500f2a
commit 5f79aaf960
3 changed files with 15 additions and 9 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder; using App.Metrics;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -28,6 +29,8 @@ namespace PluralKit.API
.AddTransient<MemberStore>() .AddTransient<MemberStore>()
.AddTransient<SwitchStore>() .AddTransient<SwitchStore>()
.AddTransient<MessageStore>() .AddTransient<MessageStore>()
.AddSingleton(svc => InitUtils.InitMetrics(svc.GetRequiredService<CoreConfig>(), "API"))
.AddScoped<TokenAuthService>() .AddScoped<TokenAuthService>()

View File

@ -115,14 +115,7 @@ namespace PluralKit.Bot
.AddTransient<MessageStore>() .AddTransient<MessageStore>()
.AddTransient<SwitchStore>() .AddTransient<SwitchStore>()
.AddSingleton<IMetrics>(svc => .AddSingleton(svc => InitUtils.InitMetrics(svc.GetRequiredService<CoreConfig>()))
{
var cfg = svc.GetRequiredService<CoreConfig>();
var builder = AppMetrics.CreateDefaultBuilder();
if (cfg.InfluxUrl != null && cfg.InfluxDb != null)
builder.Report.ToInfluxDb(cfg.InfluxUrl, cfg.InfluxDb);
return builder.Build();
})
.AddSingleton<PeriodicStatCollector>() .AddSingleton<PeriodicStatCollector>()
.AddScoped(_ => new Sentry.Scope(null)) .AddScoped(_ => new Sentry.Scope(null))

View File

@ -323,6 +323,16 @@ namespace PluralKit
.CreateLogger(); .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() => new JsonSerializerSettings().BuildSerializerSettings();
public static JsonSerializerSettings BuildSerializerSettings(this JsonSerializerSettings settings) public static JsonSerializerSettings BuildSerializerSettings(this JsonSerializerSettings settings)