Skip to content

Commit

Permalink
#186 removing dependency on Microsoft DI and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
alhardy committed Sep 2, 2017
1 parent 8bfd3f9 commit b23bffd
Show file tree
Hide file tree
Showing 98 changed files with 2,538 additions and 1,187 deletions.
1 change: 1 addition & 0 deletions AppMetrics.ruleset
Expand Up @@ -121,6 +121,7 @@
<Rule Id="SA1602" Action="None" />
<Rule Id="SA1603" Action="Error" />
<Rule Id="SA1609" Action="None" />
<Rule Id="SA1623" Action="None" />
<Rule Id="SA1633" Action="Error" />
<Rule Id="SA1636" Action="Error" />
<Rule Id="SA1642" Action="Error" />
Expand Down
2 changes: 1 addition & 1 deletion AppMetrics.sln
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26621.2
VisualStudioVersion = 15.0.26823.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2D805782-756E-4C98-B22E-F502BEE95318}"
EndProject
Expand Down
@@ -0,0 +1,13 @@
<CorruptedSettingsDescription>
<!--The backup file has been created before writing in-memory changes to the settings file. As the existing settings file is corrupted, many settings might get lost when writing the new content. If you'd like to restore the settings from the previous version, here're the changes against the backed-up version (note: subsequent changes are not reflected here).-->
<InMemoryChanges>
<SetValue>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">VISIBLE_FILES</s:String>
</ResourceDictionary>
</SetValue>
<ClearValue>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
</ClearValue>
</InMemoryChanges>
</CorruptedSettingsDescription>
Expand Up @@ -21,8 +21,6 @@
<PackageReference Include="xunit" Version="$(XunitVersion)" />
<PackageReference Include="BenchmarkDotNet" Version="$(BenchmarkDotNetVersion)" />
<PackageReference Include="BenchmarkDotNet.Core" Version="$(BenchmarkDotNetVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(AspNetCoreVersion)" />
</ItemGroup>

</Project>
Expand Up @@ -10,6 +10,7 @@
using App.Metrics.Internal;
using App.Metrics.Meter;
using App.Metrics.ReservoirSampling;
using App.Metrics.ReservoirSampling.ExponentialDecay;
using App.Metrics.Tagging;
using App.Metrics.Timer;
using BenchmarkDotNet.Attributes;
Expand All @@ -18,7 +19,7 @@ namespace App.Metrics.Benchmarks.BenchmarkDotNetBenchmarks.Metrics
{
public class DefaultMetricContextRegistryBenchmark : DefaultBenchmarkBase
{
private static readonly IBuildApdexMetrics ApdexBuilder = new DefaultApdexBuilder(new DefaultSamplingReservoirProvider());
private static readonly IBuildApdexMetrics ApdexBuilder = new DefaultApdexBuilder(new DefaultSamplingReservoirProvider(() => new DefaultForwardDecayingReservoir()));
private static readonly ApdexOptions ApdexOptions = new ApdexOptions
{
Name = "apdex"
Expand All @@ -40,7 +41,7 @@ public class DefaultMetricContextRegistryBenchmark : DefaultBenchmarkBase
Name = "gauge"
};

private static readonly IBuildHistogramMetrics HistogramBuilder = new DefaultHistogramBuilder(new DefaultSamplingReservoirProvider());
private static readonly IBuildHistogramMetrics HistogramBuilder = new DefaultHistogramBuilder(new DefaultSamplingReservoirProvider(() => new DefaultForwardDecayingReservoir()));

private static readonly HistogramOptions HistogramOptions = new HistogramOptions
{
Expand All @@ -54,7 +55,7 @@ public class DefaultMetricContextRegistryBenchmark : DefaultBenchmarkBase
Name = "meter"
};

private static readonly IBuildTimerMetrics TimerBuilder = new DefaultTimerBuilder(new DefaultSamplingReservoirProvider());
private static readonly IBuildTimerMetrics TimerBuilder = new DefaultTimerBuilder(new DefaultSamplingReservoirProvider(() => new DefaultForwardDecayingReservoir()));

private static readonly TimerOptions TimerOptions = new TimerOptions
{
Expand Down
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using App.Metrics.Benchmarks.Fixtures;
using App.Metrics.Benchmarks.Support;
using BenchmarkDotNet.Attributes;

Expand Down
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using App.Metrics.Benchmarks.Fixtures;
using App.Metrics.Benchmarks.Support;
using BenchmarkDotNet.Attributes;

Expand Down
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using App.Metrics.Benchmarks.Fixtures;
using App.Metrics.Benchmarks.Support;
using BenchmarkDotNet.Attributes;

Expand Down
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using App.Metrics.Benchmarks.Fixtures;
using App.Metrics.Benchmarks.Support;
using BenchmarkDotNet.Attributes;

Expand Down
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using App.Metrics.Benchmarks.Fixtures;
using App.Metrics.Benchmarks.Support;
using BenchmarkDotNet.Attributes;

Expand Down
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using App.Metrics.Benchmarks.Fixtures;
using App.Metrics.Benchmarks.Support;
using BenchmarkDotNet.Attributes;

Expand Down
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) Allan Hardy. All rights reserved.
// </copyright>

using App.Metrics.Benchmarks.Fixtures;
using App.Metrics.Benchmarks.Support;
using BenchmarkDotNet.Attributes;

Expand Down
Expand Up @@ -12,6 +12,7 @@
using App.Metrics.Meter;
using App.Metrics.Registry;
using App.Metrics.ReservoirSampling;
using App.Metrics.ReservoirSampling.ExponentialDecay;
using App.Metrics.Tagging;
using App.Metrics.Timer;

Expand Down Expand Up @@ -57,13 +58,15 @@ public MetricContextTestFixture()
{ "key2", "value2" }
};

var samplingProvider = new DefaultSamplingReservoirProvider(() => new DefaultForwardDecayingReservoir());

Registry = new DefaultMetricContextRegistry("context_label", tags);
ApdexBuilder = new DefaultApdexBuilder(new DefaultSamplingReservoirProvider());
HistogramBuilder = new DefaultHistogramBuilder(new DefaultSamplingReservoirProvider());
ApdexBuilder = new DefaultApdexBuilder(samplingProvider);
HistogramBuilder = new DefaultHistogramBuilder(samplingProvider);
CounterBuilder = new DefaultCounterBuilder();
GaugeBuilder = new DefaultGaugeBuilder();
MeterBuilder = new DefaultMeterBuilder();
TimerBuilder = new DefaultTimerBuilder(new DefaultSamplingReservoirProvider());
TimerBuilder = new DefaultTimerBuilder(samplingProvider);
Clock = new StopwatchClock();
}

