Exemple #1
0
 /// <summary>
 /// <para>Configures the global default <see cref="IHerculesSink"/> with given instance, which will be returned by all subsequent <see cref="Get"/> calls.</para>
 /// <para>By default, this method fails when trying to overwrite a previously configured instance. This behaviour can be changed with <paramref name="canOverwrite"/> parameter.</para>
 /// </summary>
 /// <exception cref="ArgumentNullException">Provided instance was <c>null</c>.</exception>
 /// <exception cref="InvalidOperationException">Attempted to overwrite previously configured instance.</exception>
 public static void Configure([NotNull] IHerculesSink herculesSink, bool canOverwrite = false)
 {
     if (!TryConfigure(herculesSink, canOverwrite))
     {
         throw new InvalidOperationException($"Can't overwrite existing configured Hercules sink implementation of type '{instance.GetType().Name}'.");
     }
 }
        public void Setup()
        {
            stream = "test_stream_for_logs";
            sink   = Substitute.For <IHerculesSink>();

            log = new HerculesLog(new HerculesLogSettings(sink, stream));
        }
        /// <param name="herculesSink">Hercules sink used to emit events.</param>
        /// <param name="stream">Name of the Hercules stream to use.</param>
        public HerculesLogSettings([NotNull] IHerculesSink herculesSink, [NotNull] string stream)
        {
            HerculesSink = herculesSink ?? throw new ArgumentNullException(nameof(herculesSink));

            if (string.IsNullOrEmpty(stream))
            {
                throw new ArgumentNullException(nameof(stream));
            }
            Stream = stream;
        }
        public static void Measure(IHerculesSink herculesSink, IVostokApplicationMetrics context, ILog log)
        {
            if (!(herculesSink is HerculesSink sink))
            {
                return;
            }

            // ReSharper disable once ObjectCreationAsStatement
            new HerculesSinkMetrics(sink, context.Application.WithTag(WellKnownTagKeys.Component, "HerculesSink"), log);
        }
        internal VostokHostingEnvironment(
            [NotNull] HostingShutdown hostingShutdown,
            [NotNull] ApplicationShutdown applicationShutdown,
            [NotNull] IVostokApplicationIdentity applicationIdentity,
            [NotNull] IVostokApplicationLimits applicationLimits,
            [NotNull] Func <IVostokApplicationReplicationInfo> replicationInfoProvider,
            [NotNull] IVostokApplicationMetrics metrics,
            [NotNull] IVostokApplicationDiagnostics diagnostics,
            [NotNull] ILog log,
            [NotNull] ITracer tracer,
            [NotNull] IHerculesSink herculesSink,
            [NotNull] IConfigurationSource configurationSource,
            [NotNull] IConfigurationSource secretConfigurationSource,
            [NotNull] IConfigurationProvider configurationProvider,
            [NotNull] IConfigurationProvider secretConfigurationProvider,
            [NotNull] IClusterConfigClient clusterConfigClient,
            [NotNull] IServiceBeacon serviceBeacon,
            [CanBeNull] int?port,
            [NotNull] IServiceLocator serviceLocator,
            [NotNull] IContextGlobals contextGlobals,
            [NotNull] IContextProperties contextProperties,
            [NotNull] IContextConfiguration contextConfiguration,
            [NotNull] IDatacenters datacenters,
            [NotNull] IVostokHostExtensions hostExtensions,
            [NotNull] Action onDispose)
        {
            this.hostingShutdown         = hostingShutdown ?? throw new ArgumentNullException(nameof(hostingShutdown));
            this.applicationShutdown     = applicationShutdown ?? throw new ArgumentNullException(nameof(applicationShutdown));
            this.replicationInfoProvider = replicationInfoProvider ?? throw new ArgumentNullException(nameof(replicationInfoProvider));
            this.onDispose = onDispose ?? throw new ArgumentNullException(nameof(onDispose));

            ApplicationIdentity = applicationIdentity ?? throw new ArgumentNullException(nameof(applicationIdentity));
            ApplicationLimits   = applicationLimits ?? throw new ArgumentNullException(nameof(applicationLimits));
            Metrics             = metrics ?? throw new ArgumentNullException(nameof(metrics));
            Diagnostics         = diagnostics ?? throw new ArgumentNullException(nameof(diagnostics));
            Log                         = log ?? throw new ArgumentNullException(nameof(log));
            Tracer                      = tracer ?? throw new ArgumentNullException(nameof(tracer));
            HerculesSink                = herculesSink ?? throw new ArgumentNullException(nameof(herculesSink));
            ConfigurationSource         = configurationSource ?? throw new ArgumentNullException(nameof(configurationSource));
            ConfigurationProvider       = configurationProvider ?? throw new ArgumentNullException(nameof(configurationProvider));
            SecretConfigurationSource   = secretConfigurationSource ?? throw new ArgumentNullException(nameof(secretConfigurationSource));
            SecretConfigurationProvider = secretConfigurationProvider ?? throw new ArgumentNullException(nameof(secretConfigurationProvider));
            ClusterConfigClient         = clusterConfigClient ?? throw new ArgumentNullException(nameof(clusterConfigClient));
            ServiceBeacon               = serviceBeacon ?? throw new ArgumentNullException(nameof(serviceBeacon));
            Port                        = port;
            ServiceLocator              = serviceLocator ?? throw new ArgumentNullException(nameof(serviceLocator));
            ContextGlobals              = contextGlobals ?? throw new ArgumentNullException(nameof(contextGlobals));
            ContextProperties           = contextProperties ?? throw new ArgumentNullException(nameof(contextProperties));
            ContextConfiguration        = contextConfiguration ?? throw new ArgumentNullException(nameof(contextConfiguration));
            Datacenters                 = datacenters ?? throw new ArgumentNullException(nameof(datacenters));
            HostExtensions              = hostExtensions ?? throw new ArgumentNullException(nameof(hostExtensions));
        }
        public static IDisposable Measure(IHerculesSink herculesSink, IVostokApplicationMetrics context, ILog log)
        {
            if (!(herculesSink is HerculesSink sink))
            {
                return(null);
            }

            var builtContext = context.Application.WithTag(WellKnownTagKeys.Component, "HerculesSink");

            return(builtContext.CreateMultiFuncGaugeFromEvents(new HerculesSinkMetrics(sink, builtContext, log).ProvideMetrics, new FuncGaugeConfig {
                ScrapeOnDispose = true
            }) as IDisposable);
        }
