Skip to content

Commit 0ac9ec4

Browse files
Merge pull request #4 from RagingKore/fix-default-instance-configuration-requirement
Allow Metrics instance to run without any configuration
2 parents e291928 + 6bfb016 commit 0ac9ec4

File tree

7 files changed

+77
-12
lines changed

7 files changed

+77
-12
lines changed

Ubiquitous.Metrics.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{FCC6B170
1616
EndProject
1717
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ubiquitous.Metrics.DogstatsdTests", "test\Ubiquitous.Metrics.DogstatsdTests\Ubiquitous.Metrics.DogstatsdTests.csproj", "{0B906251-A1F8-483C-B38B-CEF42542F4D5}"
1818
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ubiquitous.Metrics.Tests", "test\Ubiquitous.Metrics.Tests\Ubiquitous.Metrics.Tests.csproj", "{11674751-77D7-4267-8BF3-F626CD563100}"
20+
EndProject
1921
Global
2022
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2123
Debug|Any CPU = Debug|Any CPU
@@ -46,11 +48,16 @@ Global
4648
{0B906251-A1F8-483C-B38B-CEF42542F4D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
4749
{0B906251-A1F8-483C-B38B-CEF42542F4D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
4850
{0B906251-A1F8-483C-B38B-CEF42542F4D5}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{11674751-77D7-4267-8BF3-F626CD563100}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
52+
{11674751-77D7-4267-8BF3-F626CD563100}.Debug|Any CPU.Build.0 = Debug|Any CPU
53+
{11674751-77D7-4267-8BF3-F626CD563100}.Release|Any CPU.ActiveCfg = Release|Any CPU
54+
{11674751-77D7-4267-8BF3-F626CD563100}.Release|Any CPU.Build.0 = Release|Any CPU
4955
EndGlobalSection
5056
GlobalSection(NestedProjects) = preSolution
5157
{ADEF7CBE-D1CB-45BB-A105-B7E27A7A9B76} = {9D8B5837-914A-4683-A7AA-18E05EEAA8CD}
5258
{DB2E241F-E447-406B-AE16-1D908FE0CDEC} = {9D8B5837-914A-4683-A7AA-18E05EEAA8CD}
5359
{127825BC-ACCB-488C-9821-AF539474E627} = {9D8B5837-914A-4683-A7AA-18E05EEAA8CD}
5460
{0B906251-A1F8-483C-B38B-CEF42542F4D5} = {FCC6B170-077B-456D-9D82-5C9FE0F43B05}
61+
{11674751-77D7-4267-8BF3-F626CD563100} = {FCC6B170-077B-456D-9D82-5C9FE0F43B05}
5562
EndGlobalSection
5663
EndGlobal

src/Directory.Build.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
</ItemGroup>
3030

3131
<ItemGroup>
32-
<PackageReference Include="MinVer" Version="2.4.0">
32+
<PackageReference Include="MinVer" Version="2.5.0">
3333
<PrivateAssets>all</PrivateAssets>
3434
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3535
</PackageReference>
36-
<PackageReference Include="JetBrains.Annotations" Version="2020.3.0" PrivateAssets="All" />
36+
<PackageReference Include="JetBrains.Annotations" Version="2021.2.0" PrivateAssets="All" />
37+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
3738
</ItemGroup>
3839

3940
</Project>

src/Ubiquitous.Metrics.Diagnostics/Ubiquitous.Metrics.Diagnostics.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</PropertyGroup>
55

66
<ItemGroup>
7-
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="3.1.8"/>
8-
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0"/>
7+
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="3.1.18" />
8+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
99
</ItemGroup>
1010
</Project>

src/Ubiquitous.Metrics/Metrics.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public class Metrics {
1717
Func<MetricDefinition, IGaugeMetric>? _createGauge;
1818
Func<MetricDefinition, IHistogramMetric>? _createHistogram;
1919

20-
static Metrics() => Instance = new Metrics();
20+
static Metrics() {
21+
Instance = new Metrics();
22+
Instance = CreateUsing(new NoMetricsProvider());
23+
}
2124

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

6164
/// <summary>
6265
/// Create a histogram metric
6366
/// </summary>
6467
/// <param name="definition">Metric definition (name, description and labels)</param>
6568
/// <returns></returns>
6669
public IHistogramMetric CreateHistogram(MetricDefinition definition)
67-
=> Ensure.NotDefault(_createHistogram, "Metrics provider hasn't been configured")(definition);
70+
=> _createHistogram(definition);
6871

6972
/// <summary>
7073
/// Create a gauge metric
7174
/// </summary>
7275
/// <param name="definition">Metric definition (name, description and labels)</param>
7376
/// <returns></returns>
7477
public IGaugeMetric CreateGauge(MetricDefinition definition)
75-
=> Ensure.NotDefault(_createGauge, "Metrics provider hasn't been configured")(definition);
78+
=> _createGauge(definition);
7679

7780
[Obsolete("Use MeasureTask")]
7881
public static Task Measure(

test/Directory.Build.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="AutoFixture" Version="4.15.0"/>
9+
<PackageReference Include="AutoFixture" Version="4.17.0"/>
1010
<PackageReference Include="Bogus" Version="33.0.2"/>
11-
<PackageReference Include="FluentAssertions" Version="5.10.3"/>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1"/>
11+
<PackageReference Include="FluentAssertions" Version="6.0.0"/>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
1313
<PackageReference Include="xunit" Version="2.4.1"/>
1414
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>
1717
</PackageReference>
18-
<PackageReference Include="coverlet.collector" Version="3.0.3">
18+
<PackageReference Include="coverlet.collector" Version="3.1.0">
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>
2121
</PackageReference>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace Ubiquitous.Metrics.Tests {
6+
public class MetricsInstanceTests {
7+
[Fact]
8+
public void ShouldNotThrowIfMetricsNotConfigured() {
9+
Action captureSomeMetrics = () => TestApplicationMetrics.CountUploads(Guid.NewGuid());
10+
11+
captureSomeMetrics.Should().NotThrow<Exception>();
12+
13+
TestApplicationMetrics.UploadsCounter.Should().Be(1);
14+
}
15+
}
16+
17+
static class TestApplicationMetrics {
18+
static ICountMetric UploadsCount { get; } = Metrics.Instance.CreateCount("app_uploads", "Number of uploads", "request_id");
19+
20+
public static ulong UploadsCounter { get; private set; }
21+
22+
public static void CountUploads(Guid requestId) {
23+
UploadsCount.Inc(new[] { requestId.ToString() });
24+
UploadsCounter++;
25+
}
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
12+
<PackageReference Include="xunit" Version="2.4.1" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
<PackageReference Include="coverlet.collector" Version="3.0.2">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\..\src\Ubiquitous.Metrics\Ubiquitous.Metrics.csproj" />
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)