Expand Down
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;

namespace App.Metrics.Benchmarks.Fixtures
{
Expand All @@ -24,13 +23,20 @@ public class MetricsCoreTestFixture : IDisposable

public MetricsCoreTestFixture()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddMetrics();

var provider = services.BuildServiceProvider();

Metrics = provider.GetRequiredService<IMetrics>();
Metrics = new MetricsBuilder()
.Options.Configure(options =>
{
options.DefaultContextLabel = "Testing";
options.MetricsEnabled = true;
})
.OutputEnvInfo.AsJson()
.OutputEnvInfo.AsPlainText()
.OutputMetrics.AsPlainText()
.OutputMetrics.AsJson()
.SampleWith.AlgorithmR()
.Filter.ByIncludingOnlyTypes(MetricType.Counter)
.TimeWith.StopwatchClock()
.Build();
}

public IMetrics Metrics { get; }
Expand Down
1 change: 1 addition & 0 deletions build/common.props
Expand Up @@ -18,6 +18,7 @@
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<LangVersion>latest</LangVersion>
<CodeAnalysisRuleSet>..\..\AppMetrics.ruleset</CodeAnalysisRuleSet>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)Key.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand Down
91 changes: 32 additions & 59 deletions sandbox/MetricsSandbox/Host.cs
Expand Up @@ -6,13 +6,9 @@
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using App.Metrics;
using App.Metrics.Filtering;
using App.Metrics.Filters;
using App.Metrics.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Serilog;

