Exemplo n.º 1
0
        public TResult Build(BuildContext context)
        {
            environmentSetupContext   = context.EnvironmentSetupContext;
            configurationSetupContext = context.ConfigurationSetupContext;

            builderCustomization.Customize(builder);

            return(builder.Build(context));
        }
Exemplo n.º 2
0
        private ServiceBeacon CreateBeacon(IZooKeeperClient zooKeeperClient, BuildContext context)
        {
            var settings = new ServiceBeaconSettings();

            if (registrationDeniedFromNonActiveDatacenters)
            {
                settings.RegistrationAllowedProvider = LocalDatacenterIsActive(context.Datacenters);
            }

            settingsCustomization.Customize(settings);

            return(new ServiceBeacon(
                       zooKeeperClient,
                       s =>
            {
                s.SetProperty(WellKnownApplicationIdentityProperties.Project, context.ApplicationIdentity.Project);
                s.SetProperty(WellKnownApplicationIdentityProperties.Subproject, context.ApplicationIdentity.Subproject);
                s.SetProperty(WellKnownApplicationIdentityProperties.Environment, context.ApplicationIdentity.Environment);
                s.SetProperty(WellKnownApplicationIdentityProperties.Application, context.ApplicationIdentity.Application);
                s.SetProperty(WellKnownApplicationIdentityProperties.Instance, context.ApplicationIdentity.Instance);

                replicaInfoCustomization.Customize(s);
            },
                       settings,
                       context.Log));
        }
        public void ConfigureKestrel(KestrelServerOptions options)
        {
            var settings = kestrelCustomization.Customize(new KestrelSettings());

            options.AddServerHeader    = false;
            options.AllowSynchronousIO = false;

            options.Limits.MaxRequestBufferSize  = 256 * 1024;
            options.Limits.MaxResponseBufferSize = 256 * 1024;

            options.Limits.MaxRequestBodySize               = settings.MaxRequestBodySize;
            options.Limits.MaxRequestLineSize               = settings.MaxRequestLineSize;
            options.Limits.MaxRequestHeadersTotalSize       = settings.MaxRequestHeadersSize;
            options.Limits.MaxConcurrentUpgradedConnections = settings.MaxConcurrentWebSocketConnections;

            if (settings.KeepAliveTimeout.HasValue)
            {
                options.Limits.KeepAliveTimeout = settings.KeepAliveTimeout.Value;
            }

            if (settings.RequestHeadersTimeout.HasValue)
            {
                options.Limits.RequestHeadersTimeout = settings.RequestHeadersTimeout.Value;
            }
        }
