Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Generators/Microsoft.Gen.Metrics/MetricFactoryEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,15 @@ private void GenMetricFactoryMethods(MetricMethod metricMethod, string nspace)
OutOpenBrace();
OutLn($"return {GetMetricDictionaryName(metricMethod)}.GetOrAdd({meterParam.Name}, static _meter =>");
OutLn(" {");
OutLn($" var instrument = _meter.{createMethodName}(@\"{metricMethod.MetricName}\");");
if (string.IsNullOrEmpty(metricMethod.MetricUnit))
{
OutLn($" var instrument = _meter.{createMethodName}(@\"{metricMethod.MetricName}\");");
}
else
{
OutLn($" var instrument = _meter.{createMethodName}(@\"{metricMethod.MetricName}\", @\"{metricMethod.MetricUnit}\");");
}

OutLn($" return new {nsprefix}{metricMethod.MetricTypeName}(instrument);");
OutLn(" });");
OutCloseBrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal sealed class MetricMethod
public Dictionary<string, string> TagDescriptionDictionary = [];
public string? Name;
public string? MetricName;
public string? MetricUnit;
public string? XmlDefinition;
public bool IsExtensionMethod;
public string Modifiers = string.Empty;
Expand Down
35 changes: 25 additions & 10 deletions src/Generators/Microsoft.Gen.Metrics/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -273,13 +274,14 @@ private static bool TryGetTagNameFromAttribute(ISymbol symbol, SymbolHolder symb
return false;
}

private (string metricName, HashSet<string> tagNames, Dictionary<string, string> dimensionDescriptions) ExtractAttributeParameters(
private (string metricName, string metricUnit, HashSet<string> tagNames, Dictionary<string, string> dimensionDescriptions) ExtractAttributeParameters(
AttributeData attribute,
SemanticModel semanticModel)
{
var tagHashSet = new HashSet<string>();
var tagDescriptionMap = new Dictionary<string, string>();
string metricNameFromAttribute = string.Empty;
string metricUnitFromAttribute = string.Empty;
if (!attribute.NamedArguments.IsDefaultOrEmpty)
{
foreach (var arg in attribute.NamedArguments)
Expand All @@ -288,7 +290,11 @@ private static bool TryGetTagNameFromAttribute(ISymbol symbol, SymbolHolder symb
arg.Key is "MetricName" or "Name")
{
metricNameFromAttribute = (arg.Value.Value ?? string.Empty).ToString().Replace("\\\\", "\\");
break;
}
else if (arg.Value.Kind == TypedConstantKind.Primitive &&
arg.Key is "Unit")
{
metricUnitFromAttribute = (arg.Value.Value ?? string.Empty).ToString();
}
}
}
Expand Down Expand Up @@ -330,7 +336,7 @@ private static bool TryGetTagNameFromAttribute(ISymbol symbol, SymbolHolder symb
}
}

return (metricNameFromAttribute, tagHashSet, tagDescriptionMap);
return (metricNameFromAttribute, metricUnitFromAttribute, tagHashSet, tagDescriptionMap);
}