namespace MetricsSandbox
Expand All @@ -21,59 +17,46 @@ public static class Host
{
private static readonly Random Rnd = new Random();

public static IConfigurationRoot Configuration { get; set; }
private static IConfigurationRoot Configuration { get; set; }

// public static async Task Main(string[] args)
public static void Main(string[] args)
private static IMetricsRoot Metrics { get; set; }

public static async Task Main()
{
Init();

IServiceCollection serviceCollection = new ServiceCollection();
var metricsFilter = new DefaultMetricsFilter();

ConfigureServices(serviceCollection, metricsFilter);

var provider = serviceCollection.BuildServiceProvider();
var metrics = provider.GetRequiredService<IMetrics>();
var metricsProvider = provider.GetRequiredService<IProvideMetricValues>();
var envInfoProvider = provider.GetRequiredService<EnvironmentInfoProvider>();
var metricsOptionsAccessor = provider.GetRequiredService<IOptions<MetricsOptions>>();

var cancellationTokenSource = new CancellationTokenSource();

WriteEnv(envInfoProvider, metricsOptionsAccessor, cancellationTokenSource);
await WriteEnvAsync(cancellationTokenSource);

Console.ReadKey();

RunUntilEsc(
TimeSpan.FromSeconds(5),
cancellationTokenSource,
() =>
async () =>
{
Console.Clear();
RecordMetrics(metrics);
RecordMetrics();
WriteMetrics(metricsProvider, metricsFilter, metricsOptionsAccessor, cancellationTokenSource);
await WriteMetricsAsync(cancellationTokenSource);
});
}

private static void WriteEnv(
EnvironmentInfoProvider envInfoProvider,
IOptions<MetricsOptions> metricsOptionsAccessor,
CancellationTokenSource cancellationTokenSource)
private static async Task WriteEnvAsync(CancellationTokenSource cancellationTokenSource)
{
Console.WriteLine("Environment Information");
Console.WriteLine("-------------------------------------------");

foreach (var formatter in metricsOptionsAccessor.Value.OutputEnvFormatters)
foreach (var formatter in Metrics.OutputEnvFormatters)
{
Console.WriteLine($"Formatter: {formatter.GetType().FullName}");
Console.WriteLine("-------------------------------------------");

using (var stream = new MemoryStream())
{
formatter.WriteAsync(stream, envInfoProvider.Build(), cancellationTokenSource.Token).GetAwaiter().GetResult();
await formatter.WriteAsync(stream, Metrics.EnvironmentInfo, cancellationTokenSource.Token);

var result = Encoding.UTF8.GetString(stream.ToArray());

Expand All @@ -82,25 +65,21 @@ public static void Main(string[] args)
}
}

private static void WriteMetrics(
IProvideMetricValues metricsProvider,
IFilterMetrics metricsFilter,
IOptions<MetricsOptions> metricsOptionsAccessor,
CancellationTokenSource cancellationTokenSource)
private static async Task WriteMetricsAsync(CancellationTokenSource cancellationTokenSource)
{
var metricsData = metricsProvider.Get(metricsFilter);
var metricsData = Metrics.Snapshot.Get();

Console.WriteLine("Metrics Formatters");
Console.WriteLine("-------------------------------------------");

foreach (var formatter in metricsOptionsAccessor.Value.OutputMetricsFormatters)
foreach (var formatter in Metrics.OutputMetricsFormatters)
{
Console.WriteLine($"Formatter: {formatter.GetType().FullName}");
Console.WriteLine("-------------------------------------------");

using (var stream = new MemoryStream())
{
formatter.WriteAsync(stream, metricsData, cancellationTokenSource.Token).GetAwaiter().GetResult();
await formatter.WriteAsync(stream, metricsData, cancellationTokenSource.Token);

var result = Encoding.UTF8.GetString(stream.ToArray());

Expand All @@ -109,46 +88,39 @@ public static void Main(string[] args)
}
}

private static void RecordMetrics(IMetrics metrics)
private static void RecordMetrics()
{
metrics.Measure.Counter.Increment(ApplicationsMetricsRegistry.CounterOne);
metrics.Measure.Gauge.SetValue(ApplicationsMetricsRegistry.GaugeOne, Rnd.Next(0, 100));
metrics.Measure.Histogram.Update(ApplicationsMetricsRegistry.HistogramOne, Rnd.Next(0, 100));
metrics.Measure.Meter.Mark(ApplicationsMetricsRegistry.MeterOne, Rnd.Next(0, 100));
Metrics.Measure.Counter.Increment(ApplicationsMetricsRegistry.CounterOne);
Metrics.Measure.Gauge.SetValue(ApplicationsMetricsRegistry.GaugeOne, Rnd.Next(0, 100));
Metrics.Measure.Histogram.Update(ApplicationsMetricsRegistry.HistogramOne, Rnd.Next(0, 100));
Metrics.Measure.Meter.Mark(ApplicationsMetricsRegistry.MeterOne, Rnd.Next(0, 100));

using (metrics.Measure.Timer.Time(ApplicationsMetricsRegistry.TimerOne))
using (Metrics.Measure.Timer.Time(ApplicationsMetricsRegistry.TimerOne))
{
Thread.Sleep(Rnd.Next(0, 100));
}

using (metrics.Measure.Apdex.Track(ApplicationsMetricsRegistry.ApdexOne))
using (Metrics.Measure.Apdex.Track(ApplicationsMetricsRegistry.ApdexOne))
{
Thread.Sleep(Rnd.Next(0, 100));
}
}

private static void ConfigureServices(IServiceCollection services, IFilterMetrics metricsFilter)
private static void Init()
{
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");

Configuration = configurationBuilder.Build();

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.Seq("http://localhost:5341")
.CreateLogger();

services.AddMetrics();

// services.
// AddMetricsCore().
// AddClockType<SystemClock>().
// AddGlobalFilter(metricsFilter).
// AddDefaultReservoir(() => new DefaultAlgorithmRReservoir());
}

private static void Init()
{
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");

Configuration = builder.Build();
Metrics = AppMetrics.CreateDefaultBuilder().Build();
}

private static void RunUntilEsc(TimeSpan delayBetweenRun, CancellationTokenSource cancellationTokenSource, Action action)
Expand All @@ -160,6 +132,7 @@ private static void RunUntilEsc(TimeSpan delayBetweenRun, CancellationTokenSourc
while (!Console.KeyAvailable)
{
action();

Thread.Sleep(delayBetweenRun);
}

Expand Down
1 change: 0 additions & 1 deletion sandbox/MetricsSandbox/MetricsSandbox.csproj
Expand Up @@ -14,7 +14,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="3.3.3" />
</ItemGroup>
Expand Down

0 comments on commit b23bffd

Please sign in to comment.