public static void Main(string[] args) { Console.WriteLine("Enabling prometheus-net.DotNetStats..."); DotNetRuntimeStatsBuilder.Default().StartCollecting(); CreateHostBuilder(args).Build().Run(); }
public virtual void ConfigureMetrics(IApplicationBuilder app) { var callsCounter = Metrics.CreateCounter("request_total", "Counts the requests to the Country API endpoints", new CounterConfiguration() { LabelNames = new[] { "method", "endpoint" } }); app.Use((context, next) => { callsCounter.WithLabels(context.Request.Method, context.Request.Path); return(next()); }); var collector = DotNetRuntimeStatsBuilder .Customize() .WithContentionStats() .WithJitStats() .WithThreadPoolSchedulingStats() .WithThreadPoolStats() .WithGcStats() .WithExceptionStats() .StartCollecting(); }
public void SetUp() { _collector = (DotNetRuntimeStatsCollector)ConfigureBuilder(DotNetRuntimeStatsBuilder.Customize()) .StartCollecting(Prometheus.Metrics.NewCustomRegistry()); MetricProducer = (TMetricProducer)_collector.ServiceProvider.GetServices <IMetricProducer>().Single(x => x is TMetricProducer); // wait for event listener thread to spin up var waitingFor = Stopwatch.StartNew(); var waitFor = TimeSpan.FromSeconds(10); while (!_collector.EventListeners.All(x => x.StartedReceivingEvents)) { Thread.Sleep(10); Console.Write("Waiting for event listeners to be active.. "); if (waitingFor.Elapsed > waitFor) { Assert.Fail($"Waited {waitFor} and still not all event listeners were ready! Event listeners not ready: {string.Join(", ", _collector.EventListeners.Where(x => !x.StartedReceivingEvents))}"); return; } } Console.WriteLine("All event listeners should be active now."); }
public void StartCollecting_Does_Not_Allow_Two_Collectors_To_Run_Simultaneously_For_Each_Registry_Instance() { var registry1 = NewRegistry();; var registry2 = NewRegistry();; using (DotNetRuntimeStatsBuilder.Customize().StartCollecting()) { Assert.Throws <InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting()); using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1)) { Assert.Throws <InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting()); Assert.Throws <InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1)); using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry2)) { Assert.Throws <InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting()); Assert.Throws <InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1)); Assert.Throws <InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry2)); } Assert.Throws <InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting()); Assert.Throws <InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1)); } Assert.Throws <InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting()); } }
public static IDisposable CreateCollector() { _logger.LogInformation($"Configuring prometheus-net.DotNetRuntime: will recycle event listeners every {_options.RecycleEvery} ({_options.RecycleEvery.TotalSeconds:N0} seconds)."); var builder = DotNetRuntimeStatsBuilder.Default(); if (!_options.UseDefaultMetrics) { builder = DotNetRuntimeStatsBuilder.Customize() .WithContentionStats(CaptureLevel.Informational) .WithGcStats(CaptureLevel.Verbose) .WithThreadPoolStats(CaptureLevel.Informational) .WithExceptionStats(CaptureLevel.Errors) .WithJitStats() .WithKestrelStats(CaptureLevel.Informational); } builder #if NET5_0 .RecycleCollectorsEvery(_options.RecycleEvery) #endif .WithErrorHandler(ex => _logger.LogError(ex, "Unexpected exception occurred in prometheus-net.DotNetRuntime")); if (_options.UseDebuggingMetrics) { _logger.LogInformation("Using debugging metrics."); builder.WithDebuggingMetrics(true); } _logger.LogInformation("Starting prometheus-net.DotNetRuntime..."); return(builder .StartCollecting()); }
/// <summary> /// The program entrypoint. /// </summary> /// <param name="args">The command line arguments.</param> public static async Task Main(string[] args) { NeonService.Initialize(); try { Service = new Service(KubeService.NeonSsoSessionProxy); Service.MetricsOptions.Mode = MetricsMode.Scrape; Service.MetricsOptions.GetCollector = () => { return(DotNetRuntimeStatsBuilder .Default() .StartCollecting()); }; Environment.Exit(await Service.RunAsync()); } catch (Exception e) { // We really shouldn't see exceptions here but let's log something // just in case. Note that logging may not be initialized yet so // we'll just output a string. Console.Error.WriteLine(NeonHelper.ExceptionError(e)); Environment.Exit(-1); } }
public void StartCollecting_Allows_A_New_Collector_To_Run_After_Disposing_Previous_Collector_For_Each_Registry_Instance() { var registry1 = NewRegistry(); var registry2 = NewRegistry(); using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1)) { using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry2)) { } using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry2)) { } } using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry2)) { using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1)) { } using (DotNetRuntimeStatsBuilder.Customize().StartCollecting(registry1)) { } } }
public static void Main(string[] args) { Serilog.Debugging.SelfLog.Enable(Console.Error); Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Console() .CreateBootstrapLogger(); IDisposable dotNetRuntimeStats = null; try { dotNetRuntimeStats = DotNetRuntimeStatsBuilder.Default().StartCollecting(); CreateHostBuilder(args).Build().Run(); } catch (Exception ex) { Log.Fatal(ex, "An unhandled exception occurred during bootstrapping"); } finally { dotNetRuntimeStats?.Dispose(); Log.CloseAndFlush(); } }
static void Main(string[] args) { var p = new Prometheus.MetricServer(12203); p.Start(); var collector = DotNetRuntimeStatsBuilder.Default().StartCollecting(); var tasks = Enumerable.Range(1, 2_000_000) .Select(_ => Task.Run(() => 1)) .ToArray(); var b = new byte[1024 * 1000]; var b2 = new byte[1024 * 1000]; var b3 = new byte[1024 * 1000]; Task.WaitAll(tasks); Console.WriteLine("Done"); Console.ReadLine(); return; BenchmarkRunner.Run <TestBenchmark>( DefaultConfig.Instance .With( Job.Core .WithLaunchCount(1) .WithIterationTime(TimeInterval.FromMilliseconds(200)) .With(Platform.X64) .With(Jit.RyuJit) ) ); }
public static async Task Main(string[] args) { var service = new MyService(); service.MetricsOptions.Mode = MetricsMode.Scrape; // For .NET Core 2.2+ based services, we highly recommend that you enable // collection of the .NET Runtime metrics as well to capture information // about threads, memory, exceptions, JIT statistics, etc. You can do this // with just one more statement: service.MetricsOptions.GetCollector = () => DotNetRuntimeStatsBuilder .Default() .StartCollecting(); // The line above collects all of the available runtime metrics. You can // customize which metrics are collected using this commented line, but // we recommend collecting everything because you never know what you'll // need: //service.MetricsOptions.GetCollector = () => // DotNetRuntimeStatsBuilder // .Customize() // .WithContentionStats() // .WithJitStats() // .WithThreadPoolStats() // .WithGcStats() // .WithExceptionStats() // .StartCollecting(); await new MyService().RunAsync(); }
public void StartCollecting_Does_Not_Allow_Two_Collectors_To_Run_Simultaneously() { using (DotNetRuntimeStatsBuilder.Customize().StartCollecting()) { Assert.Throws<InvalidOperationException>(() => DotNetRuntimeStatsBuilder.Customize().StartCollecting()); } }
public void Start() { _collector = DotNetRuntimeStatsBuilder.Default().StartCollecting(); _logger.LogInformation("LifetimeManager starting..."); _client.Start(); _logger.LogInformation("LifetimeManager sleep 2 second..."); Thread.Sleep(2000); _logger.LogInformation("sessionReader - count: {Count}", _sessionReader.Count()); _logger.LogInformation("registrationTokenReader - count: {Count}", _registrationTokenReader.Count()); _logger.LogInformation("clientWalletReader - count: {Count}", _clientWalletReader.Count()); _logger.LogInformation("assetsReader - count: {Count}", _assetsReader.Count()); _logger.LogInformation("assetPairsReader - count: {Count}", _assetPairsReader.Count()); _logger.LogInformation("personalDataReader - count: {Count}", _personalDataReader.Count()); _logger.LogInformation("authDataReader - count: {Count}", _authDataReader.Count()); _logger.LogInformation("authIndexNyIdDataReader - count: {Count}", _authIndexNyIdDataReader.Count()); _logger.LogInformation("clientProfileDataReader - count: {Count}", _clientProfileDataReader.Count()); _logger.LogInformation("orderBookDataReader - count: {Count}", _orderBookDataReader.Count()); _logger.LogInformation("priceDataReader - count: {Count}", _priceDataReader.Count()); _logger.LogInformation("LifetimeManager started"); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Database database, KeyStorage keyStorage, ILogger <Startup> logger) { logger.LogInformation("Startup.Configure called"); keyStorage.LoadKeysFromEnvIfNeeded(); if (DotNetRuntimeCollector == null && String.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("DOTNET_DISABLE_EXTENDED_METRICS"))) { // https://github.com/djluck/prometheus-net.DotNetRuntime DotNetRuntimeCollector = DotNetRuntimeStatsBuilder.Customize() // Only 1 in 10 contention events will be sampled .WithContentionStats(sampleRate: SampleEvery.TenEvents) // Only 1 in 100 JIT events will be sampled .WithJitStats(sampleRate: SampleEvery.HundredEvents) // Every event will be sampled (disables sampling) .WithThreadPoolSchedulingStats(sampleRate: SampleEvery.OneEvent) .StartCollecting(); } database.InitializeDatabase(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseSerilogRequestLogging(); //app.UseHttpMetrics(); // doesn't work. Probably because we have our own mapping, and something is missing app.UseEndpoints(Endpoint.All); app.UseEndpoints(endpoints => { endpoints.MapMetrics(); }); app.UseHangfireServer(); app.UseHangfireDashboard(); UserCleanupJob.StartRecurringJob(); }
protected override DotNetRuntimeStatsBuilder.Builder GetStatsBuilder() { return(DotNetRuntimeStatsBuilder.Default() .WithThreadPoolSchedulingStats(sampleRate: SampleEvery.OneEvent) .WithJitStats(SampleEvery.OneEvent) .WithContentionStats(SampleEvery.OneEvent)); }
public Measurer(IOptions <MeasurerConfiguration> measurerConfigurationOptions, IOptions <CommonConfiguration> commonConfigurationOptions) { var measurerConfiguration = measurerConfigurationOptions.Value; _collector = DotNetRuntimeStatsBuilder.Default().StartCollecting(); _heartbeatCounter = Prometheus.Metrics.CreateCounter("heartbeat_count", "heartbeat"); _httpRequestCounter = Prometheus.Metrics.CreateCounter("request_total", "HTTP Requests Total", new CounterConfiguration { LabelNames = new[] { "path", "method", "status", "correlation-id" } }); if (measurerConfiguration.UsePushMetricsServer) { //push model _pusher = new MetricPusher(new MetricPusherOptions { Endpoint = measurerConfiguration.PushMetricsServerEndpoint, Job = "push-metrics-job", Instance = commonConfigurationOptions.Value.InstanceId, IntervalMilliseconds = (long)measurerConfiguration.FlushPeriod.TotalMilliseconds }); _pusher.Start(); } }
public Task StartAsync(CancellationToken cancellationToken) { if (!_options.Enabled) return Task.CompletedTask; _logger.LogInformation("Configuring prometheus-net.DotNetRuntime"); var builder = DotNetRuntimeStatsBuilder.Default(); if (_options.Verbose) { builder = DotNetRuntimeStatsBuilder.Customize() .WithContentionStats(CaptureLevel.Informational) .WithGcStats(CaptureLevel.Verbose) .WithThreadPoolStats(CaptureLevel.Informational) .WithExceptionStats(CaptureLevel.Errors) .WithJitStats(); } builder.WithErrorHandler(ex => _logger.LogError(ex, "Unexpected exception occurred in prometheus-net.DotNetRuntime")); if (_options.Debug) { _logger.LogInformation("Using debugging metrics"); builder.WithDebuggingMetrics(true); } _logger.LogInformation("Starting prometheus-net.DotNetRuntime"); _metrics = builder.StartCollecting(); return Task.CompletedTask; }
public async Task Default_registers_all_expected_stats() { // arrange using (DotNetRuntimeStatsBuilder.Default().StartCollecting()) { await Assert_Expected_Stats_Are_Present_In_Registry(GetDefaultRegistry()); } }
public Startup(IConfiguration configuration) { Configuration = configuration; DotNetRuntimeStatsBuilder.Customize() .WithExceptionStats(CaptureLevel.Errors) .StartCollecting(); }
public static void Main(string[] args) { DotNetRuntimeStatsBuilder.Default().WithErrorHandler(e => { Console.WriteLine(e.ToString()); }).StartCollecting(); CreateWebHostBuilder(args).Build().Run(); }
public void WithCustomCollector_will_not_register_the_same_collector_twice() { var builder = DotNetRuntimeStatsBuilder .Customize() .WithGcStats() .WithCustomCollector(new GcStatsCollector()); Assert.That(builder.StatsCollectors.Count, Is.EqualTo(1)); }
public static void Main(string[] args) { DotNetRuntimeStatsBuilder.Default().WithErrorHandler(e => { Console.WriteLine(e.ToString()); }).StartCollecting(); Serilog.Debugging.SelfLog.Enable(Console.Error); CreateHostBuilder(args).Build().Run(); }
public void StartCollecting_Allows_A_New_Collector_To_Run_After_Disposing_A_Previous_Collector() { using (DotNetRuntimeStatsBuilder.Customize().StartCollecting()) { } using (DotNetRuntimeStatsBuilder.Customize().StartCollecting()) { } }
public async Task Debugging_Metrics_Works_Correctly() { // arrange var registry = NewRegistry(); using (DotNetRuntimeStatsBuilder.Default().WithDebuggingMetrics(true).StartCollecting(registry)) { await Assert_Expected_Stats_Are_Present_In_Registry(registry, shouldContainDebug : true); } }
public async Task Default_registers_all_expected_stats_to_a_custom_registry() { // arrange var registry = NewRegistry(); using (DotNetRuntimeStatsBuilder.Default().StartCollecting(registry)) { await Assert_Expected_Stats_Are_Present_In_Registry(registry); } }
public void Cannot_Register_Tasks_At_Unsupported_Levels() { var ex = Assert.Throws <UnsupportedCaptureLevelException>(() => DotNetRuntimeStatsBuilder.Customize().WithGcStats(CaptureLevel.Errors)); Assert.That(ex.SpecifiedLevel, Is.EqualTo(CaptureLevel.Errors)); Assert.That(ex.SupportedLevels, Is.EquivalentTo(new [] { CaptureLevel.Verbose, CaptureLevel.Informational })); ex = Assert.Throws <UnsupportedCaptureLevelException>(() => DotNetRuntimeStatsBuilder.Customize().WithThreadPoolStats(CaptureLevel.Verbose)); Assert.That(ex.SpecifiedLevel, Is.EqualTo(CaptureLevel.Verbose)); Assert.That(ex.SupportedLevels, Is.EquivalentTo(new [] { CaptureLevel.Counters, CaptureLevel.Informational })); }
public static void Main(string[] args) { var collector = DotNetRuntimeStatsBuilder.Default().StartCollecting(); //var server = new MetricServer(hostname: "*", port: 1234); var server = new MetricServer(hostname: "*", port: 1234); server.Start(); _timer = new Timer(LogNetworkAndGcInfo, null, TimeSpan.Zero, TimeSpan.FromSeconds(1)); CreateHostBuilder(args).Build().Run(); }
public static void Main(string[] args) { DotNetRuntimeStatsBuilder.Customize() .WithThreadPoolStats() .WithContentionStats() .WithGcStats() .WithJitStats() .WithThreadPoolStats() .StartCollecting(); CreateHostBuilder(args).Build().Run(); }
public void WithCustomCollector_will_not_register_the_same_collector_twice() { var expectedCollector = new GcStatsCollector(); var builder = DotNetRuntimeStatsBuilder .Customize() .WithGcStats() .WithCustomCollector(expectedCollector); Assert.That(builder.StatsCollectors.Count, Is.EqualTo(1)); Assert.That(builder.StatsCollectors.TryGetValue(new GcStatsCollector(), out var actualColector), Is.True); Assert.That(actualColector, Is.SameAs(expectedCollector)); }
public async Task StartCollecting_Allows_A_New_Collector_To_Run_After_Disposing_A_Previous_Collector() { using (DotNetRuntimeStatsBuilder.Customize().StartCollecting()) { await Assert_Expected_Stats_Are_Present_In_Registry(GetDefaultRegistry()); } using (DotNetRuntimeStatsBuilder.Customize().StartCollecting()) { await Assert_Expected_Stats_Are_Present_In_Registry(GetDefaultRegistry()); } }
public async Task RuntimeMetrics() { // Verify that we can also expose .NET Runtime exposes metrics via the [GetCollector()] callback. var service = new TestService(); var metricPort = NetHelper.GetUnusedTcpPort(IPAddress.Loopback); var metricsPath = "foo/"; service.MetricsOptions.Mode = MetricsMode.Scrape; service.MetricsOptions.Port = metricPort; service.MetricsOptions.Path = metricsPath; service.MetricsOptions.GetCollector = () => DotNetRuntimeStatsBuilder.Default().StartCollecting(); var runTask = service.RunAsync(); // Wait for the test service to do its thing. await service.ReadyToExitEvent.WaitAsync(); using (var httpClient = new HttpClient()) { var scrapedMetrics = await httpClient.GetStringAsync($"http://127.0.0.1:{metricPort}/{metricsPath}"); var metrics = ParseMetrics(scrapedMetrics); // Verify the test counter. Assert.True(metrics["test_counter"] > 0); // Verify Neon logging counters. Assert.True(metrics[@"neon_log_events_total{level=""warn""}"] > 0); Assert.True(metrics[@"neon_log_events_total{level=""critical""}"] > 0); Assert.True(metrics[@"neon_log_events_total{level=""transient""}"] > 0); Assert.True(metrics[@"neon_log_events_total{level=""sinfo""}"] > 0); Assert.True(metrics[@"neon_log_events_total{level=""error""}"] > 0); Assert.True(metrics[@"neon_log_events_total{level=""debug""}"] > 0); Assert.True(metrics[@"neon_log_events_total{level=""serror""}"] > 0); Assert.True(metrics[@"neon_log_events_total{level=""info""}"] > 0); // Verify some .NET Runtime metrics Assert.True(metrics.ContainsKey("process_private_memory_bytes")); Assert.True(metrics.ContainsKey("dotnet_exceptions_total")); Assert.True(metrics.ContainsKey("dotnet_threadpool_num_threads")); } // Tell the service it can exit. service.CanExitEvent.Set(); await runTask; }