Skip to content

Commit

Permalink
Merge pull request #4 from RagingKore/fix-default-instance-configurat…
Browse files Browse the repository at this point in the history
…ion-requirement

Allow Metrics instance to run without any configuration
  • Loading branch information
alexeyzimarev authored Aug 16, 2021
2 parents e291928 + 6bfb016 commit 0ac9ec4
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Ubiquitous.Metrics.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{FCC6B170
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ubiquitous.Metrics.DogstatsdTests", "test\Ubiquitous.Metrics.DogstatsdTests\Ubiquitous.Metrics.DogstatsdTests.csproj", "{0B906251-A1F8-483C-B38B-CEF42542F4D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ubiquitous.Metrics.Tests", "test\Ubiquitous.Metrics.Tests\Ubiquitous.Metrics.Tests.csproj", "{11674751-77D7-4267-8BF3-F626CD563100}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -46,11 +48,16 @@ Global
{0B906251-A1F8-483C-B38B-CEF42542F4D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B906251-A1F8-483C-B38B-CEF42542F4D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B906251-A1F8-483C-B38B-CEF42542F4D5}.Release|Any CPU.Build.0 = Release|Any CPU
{11674751-77D7-4267-8BF3-F626CD563100}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11674751-77D7-4267-8BF3-F626CD563100}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11674751-77D7-4267-8BF3-F626CD563100}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11674751-77D7-4267-8BF3-F626CD563100}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{ADEF7CBE-D1CB-45BB-A105-B7E27A7A9B76} = {9D8B5837-914A-4683-A7AA-18E05EEAA8CD}
{DB2E241F-E447-406B-AE16-1D908FE0CDEC} = {9D8B5837-914A-4683-A7AA-18E05EEAA8CD}
{127825BC-ACCB-488C-9821-AF539474E627} = {9D8B5837-914A-4683-A7AA-18E05EEAA8CD}
{0B906251-A1F8-483C-B38B-CEF42542F4D5} = {FCC6B170-077B-456D-9D82-5C9FE0F43B05}
{11674751-77D7-4267-8BF3-F626CD563100} = {FCC6B170-077B-456D-9D82-5C9FE0F43B05}
EndGlobalSection
EndGlobal
5 changes: 3 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MinVer" Version="2.4.0">
<PackageReference Include="MinVer" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="2020.3.0" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2021.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="3.1.8"/>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0"/>
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="3.1.18" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
</ItemGroup>
</Project>
11 changes: 7 additions & 4 deletions src/Ubiquitous.Metrics/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public class Metrics {
Func<MetricDefinition, IGaugeMetric>? _createGauge;
Func<MetricDefinition, IHistogramMetric>? _createHistogram;

static Metrics() => Instance = new Metrics();
static Metrics() {
Instance = new Metrics();
Instance = CreateUsing(new NoMetricsProvider());
}

/// <summary>
/// Get the Metrics instance. Normally, you'd need only one Metrics instance per application.
Expand Down Expand Up @@ -56,23 +59,23 @@ public static Metrics CreateUsing(params IMetricsProvider[] configurators) {
/// <param name="definition">Metric definition (name, description and labels)</param>
/// <returns></returns>
public ICountMetric CreateCount(MetricDefinition definition)
=> Ensure.NotDefault(_createCount, "Metrics provider hasn't been configured")(definition);
=> _createCount(definition);

/// <summary>
/// Create a histogram metric
/// </summary>
/// <param name="definition">Metric definition (name, description and labels)</param>
/// <returns></returns>
public IHistogramMetric CreateHistogram(MetricDefinition definition)
=> Ensure.NotDefault(_createHistogram, "Metrics provider hasn't been configured")(definition);
=> _createHistogram(definition);

/// <summary>
/// Create a gauge metric
/// </summary>
/// <param name="definition">Metric definition (name, description and labels)</param>
/// <returns></returns>
public IGaugeMetric CreateGauge(MetricDefinition definition)
=> Ensure.NotDefault(_createGauge, "Metrics provider hasn't been configured")(definition);
=> _createGauge(definition);

[Obsolete("Use MeasureTask")]
public static Task Measure(
Expand Down
8 changes: 4 additions & 4 deletions test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.15.0"/>
<PackageReference Include="AutoFixture" Version="4.17.0"/>
<PackageReference Include="Bogus" Version="33.0.2"/>
<PackageReference Include="FluentAssertions" Version="5.10.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1"/>
<PackageReference Include="FluentAssertions" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.3">
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
27 changes: 27 additions & 0 deletions test/Ubiquitous.Metrics.Tests/MetricsInstanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using FluentAssertions;
using Xunit;

namespace Ubiquitous.Metrics.Tests {
public class MetricsInstanceTests {
[Fact]
public void ShouldNotThrowIfMetricsNotConfigured() {
Action captureSomeMetrics = () => TestApplicationMetrics.CountUploads(Guid.NewGuid());

captureSomeMetrics.Should().NotThrow<Exception>();

TestApplicationMetrics.UploadsCounter.Should().Be(1);
}
}

static class TestApplicationMetrics {
static ICountMetric UploadsCount { get; } = Metrics.Instance.CreateCount("app_uploads", "Number of uploads", "request_id");

public static ulong UploadsCounter { get; private set; }

public static void CountUploads(Guid requestId) {
UploadsCount.Inc(new[] { requestId.ToString() });
UploadsCounter++;
}
}
}
27 changes: 27 additions & 0 deletions test/Ubiquitous.Metrics.Tests/Ubiquitous.Metrics.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ubiquitous.Metrics\Ubiquitous.Metrics.csproj" />
</ItemGroup>

</Project>

0 comments on commit 0ac9ec4

Please sign in to comment.