Exemplo n.º 4
0
        private HealthTracker BuildHealthTracker(BuildContext context)
        {
            var healthSettings = healthSettingsCustomization.Customize(new HealthTrackerSettings());
            var healthTracker  = new HealthTracker(healthSettings.ChecksPeriod, context.Log);

            if (healthSettings.AddDatacenterWhitelistCheck)
            {
                healthTracker.RegisterCheck(WellKnownHealthCheckNames.DatacenterWhitelist, new DatacenterWhitelistCheck(context.Datacenters));
            }

            if (healthSettings.AddThreadPoolStarvationCheck)
            {
                healthTracker.RegisterCheck(WellKnownHealthCheckNames.ThreadPoolStarvation, new ThreadPoolStarvationCheck());
            }

            if (healthSettings.AddZooKeeperConnectionCheck && context.ZooKeeperClient is ZooKeeperClient realClient)
            {
                healthTracker.RegisterCheck(WellKnownHealthCheckNames.ZooKeeperConnection, new ZooKeeperConnectionCheck(realClient));
            }

            if (healthSettings.AddDnsResolutionCheck && RuntimeDetector.IsDotNet50AndNewer)
            {
                healthTracker.RegisterCheck(WellKnownHealthCheckNames.DnsResolution, context.RegisterDisposable(new DnsResolutionCheck()));
            }

            return(healthTracker);
        }
        public HerculesServiceDiscoveryEventsSender Build(BuildContext context)
        {
            if (!enabled)
            {
                context.LogDisabled("HerculesServiceDiscoveryEventsSender");
                return(null);
            }

            var herculesSink = context.HerculesSink;

            if (herculesSink == null)
            {
                context.LogDisabled("HerculesServiceDiscoveryEventsSender", "disabled HerculesSink");
                return(null);
            }

            if (stream == null)
            {
                context.LogDisabled("HerculesServiceDiscoveryEventsSender", "stream not configured");
                return(null);
            }

            if (apiKeyProvider != null)
            {
                herculesSink.ConfigureStream(stream, new StreamSettings {
                    ApiKeyProvider = apiKeyProvider
                });
            }

            var settings = new HerculesServiceDiscoveryEventsSenderSettings(herculesSink, stream);

            settingsCustomization.Customize(settings);

            return(new HerculesServiceDiscoveryEventsSender(settings));
        }
        public void Register(IServiceCollection services)
        {
            var middlewares        = new List <Type>();
            var diagnosticSettings = diagnosticFeaturesCustomization.Customize(new DiagnosticFeaturesSettings());

            RegisterThrottlingProvider(services, diagnosticSettings);
            RegisterRequestTracker(services, diagnosticSettings);

            Register <HttpContextTweakSettings, HttpContextTweakMiddleware>(services, httpContextTweaksCustomization, middlewares);
            Register <FillRequestInfoSettings, FillRequestInfoMiddleware>(services, fillRequestInfoCustomization, middlewares);
            Register <DistributedContextSettings, DistributedContextMiddleware>(services, distributedContextCustomization, middlewares);
            Register <TracingSettings, TracingMiddleware>(services, tracingCustomization, middlewares);
            Register <ThrottlingSettings, ThrottlingMiddleware>(services, throttlingBuilder.MiddlewareCustomization, middlewares);
            Register <LoggingSettings, LoggingMiddleware>(services, loggingCustomization, middlewares);
            Register <DatacenterAwarenessSettings, DatacenterAwarenessMiddleware> (services, datacenterAwarenessCustomization, middlewares);
            Register <UnhandledExceptionSettings, UnhandledExceptionMiddleware> (services, errorHandlingCustomization, middlewares);
            Register <PingApiSettings, PingApiMiddleware> (services, pingApiCustomization, middlewares);
            Register <DiagnosticApiSettings, DiagnosticApiMiddleware> (services, diagnosticApiCustomization, middlewares);

            if (middlewares.Count == 0)
            {
                return;
            }

            services.AddTransient <IStartupFilter>(_ => new AddMiddlewaresStartupFilter(middlewares));
        }
Exemplo n.º 7
0
        public void Build(BuildContext context, IVostokHostingEnvironment environment)
        {
            this.environment = environment;

            if (context.ZooKeeperClient != null && !(context.ZooKeeperClient is DevNullZooKeeperClient))
            {
                HostExtensions.Add(context.ZooKeeperClient);
            }
            if (!(context.ServiceDiscoveryEventsContext is DevNullServiceDiscoveryEventsContext))
            {
                HostExtensions.Add(context.ServiceDiscoveryEventsContext);
            }

            HostExtensions.Add <IVostokApplicationDiagnostics>(context.DiagnosticsHub);
            HostExtensions.Add(context.Logs.LogEventLevelCounterFactory);
            HostExtensions.Add <IConfigurationSource>("MergedConfigurationSource", context.MergedConfigurationSource);

            builderCustomization.Customize(this);

            context.HostExtensions = HostExtensions;

            // note (kungurtsev, 02.11.2021): user components should be disposed right after application in reverse order
            disposables.Reverse();
            context.Disposables.InsertRange(0, disposables);
        }
