Skip to content

Commit 7abcfef

Browse files
committed
trace2: update span handling
Previously, we only set up custom performance format span handling for time-related information (see [1]). However, with the upcoming introduction of regions, we will need custom spans for the category and repo strings. This change refactors and adds to the current logic to correctly handle all 3 span types (i.e. time, repo, and category). [1] 59a2692
1 parent 58f7154 commit 7abcfef

File tree

2 files changed

+84
-25
lines changed

2 files changed

+84
-25
lines changed

src/shared/Core.Tests/Trace2MessageTests.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,33 @@ namespace Core.Tests;
66
public class Trace2MessageTests
77
{
88
[Theory]
9-
[InlineData(0.013772, " 0.013772 ")]
10-
[InlineData(26.316083, " 26.316083 ")]
11-
[InlineData(100.316083, "100.316083 ")]
9+
[InlineData(0.013772, " 0.013772 ")]
10+
[InlineData(26.316083, " 26.316083 ")]
11+
[InlineData(100.316083, "100.316083 ")]
12+
[InlineData(1000.316083, "1000.316083")]
1213
public void BuildTimeSpan_Match_Returns_Expected_String(double input, string expected)
1314
{
1415
var actual = Trace2Message.BuildTimeSpan(input);
1516
Assert.Equal(expected, actual);
1617
}
18+
19+
[Fact]
20+
public void BuildRepoSpan_Match_Returns_Expected_String()
21+
{
22+
var input = 1;
23+
var expected = " r1 ";
24+
var actual = Trace2Message.BuildRepoSpan(input);
25+
Assert.Equal(expected, actual);
26+
}
27+
28+
[Theory]
29+
[InlineData("foo", " foo ")]
30+
[InlineData("foobar", " foobar ")]
31+
[InlineData("foo_bar_baz", " foo_bar_baz ")]
32+
[InlineData("foobarbazfoo", " foobarbazfo ")]
33+
public void BuildCategorySpan_Match_Returns_Expected_String(string input, string expected)
34+
{
35+
var actual = Trace2Message.BuildCategorySpan(input);
36+
Assert.Equal(expected, actual);
37+
}
1738
}

src/shared/Core/Trace2Message.cs

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ protected string BuildPerformanceString()
8282

8383
protected abstract string GetEventMessage(Trace2FormatTarget formatTarget);
8484

85+
private string GetSource()
86+
{
87+
// Source column format is file:line
88+
string source = $"{File}:{Line}";
89+
if (source.Length > SourceColumnMaxWidth)
90+
{
91+
return TraceUtils.FormatSource(source, SourceColumnMaxWidth);
92+
}
93+
94+
return source;
95+
}
96+
8597
internal static string BuildTimeSpan(double time)
8698
{
8799
var timeString = time.ToString("F6");
@@ -91,46 +103,72 @@ internal static string BuildTimeSpan(double time)
91103
BeginPadding = 2,
92104
EndPadding = 1
93105
};
94-
AdjustPadding(component, timeString);
95106

96-
var beginPadding = new string(' ', component.BeginPadding);
97-
var endPadding = new string(' ', component.EndPadding);
107+
return BuildSpan(component, timeString);
108+
}
98109

99-
return $"{beginPadding}{timeString}{endPadding}";
110+
internal static string BuildCategorySpan(string category)
111+
{
112+
var component = new PerformanceFormatSpan()
113+
{
114+
Size = 13,
115+
BeginPadding = 1,
116+
EndPadding = 1
117+
};
118+
119+
return BuildSpan(component, category);
100120
}
101121

102-
private string GetSource()
122+
internal static string BuildRepoSpan(int repo)
103123
{
104-
// Source column format is file:line
105-
string source = $"{File}:{Line}";
106-
if (source.Length > SourceColumnMaxWidth)
124+
var component = new PerformanceFormatSpan()
107125
{
108-
return TraceUtils.FormatSource(source, SourceColumnMaxWidth);
109-
}
126+
Size = 5,
127+
BeginPadding = 1,
128+
EndPadding = 2
129+
};
110130

111-
return source;
131+
return BuildSpan(component, $"r{repo}");
112132
}
113133

114-
private static void AdjustPadding(PerformanceFormatSpan span, string data)
134+
private static string BuildSpan(PerformanceFormatSpan component, string data)
115135
{
116-
var paddingTotal = span.BeginPadding + span.EndPadding;
117-
// Size difference between the expected size and the actual size of the data
118-
var sizeDifference = span.Size - paddingTotal - data.Length;
136+
var paddingTotal = component.BeginPadding + component.EndPadding;
137+
var dataLimit = component.Size - paddingTotal;
138+
var sizeDifference = dataLimit - data.Length;
119139

120-
if (sizeDifference < 0)
140+
if (sizeDifference <= 0)
121141
{
122-
// Remove all padding for values that take up the entire span
123-
if (Math.Abs(sizeDifference) == paddingTotal)
142+
if (double.TryParse(data, out _))
124143
{
125-
span.BeginPadding = 0;
126-
span.EndPadding = 0;
144+
// Remove all padding for values that take up the entire span
145+
if (Math.Abs(sizeDifference) == paddingTotal)
146+
{
147+
component.BeginPadding = 0;
148+
component.EndPadding = 0;
149+
}
150+
else
151+
{
152+
// Decrease BeginPadding for large time values that don't occupy entire span
153+
component.BeginPadding += sizeDifference;
154+
}
127155
}
128156
else
129157
{
130-
// Decrease BeginPadding for large time values that don't occupy entire span
131-
span.BeginPadding += sizeDifference;
158+
// Truncate value
159+
data = data.Substring(0, dataLimit);
132160
}
133161
}
162+
163+
if (data.Length < dataLimit)
164+
{
165+
component.EndPadding += Math.Abs(sizeDifference);
166+
}
167+
168+
var beginPadding = new string(' ', component.BeginPadding);
169+
var endPadding = new string(' ', component.EndPadding);
170+
171+
return $"{beginPadding}{data}{endPadding}";
134172
}
135173
}
136174

0 commit comments

Comments
 (0)