From d18a5bda30d399741100dea53963c44b0ee5d647 Mon Sep 17 00:00:00 2001 From: patrofimov Date: Thu, 30 Sep 2021 10:49:10 +0500 Subject: [PATCH 01/15] dev null metrics context should be used if all metric senders are absent --- .../VostokHostingEnvironmentFactory_Tests.cs | 61 +++++++++++++------ .../Environment/EnvironmentBuilder.cs | 2 +- Vostok.Hosting/Helpers/CustomizableBuilder.cs | 20 +++++- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs index 5f03874f..d6dd8ee9 100644 --- a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs +++ b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs @@ -10,6 +10,7 @@ using Vostok.Hosting.Components.Shutdown; using Vostok.Hosting.Components.ZooKeeper; using Vostok.Hosting.Setup; +using Vostok.Metrics; using Vostok.Metrics.System.Gc; using Vostok.Metrics.System.Process; @@ -30,7 +31,7 @@ public void TestSetup() public void Should_produce_an_environment_with_linked_cancellation_tokens() { var environment = VostokHostingEnvironmentFactory.Create(Setup); - + shutdown.Cancel(); Action assertion = () => environment.ShutdownToken.IsCancellationRequested.Should().BeTrue(); @@ -65,10 +66,12 @@ public void Should_produce_an_environment_with_ticking_shutdown_budget_upon_canc [Test] public void Should_substract_beacon_shutdown_timeout_from_total() { - var environment = VostokHostingEnvironmentFactory.Create(SetupWithServiceDiscovery, new VostokHostingEnvironmentFactorySettings - { - BeaconShutdownTimeout = 3.Seconds() - }); + var environment = VostokHostingEnvironmentFactory.Create( + SetupWithServiceDiscovery, + new VostokHostingEnvironmentFactorySettings + { + BeaconShutdownTimeout = 3.Seconds() + }); environment.ShutdownTimeout.Should().Be(27.Seconds()); } @@ -76,10 +79,12 @@ public void Should_substract_beacon_shutdown_timeout_from_total() [Test] public void Should_not_substract_beacon_shutdown_timeout_from_total_if_there_is_no_real_beacon() { - var environment = VostokHostingEnvironmentFactory.Create(Setup, new VostokHostingEnvironmentFactorySettings - { - BeaconShutdownTimeout = 3.Seconds() - }); + var environment = VostokHostingEnvironmentFactory.Create( + Setup, + new VostokHostingEnvironmentFactorySettings + { + BeaconShutdownTimeout = 3.Seconds() + }); environment.ShutdownTimeout.Should().Be(30.Seconds()); } @@ -87,10 +92,12 @@ public void Should_not_substract_beacon_shutdown_timeout_from_total_if_there_is_ [Test] public void Should_wait_after_beacon_shutdown_if_instructed_to_do_so() { - var environment = VostokHostingEnvironmentFactory.Create(SetupWithServiceDiscovery, new VostokHostingEnvironmentFactorySettings - { - BeaconShutdownTimeout = 1.Seconds() - }); + var environment = VostokHostingEnvironmentFactory.Create( + SetupWithServiceDiscovery, + new VostokHostingEnvironmentFactorySettings + { + BeaconShutdownTimeout = 1.Seconds() + }); var watch = Stopwatch.StartNew(); @@ -104,11 +111,13 @@ public void Should_wait_after_beacon_shutdown_if_instructed_to_do_so() [Test] public void Should_not_wait_after_beacon_shutdown_if_disabled() { - var environment = VostokHostingEnvironmentFactory.Create(SetupWithServiceDiscovery, new VostokHostingEnvironmentFactorySettings - { - BeaconShutdownTimeout = 2.Seconds(), - BeaconShutdownWaitEnabled = false - }); + var environment = VostokHostingEnvironmentFactory.Create( + SetupWithServiceDiscovery, + new VostokHostingEnvironmentFactorySettings + { + BeaconShutdownTimeout = 2.Seconds(), + BeaconShutdownWaitEnabled = false + }); var watch = Stopwatch.StartNew(); @@ -142,6 +151,20 @@ public void Should_provide_system_metrics_extensions() environment.HostExtensions.TryGet(out _).Should().BeTrue(); } + [Test] + public void Should_provide_dev_null_metric_context_when_no_metric_event_senders_built() + { + var environment = VostokHostingEnvironmentFactory.Create(SetupForDevNullMetricContext, new VostokHostingEnvironmentFactorySettings()); + + environment.Metrics.Root.Should().BeOfType(); + + void SetupForDevNullMetricContext(IVostokHostingEnvironmentBuilder builder) + { + Setup(builder); + builder.SetupDiagnostics(x => x.CustomizeInfo(s => s.AddApplicationMetricsInfo = false)); // disable ApplicationMetricsProvider metrics sender + } + } + private void Setup(IVostokHostingEnvironmentBuilder builder) { builder.DisableClusterConfig(); @@ -178,4 +201,4 @@ private void SetupCommons(IVostokHostingEnvironmentBuilder builder) builder.SetupLog(log => log.SetupConsoleLog()); } } -} +} \ No newline at end of file diff --git a/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs b/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs index 244c7411..7f2e6553 100644 --- a/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs +++ b/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs @@ -188,7 +188,7 @@ private VostokHostingEnvironment BuildInner(BuildContext context) context.SubstituteTracer(tracerBuilder.Build(context)); - if (diagnosticsBuilder.Builder.NeedsApplicationMetricsProvider) + if (diagnosticsBuilder.GetCustomizedBuilderIntermediate(context).NeedsApplicationMetricsProvider) { context.MetricsInfoProvider = new ApplicationMetricsProvider(); metricsBuilder.AddCustomization(metrics => metrics.AddMetricEventSender(context.MetricsInfoProvider)); diff --git a/Vostok.Hosting/Helpers/CustomizableBuilder.cs b/Vostok.Hosting/Helpers/CustomizableBuilder.cs index 7c803b2d..9b308550 100644 --- a/Vostok.Hosting/Helpers/CustomizableBuilder.cs +++ b/Vostok.Hosting/Helpers/CustomizableBuilder.cs @@ -10,7 +10,7 @@ internal class CustomizableBuilder { private readonly TBuilder builder; private readonly Customization builderCustomization; - + private volatile IVostokHostingEnvironmentSetupContext environmentSetupContext; private volatile IVostokConfigurationSetupContext configurationSetupContext; @@ -20,8 +20,6 @@ public CustomizableBuilder(TBuilder builder) builderCustomization = new Customization(); } - public TBuilder Builder => builder; - public void AddCustomization(Action setup) => builderCustomization.AddCustomization(setup); @@ -40,5 +38,21 @@ public TResult Build(BuildContext context) return builder.Build(context); } + + public TBuilder GetCustomizedBuilderIntermediate(BuildContext context) + { + try + { + environmentSetupContext = context.EnvironmentSetupContext; + configurationSetupContext = context.ConfigurationSetupContext; + + return builderCustomization.Customize(builder); + } + finally + { + environmentSetupContext = null; + configurationSetupContext = null; + } + } } } \ No newline at end of file From d275a572aa25a9a75c0b4d6904ffd00f2e7cd1ec Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Thu, 30 Sep 2021 13:09:49 +0500 Subject: [PATCH 02/15] + test --- .../VostokHostingEnvironmentFactory_Tests.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs index d6dd8ee9..9643cf1c 100644 --- a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs +++ b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs @@ -164,6 +164,20 @@ void SetupForDevNullMetricContext(IVostokHostingEnvironmentBuilder builder) builder.SetupDiagnostics(x => x.CustomizeInfo(s => s.AddApplicationMetricsInfo = false)); // disable ApplicationMetricsProvider metrics sender } } + + [Test] + public void Should_provide_not_dev_null_metric_context_when_metric_event_senders_built() + { + var environment = VostokHostingEnvironmentFactory.Create(SetupForDevNullMetricContext, new VostokHostingEnvironmentFactorySettings()); + + environment.Metrics.Root.Should().BeOfType(); + + void SetupForDevNullMetricContext(IVostokHostingEnvironmentBuilder builder) + { + Setup(builder); + builder.SetupDiagnostics(x => x.CustomizeInfo(s => s.AddApplicationMetricsInfo = true)); // enable ApplicationMetricsProvider metrics sender + } + } private void Setup(IVostokHostingEnvironmentBuilder builder) { From 99dbd50bac33aab88b0f4afc79b3bd9a88b8400c Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Thu, 30 Sep 2021 13:15:32 +0500 Subject: [PATCH 03/15] fix name --- Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs | 2 +- Vostok.Hosting/Helpers/CustomizableBuilder.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs b/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs index 7f2e6553..f7981028 100644 --- a/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs +++ b/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs @@ -188,7 +188,7 @@ private VostokHostingEnvironment BuildInner(BuildContext context) context.SubstituteTracer(tracerBuilder.Build(context)); - if (diagnosticsBuilder.GetCustomizedBuilderIntermediate(context).NeedsApplicationMetricsProvider) + if (diagnosticsBuilder.GetIntermediateBuilder(context).NeedsApplicationMetricsProvider) { context.MetricsInfoProvider = new ApplicationMetricsProvider(); metricsBuilder.AddCustomization(metrics => metrics.AddMetricEventSender(context.MetricsInfoProvider)); diff --git a/Vostok.Hosting/Helpers/CustomizableBuilder.cs b/Vostok.Hosting/Helpers/CustomizableBuilder.cs index 9b308550..7ad4ea06 100644 --- a/Vostok.Hosting/Helpers/CustomizableBuilder.cs +++ b/Vostok.Hosting/Helpers/CustomizableBuilder.cs @@ -39,7 +39,7 @@ public TResult Build(BuildContext context) return builder.Build(context); } - public TBuilder GetCustomizedBuilderIntermediate(BuildContext context) + public TBuilder GetIntermediateBuilder(BuildContext context) { try { From e6897195365f42bc19fe6e935e9f82e3eda51790 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Thu, 30 Sep 2021 13:21:36 +0500 Subject: [PATCH 04/15] fix flapping test --- .../VostokHostingEnvironmentFactory_Tests.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs index 9643cf1c..c420dda4 100644 --- a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs +++ b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs @@ -56,11 +56,10 @@ public void Should_produce_an_environment_with_ticking_shutdown_budget_upon_canc var immediatelyAfter = environment.ShutdownTimeout; - Thread.Sleep(100); - - var awhileAfter = environment.ShutdownTimeout; - - awhileAfter.Should().BeLessThan(immediatelyAfter); + new Action(() => + { + environment.ShutdownTimeout.Should().BeLessThan(immediatelyAfter); + }).ShouldPassIn(5.Seconds()); } [Test] From fa8e0966ff8a64d0fdb5d72d03453d22b0014445 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Thu, 30 Sep 2021 14:08:02 +0500 Subject: [PATCH 05/15] fix flapping test 2 --- Vostok.Hosting.Tests/VostokHost_Tests_Dispose.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Vostok.Hosting.Tests/VostokHost_Tests_Dispose.cs b/Vostok.Hosting.Tests/VostokHost_Tests_Dispose.cs index 8e74c157..bb0c8f66 100644 --- a/Vostok.Hosting.Tests/VostokHost_Tests_Dispose.cs +++ b/Vostok.Hosting.Tests/VostokHost_Tests_Dispose.cs @@ -51,13 +51,11 @@ public void Should_not_block_on_dispose_longer_than_shutdown_timeout_allows() var host = new VostokHost(new TestHostSettings(app, SetupEnvironment)); - host.Start(); - var watch = Stopwatch.StartNew(); - host.Stop().State.Should().Be(VostokApplicationState.Exited); + host.Run().State.Should().Be(VostokApplicationState.Exited); - watch.Elapsed.Should().BeLessThan(2.Seconds()); + watch.Elapsed.Should().BeLessThan(5.Seconds()); } private static void SetupEnvironment(IVostokHostingEnvironmentBuilder builder) From aa30a5f97c122ae33f641610b95ed64e55e5e3b1 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Thu, 30 Sep 2021 15:28:46 +0500 Subject: [PATCH 06/15] try to write mem test --- .../Vostok.Hosting.Tests.csproj | 1 + .../VostokHostingEnvironmentFactory_Tests.cs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Vostok.Hosting.Tests/Vostok.Hosting.Tests.csproj b/Vostok.Hosting.Tests/Vostok.Hosting.Tests.csproj index f1b91240..27dc5632 100644 --- a/Vostok.Hosting.Tests/Vostok.Hosting.Tests.csproj +++ b/Vostok.Hosting.Tests/Vostok.Hosting.Tests.csproj @@ -17,6 +17,7 @@ + diff --git a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs index c420dda4..105c914e 100644 --- a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs +++ b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs @@ -3,6 +3,7 @@ using System.Threading; using FluentAssertions; using FluentAssertions.Extensions; +using JetBrains.dotMemoryUnit; using NUnit.Framework; using Vostok.Commons.Environment; using Vostok.Commons.Testing; @@ -177,6 +178,24 @@ void SetupForDevNullMetricContext(IVostokHostingEnvironmentBuilder builder) builder.SetupDiagnostics(x => x.CustomizeInfo(s => s.AddApplicationMetricsInfo = true)); // enable ApplicationMetricsProvider metrics sender } } + + [Test, Explicit] + public void Should_not_leak() + { + var checkPoint = dotMemory.Check(); + + var environment = VostokHostingEnvironmentFactory.Create(Setup); + (environment as IDisposable)?.Dispose(); + + dotMemory.Check(memory => + { + memory.GetDifference(checkPoint) + .GetSurvivedObjects() + .GetObjects(o => o.Namespace.Like("Vostok")) + .ObjectsCount.Should() + .Be(0); + }); + } private void Setup(IVostokHostingEnvironmentBuilder builder) { From 511ab853c6bd82d3e507cb6bfbfff8d566526765 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Thu, 30 Sep 2021 16:51:09 +0500 Subject: [PATCH 07/15] remove DotMemoryUnit --- .../Vostok.Hosting.Tests.csproj | 1 - .../VostokHostingEnvironmentFactory_Tests.cs | 21 +++++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/Vostok.Hosting.Tests/Vostok.Hosting.Tests.csproj b/Vostok.Hosting.Tests/Vostok.Hosting.Tests.csproj index 27dc5632..f1b91240 100644 --- a/Vostok.Hosting.Tests/Vostok.Hosting.Tests.csproj +++ b/Vostok.Hosting.Tests/Vostok.Hosting.Tests.csproj @@ -17,7 +17,6 @@ - diff --git a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs index 105c914e..eed25977 100644 --- a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs +++ b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs @@ -3,7 +3,6 @@ using System.Threading; using FluentAssertions; using FluentAssertions.Extensions; -using JetBrains.dotMemoryUnit; using NUnit.Framework; using Vostok.Commons.Environment; using Vostok.Commons.Testing; @@ -182,19 +181,15 @@ void SetupForDevNullMetricContext(IVostokHostingEnvironmentBuilder builder) [Test, Explicit] public void Should_not_leak() { - var checkPoint = dotMemory.Check(); - - var environment = VostokHostingEnvironmentFactory.Create(Setup); - (environment as IDisposable)?.Dispose(); - - dotMemory.Check(memory => + for (int i = 0; i < 100; i++) { - memory.GetDifference(checkPoint) - .GetSurvivedObjects() - .GetObjects(o => o.Namespace.Like("Vostok")) - .ObjectsCount.Should() - .Be(0); - }); + var environment = VostokHostingEnvironmentFactory.Create(Setup, + new VostokHostingEnvironmentFactorySettings + { + ConfigureStaticProviders = false + }); + (environment as IDisposable)?.Dispose(); + } } private void Setup(IVostokHostingEnvironmentBuilder builder) From 6cbe0503d787cff7a4501e5d483b189b922ee50b Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Thu, 30 Sep 2021 17:56:11 +0500 Subject: [PATCH 08/15] + HostMetricsEnabled --- .../Environment/EnvironmentBuilder.cs | 23 ++++++++++++------- .../MultiHost/VostokMultiHostApplication.cs | 1 + Vostok.Hosting/VostokHost.cs | 3 ++- Vostok.Hosting/VostokHostSettings.cs | 5 ++++ ...VostokHostingEnvironmentFactorySettings.cs | 3 +++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs b/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs index f7981028..5604384a 100644 --- a/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs +++ b/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs @@ -141,7 +141,7 @@ private VostokHostingEnvironment BuildInner(BuildContext context) context.SecretConfigurationProvider) = configurationBuilder.Build(context); } - if (settings.ConfigureStaticProviders && context.ConfigurationProvider is ConfigurationProvider configProvider) + if (settings.ConfigureStaticProviders && context.ConfigurationProvider is {} configProvider) ConfigurationProvider.TrySetDefault(configProvider); context.EnvironmentSetupContext = new EnvironmentSetupContext( @@ -188,7 +188,7 @@ private VostokHostingEnvironment BuildInner(BuildContext context) context.SubstituteTracer(tracerBuilder.Build(context)); - if (diagnosticsBuilder.GetIntermediateBuilder(context).NeedsApplicationMetricsProvider) + if (settings.HostMetricsEnabled && diagnosticsBuilder.GetIntermediateBuilder(context).NeedsApplicationMetricsProvider) { context.MetricsInfoProvider = new ApplicationMetricsProvider(); metricsBuilder.AddCustomization(metrics => metrics.AddMetricEventSender(context.MetricsInfoProvider)); @@ -201,7 +201,8 @@ private VostokHostingEnvironment BuildInner(BuildContext context) if (settings.SendAnnotations) AnnotationsHelper.ReportLaunching(context.ApplicationIdentity, context.Metrics.Instance); - HerculesSinkMetrics.Measure(context.HerculesSink, context.Metrics, context.Log); + if (settings.HostMetricsEnabled) + HerculesSinkMetrics.Measure(context.HerculesSink, context.Metrics, context.Log); if (settings.ConfigureStaticProviders) FlowingContext.Configuration.ErrorCallback = (errorMessage, error) => context.Log.ForContext(typeof(FlowingContext)).Error(error, errorMessage); @@ -218,7 +219,9 @@ private VostokHostingEnvironment BuildInner(BuildContext context) context.ConfigurationSource.SwitchTo(src => src.Substitute(configSubstitutions)); context.SecretConfigurationSource.SwitchTo(src => src.Substitute(configSubstitutions)); - context.DiagnosticsHub = diagnosticsBuilder.Build(context); + context.DiagnosticsHub = settings.HostMetricsEnabled + ? diagnosticsBuilder.Build(context) + : new DiagnosticsHub(new DiagnosticInfo(), new HealthTracker(TimeSpan.MaxValue, context.Log)); var (hostingShutdown, applicationShutdown) = ShutdownFactory.Create( context.ServiceBeacon, @@ -261,7 +264,8 @@ private VostokHostingEnvironment BuildInner(BuildContext context) hostExtensionsBuilder.Build(context, vostokHostingEnvironment); - systemMetricsBuilder.Build(context, vostokHostingEnvironment); + if (settings.HostMetricsEnabled) + systemMetricsBuilder.Build(context, vostokHostingEnvironment); if (!hasLogs) { @@ -270,14 +274,17 @@ private VostokHostingEnvironment BuildInner(BuildContext context) context.Log = context.Logs.BuildCompositeLog(out _); } - LogLevelMetrics.Measure(context.Logs.LogEventLevelCounterFactory.CreateCounter(), context.Metrics); + if (settings.HostMetricsEnabled) + LogLevelMetrics.Measure(context.Logs.LogEventLevelCounterFactory.CreateCounter(), context.Metrics); if (settings.ConfigureStaticProviders) StaticProvidersHelper.Configure(vostokHostingEnvironment); - context.DiagnosticsHub.HealthTracker.LaunchPeriodicalChecks(vostokHostingEnvironment.ShutdownToken); + if (settings.HostMetricsEnabled) + context.DiagnosticsHub.HealthTracker.LaunchPeriodicalChecks(vostokHostingEnvironment.ShutdownToken); - HealthCheckMetrics.Measure(context.DiagnosticsHub.HealthTracker, context.Metrics); + if (settings.HostMetricsEnabled) + HealthCheckMetrics.Measure(context.DiagnosticsHub.HealthTracker, context.Metrics); return vostokHostingEnvironment; } diff --git a/Vostok.Hosting/MultiHost/VostokMultiHostApplication.cs b/Vostok.Hosting/MultiHost/VostokMultiHostApplication.cs index 9d9c82c3..2fa8ea53 100644 --- a/Vostok.Hosting/MultiHost/VostokMultiHostApplication.cs +++ b/Vostok.Hosting/MultiHost/VostokMultiHostApplication.cs @@ -91,6 +91,7 @@ private void CreateVostokHost(bool throwsException = true) ConfigureStaticProviders = false, WarmupConfiguration = false, WarmupZooKeeper = false, + HostMetricsEnabled = false }; vostokHost = new VostokHost(vostokHostSettings); diff --git a/Vostok.Hosting/VostokHost.cs b/Vostok.Hosting/VostokHost.cs index b1f925bb..85cd1f03 100644 --- a/Vostok.Hosting/VostokHost.cs +++ b/Vostok.Hosting/VostokHost.cs @@ -208,7 +208,8 @@ private VostokApplicationRunResult BuildEnvironment() ConfigureStaticProviders = settings.ConfigureStaticProviders, BeaconShutdownTimeout = settings.BeaconShutdownTimeout, BeaconShutdownWaitEnabled = settings.BeaconShutdownWaitEnabled, - SendAnnotations = settings.SendAnnotations + SendAnnotations = settings.SendAnnotations, + HostMetricsEnabled = settings.HostMetricsEnabled }; environment = EnvironmentBuilder.Build(SetupEnvironment, environmentFactorySettings); diff --git a/Vostok.Hosting/VostokHostSettings.cs b/Vostok.Hosting/VostokHostSettings.cs index b2e60f1d..bc0d4aee 100644 --- a/Vostok.Hosting/VostokHostSettings.cs +++ b/Vostok.Hosting/VostokHostSettings.cs @@ -67,6 +67,11 @@ public VostokHostSettings([NotNull] IVostokApplication application, [NotNull] Vo /// If set to true, sends annotations with application lifecycle events (launching, initialized, stopping). /// public bool SendAnnotations { get; set; } = true; + + /// + /// If set to true, reports diagnostic and system metrics. + /// + public bool HostMetricsEnabled { get; set; } = true; /// /// If enabled, will wait for start. diff --git a/Vostok.Hosting/VostokHostingEnvironmentFactorySettings.cs b/Vostok.Hosting/VostokHostingEnvironmentFactorySettings.cs index 0c9ce598..f53e06e6 100644 --- a/Vostok.Hosting/VostokHostingEnvironmentFactorySettings.cs +++ b/Vostok.Hosting/VostokHostingEnvironmentFactorySettings.cs @@ -22,5 +22,8 @@ public class VostokHostingEnvironmentFactorySettings /// public bool SendAnnotations { get; set; } = true; + + /// + public bool HostMetricsEnabled { get; set; } = true; } } From 805ce18479317889a498d315e9f6fa7688e50786 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Fri, 1 Oct 2021 12:35:05 +0500 Subject: [PATCH 09/15] better name and docs --- .../Components/Environment/EnvironmentBuilder.cs | 14 +++++++------- .../MultiHost/VostokMultiHostApplication.cs | 2 +- Vostok.Hosting/VostokHost.cs | 2 +- Vostok.Hosting/VostokHostSettings.cs | 11 ++++++++--- .../VostokHostingEnvironmentFactorySettings.cs | 4 ++-- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs b/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs index 5604384a..55d872b5 100644 --- a/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs +++ b/Vostok.Hosting/Components/Environment/EnvironmentBuilder.cs @@ -188,7 +188,7 @@ private VostokHostingEnvironment BuildInner(BuildContext context) context.SubstituteTracer(tracerBuilder.Build(context)); - if (settings.HostMetricsEnabled && diagnosticsBuilder.GetIntermediateBuilder(context).NeedsApplicationMetricsProvider) + if (settings.DiagnosticMetricsEnabled && diagnosticsBuilder.GetIntermediateBuilder(context).NeedsApplicationMetricsProvider) { context.MetricsInfoProvider = new ApplicationMetricsProvider(); metricsBuilder.AddCustomization(metrics => metrics.AddMetricEventSender(context.MetricsInfoProvider)); @@ -201,7 +201,7 @@ private VostokHostingEnvironment BuildInner(BuildContext context) if (settings.SendAnnotations) AnnotationsHelper.ReportLaunching(context.ApplicationIdentity, context.Metrics.Instance); - if (settings.HostMetricsEnabled) + if (settings.DiagnosticMetricsEnabled) HerculesSinkMetrics.Measure(context.HerculesSink, context.Metrics, context.Log); if (settings.ConfigureStaticProviders) @@ -219,7 +219,7 @@ private VostokHostingEnvironment BuildInner(BuildContext context) context.ConfigurationSource.SwitchTo(src => src.Substitute(configSubstitutions)); context.SecretConfigurationSource.SwitchTo(src => src.Substitute(configSubstitutions)); - context.DiagnosticsHub = settings.HostMetricsEnabled + context.DiagnosticsHub = settings.DiagnosticMetricsEnabled ? diagnosticsBuilder.Build(context) : new DiagnosticsHub(new DiagnosticInfo(), new HealthTracker(TimeSpan.MaxValue, context.Log)); @@ -264,7 +264,7 @@ private VostokHostingEnvironment BuildInner(BuildContext context) hostExtensionsBuilder.Build(context, vostokHostingEnvironment); - if (settings.HostMetricsEnabled) + if (settings.DiagnosticMetricsEnabled) systemMetricsBuilder.Build(context, vostokHostingEnvironment); if (!hasLogs) @@ -274,16 +274,16 @@ private VostokHostingEnvironment BuildInner(BuildContext context) context.Log = context.Logs.BuildCompositeLog(out _); } - if (settings.HostMetricsEnabled) + if (settings.DiagnosticMetricsEnabled) LogLevelMetrics.Measure(context.Logs.LogEventLevelCounterFactory.CreateCounter(), context.Metrics); if (settings.ConfigureStaticProviders) StaticProvidersHelper.Configure(vostokHostingEnvironment); - if (settings.HostMetricsEnabled) + if (settings.DiagnosticMetricsEnabled) context.DiagnosticsHub.HealthTracker.LaunchPeriodicalChecks(vostokHostingEnvironment.ShutdownToken); - if (settings.HostMetricsEnabled) + if (settings.DiagnosticMetricsEnabled) HealthCheckMetrics.Measure(context.DiagnosticsHub.HealthTracker, context.Metrics); return vostokHostingEnvironment; diff --git a/Vostok.Hosting/MultiHost/VostokMultiHostApplication.cs b/Vostok.Hosting/MultiHost/VostokMultiHostApplication.cs index 2fa8ea53..b074d1be 100644 --- a/Vostok.Hosting/MultiHost/VostokMultiHostApplication.cs +++ b/Vostok.Hosting/MultiHost/VostokMultiHostApplication.cs @@ -91,7 +91,7 @@ private void CreateVostokHost(bool throwsException = true) ConfigureStaticProviders = false, WarmupConfiguration = false, WarmupZooKeeper = false, - HostMetricsEnabled = false + DiagnosticMetricsEnabled = false }; vostokHost = new VostokHost(vostokHostSettings); diff --git a/Vostok.Hosting/VostokHost.cs b/Vostok.Hosting/VostokHost.cs index 85cd1f03..6f2b87c4 100644 --- a/Vostok.Hosting/VostokHost.cs +++ b/Vostok.Hosting/VostokHost.cs @@ -209,7 +209,7 @@ private VostokApplicationRunResult BuildEnvironment() BeaconShutdownTimeout = settings.BeaconShutdownTimeout, BeaconShutdownWaitEnabled = settings.BeaconShutdownWaitEnabled, SendAnnotations = settings.SendAnnotations, - HostMetricsEnabled = settings.HostMetricsEnabled + DiagnosticMetricsEnabled = settings.DiagnosticMetricsEnabled }; environment = EnvironmentBuilder.Build(SetupEnvironment, environmentFactorySettings); diff --git a/Vostok.Hosting/VostokHostSettings.cs b/Vostok.Hosting/VostokHostSettings.cs index bc0d4aee..eaf65a96 100644 --- a/Vostok.Hosting/VostokHostSettings.cs +++ b/Vostok.Hosting/VostokHostSettings.cs @@ -69,9 +69,14 @@ public VostokHostSettings([NotNull] IVostokApplication application, [NotNull] Vo public bool SendAnnotations { get; set; } = true; /// - /// If set to true, reports diagnostic and system metrics. - /// - public bool HostMetricsEnabled { get; set; } = true; + /// If set to false, forcibly disables: + /// + /// All diagnostic metrics + /// All system metrics + /// All health check metrics + /// + /// + public bool DiagnosticMetricsEnabled { get; set; } = true; /// /// If enabled, will wait for start. diff --git a/Vostok.Hosting/VostokHostingEnvironmentFactorySettings.cs b/Vostok.Hosting/VostokHostingEnvironmentFactorySettings.cs index f53e06e6..d06dc955 100644 --- a/Vostok.Hosting/VostokHostingEnvironmentFactorySettings.cs +++ b/Vostok.Hosting/VostokHostingEnvironmentFactorySettings.cs @@ -23,7 +23,7 @@ public class VostokHostingEnvironmentFactorySettings /// public bool SendAnnotations { get; set; } = true; - /// - public bool HostMetricsEnabled { get; set; } = true; + /// + public bool DiagnosticMetricsEnabled { get; set; } = true; } } From a84ec21583c1361b8a459b0b32f42b93b9b25759 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Fri, 1 Oct 2021 12:35:36 +0500 Subject: [PATCH 10/15] fix --- Vostok.Hosting/VostokHostSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Vostok.Hosting/VostokHostSettings.cs b/Vostok.Hosting/VostokHostSettings.cs index eaf65a96..f0a7e848 100644 --- a/Vostok.Hosting/VostokHostSettings.cs +++ b/Vostok.Hosting/VostokHostSettings.cs @@ -73,7 +73,7 @@ public VostokHostSettings([NotNull] IVostokApplication application, [NotNull] Vo /// /// All diagnostic metrics /// All system metrics - /// All health check metrics + /// All health checks /// /// public bool DiagnosticMetricsEnabled { get; set; } = true; From aa168bb62f879dfa8a091be65ae6dd180bdfe999 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Fri, 1 Oct 2021 12:55:53 +0500 Subject: [PATCH 11/15] + DiagnosticMetricsEnabled for multi host --- Vostok.Hosting/MultiHost/VostokMultiHost.cs | 3 ++- Vostok.Hosting/MultiHost/VostokMultiHostSettings.cs | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Vostok.Hosting/MultiHost/VostokMultiHost.cs b/Vostok.Hosting/MultiHost/VostokMultiHost.cs index c8f606b7..addb0ff3 100644 --- a/Vostok.Hosting/MultiHost/VostokMultiHost.cs +++ b/Vostok.Hosting/MultiHost/VostokMultiHost.cs @@ -247,7 +247,8 @@ private VostokHostingEnvironment SetupEnvironment() { var environmentFactorySettings = new VostokHostingEnvironmentFactorySettings { - ConfigureStaticProviders = settings.ConfigureStaticProviders + ConfigureStaticProviders = settings.ConfigureStaticProviders, + DiagnosticMetricsEnabled = settings.DiagnosticMetricsEnabled }; return EnvironmentBuilder.Build( diff --git a/Vostok.Hosting/MultiHost/VostokMultiHostSettings.cs b/Vostok.Hosting/MultiHost/VostokMultiHostSettings.cs index 27f41d69..b74a19fd 100644 --- a/Vostok.Hosting/MultiHost/VostokMultiHostSettings.cs +++ b/Vostok.Hosting/MultiHost/VostokMultiHostSettings.cs @@ -30,5 +30,8 @@ public VostokMultiHostSettings(VostokHostingEnvironmentSetup builder) /// public int ThreadPoolTuningMultiplier { get; set; } = ThreadPoolConstants.DefaultThreadPoolMultiplier; + + /// + public bool DiagnosticMetricsEnabled { get; set; } } } \ No newline at end of file From f4bf238d09ded6d2d3e66eee688ad6780fd90059 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Fri, 1 Oct 2021 14:05:57 +0500 Subject: [PATCH 12/15] CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bb9e809..7b33b8d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.3.37 (01-10-2021): + +- Added `DiagnosticMetricsEnabled` settings +- Fixed #60 + ## 0.3.26 (16-09-2021): Add possibility to target setup of host metrics. From 801f883232d0c282db742ec3305a3d9814254274 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Fri, 1 Oct 2021 14:07:25 +0500 Subject: [PATCH 13/15] + test --- .../VostokHostingEnvironmentFactory_Tests.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs index eed25977..8ec834fa 100644 --- a/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs +++ b/Vostok.Hosting.Tests/VostokHostingEnvironmentFactory_Tests.cs @@ -150,6 +150,17 @@ public void Should_provide_system_metrics_extensions() environment.HostExtensions.TryGet(out _).Should().BeTrue(); } + [Test] + public void Should_provide_dev_null_metric_context_when_diagnostic_metrics_disabled_and_no_senders() + { + var environment = VostokHostingEnvironmentFactory.Create(Setup, new VostokHostingEnvironmentFactorySettings + { + DiagnosticMetricsEnabled = false + }); + + environment.Metrics.Root.Should().BeOfType(); + } + [Test] public void Should_provide_dev_null_metric_context_when_no_metric_event_senders_built() { @@ -163,7 +174,7 @@ void SetupForDevNullMetricContext(IVostokHostingEnvironmentBuilder builder) builder.SetupDiagnostics(x => x.CustomizeInfo(s => s.AddApplicationMetricsInfo = false)); // disable ApplicationMetricsProvider metrics sender } } - + [Test] public void Should_provide_not_dev_null_metric_context_when_metric_event_senders_built() { From 45734c7b7c9f1d5157a9356343207fdf8778d972 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Fri, 1 Oct 2021 14:08:19 +0500 Subject: [PATCH 14/15] CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b33b8d0..893aaca3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.3.37 (01-10-2021): +## 0.3.27 (01-10-2021): - Added `DiagnosticMetricsEnabled` settings - Fixed #60 From db2a090f70a4f97b331d7327587712b369453dd5 Mon Sep 17 00:00:00 2001 From: kungurtsev Date: Fri, 1 Oct 2021 14:08:27 +0500 Subject: [PATCH 15/15] Bumped version to 0.3.28. --- Vostok.Hosting/Vostok.Hosting.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Vostok.Hosting/Vostok.Hosting.csproj b/Vostok.Hosting/Vostok.Hosting.csproj index 835ff9f0..60722338 100644 --- a/Vostok.Hosting/Vostok.Hosting.csproj +++ b/Vostok.Hosting/Vostok.Hosting.csproj @@ -8,7 +8,7 @@ latest - 0.3.27 + 0.3.28 Vostok.Hosting