Exemplo n.º 8
0
        public ILog Build(BuildContext context)
        {
            if (!enabled)
            {
                context.LogDisabled("HerculesLog");
                return(null);
            }

            var herculesSink = context.HerculesSink;

            if (herculesSink == null)
            {
                context.LogDisabled("HerculesLog", "disabled HerculesSink");
                return(null);
            }

            if (stream == null)
            {
                context.LogDisabled("HerculesLog", "unconfigured stream");
                return(null);
            }

            if (apiKeyProvider != null)
            {
                herculesSink.ConfigureStream(stream, new StreamSettings {
                    ApiKeyProvider = apiKeyProvider
                });
            }

            var settings = new HerculesLogSettings(herculesSink, stream);

            settingsCustomization.Customize(settings);

            return(logCustomization.Customize(new HerculesLog(settings)));
        }
Exemplo n.º 9
0
        private DiagnosticInfo BuildDiagnosticInfo(BuildContext context, IHealthTracker healthTracker)
        {
            var infoSettings = infoSettingsCustomization.Customize(new DiagnosticInfoSettings());
            var info         = new DiagnosticInfo();

            if (infoSettings.AddEnvironmentInfo)
            {
                info.RegisterProvider(CreateEntry("environment-info"), new EnvironmentInfoProvider(context.Datacenters));
            }

            if (infoSettings.AddSystemMetricsInfo)
            {
                info.RegisterProvider(CreateEntry("system-metrics"), new SystemMetricsProvider());
            }

            if (infoSettings.AddLoadedAssembliesInfo)
            {
                info.RegisterProvider(CreateEntry("loaded-assemblies"), new LoadedAssembliesProvider());
            }

            if (infoSettings.AddHealthChecksInfo)
            {
                info.RegisterProvider(CreateEntry("health-checks"), new HealthChecksInfoProvider(healthTracker));
            }

            if (infoSettings.AddConfigurationInfo)
            {
                info.RegisterProvider(CreateEntry("configuration"), new ConfigurationInfoProvider(context.ConfigurationSource));
            }

            if (infoSettings.AddHerculesSinkInfo && context.HerculesSink is HerculesSink realSink)
            {
                info.RegisterProvider(CreateEntry("hercules-sink"), new HerculesSinkInfoProvider(realSink));
            }

            if (infoSettings.AddApplicationMetricsInfo && context.MetricsInfoProvider != null)
            {
                info.RegisterProvider(CreateEntry("application-metrics"), context.MetricsInfoProvider);
            }

            if (infoSettings.AddApplicationInfo)
            {
                info.RegisterProvider(
                    CreateEntry("application-info"),
                    new ApplicationInfoProvider(
                        context.ApplicationIdentity,
                        context.ApplicationLimits,
                        context.ApplicationReplication));
            }

            return(info);
        }
Exemplo n.º 10
0
        private DiagnosticInfo BuildDiagnosticInfo(BuildContext context, IHealthTracker healthTracker)
        {
            var infoSettings = infoSettingsCustomization.Customize(new DiagnosticInfoSettings());
            var info         = new DiagnosticInfo();

            if (infoSettings.AddEnvironmentInfo)
            {
                info.RegisterProvider(CreateEntry(WellKnownDiagnosticInfoProvidersNames.EnvironmentInfo), new EnvironmentInfoProvider(context.Datacenters));
            }

            if (infoSettings.AddSystemMetricsInfo)
            {
                info.RegisterProvider(CreateEntry(WellKnownDiagnosticInfoProvidersNames.SystemMetrics), context.RegisterDisposable(new SystemMetricsProvider()));
            }

            if (infoSettings.AddLoadedAssembliesInfo)
            {
                info.RegisterProvider(CreateEntry(WellKnownDiagnosticInfoProvidersNames.LoadedAssemblies), new LoadedAssembliesProvider());
            }

            if (infoSettings.AddHealthChecksInfo)
            {
                info.RegisterProvider(CreateEntry(WellKnownDiagnosticInfoProvidersNames.HealthChecks), new HealthChecksInfoProvider(healthTracker));
            }

            if (infoSettings.AddConfigurationInfo)
            {
                info.RegisterProvider(CreateEntry(WellKnownDiagnosticInfoProvidersNames.Configuration), new ConfigurationInfoProvider(context.ConfigurationSource));
            }

            if (infoSettings.AddHerculesSinkInfo && context.HerculesSink is HerculesSink realSink)
            {
                info.RegisterProvider(CreateEntry(WellKnownDiagnosticInfoProvidersNames.HerculesSink), new HerculesSinkInfoProvider(realSink));
            }

            if (infoSettings.AddApplicationMetricsInfo && context.MetricsInfoProvider != null)
            {
                info.RegisterProvider(CreateEntry(WellKnownDiagnosticInfoProvidersNames.ApplicationMetrics), context.MetricsInfoProvider);
            }

            if (infoSettings.AddApplicationInfo)
            {
                info.RegisterProvider(
                    CreateEntry(WellKnownDiagnosticInfoProvidersNames.ApplicationInfo),
                    new ApplicationInfoProvider(
                        context.ApplicationIdentity,
                        context.ApplicationLimits,
                        context.ApplicationReplication));
            }

            return(info);
        }
