Skip to content

Commit

Permalink
Merge pull request #26 from purview-dev/metrics-naming-issue
Browse files Browse the repository at this point in the history
fix: fixed invalid prefix modes for instruments
  • Loading branch information
kieronlanning authored May 1, 2024
2 parents bb32da8 + b1867ec commit 7c6265a
Show file tree
Hide file tree
Showing 179 changed files with 9,968 additions and 133 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SOLUTION_FILE = $(ROOT_FOLDER)Purview.Telemetry.SourceGenerator.sln
TEST_PROJECT = $(ROOT_FOLDER)Purview.Telemetry.SourceGenerator.sln
CONFIGURATION = Release

PACK_VERSION = 1.0.3
PACK_VERSION = 1.0.4
ARTIFACT_FOLDER = p:/sync-projects/.local-nuget/

# Targets
Expand Down
3 changes: 2 additions & 1 deletion samples/SampleApp/SampleApp.Host/SampleApp.Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="1.0.3" />
<PackageReference Include="Purview.Telemetry.SourceGenerator" Version="1.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
namespace Purview.Telemetry.SourceGenerator.Metrics;

partial class TelemetrySourceGeneratorMetricsTests
{
[Fact]
public async Task Generate_GivenNameWithInterfacePrefix_GeneratesMetricsWithPrefix()
{
// Arrange
var basicMetric = @$"
using Purview.Telemetry.Metrics;
namespace Testing;
[Meter(""testing-meter"", InstrumentPrefix = ""This.Is.A.Prefix"")]
interface ITestMetrics
{{
[AutoCounter]
void AutoCounterMetric();
}}
";

// Act
var generationResult = await GenerateAsync(basicMetric);

// Assert
await TestHelpers.Verify(generationResult);
}

[Fact]
public async Task Generate_GivenNameWithAssemblyPrefix_GeneratesMetricsWithPrefix()
{
// Arrange
var basicMetric = @$"
using Purview.Telemetry.Metrics;
[assembly: MeterGeneration(InstrumentPrefix = ""This.Is.An.Assembly.Prefix"")]
namespace Testing;
[Meter(""testing-meter"")]
interface ITestMetrics
{{
[AutoCounter]
void AutoCounterMetric();
}}
";

// Act
var generationResult = await GenerateAsync(basicMetric);

// Assert
await TestHelpers.Verify(generationResult);
}

[Fact]
public async Task Generate_GivenNameWithAssemblyAndInterfacePrefix_GeneratesMetricsWithPrefix()
{
// Arrange
var basicMetric = @$"
using Purview.Telemetry.Metrics;
[assembly: MeterGeneration(InstrumentPrefix = ""This.Is.An.Assembly.Prefix"")]
namespace Testing;
[Meter(""testing-meter"", InstrumentPrefix = ""This.Is.A.Prefix"")]
interface ITestMetrics
{{
[AutoCounter]
void AutoCounterMetric();
}}
";

// Act
var generationResult = await GenerateAsync(basicMetric);

// Assert
await TestHelpers.Verify(generationResult);
}

[Fact]
public async Task Generate_GivenNameShouldBeLowerCase_GeneratesMetricsWithLowercaseName()
{
// Arrange
var basicMetric = @$"
using Purview.Telemetry.Metrics;
namespace Testing;
[Meter(""testing-meter"")]
interface ITestMetrics
{{
[AutoCounter]
void AutoCounterMetric();
}}
";

// Act
var generationResult = await GenerateAsync(basicMetric);

// Assert
await TestHelpers.Verify(generationResult);
}

[Fact]
public async Task Generate_GivenNameShouldBeDefaultLowerCase_GeneratesMetricsWithLowercaseName()
{
// Arrange
var basicMetric = @$"
using Purview.Telemetry.Metrics;
namespace Testing;
[Meter(""testing-meter"")]
interface ITestMetrics
{{
[AutoCounter]
void AutoCounterMetric();
}}
";

// Act
var generationResult = await GenerateAsync(basicMetric);

// Assert
await TestHelpers.Verify(generationResult);
}

[Fact]
public async Task Generate_GivenNameShouldBeDefinedCase_GeneratesMetricsWithLowercaseName()
{
// Arrange
var basicMetric = @$"
using Purview.Telemetry.Metrics;
namespace Testing;
[Meter(""testing-meter"", LowercaseInstrumentName = false)]
interface ITestMetrics
{{
[AutoCounter]
void AutoCounterMetric();
}}
";

// Act
var generationResult = await GenerateAsync(basicMetric);

// Assert
await TestHelpers.Verify(generationResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counterInstrument = _meter.CreateCounter<int>(name: "Counter", unit: null, description: null
_counterInstrument = _meter.CreateCounter<int>(name: "counter", unit: null, description: null
#if !NET7_0
, tags: counterTags
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sealed partial class TestMetricsCore : Testing.ITestMetrics
{
System.Diagnostics.Metrics.Meter _meter = default!;

System.Diagnostics.Metrics.Counter<System.Int32>? _autoCounterInstrument = null;
System.Diagnostics.Metrics.Counter<int>? _autoCounterInstrument = null;

public TestMetricsCore(
#if NET8_0_OR_GREATER
Expand Down Expand Up @@ -71,7 +71,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_autoCounterInstrument = _meter.CreateCounter<System.Int32>(name: "AutoCounter", unit: null, description: null
_autoCounterInstrument = _meter.CreateCounter<int>(name: "autocounter", unit: null, description: null
#if !NET7_0
, tags: autoCounterTags
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ sealed partial class TestMetricsCore : Testing.ITestMetrics
{
System.Diagnostics.Metrics.Meter _meter = default!;

System.Diagnostics.Metrics.Counter<System.Int32>? _counter1Instrument = null;
System.Diagnostics.Metrics.Counter<System.Int32>? _counter2Instrument = null;
System.Diagnostics.Metrics.Counter<System.Int32>? _counter3Instrument = null;
System.Diagnostics.Metrics.Counter<int>? _counter1Instrument = null;
System.Diagnostics.Metrics.Counter<int>? _counter2Instrument = null;
System.Diagnostics.Metrics.Counter<int>? _counter3Instrument = null;

public TestMetricsCore(
#if NET8_0_OR_GREATER
Expand Down Expand Up @@ -73,7 +73,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counter1Instrument = _meter.CreateCounter<System.Int32>(name: "Counter1", unit: null, description: null
_counter1Instrument = _meter.CreateCounter<int>(name: "counter1", unit: null, description: null
#if !NET7_0
, tags: counter1Tags
#endif
Expand All @@ -87,7 +87,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counter2Instrument = _meter.CreateCounter<System.Int32>(name: "Counter2", unit: null, description: null
_counter2Instrument = _meter.CreateCounter<int>(name: "counter2", unit: null, description: null
#if !NET7_0
, tags: counter2Tags
#endif
Expand All @@ -101,7 +101,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counter3Instrument = _meter.CreateCounter<System.Int32>(name: "Counter3", unit: null, description: null
_counter3Instrument = _meter.CreateCounter<int>(name: "counter3", unit: null, description: null
#if !NET7_0
, tags: counter3Tags
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counterInstrument = _meter.CreateCounter<int>(name: "Counter", unit: null, description: null
_counterInstrument = _meter.CreateCounter<int>(name: "counter", unit: null, description: null
#if !NET7_0
, tags: counterTags
#endif
Expand All @@ -91,7 +91,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counter2Instrument = _meter.CreateCounter<byte>(name: "Counter2", unit: null, description: null
_counter2Instrument = _meter.CreateCounter<byte>(name: "counter2", unit: null, description: null
#if !NET7_0
, tags: counter2Tags
#endif
Expand All @@ -105,7 +105,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counter3Instrument = _meter.CreateCounter<long>(name: "Counter3", unit: null, description: null
_counter3Instrument = _meter.CreateCounter<long>(name: "counter3", unit: null, description: null
#if !NET7_0
, tags: counter3Tags
#endif
Expand All @@ -119,7 +119,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counter4Instrument = _meter.CreateCounter<short>(name: "Counter4", unit: null, description: null
_counter4Instrument = _meter.CreateCounter<short>(name: "counter4", unit: null, description: null
#if !NET7_0
, tags: counter4Tags
#endif
Expand All @@ -133,7 +133,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counter5Instrument = _meter.CreateCounter<double>(name: "Counter5", unit: null, description: null
_counter5Instrument = _meter.CreateCounter<double>(name: "counter5", unit: null, description: null
#if !NET7_0
, tags: counter5Tags
#endif
Expand All @@ -147,7 +147,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counter6Instrument = _meter.CreateCounter<float>(name: "Counter6", unit: null, description: null
_counter6Instrument = _meter.CreateCounter<float>(name: "counter6", unit: null, description: null
#if !NET7_0
, tags: counter6Tags
#endif
Expand All @@ -161,7 +161,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_counter7Instrument = _meter.CreateCounter<decimal>(name: "Counter7", unit: null, description: null
_counter7Instrument = _meter.CreateCounter<decimal>(name: "counter7", unit: null, description: null
#if !NET7_0
, tags: counter7Tags
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_histogramInstrument = _meter.CreateHistogram<int>(name: "Histogram", unit: null, description: null
_histogramInstrument = _meter.CreateHistogram<int>(name: "histogram", unit: null, description: null
#if !NET7_0
, tags: histogramTags
#endif
Expand All @@ -86,7 +86,7 @@ System.Diagnostics.Metrics.IMeterFactory meterFactory

#endif

_histogram1Instrument = _meter.CreateHistogram<int>(name: "Histogram1", unit: null, description: null
_histogram1Instrument = _meter.CreateHistogram<int>(name: "histogram1", unit: null, description: null
#if !NET7_0
, tags: histogram1Tags
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void ObservableCounter(System.Func<int> f, int intParam, bool boolParam)
observableCounterTagList.Add("intparam", intParam);
observableCounterTagList.Add("boolparam", boolParam);

_observableCounterInstrument = _meter.CreateObservableCounter<int>("ObservableCounter", f, unit: null, description: null
_observableCounterInstrument = _meter.CreateObservableCounter<int>("observablecounter", f, unit: null, description: null
#if !NET7_0
, tags: observableCounterTagList
#endif
Expand All @@ -101,15 +101,15 @@ public void ObservableCounter2(System.Func<System.Diagnostics.Metrics.Measuremen
{
if (_observableCounter2Instrument != null)
{
throw new System.Exception("ObservableCounter2 has already been initialized.");
throw new System.Exception("observablecounter2 has already been initialized.");
}

System.Diagnostics.TagList observableCounter2TagList = new System.Diagnostics.TagList();

observableCounter2TagList.Add("intparam", intParam);
observableCounter2TagList.Add("boolparam", boolParam);

_observableCounter2Instrument = _meter.CreateObservableCounter<int>("ObservableCounter2", f, unit: null, description: null
_observableCounter2Instrument = _meter.CreateObservableCounter<int>("observablecounter2", f, unit: null, description: null
#if !NET7_0
, tags: observableCounter2TagList
#endif
Expand All @@ -129,7 +129,7 @@ public void ObservableCounter3(System.Func<System.Collections.Generic.IEnumerabl
observableCounter3TagList.Add("intparam", intParam);
observableCounter3TagList.Add("boolparam", boolParam);

_observableCounter3Instrument = _meter.CreateObservableCounter<int>("ObservableCounter3", f, unit: null, description: null
_observableCounter3Instrument = _meter.CreateObservableCounter<int>("observablecounter3", f, unit: null, description: null
#if !NET7_0
, tags: observableCounter3TagList
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void ObservableGauge(System.Func<int> f, int intParam, bool boolParam)
observableGaugeTagList.Add("intparam", intParam);
observableGaugeTagList.Add("boolparam", boolParam);

_observableGaugeInstrument = _meter.CreateObservableGauge<int>("ObservableGauge", f, unit: null, description: null
_observableGaugeInstrument = _meter.CreateObservableGauge<int>("observablegauge", f, unit: null, description: null
#if !NET7_0
, tags: observableGaugeTagList
#endif
Expand All @@ -101,15 +101,15 @@ public void ObservableGauge2(System.Func<System.Diagnostics.Metrics.Measurement<
{
if (_observableGauge2Instrument != null)
{
throw new System.Exception("ObservableGauge2 has already been initialized.");
throw new System.Exception("observablegauge2 has already been initialized.");
}

System.Diagnostics.TagList observableGauge2TagList = new System.Diagnostics.TagList();

observableGauge2TagList.Add("intparam", intParam);
observableGauge2TagList.Add("boolparam", boolParam);

_observableGauge2Instrument = _meter.CreateObservableGauge<int>("ObservableGauge2", f, unit: null, description: null
_observableGauge2Instrument = _meter.CreateObservableGauge<int>("observablegauge2", f, unit: null, description: null
#if !NET7_0
, tags: observableGauge2TagList
#endif
Expand All @@ -129,7 +129,7 @@ public void ObservableGauge3(System.Func<System.Collections.Generic.IEnumerable<
observableGauge3TagList.Add("intparam", intParam);
observableGauge3TagList.Add("boolparam", boolParam);

_observableGauge3Instrument = _meter.CreateObservableGauge<int>("ObservableGauge3", f, unit: null, description: null
_observableGauge3Instrument = _meter.CreateObservableGauge<int>("observablegauge3", f, unit: null, description: null
#if !NET7_0
, tags: observableGauge3TagList
#endif
Expand Down
Loading

0 comments on commit 7c6265a

Please sign in to comment.