public static string CreateTagsList(StringBuilder sb, TagListGenerator.TagList tagList) { sb.Append(@"// <auto-generated/> #nullable enable using Datadog.Trace.Processors; using Datadog.Trace.Tagging; namespace "); sb.Append(tagList.Namespace) .Append( @" { partial class ") .Append(tagList.ClassName) .Append( @" {"); if (tagList.MetricProperties is not null) { foreach (var property in tagList.MetricProperties) { var tagBytes = Encoding.UTF8.GetBytes(property.TagValue); var tagArray = new string[tagBytes.Length]; for (var i = 0; i < tagBytes.Length; i++) { tagArray[i] = tagBytes[i].ToString(); } var tagByteArray = string.Join(", ", tagArray); sb.Append( @" // ") .Append(property.PropertyName) .Append(@"Bytes = System.Text.Encoding.UTF8.GetBytes(""") .Append(property.TagValue) .Append(@""");"); sb.Append( @" private static readonly byte[] ") .Append(property.PropertyName) .Append(@"Bytes = new byte[] { ") .Append(tagByteArray) .Append(@" };"); } } if (tagList.TagProperties is not null) { foreach (var property in tagList.TagProperties) { var tagBytes = Encoding.UTF8.GetBytes(property.TagValue); var tagArray = new string[tagBytes.Length]; for (var i = 0; i < tagBytes.Length; i++) { tagArray[i] = tagBytes[i].ToString(); } var tagByteArray = string.Join(", ", tagArray); sb.Append( @" // ") .Append(property.PropertyName) .Append(@"Bytes = System.Text.Encoding.UTF8.GetBytes(""") .Append(property.TagValue) .Append(@""");"); sb.Append( @" private static readonly byte[] ") .Append(property.PropertyName) .Append(@"Bytes = new byte[] { ") .Append(tagByteArray) .Append(@" };"); } sb.Append( @" public override string? GetTag(string key) { return key switch { "); for (int i = 0; i < tagList.TagProperties.Count; i++) { var property = tagList.TagProperties[i]; sb.Append('"') .Append(property.TagValue) .Append(@""" => ") .Append(property.PropertyName) .Append( @", "); } sb.Append( @"_ => base.GetTag(key), }; } public override void SetTag(string key, string value) { switch(key) { "); for (int i = 0; i < tagList.TagProperties.Count; i++) { var property = tagList.TagProperties[i]; if (property.IsReadOnly) { continue; } sb.Append(@"case """) .Append(property.TagValue) .Append( @""": ") .Append(property.PropertyName) .Append( @" = value; break; "); } sb.Append( @"default: base.SetTag(key, value); break; } } protected static Datadog.Trace.Tagging.IProperty<string?>[] "); sb.Append(tagList.ClassName); sb.Append( @"Properties => "); sb.Append(@"Datadog.Trace.ExtensionMethods.ArrayExtensions.Concat("); sb.Append(tagList.BaseClassName); sb.Append(@"Properties, "); for (int i = 0; i < tagList.TagProperties.Count; i++) { var property = tagList.TagProperties[i]; sb.AppendFormat(@" new Datadog.Trace.Tagging.Property<{0}, string?>(""{1}"", t => t.{2})", tagList.ClassName, property.TagValue, property.PropertyName); if (i != tagList.TagProperties.Count - 1) { sb.Append(value: ','); } sb.AppendLine(); } sb.Append( @" ); public override void EnumerateTags<TProcessor>(ref TProcessor processor) { "); foreach (var property in tagList.TagProperties) { 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)); } "); } sb.Append( @"base.EnumerateTags(ref processor); } protected override Datadog.Trace.Tagging.IProperty<string?>[] GetAdditionalTags() { return "); sb.Append(tagList.ClassName); sb.Append(@"Properties; } protected override int WriteAdditionalTags(ref byte[] bytes, ref int offset, ITagProcessor[] tagProcessors) { var count = 0; "); foreach (var property in tagList.TagProperties) { sb.Append(@"if (") .Append(property.PropertyName) .Append(@" is not null) { count++; WriteTag(ref bytes, ref offset, ") .Append(property.PropertyName) .Append(@"Bytes, ") .Append(property.PropertyName) .Append(@", tagProcessors); } "); } sb.Append( @"return count + base.WriteAdditionalTags(ref bytes, ref offset, tagProcessors); } protected override void WriteAdditionalTags(System.Text.StringBuilder sb) { "); foreach (var property in tagList.TagProperties) { sb.Append(@"if (") .Append(property.PropertyName) .Append( @" is not null) { sb.Append(""") .Append(property.TagValue) .Append(@" (tag):"") .Append(") .Append(property.PropertyName) .Append( @") .Append(','); } "); } sb.Append(@"base.WriteAdditionalTags(sb); }"); } else { sb.AppendFormat( @" protected static Datadog.Trace.Tagging.IProperty<string?>[] {0}Properties => {1}Properties; protected override Datadog.Trace.Tagging.IProperty<string?>[] GetAdditionalTags() {{ return {0}Properties; }}", tagList.ClassName, tagList.BaseClassName); } if (tagList.MetricProperties is not null) { if (tagList.TagProperties is null) { sb.AppendLine(); } sb.Append( @" public override double? GetMetric(string key) { return key switch { "); foreach (var property in tagList.MetricProperties) { sb.Append('"') .Append(property.TagValue) .Append(@""" => ") .Append(property.PropertyName) .Append( @", "); } sb.Append( @"_ => base.GetMetric(key), }; } public override void SetMetric(string key, double? value) { switch(key) { "); foreach (var property in tagList.MetricProperties) { if (property.IsReadOnly) { continue; } sb.Append(@"case """) .Append(property.TagValue) .Append( @""": ") .Append(property.PropertyName) .Append( @" = value; break; "); } sb.Append( @"default: base.SetMetric(key, value); break; } } public override void EnumerateMetrics<TProcessor>(ref TProcessor processor) { "); foreach (var property in tagList.MetricProperties) { sb.Append(@"if (") .Append(property.PropertyName) .Append(@" is not null) { processor.Process(new TagItem<double>(""") .Append(property.TagValue) .Append(@""", ") .Append(property.PropertyName) .Append(@".Value, ") .Append(property.PropertyName) .Append(@"Bytes)); } "); } sb.Append( @"base.EnumerateMetrics(ref processor); } protected override void WriteAdditionalMetrics(System.Text.StringBuilder sb) { "); foreach (var property in tagList.MetricProperties) { sb.Append(@"if (") .Append(property.PropertyName) .Append( @" is not null) { sb.Append(""") .Append(property.TagValue) .Append(@" (metric):"") .Append(") .Append(property.PropertyName) .Append( @".Value) .Append(','); } "); } sb.Append(@"base.WriteAdditionalMetrics(sb); }"); } sb.AppendLine(@" } }"); return(sb.ToString()); }
public static string CreateTagsList(StringBuilder sb, TagListGenerator.TagList tagList) { sb.Append(@"// <auto-generated/> #nullable enable using Datadog.Trace.Processors; namespace "); sb.Append(tagList.Namespace) .Append( @" { partial class ") .Append(tagList.ClassName) .Append( @" {"); if (tagList.MetricProperties is not null) { foreach (var property in tagList.MetricProperties) { sb.Append( @" private static readonly byte[] ") .Append(property.PropertyName) .Append(@"Bytes = Datadog.Trace.Vendors.MessagePack.StringEncoding.UTF8.GetBytes(""") .Append(property.TagValue) .Append(@""");"); } } if (tagList.TagProperties is not null) { foreach (var property in tagList.TagProperties) { sb.Append( @" private static readonly byte[] ") .Append(property.PropertyName) .Append(@"Bytes = Datadog.Trace.Vendors.MessagePack.StringEncoding.UTF8.GetBytes(""") .Append(property.TagValue) .Append(@""");"); } sb.Append( @" public override string? GetTag(string key) { return key switch { "); for (int i = 0; i < tagList.TagProperties.Count; i++) { var property = tagList.TagProperties[i]; sb.Append('"') .Append(property.TagValue) .Append(@""" => ") .Append(property.PropertyName) .Append( @", "); } sb.Append( @"_ => base.GetTag(key), }; } public override void SetTag(string key, string value) { switch(key) { "); for (int i = 0; i < tagList.TagProperties.Count; i++) { var property = tagList.TagProperties[i]; if (property.IsReadOnly) { continue; } sb.Append(@"case """) .Append(property.TagValue) .Append( @""": ") .Append(property.PropertyName) .Append( @" = value; break; "); } sb.Append( @"default: base.SetTag(key, value); break; } } protected override int WriteAdditionalTags(ref byte[] bytes, ref int offset, ITagProcessor[] tagProcessors) { var count = 0; "); foreach (var property in tagList.TagProperties) { sb.Append(@"if (") .Append(property.PropertyName) .Append(@" != null) { count++; WriteTag(ref bytes, ref offset, ") .Append(property.PropertyName) .Append(@"Bytes, ") .Append(property.PropertyName) .Append(@", tagProcessors); } "); } sb.Append( @"return count + base.WriteAdditionalTags(ref bytes, ref offset, tagProcessors); } protected override void WriteAdditionalTags(System.Text.StringBuilder sb) { "); foreach (var property in tagList.TagProperties) { sb.Append(@"if (") .Append(property.PropertyName) .Append( @" != null) { sb.Append(""") .Append(property.TagValue) .Append(@" (tag):"") .Append(") .Append(property.PropertyName) .Append( @") .Append(','); } "); } sb.Append(@"base.WriteAdditionalTags(sb); }"); } if (tagList.MetricProperties is not null) { if (tagList.TagProperties is null) { sb.AppendLine(); } sb.Append( @" public override double? GetMetric(string key) { return key switch { "); foreach (var property in tagList.MetricProperties) { sb.Append('"') .Append(property.TagValue) .Append(@""" => ") .Append(property.PropertyName) .Append( @", "); } sb.Append( @"_ => base.GetMetric(key), }; } public override void SetMetric(string key, double? value) { switch(key) { "); foreach (var property in tagList.MetricProperties) { if (property.IsReadOnly) { continue; } sb.Append(@"case """) .Append(property.TagValue) .Append( @""": ") .Append(property.PropertyName) .Append( @" = value; break; "); } sb.Append( @"default: base.SetMetric(key, value); break; } } protected override int WriteAdditionalMetrics(ref byte[] bytes, ref int offset, ITagProcessor[] tagProcessors) { var count = 0; "); foreach (var property in tagList.MetricProperties) { sb.Append(@"if (") .Append(property.PropertyName) .Append(@" != null) { count++; WriteMetric(ref bytes, ref offset, ") .Append(property.PropertyName) .Append(@"Bytes, ") .Append(property.PropertyName) .Append(@".Value, tagProcessors); } "); } sb.Append( @"return count + base.WriteAdditionalMetrics(ref bytes, ref offset, tagProcessors); } protected override void WriteAdditionalMetrics(System.Text.StringBuilder sb) { "); foreach (var property in tagList.MetricProperties) { sb.Append(@"if (") .Append(property.PropertyName) .Append( @" != null) { sb.Append(""") .Append(property.TagValue) .Append(@" (metric):"") .Append(") .Append(property.PropertyName) .Append( @".Value) .Append(','); } "); } sb.Append(@"base.WriteAdditionalMetrics(sb); }"); } sb.AppendLine(@" } }"); return(sb.ToString()); }