Exemplo n.º 11
0
        public void Build(BuildContext context, IVostokHostingEnvironment environment)
        {
            this.environment = environment;

            if (context.ZooKeeperClient != null && !(context.ZooKeeperClient is DevNullZooKeeperClient))
            {
                HostExtensions.Add(context.ZooKeeperClient);
            }

            builderCustomization.Customize(this);

            context.DisposableHostExtensions = disposable;
        }
        private void Register <TSettings, TMiddleware>(IServiceCollection services, Customization <TSettings> customization, List <Type> middlewares)
            where TSettings : class
        {
            if (preVostokMiddlewares.TryGetValue(typeof(TMiddleware), out var injected))
            {
                middlewares.AddRange(injected);
            }

            if (IsEnabled <TMiddleware>())
            {
                services.Configure <TSettings>(settings => customization.Customize(settings));

                middlewares.Add(typeof(TMiddleware));
            }
        }
Exemplo n.º 13
0
        public IServiceLocator Build(BuildContext context)
        {
            var zooKeeperClient = context.ZooKeeperClient;

            if (zooKeeperClient == null)
            {
                context.LogDisabled("ServiceLocator", "disabled ZooKeeperClient");
                return(new DevNullServiceLocator());
            }

            var settings = new ServiceLocatorSettings();

            settingsCustomization.Customize(settings);

            return(new ServiceLocator(zooKeeperClient, settings));
        }
Exemplo n.º 14
0
        public IServiceDiscoveryEventsContext Build(BuildContext context)
        {
            var herculesEventsSender = herculesServiceDiscoveryEventsSenderBuilder.Build(context);

            if (herculesEventsSender == null)
            {
                context.LogDisabled("ServiceDiscoveryEventsContext", "disabled HerculesServiceDiscoveryEventsSender");
                return(new DevNullServiceDiscoveryEventsContext());
            }

            var setting = new ServiceDiscoveryEventsContextConfig(herculesEventsSender);

            settingsCustomization.Customize(setting);

            return(new ServiceDiscoveryEventsContext(setting));
        }
Exemplo n.º 15
0
        public ILog Build(BuildContext context)
        {
            if (!enabled)
            {
                context.LogDisabled("FileLog");
                return(null);
            }

            if (settingsProvider == null)
            {
                var settings = new FileLogSettings();
                settingsCustomization.Customize(settings);
                settingsProvider = () => settings;
            }

            return(logCustomization.Customize(new FileLog(settingsProvider)));
        }
Exemplo n.º 16
0
        public void Build(BuildContext context, IVostokHostingEnvironment environment)
        {
            this.environment = environment;

            if (context.ZooKeeperClient != null && !(context.ZooKeeperClient is DevNullZooKeeperClient))
            {
                HostExtensions.Add(context.ZooKeeperClient);
            }

            HostExtensions.Add <IVostokApplicationDiagnostics>(context.DiagnosticsHub);
            HostExtensions.Add(context.Logs.LogEventLevelCounterFactory);

            builderCustomization.Customize(this);

            context.DisposableHostExtensions = disposable;
            context.HostExtensions           = HostExtensions;
        }
