Skip to content

Commit

Permalink
test: added test to validate meter prefix and specific meter name
Browse files Browse the repository at this point in the history
  • Loading branch information
kieronlanning committed Jun 21, 2024
1 parent e1dc671 commit 68c81fa
Show file tree
Hide file tree
Showing 32 changed files with 2,162 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,32 @@ interface ITestMetrics
await TestHelpers.Verify(generationResult);
}

[Fact]
public async Task Generate_GivenNameWithAssemblyAndInterfacePrefixAndName_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(""auto-counter"")]
void AutoCounterMetric();
}}
";

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

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

[Fact]
public async Task Generate_GivenNameShouldBeLowerCase_GeneratesMetricsWithLowercaseName()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//HintName: ActivityAttribute.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the Purview.Telemetry.SourceGenerator
// on {Scrubbed}.
//
// Changes to this file may cause incorrect behaviour and will be lost
// when the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable

namespace Purview.Telemetry.Activities;

/// <summary>
/// Marker attribute used to control the generation
/// of <see cref="System.Diagnostics.Activity">activities</see>.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = false)]
[System.Diagnostics.Conditional("PURVIEW_TELEMETRY_ATTRIBUTES")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed class ActivityAttribute : System.Attribute
{
/// <summary>
/// Constructs a new <see cref="ActivityAttribute"/>.
/// </summary>
public ActivityAttribute()
{
}

/// <summary>
/// Constructs a new <see cref="ActivityAttribute"/> and specifies the <see cref="Name"/>.
/// </summary>
/// <param name="name">Specifies the <see cref="Name"/>.</param>
public ActivityAttribute(string name)
{
Name = name;
}

/// <summary>
/// Constructs a new <see cref="ActivityAttribute"/> and specifies the <see cref="Kind"/>.
/// </summary>
/// <param name="kind">Specifies the <see cref="Kind"/>.</param>
public ActivityAttribute(System.Diagnostics.ActivityKind kind)
{
Kind = kind;
}

/// <summary>
/// Constructs a new <see cref="ActivityAttribute"/> and specifies the <see cref="Name" /> and
/// optionally the <see cref="Kind"/> and/ or <see cref="CreateOnly"/>.
/// </summary>
/// <param name="name">Specifies the <see cref="Name"/>.</param>
/// <param name="kind">Optionally specifies the <see cref="Kind"/>.</param>
/// <param name="createOnly">Optionally specifies <see cref="CreateOnly"/>.</param>
public ActivityAttribute(string name, System.Diagnostics.ActivityKind kind, bool createOnly = false)
{
Name = name;
Kind = kind;
CreateOnly = createOnly;
}

/// <summary>
/// Optional. Gets/ sets the name of the <see cref="System.Diagnostics.Activity"/>.
/// If this is not specified, the name of the method is used.
/// </summary>
public string? Name { get; set; }

/// <summary>
/// Optional. Gets/ sets the <see cref="System.Diagnostics.ActivityKind">kind</see> of the
/// activity. Defaults to <see cref="System.Diagnostics.ActivityKind.Internal"/>.
/// </summary>
public System.Diagnostics.ActivityKind Kind { get; set; } = System.Diagnostics.ActivityKind.Internal;

/// <summary>
/// If true, the <see cref="System.Diagnostics.Activity"/> is created using
/// <see cref="System.Diagnostics.ActivitySource.CreateActivity(string, System.Diagnostics.ActivityKind, string?, System.Collections.Generic.IEnumerable{KeyValuePair{string, object?}}?, System.Collections.Generic.IEnumerable{System.Diagnostics.ActivityLink}?, System.Diagnostics.ActivityIdFormat)" />, meaning it is not started by default. Otherwise
/// <see cref="System.Diagnostics.ActivitySource.StartActivity(string, System.Diagnostics.ActivityKind, string?, System.Collections.Generic.IEnumerable{KeyValuePair{string, object?}}?, System.Collections.Generic.IEnumerable{System.Diagnostics.ActivityLink}?, System.DateTimeOffset)" />is used. The default is false.
/// </summary>
public bool CreateOnly { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//HintName: ActivitySourceAttribute.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the Purview.Telemetry.SourceGenerator
// on {Scrubbed}.
//
// Changes to this file may cause incorrect behaviour and will be lost
// when the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable

namespace Purview.Telemetry.Activities;

/// <summary>
/// Marker attribute required for <see cref="System.Diagnostics.Activity"/>
/// and <see cref="System.Diagnostics.ActivityEvent"/> generation.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Interface, AllowMultiple = false)]
[System.Diagnostics.Conditional("PURVIEW_TELEMETRY_ATTRIBUTES")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed class ActivitySourceAttribute : System.Attribute
{
/// <summary>
/// Constructs a new <see cref="ActivitySourceAttribute"/>.
/// </summary>
public ActivitySourceAttribute()
{
}

/// <summary>
/// Constructs a new <see cref="ActivitySourceAttribute"/> specifying the <see cref="Name"/>.
/// </summary>
/// <param name="name">The <see cref="Name"/>.</param>
public ActivitySourceAttribute(string name)
{
Name = name;
}

/// <summary>
/// Sets the name for the generated <see cref="System.Diagnostics.ActivitySource.Name"/>,
/// overriding the <see cref="ActivitySourceGenerationAttribute.Name"/>.
/// </summary>
public string? Name { get; set; }

/// <summary>
/// Specifies the default when inferring between
/// <see cref="Purview.Telemetry.TagAttribute"/> or
/// <see cref="Purview.Telemetry.Activities.BaggageAttribute"/>, unless
/// explicitly marked.
/// </summary>
public bool DefaultToTags { get; set; } = true;

/// <summary>
/// Prefix used to when generating the tag or baggage name. Prepended
/// before the <see cref="Purview.Telemetry.TagAttribute.Name"/> or
/// <see cref="Purview.Telemetry.Activities.BaggageAttribute.Name"/>.
/// </summary>
public string? BaggageAndTagPrefix { get; set; }

/// <summary>
/// Determines if the <see cref="Name"/> (or <see cref="ActivitySourceGenerationAttribute.BaggageAndTagPrefix"/>)
/// is used as a prefix, before the <see cref="BaggageAndTagPrefix"/>.
/// </summary>
public bool IncludeActivitySourcePrefix { get; set; } = true;

/// <summary>
/// Determines if the <see cref="Purview.Telemetry.TagAttribute.Name"/> or
/// <see cref="Purview.Telemetry.Activities.BaggageAttribute.Name"/> (including
/// any prefixes) are lowercased.
/// </summary>
public bool LowercaseBaggageAndTagKeys { get; set; } = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//HintName: ActivitySourceGenerationAttribute.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the Purview.Telemetry.SourceGenerator
// on {Scrubbed}.
//
// Changes to this file may cause incorrect behaviour and will be lost
// when the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable

namespace Purview.Telemetry.Activities;

/// <summary>
/// Determines the default <see cref="System.Diagnostics.ActivitySource.Name" /> for generated
/// <see cref="System.Diagnostics.Activity">activities</see> and <see cref="System.Diagnostics.ActivityEvent">events</see>.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple = false)]
[System.Diagnostics.Conditional("PURVIEW_TELEMETRY_ATTRIBUTES")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed class ActivitySourceGenerationAttribute : System.Attribute
{
/// <summary>
/// Constructs a new <see cref="ActivitySourceGenerationAttribute"/>.
/// </summary>
/// <param name="name">The name of the activity source.</param>
/// <param name="defaultToTags">Determines if the default for method parameters are Tags (default) or Baggage.</param>
/// <param name="generateDiagnosticsForMissingActivity">Determines if diagnostics are generated for missing activities. Defaults to true.</param>
/// <exception cref="ArgumentNullException">If the <paramref name="name"/> is null, empty or whitespace.</exception>
public ActivitySourceGenerationAttribute(string name, bool defaultToTags = true, bool generateDiagnosticsForMissingActivity = true)
{
if (string.IsNullOrWhiteSpace(name))
throw new System.ArgumentNullException(nameof(name));

Name = name;
DefaultToTags = defaultToTags;
GenerateDiagnosticsForMissingActivity = generateDiagnosticsForMissingActivity;
}

/// <summary>
/// Specifies the default <see cref="System.Diagnostics.ActivitySource.Name"/> to use.
/// </summary>
public string Name { get; }

/// <summary>
/// Specifies the default used when inferring between
/// <see cref="Purview.Telemetry.TagAttribute"/>
/// or <see cref="Purview.Telemetry.Activities.BaggageAttribute"/>, unless
/// explicitly marked. Overridden when specifying <see cref="ActivitySourceAttribute.DefaultToTags"/>.
/// </summary>
public bool DefaultToTags { get; set; } = true;

/// <summary>
/// Prefix used to when generating the tag or baggage name. Prepended
/// before the <see cref="Purview.Telemetry.TagAttribute.Name"/> or
/// <see cref="Purview.Telemetry.Activities.BaggageAttribute.Name"/>, unless
/// explicitly marked. Overridden when specifying <see cref="ActivitySourceAttribute.BaggageAndTagPrefix"/>.
/// </summary>
public string? BaggageAndTagPrefix { get; set; }

/// <summary>
/// Determines the separator used between the <see cref="System.Diagnostics.ActivitySource.Name"/> and
/// the various prefix options. The default is a period.
/// </summary>
public string BaggageAndTagSeparator { get; set; } = ".";

/// <summary>
/// Determines if the <see cref="Purview.Telemetry.TagAttribute.Name"/> or
/// <see cref="Purview.Telemetry.Activities.BaggageAttribute.Name"/> (including
/// any prefixes) are lowercased, unless
/// explicitly marked. Overridden when specifying <see cref="ActivitySourceAttribute.LowercaseBaggageAndTagKeys"/>.
/// </summary>
public bool LowercaseBaggageAndTagKeys { get; set; } = true;

/// <summary>
/// Determines if diagnostics are generated for when an Activity method does not return an activity, or
/// when an Event or Context method does not include and Activity as a parameter.
/// </summary>
public bool GenerateDiagnosticsForMissingActivity { get; set; } = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//HintName: AutoCounterAttribute.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the Purview.Telemetry.SourceGenerator
// on {Scrubbed}.
//
// Changes to this file may cause incorrect behaviour and will be lost
// when the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable

namespace Purview.Telemetry.Metrics;

/// <summary>
/// Specifies the meter type generated corresponds to a
/// <see cref="System.Diagnostics.Metrics.Counter{T}"/> that auto-increments when called.
///
/// This is equivalent to applying the <see cref="CounterAttribute"/> with the
/// <see cref="CounterAttribute.AutoIncrement"/> property set to true.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional("PURVIEW_TELEMETRY_ATTRIBUTES")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed class AutoCounterAttribute : System.Attribute
{
/// <summary>
/// Creates a new instance of the <see cref="AutoCounterAttribute"/> class.
/// </summary>
public AutoCounterAttribute()
{
}

/// <summary>
/// Creates a new instance of the <see cref="AutoCounterAttribute"/> class, and specifies the
/// <see cref="Name"/>, and optionally the <see cref="Unit"/> and <see cref="Description"/>.
/// </summary>
/// <param name="name">Specifies the <see cref="Name"/>.</param>
/// <param name="unit">Optionally specifies the <see cref="Unit"/>.</param>
/// <param name="description">Optionally specifies the <see cref="Description"/>.</param>
public AutoCounterAttribute(string name, string? unit = null, string? description = null)
{
Name = name;
Unit = unit;
Description = description;
}

/// <summary>
/// Optionally specifies the name of the meter. If one is not specified, the name
/// of the method is used.
/// </summary>

public string? Name { get; set; }

/// <summary>
/// Optionally specifies the unit of the meter.
/// </summary>
public string? Unit { get; set; }

/// <summary>
/// Optionally specifies the description of the meter.
/// </summary>
public string? Description { get; set; }
}
Loading

0 comments on commit 68c81fa

Please sign in to comment.