private string GetSymbolXmlCommentSummary(ISymbol symbol)
Expand Down Expand Up @@ -422,20 +428,20 @@ private void GetTagDescription(
if (!methodAttribute.ConstructorArguments.IsDefaultOrEmpty
&& methodAttribute.ConstructorArguments[0].Kind == TypedConstantKind.Type)
{
KeyValuePair<string, TypedConstant> namedArg = default;
ImmutableArray<KeyValuePair<string, TypedConstant>> namedArgs = ImmutableArray<KeyValuePair<string, TypedConstant>>.Empty;
var ctorArg = methodAttribute.ConstructorArguments[0];

if (!methodAttribute.NamedArguments.IsDefaultOrEmpty)
{
namedArg = methodAttribute.NamedArguments[0];
namedArgs = methodAttribute.NamedArguments;
}

strongTypeAttrParams = ExtractStrongTypeAttributeParameters(ctorArg, namedArg, symbols);
strongTypeAttrParams = ExtractStrongTypeAttributeParameters(ctorArg, namedArgs, symbols);
}
else
{
var parameters = ExtractAttributeParameters(methodAttribute, semanticModel);
(strongTypeAttrParams.MetricNameFromAttribute, strongTypeAttrParams.TagHashSet, strongTypeAttrParams.TagDescriptionDictionary) = parameters;
(strongTypeAttrParams.MetricNameFromAttribute, strongTypeAttrParams.MetricUnitFromAttribute, strongTypeAttrParams.TagHashSet, strongTypeAttrParams.TagDescriptionDictionary) = parameters;
}

string metricNameFromMethod = methodSymbol.ReturnType.Name;
Expand All @@ -444,6 +450,7 @@ private void GetTagDescription(
{
Name = methodSymbol.Name,
MetricName = string.IsNullOrWhiteSpace(strongTypeAttrParams.MetricNameFromAttribute) ? metricNameFromMethod : strongTypeAttrParams.MetricNameFromAttribute,
MetricUnit = strongTypeAttrParams.MetricUnitFromAttribute,
InstrumentKind = instrumentKind,
GenericType = genericType.ToDisplayString(_genericTypeSymbolFormat),
TagKeys = strongTypeAttrParams.TagHashSet,
Expand Down Expand Up @@ -605,14 +612,22 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[

private StrongTypeAttributeParameters ExtractStrongTypeAttributeParameters(
TypedConstant constructorArg,
KeyValuePair<string, TypedConstant> namedArgument,
ImmutableArray<KeyValuePair<string, TypedConstant>> namedArguments,
SymbolHolder symbols)
{
var strongTypeAttributeParameters = new StrongTypeAttributeParameters();

if (namedArgument is { Key: "Name", Value.Value: { } })
// Extract "Name" and "Unit" values from the named arguments, if present, and assign them to the corresponding properties.
foreach (var namedArgument in namedArguments)
{
strongTypeAttributeParameters.MetricNameFromAttribute = namedArgument.Value.Value.ToString();
if (namedArgument.Key == "Name" && namedArgument.Value.Value is { } nameValue)
{
strongTypeAttributeParameters.MetricNameFromAttribute = nameValue.ToString();
}
else if (namedArgument.Key == "Unit" && namedArgument.Value.Value is { } unitValue)
{
strongTypeAttributeParameters.MetricUnitFromAttribute = unitValue.ToString();
}
}

if (constructorArg.IsNull ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Gen.Metrics;
internal sealed class StrongTypeAttributeParameters
{
public string MetricNameFromAttribute = string.Empty;
public string MetricUnitFromAttribute = string.Empty;
public HashSet<string> TagHashSet = [];
public Dictionary<string, string> TagDescriptionDictionary = [];
public List<StrongTypeConfig> StrongTypeConfigs = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Extensions.Diagnostics.Metrics;

Expand Down Expand Up @@ -73,7 +75,13 @@ public CounterAttribute(Type type)
public string[]? TagNames { get; }

/// <summary>
/// Gets the type that supplies metric tags values.
/// Gets the type that supplies metric tag values.
/// </summary>
public Type? Type { get; }

/// <summary>
/// Gets or sets the unit of measurement for the metric.
/// </summary>
[Experimental(DiagnosticIds.Experiments.Telemetry)]
public string? Unit { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Extensions.Diagnostics.Metrics;

Expand Down Expand Up @@ -82,4 +84,10 @@ public CounterAttribute(Type type)
/// Gets the type that supplies metric tag values.
/// </summary>
public Type? Type { get; }

/// <summary>
/// Gets or sets the unit of measurement for the metric.
/// </summary>
[Experimental(DiagnosticIds.Experiments.Telemetry)]
public string? Unit { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Extensions.Diagnostics.Metrics;

Expand Down Expand Up @@ -75,4 +77,10 @@ public GaugeAttribute(Type type)
/// Gets the type that supplies metric tag values.
/// </summary>
public Type? Type { get; }

/// <summary>
/// Gets or sets the unit of measurement for the metric.
/// </summary>
[Experimental(DiagnosticIds.Experiments.Telemetry)]
public string? Unit { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Extensions.Diagnostics.Metrics;

Expand Down Expand Up @@ -75,4 +77,10 @@ public HistogramAttribute(Type type)
/// Gets the type that supplies metric tag values.
/// </summary>
public Type? Type { get; }

/// <summary>
/// Gets or sets the unit of measurement for the metric.
/// </summary>
[Experimental(DiagnosticIds.Experiments.Telemetry)]
public string? Unit { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Shared.DiagnosticIds;

namespace Microsoft.Extensions.Diagnostics.Metrics;

Expand Down Expand Up @@ -82,4 +84,10 @@ public HistogramAttribute(Type type)
/// Gets the type that supplies metric tag values.
/// </summary>
public Type? Type { get; }

/// <summary>
/// Gets or sets the unit of measurement for the metric.
/// </summary>
[Experimental(DiagnosticIds.Experiments.Telemetry)]
public string? Unit { get; set; }
}
Loading
Loading