Exemplo n.º 17
0
        public void Build(BuildContext context, IVostokHostingEnvironment environment)
        {
            this.environment = environment;

            var settings = settingsCustomization.Customize(new SystemMetricsSettings());

            var processMetricsContext = context.Metrics.Instance.WithTag("SystemMetricsType", "Process");
            var hostMetricsContext    = context.Metrics.Instance.WithTag("SystemMetricsType", "Host");

            if (RuntimeDetector.IsDotNetCore30AndNewer)
            {
                RegisterGcMonitor(settings, context, processMetricsContext);
            }

            RegisterProcessMonitor(settings, context, processMetricsContext);

            RegisterHostMonitor(settings, context, hostMetricsContext);
        }
Exemplo n.º 18
0
        public IHerculesSink Build(BuildContext context)
        {
            if (!enabled)
            {
                context.LogDisabled("HerculesSink");
                return(null);
            }

            if (instance != null)
            {
                return(instance);
            }

            var cluster = clusterProviderBuilder?.Build(context);

            if (cluster == null)
            {
                context.LogDisabled("HerculesSink", "unconfigured cluster provider");
                return(null);
            }

            var log = context.Log;

            if (!verboseLogging)
            {
                log = log.WithMinimumLevel(LogLevel.Warn);
            }

            log = log.DropEvents(evt => evt?.MessageTemplate != null && evt.MessageTemplate.Contains("put event to a disposed"));

            // Note(kungurtsev): allow null api key provider, streams can be configured later.
            var settings = new HerculesSinkSettings(cluster, apiKeyProvider ?? (() => null))
            {
                AdditionalSetup = setup =>
                {
                    setup.ClientApplicationName = context.ApplicationIdentity.FormatServiceName();
                    setup.SetupDistributedTracing(context.Tracer);
                }
            };

            settingsCustomization.Customize(settings);

            return(new HerculesSink(settings, log));
        }
        private void ConfigureWebHostInternal(IWebHostBuilder webHostBuilder)
        {
            ConfigureUrl(webHostBuilder);

            var urlsBefore = webHostBuilder.GetSetting(WebHostDefaults.ServerUrlsKey);

            webHostBuilder.UseKestrel(kestrelBuilder.ConfigureKestrel);
            webHostBuilder.UseSockets();
            webHostBuilder.UseShutdownTimeout(environment.ShutdownTimeout.Cut(100.Milliseconds(), 0.05));

            if (typeof(TStartup) != typeof(EmptyStartup))
            {
                webHostBuilder.UseStartup <TStartup>();
            }

            webHostCustomization.Customize(webHostBuilder);

            EnsureUrlsNotChanged(urlsBefore, webHostBuilder.GetSetting(WebHostDefaults.ServerUrlsKey));
        }
Exemplo n.º 20
0
        public ILog Build(BuildContext context)
        {
            if (!enabled)
            {
                context.LogDisabled("ConsoleLog");
                return(null);
            }

            var settings = new ConsoleLogSettings
            {
                ColorsEnabled = true
            };

            settingsCustomization.Customize(settings);

            var log = synchronous ? (ILog) new SynchronousConsoleLog(settings) : new ConsoleLog(settings);

            return(logCustomization.Customize(log));
        }
Exemplo n.º 21
0
        public IVostokApplicationMetrics Build(BuildContext context)
        {
            var sender = BuildCompositeMetricEventSender(context);

            if (sender == null)
            {
                return(new VostokApplicationMetrics(new DevNullMetricContext(), context.ApplicationIdentity));
            }

            var settings = new MetricContextConfig(sender)
            {
                ErrorCallback = e => context.Log.ForContext <MetricContext>().Error(e, "Failed to send metrics.")
            };

            settingsCustomization.Customize(settings);

            var root = new MetricContext(settings);

            return(new VostokApplicationMetrics(root, context.ApplicationIdentity));
        }
