Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Datadog.Trace.SourceGenerators.TagsListGenerator.Diagnostics
internal static class InvalidTagPropertyReturnTypeDiagnostic
{
internal const string Id = "TL3";
private const string Message = "A tag property must return a string";
private const string Message = "A tag property must return a string or a nullable int";
private const string Title = "Invalid return type";

public static Diagnostic Create(SyntaxNode? currentNode) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,18 @@ partial class ")
sb.Append('"')
.Append(property.TagValue)
.Append(@""" => ")
.Append(property.PropertyName)
.Append(
.Append(property.PropertyName);

switch (property.PropertyType)
{
case TagListGenerator.PropertyType.NullableInt:
sb.Append(" is null ? null : Datadog.Trace.Util.IntStringCache.ToInvariantString(")
.Append(property.PropertyName)
.Append(".Value)");
break;
}

sb.Append(
@",
");
}
Expand Down Expand Up @@ -163,12 +173,42 @@ public override void SetTag(string key, string? value)
.Append(property.TagValue)
.Append(
@""":
")
.Append(property.PropertyName)
.Append(
@" = value;
");

if (property.PropertyType is TagListGenerator.PropertyType.NullableInt)
{
// Invalid values (null and anything that isn't a valid integer) remove the tag
sb.Append("if (int.TryParse(value, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var parsed")
Comment thread
andrewlock marked this conversation as resolved.
.Append(property.PropertyName)
.Append(
@"))
{
")
.Append(property.PropertyName)
.Append(@" = parsed")
.Append(property.PropertyName)
.Append(
@";
}
else
{
")
.Append(property.PropertyName)
.Append(
@" = null;
}

break;
");
}
else
{
sb.Append(property.PropertyName)
.Append(
@" = value;
break;
");
}
}

var haveReadOnlyTags = false;
Expand Down Expand Up @@ -209,20 +249,41 @@ public override void EnumerateTags<TProcessor>(ref TProcessor processor)
");
foreach (var property in tagList.TagProperties)
{
sb.Append(@"if (")
.Append(property.PropertyName)
.Append(@" is not null)
switch (property.PropertyType)
{
case TagListGenerator.PropertyType.NullableInt:
sb.Append(@"if (")
.Append(property.PropertyName)
.Append(@" is not null)
Comment thread
zacharycmontoya marked this conversation as resolved.
{
processor.Process(new TagItem<int>(""")
.Append(property.TagValue)
.Append(@""", ")
.Append(property.PropertyName)
.Append(@".Value, ")
.Append(property.PropertyName)
.Append(@"Bytes));
}

");
break;
default:
sb.Append(@"if (")
.Append(property.PropertyName)
.Append(@" is not null)
{
processor.Process(new TagItem<string>(""")
.Append(property.TagValue)
.Append(@""", ")
.Append(property.PropertyName)
.Append(@", ")
.Append(property.PropertyName)
.Append(@"Bytes));
.Append(property.TagValue)
.Append(@""", ")
.Append(property.PropertyName)
.Append(@", ")
.Append(property.PropertyName)
.Append(@"Bytes));
}

");
break;
}
}

sb.Append(
Expand All @@ -234,22 +295,45 @@ protected override void WriteAdditionalTags(System.Text.StringBuilder sb)
");
foreach (var property in tagList.TagProperties)
{
sb.Append(@"if (")
.Append(property.PropertyName)
.Append(
@" is not null)
switch (property.PropertyType)
{
case TagListGenerator.PropertyType.NullableInt:
sb.Append(@"if (")
.Append(property.PropertyName)
.Append(
@" is not null)
{
sb.Append(""")
.Append(property.TagValue)
.Append(@" (tag):"")
.Append(property.TagValue)
.Append(@" (tag):"")
.Append(")
.Append(property.PropertyName)
.Append(
@")
.Append(property.PropertyName)
.Append(
@".Value.ToString(System.Globalization.CultureInfo.InvariantCulture))
.Append(',');
}

");
break;
default:
sb.Append(@"if (")
.Append(property.PropertyName)
.Append(
@" is not null)
{
sb.Append(""")
.Append(property.TagValue)
.Append(@" (tag):"")
.Append(")
.Append(property.PropertyName)
.Append(
@")
.Append(',');
}

");
break;
}
}

sb.Append(@"base.WriteAdditionalTags(sb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public class TagListGenerator : IIncrementalGenerator
private const string TagAttributeFullName = "Datadog.Trace.SourceGenerators.TagAttribute";
private const string MetricAttributeFullName = "Datadog.Trace.SourceGenerators.MetricAttribute";

internal enum PropertyType
{
String,
NullableInt,
NullableDouble,
}

/// <inheritdoc />
public void Initialize(IncrementalGeneratorInitializationContext context)
{
Expand Down Expand Up @@ -187,11 +194,28 @@ private static IEnumerable<TagList> GetTagLists(ImmutableArray<PropertyTag> tagP
}
}

var hasRequiredReturnType =
isTag
? propertySymbol.Type.Name == "String"
: propertySymbol.Type is INamedTypeSymbol { Name: "Nullable", TypeArguments: { Length: 1 } typeArgs }
&& typeArgs[0].Name == "Double";
PropertyType propertyType = PropertyType.String;
bool hasRequiredReturnType = false;
if (isTag)
{
if (propertySymbol.Type.Name == "String")
{
propertyType = PropertyType.String;
hasRequiredReturnType = true;
}
else if (propertySymbol.Type is INamedTypeSymbol { Name: "Nullable", TypeArguments: { Length: 1 } intTypeArgs }
&& intTypeArgs[0].Name == "Int32")
{
propertyType = PropertyType.NullableInt;
hasRequiredReturnType = true;
}
}
else if (propertySymbol.Type is INamedTypeSymbol { Name: "Nullable", TypeArguments: { Length: 1 } doubleTypeArgs }
&& doubleTypeArgs[0].Name == "Double")
{
propertyType = PropertyType.NullableDouble;
hasRequiredReturnType = true;
}

if (!hasRequiredReturnType)
{
Expand All @@ -218,7 +242,8 @@ private static IEnumerable<TagList> GetTagLists(ImmutableArray<PropertyTag> tagP
isReadOnly: propertySymbol!.IsReadOnly,
propertyName: propertySymbol.Name,
tagValue: key!,
isTag: isTag);
isTag: isTag,
propertyType: propertyType);

return new Result<(PropertyTag PropertyTag, bool IsValid)>((tag, true), errors);
}
Expand Down Expand Up @@ -280,15 +305,17 @@ internal readonly record struct PropertyTag
public readonly string PropertyName;
public readonly string TagValue;
public readonly bool IsTag;
public readonly PropertyType PropertyType;

public PropertyTag(string nameSpace, string className, bool isReadOnly, string propertyName, string tagValue, bool isTag)
public PropertyTag(string nameSpace, string className, bool isReadOnly, string propertyName, string tagValue, bool isTag, PropertyType propertyType)
{
IsReadOnly = isReadOnly;
PropertyName = propertyName;
TagValue = tagValue;
IsTag = isTag;
Namespace = nameSpace;
ClassName = className;
PropertyType = propertyType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ private byte[] GetAppSecRulesetVersion(string version)
}
}

internal struct TagWriter : IItemProcessor<string>, IItemProcessor<double>, IItemProcessor<byte[]>
internal struct TagWriter : IItemProcessor<string>, IItemProcessor<int>, IItemProcessor<double>, IItemProcessor<byte[]>
{
private readonly SpanMessagePackFormatter _formatter;
private readonly ITagProcessor[] _tagProcessors;
Expand Down Expand Up @@ -1039,6 +1039,24 @@ public void Process(TagItem<string> item)
Count++;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Process(TagItem<int> item)
{
// int-backed tags are serialized as strings; IntStringCache keeps this allocation-free
var value = IntStringCache.ToInvariantString(item.Value);

if (item.SerializedKey.IsEmpty)
{
_formatter.WriteTag(ref Bytes, ref Offset, item.Key, value, _tagProcessors);
}
else
{
_formatter.WriteTag(ref Bytes, ref Offset, item.SerializedKey, value, _tagProcessors);
}

Count++;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Process(TagItem<double> item)
{
Expand Down
8 changes: 1 addition & 7 deletions tracer/src/Datadog.Trace/Agent/NullStatsAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,12 @@ public Task DisposeAsync()

public StatsAggregationKey BuildKey(Span span)
{
var rawHttpStatusCode = span.GetTag(Tags.HttpStatusCode);
if (rawHttpStatusCode is null || !int.TryParse(rawHttpStatusCode, out var httpStatusCode))
{
httpStatusCode = 0;
}

return new StatsAggregationKey(
span.ResourceName,
span.ServiceName,
span.OperationName,
span.Type,
httpStatusCode,
span.GetHttpStatusCode() ?? 0,
isSyntheticsRequest: span.Context.Origin?.StartsWith("synthetics") == true,
spanKind: string.Empty,
isError: false,
Expand Down
9 changes: 1 addition & 8 deletions tracer/src/Datadog.Trace/Agent/StatsAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,6 @@ public StatsAggregationKey BuildKey(Span span)
[TestingAndPrivateOnly]
internal StatsAggregationKey BuildKey(Span span, List<PeerTagKey> peerTagKeys, out PeerTagResults peerTagResults, out AdditionalTagResults additionalTagResults)
{
var rawHttpStatusCode = span.GetTag(Tags.HttpStatusCode);

if (rawHttpStatusCode == null || !int.TryParse(rawHttpStatusCode, out var httpStatusCode))
{
httpStatusCode = 0;
}

// Check gRPC status code tags in priority order per CSS v1.2.0 spec.
// Stored as string to match the Go agent's wire format (GRPCStatusCode is a string field).
// This preserves the distinction between "0" (gRPC OK) and "" (no gRPC status).
Expand Down Expand Up @@ -459,7 +452,7 @@ internal StatsAggregationKey BuildKey(Span span, List<PeerTagKey> peerTagKeys, o
span.ServiceName,
span.OperationName,
span.Type,
httpStatusCode,
span.GetHttpStatusCode() ?? 0,
span.Context.Origin?.StartsWith("synthetics") == true,
spanKind,
_isOtlp ? span.Error : false,
Expand Down
19 changes: 18 additions & 1 deletion tracer/src/Datadog.Trace/Agent/TraceSamplers/TraceFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text.RegularExpressions;
using Datadog.Trace.Agent.DiscoveryService;
using Datadog.Trace.Tagging;
using Datadog.Trace.Util;

namespace Datadog.Trace.Agent.TraceSamplers;

Expand Down Expand Up @@ -237,7 +238,7 @@ public RegexTagFilter(Regex keyPattern, Regex? valuePattern)
}
}

private struct RegexTagFilterProcessor : IItemProcessor<string>
private struct RegexTagFilterProcessor : IItemProcessor<string>, IItemProcessor<int>
{
private readonly RegexTagFilter _filter;
public bool Matched;
Expand All @@ -261,5 +262,21 @@ public void Process(TagItem<string> item)
}
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Process(TagItem<int> item)
{
if (!Matched)
{
if (_filter.KeyPattern.IsMatch(item.Key))
{
// Key-only filter: any matching key is sufficient
// Key:Value filter: value must also match
// Regex.IsMatch(ReadOnlySpan<char>) is .NET 7+, so we have to hand it a string;
// IntStringCache keeps that allocation-free for the values we actually see.
Matched = _filter.ValuePattern is null || _filter.ValuePattern.IsMatch(IntStringCache.ToInvariantString(item.Value));
}
}
}
}
}
Loading
Loading