Skip to content

Commit

Permalink
feat: added activities and events, moving on to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kieronlanning committed Mar 13, 2024
1 parent 8ae4451 commit fd1cdf2
Show file tree
Hide file tree
Showing 285 changed files with 2,832 additions and 439 deletions.
4 changes: 2 additions & 2 deletions src/.plan.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Plan

Seperate each of the metric types into their own telemetry type.
Separate each of the metric types into their own telemetry type.

Combine them using _either_ a single class and combined interface that pulls in all functionality,
or have a combined class that utilies properties for the telemetry types.
or have a combined class that utilises properties for the telemetry types.

IMyActivities
IMyLogs
Expand Down
20 changes: 20 additions & 0 deletions src/Purview.Telemetry.Shared/Metrics/CounterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed public class CounterAttribute : MetricAttributeBase {
public CounterAttribute() {
}

public CounterAttribute(bool autoIncrement) {
AutoIncrement = autoIncrement;
}

public CounterAttribute(string name, string? unit = null, string? description = null, bool autoIncrement = false)
: base(name, unit, description) {
AutoIncrement = autoIncrement;
}

public bool AutoIncrement { get; set; }
}
12 changes: 12 additions & 0 deletions src/Purview.Telemetry.Shared/Metrics/HistogramAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
sealed public class HistogramAttribute : MetricAttributeBase {
public HistogramAttribute() {
}

public HistogramAttribute(string name, string? unit = null, string? description = null)
: base(name, unit, description) {
}
}
18 changes: 18 additions & 0 deletions src/Purview.Telemetry.Shared/Metrics/MeasurementTagAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed public class MeasurementTagAttribute : Attribute {
public MeasurementTagAttribute() {
}

public MeasurementTagAttribute(string name) {
Name = name;
}

/// <summary>
/// Optional. Gets the overridden name of the tag.
/// </summary>
public string? Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
sealed public class MeasurementValueAttribute : Attribute {
}
18 changes: 18 additions & 0 deletions src/Purview.Telemetry.Shared/Metrics/MeterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed public class MeterAttribute : Attribute {
public MeterAttribute() {
}

public MeterAttribute(string name) {
Name = name;
}

/// <summary>
/// Optional. Gets/ sets the name of the metric.
/// </summary>
public string? Name { get; set; }
}
19 changes: 19 additions & 0 deletions src/Purview.Telemetry.Shared/Metrics/MetricAttributeBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Purview.Telemetry.Metrics;

[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
abstract public class MetricAttributeBase : Attribute {
protected MetricAttributeBase() {
}

protected MetricAttributeBase(string name, string? unit = null, string? description = null) {
Name = name;
Unit = unit;
Description = description;
}

public string? Name { get; set; }

public string? Unit { get; set; }

public string? Description { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
sealed public class MetricExcludeAttribute : Attribute {
}
12 changes: 12 additions & 0 deletions src/Purview.Telemetry.Shared/Metrics/ObservableCounterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
sealed public class ObservableCounterAttribute : MetricAttributeBase {
public ObservableCounterAttribute() {
}

public ObservableCounterAttribute(string name, string? unit = null, string? description = null)
: base(name, unit, description) {
}
}
12 changes: 12 additions & 0 deletions src/Purview.Telemetry.Shared/Metrics/ObservableGaugeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
sealed public class ObservableGaugeAttribute : MetricAttributeBase {
public ObservableGaugeAttribute() {
}

public ObservableGaugeAttribute(string name, string? unit = null, string? description = null)
: base(name, unit, description) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
sealed public class ObservableUpDownCounterAttribute : MetricAttributeBase {
public ObservableUpDownCounterAttribute() {
}

public ObservableUpDownCounterAttribute(string name, string? unit = null, string? description = null)
: base(name, unit, description) {
}
}
12 changes: 12 additions & 0 deletions src/Purview.Telemetry.Shared/Metrics/UpDownCounterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Purview.Telemetry.Metrics;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[System.Diagnostics.Conditional(Constants.EmbedAttributesHashDefineName)]
sealed public class UpDownCounterAttribute : MetricAttributeBase {
public UpDownCounterAttribute() {
}

public UpDownCounterAttribute(string name, string? unit, string? description)
: base(name, unit, description) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async public Task Generate_GivenBasicGen_GeneratesActivity() {
namespace Testing;
[ActivityTarget]
[ActivityTarget(""testing-activity-source"")]
public interface ITestActivities {
[Activity]
void Activity([Baggage]string stringParam, [Tag]int intParam, bool boolParam);
Expand All @@ -25,4 +25,56 @@ public interface ITestActivities {
// Assert
await TestHelpers.Verify(generationResult);
}

[Fact]
async public Task Generate_GivenBasicGenWithReturningActivity_GeneratesActivity() {
// Arrange
const string basicActivity = @"
using Purview.Telemetry.Activities;
using System.Diagnostics.Activities;
namespace Testing;
[ActivityTarget(""testing-activity-source"")]
public interface ITestActivities {
[Activity]
Activity Activity([Baggage]string stringParam, [Tag]int intParam, bool boolParam);
[ActivityEvent]
Activity Event([Baggage]string stringParam, [Tag]int intParam, bool boolParam);
}
";

// Act
GenerationResult generationResult = await GenerateAsync(basicActivity);

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

[Fact]
async public Task Generate_GivenBasicGenWithReturningNullableActivity_GeneratesActivity() {
// Arrange
const string basicActivity = @"
using Purview.Telemetry.Activities;
using System.Diagnostics.Activities;
namespace Testing;
[ActivityTarget(""testing-activity-source"")]
public interface ITestActivities {
[Activity]
Activity? Activity([Baggage]string stringParam, [Tag]int intParam, bool boolParam);
[ActivityEvent]
Activity? Event([Baggage]string stringParam, [Tag]int intParam, bool boolParam);
}
";

// Act
GenerationResult generationResult = await GenerateAsync(basicActivity);

// Assert
await TestHelpers.Verify(generationResult);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//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

#if PURVIEW_TELEMETRY_EMBED_ATTRIBUTES

namespace Purview.Telemetry.Activities;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed class ActivityAttribute : Attribute {

public ActivityAttribute() {
}

public ActivityAttribute(string name) {
Name = name;
}

public ActivityAttribute(ActivityGeneratedKind kind) {
Kind = kind;
}

public ActivityAttribute(string name, ActivityGeneratedKind kind, bool createOnly = false) {
Name = name;
Kind = kind;
CreateOnly = createOnly;
}

/// <summary>
/// Optional. Gets the name of the activity.
/// </summary>
public string? Name { get; set; }

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

/// <summary>
/// If true, the Activity is crated via ActivitySource.CreateActivity, meaning it is not started by default. Otherwise
/// ActivitySource.StartActivity is used. The default is false.
/// </summary>
public bool CreateOnly { get; set; }
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//HintName: ActivityEventAttribute.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

#if PURVIEW_TELEMETRY_EMBED_ATTRIBUTES

namespace Purview.Telemetry.Activities;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed class ActivityEventAttribute : Attribute {

public ActivityEventAttribute() {
}

public ActivityEventAttribute(string name) {
Name = name;
}

/// <summary>
/// Optional. Gets/ sets the name of the event.
/// </summary>
public string? Name { get; set; }
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//HintName: ActivityExcludeAttribute.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

#if PURVIEW_TELEMETRY_EMBED_ATTRIBUTES

namespace Purview.Telemetry.Activities;

/// <summary>
/// Excludes the method from any activity generation.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
sealed class ActivityExcludeAttribute : Attribute {
}

#endif
Loading

0 comments on commit fd1cdf2

Please sign in to comment.