Exemple #7
0
        public async Task Should_be_able_to_override_common_components()
        {
            IHerculesSink extractedSink = null;

            void Assert(IVostokHostingEnvironment environment)
            {
                environment.HerculesSink.Should().BeSameAs(extractedSink);
            }

            void Extract(IVostokHostingEnvironment environment)
            {
                extractedSink = environment.HerculesSink;
            }

            var initialSink = new DevNullHerculesSink();

            var vostokMultiHost = new VostokMultiHost(
                new VostokMultiHostSettings(
                    builder =>
            {
                builder.SetupApplicationIdentity(identityBuilder => identityBuilder.SetProject("testProject").SetEnvironment("testEnvironment"));
                builder.SetupHerculesSink(sinkBuilder => sinkBuilder.UseInstance(initialSink));
                builder.SetupLog(logBuilder => logBuilder.SetupConsoleLog());
            }));

            var extractIdentifier = ("testApplication", "testInstance");

            var extractApplication = new VostokMultiHostApplicationSettings(
                new TesterApplication(Extract),
                extractIdentifier,
                SetupMultiHostApplication);

            var assertIdentifier = ("testApplication", "testInstance2");

            var assertApplication = new VostokMultiHostApplicationSettings(
                new TesterApplication(Assert),
                assertIdentifier,
                SetupMultiHostApplication);

            await vostokMultiHost.StartAsync();

            await vostokMultiHost.RunSequentiallyAsync(extractApplication, assertApplication);

            var results = await vostokMultiHost.StopAsync();

            // ReSharper disable once PossibleNullReferenceException
            results.ApplicationRunResults[assertIdentifier].EnsureSuccess();

            extractedSink.Should().BeSameAs(initialSink);
        }
        public void IsEnabledFor_should_respect_enabled_log_levels()
        {
            sink = Substitute.For <IHerculesSink>();

            log = new HerculesLog(new HerculesLogSettings(sink, "stream")
            {
                EnabledLogLevels = new[] { LogLevel.Debug, LogLevel.Warn, LogLevel.Fatal }
            });

            log.IsEnabledFor(LogLevel.Debug).Should().BeTrue();
            log.IsEnabledFor(LogLevel.Info).Should().BeFalse();
            log.IsEnabledFor(LogLevel.Warn).Should().BeTrue();
            log.IsEnabledFor(LogLevel.Error).Should().BeFalse();
            log.IsEnabledFor(LogLevel.Fatal).Should().BeTrue();
        }
Exemple #9
0
        /// <summary>
        /// <para>Configures the global default <see cref="IHerculesSink"/> with given instance, which will be returned by all subsequent <see cref="Get"/> calls.</para>
        /// <para>By default, this method returns <c>false</c> when trying to overwrite a previously configured instance. This behaviour can be changed with <paramref name="canOverwrite"/> parameter.</para>
        /// </summary>
        /// <exception cref="ArgumentNullException">Provided instance was <c>null</c>.</exception>
        public static bool TryConfigure([NotNull] IHerculesSink herculesSink, bool canOverwrite = false)
        {
            if (herculesSink == null)
            {
                throw new ArgumentNullException(nameof(herculesSink));
            }

            if (canOverwrite)
            {
                Interlocked.Exchange(ref instance, herculesSink);
                return(true);
            }

            return(Interlocked.CompareExchange(ref instance, herculesSink, null) == null);
        }
        public void TestSetup()
        {
            originalSpan = Substitute.For <ISpan>();
            originalSpan.TraceId.Returns(Guid.NewGuid());
            originalSpan.SpanId.Returns(Guid.NewGuid());

            builder = new HerculesEventBuilder();

            @event = null;

            sink = Substitute.For <IHerculesSink>();
            sink
            .When(s => s.Put(Arg.Any <string>(), Arg.Any <Action <IHerculesEventBuilder> >()))
            .Do(info => info.Arg <Action <IHerculesEventBuilder> >().Invoke(builder));

            sender = new HerculesSpanSender(new HerculesSpanSenderSettings(sink, Guid.NewGuid().ToString()));
        }
        public void Log_should_respect_enabled_log_levels()
        {
            sink = Substitute.For <IHerculesSink>();

            log = new HerculesLog(new HerculesLogSettings(sink, "stream")
            {
                EnabledLogLevels = new[] { LogLevel.Debug, LogLevel.Warn, LogLevel.Fatal }
            });

            log.Debug("");
            sink.ReceivedCalls().Should().HaveCount(1);

            log.Info("");
            sink.ReceivedCalls().Should().HaveCount(1);

            log.Warn("");
            sink.ReceivedCalls().Should().HaveCount(2);

            log.Error("");
            sink.ReceivedCalls().Should().HaveCount(2);

            log.Fatal("");
            sink.ReceivedCalls().Should().HaveCount(3);
        }
 public HerculesSpanSenderSettings([NotNull] IHerculesSink sink, [NotNull] string stream)
 {
     Sink   = sink ?? throw new ArgumentNullException(nameof(sink));
     Stream = stream ?? throw new ArgumentNullException(nameof(stream));
 }