Exemplo n.º 22
0
        private HealthTracker BuildHealthTracker(BuildContext context)
        {
            var healthSettings = healthSettingsCustomization.Customize(new HealthTrackerSettings());
            var healthTracker  = new HealthTracker(healthSettings.ChecksPeriod, context.Log);

            if (healthSettings.AddDatacenterWhitelistCheck)
            {
                healthTracker.RegisterCheck("Datacenter whitelist", new DatacenterWhitelistCheck(context.Datacenters ?? new EmptyDatacenters()));
            }

            if (healthSettings.AddThreadPoolStarvationCheck)
            {
                healthTracker.RegisterCheck("Thread pool", new ThreadPoolStarvationCheck());
            }

            if (healthSettings.AddZooKeeperConnectionCheck && context.ZooKeeperClient is ZooKeeperClient realClient)
            {
                healthTracker.RegisterCheck("ZooKeeper connection", new ZooKeeperConnectionCheck(realClient));
            }

            return(healthTracker);
        }
Exemplo n.º 23
0
        public void Should_perform_customize_action_in_order()
        {
            var customization = new Customization <List <int> >();

            customization.AddCustomization(
                l => { l.Add(1); });

            customization.AddCustomization(
                l =>
            {
                var ll = new List <int>(l)
                {
                    2
                };
                return(ll);
            });

            customization.AddCustomization(
                l => { l.Add(3); });

            customization.AddCustomization(
                l =>
            {
                var ll = new List <int>(l)
                {
                    4
                };
                return(ll);
            });

            var list = customization.Customize(new List <int> {
                0
            });

            list.Should().BeEquivalentTo(new List <int> {
                0, 1, 2, 3, 4
            }, options => options.WithStrictOrdering());
        }
Exemplo n.º 24
0
        public IHerculesSink Build(BuildContext context)
        {
            if (!enabled)
            {
                context.LogDisabled("HerculesSink");
                return(null);
            }

            if (instance != null)
            {
                context.ExternalComponents.Add(instance);
                return(instance);
            }

            var cluster = clusterProviderBuilder?.Build(context);

            if (cluster == null)
            {
                context.LogDisabled("HerculesSink", "unconfigured cluster provider");
                return(null);
            }

            var log = context.Log;

            if (!verboseLogging)
            {
                log = log.WithMinimumLevel(LogLevel.Warn);
            }

            log = log.DropEvents(evt => evt?.MessageTemplate != null && evt.MessageTemplate.Contains("put event to a disposed"));

            var settings = BuildHerculesSettings(cluster, context);

            settingsCustomization.Customize(settings);

            return(new HerculesSink(settings, log));
        }
Exemplo n.º 25
0
        public IHostBuilder CreateHostBuilder()
        {
            var hostBuilder = Host.CreateDefaultBuilder();

            hostBuilder.ConfigureHostConfiguration(config => config.AddDefaultLoggingFilters());

            hostBuilder.ConfigureLogging(log => log.AddVostokLogging(environment, GetLoggerSettings()));

            hostBuilder.ConfigureAppConfiguration(config => config.AddVostokSources(environment));

            hostBuilder.ConfigureServices(
                services =>
            {
                services.AddSingleton <IHostLifetime, GenericHostEmptyLifetime>();

                services.AddVostokEnvironment(environment, application);

                services.Configure <HostOptions>(options => options.ShutdownTimeout = environment.ShutdownTimeout.Cut(100.Milliseconds(), 0.05));
            });

            hostCustomization.Customize(new GenericHostBuilderWrapper(hostBuilder));

            return(hostBuilder);
        }
Exemplo n.º 26
0
        public ILog Build(BuildContext context)
        {
            if (!enabled)
            {
                context.LogDisabled("ConsoleLog");
                return(null);
            }

            var settings = new ConsoleLogSettings
            {
                ColorsEnabled = true
            };

            settingsCustomization.Customize(settings);

            var log = synchronous ? (ILog) new SynchronousConsoleLog(settings) : new ConsoleLog(settings);

            if (minLevelProvider != null)
            {
                log = log.WithMinimumLevel(minLevelProvider);
            }

            return(logCustomization.Customize(log));
        }