Skip to content

Commit ed3a115

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 596b3e6 commit ed3a115

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
@@ -93,6 +93,18 @@ protected string BuildPerformanceString()
9393

9494
protected abstract string GetEventMessage(Trace2FormatTarget formatTarget);
9595

96+
private string GetSource()
97+
{
98+
// Source column format is file:line
99+
string source = $"{File}:{Line}";
100+
if (source.Length > SourceColumnMaxWidth)
101+
{
102+
return TraceUtils.FormatSource(source, SourceColumnMaxWidth);
103+
}
104+
105+
return source;
106+
}
107+
96108
internal static string BuildTimeSpan(double time)
97109
{
98110
var timeString = time.ToString("F6");
@@ -102,46 +114,72 @@ internal static string BuildTimeSpan(double time)
102114
BeginPadding = 2,
103115
EndPadding = 1
104116
};
105-
AdjustPadding(component, timeString);
106117

107-
var beginPadding = new string(' ', component.BeginPadding);
108-
var endPadding = new string(' ', component.EndPadding);
118+
return BuildSpan(component, timeString);
119+
}
109120

110-
return $"{beginPadding}{timeString}{endPadding}";
121+
internal static string BuildCategorySpan(string category)
122+
{
123+
var component = new PerformanceFormatSpan()
124+
{
125+
Size = 13,
126+
BeginPadding = 1,
127+
EndPadding = 1
128+
};
129+
130+
return BuildSpan(component, category);
111131
}
112132

113-
private string GetSource()
133+
internal static string BuildRepoSpan(int repo)
114134
{
115-
// Source column format is file:line
116-
string source = $"{File}:{Line}";
117-
if (source.Length > SourceColumnMaxWidth)
135+
var component = new PerformanceFormatSpan()
118136
{
119-
return TraceUtils.FormatSource(source, SourceColumnMaxWidth);
120-
}
137+
Size = 5,
138+
BeginPadding = 1,
139+
EndPadding = 2
140+
};
121141

122-
return source;
142+
return BuildSpan(component, $"r{repo}");
123143
}
124144

125-
private static void AdjustPadding(PerformanceFormatSpan span, string data)
145+
private static string BuildSpan(PerformanceFormatSpan component, string data)
126146
{
127-
var paddingTotal = span.BeginPadding + span.EndPadding;
128-
// Size difference between the expected size and the actual size of the data
129-
var sizeDifference = span.Size - paddingTotal - data.Length;
147+
var paddingTotal = component.BeginPadding + component.EndPadding;
148+
var dataLimit = component.Size - paddingTotal;
149+
var sizeDifference = dataLimit - data.Length;
130150

131-
if (sizeDifference < 0)
151+
if (sizeDifference <= 0)
132152
{
133-
// Remove all padding for values that take up the entire span
134-
if (Math.Abs(sizeDifference) == paddingTotal)
153+
if (double.TryParse(data, out _))
135154
{
136-
span.BeginPadding = 0;
137-
span.EndPadding = 0;
155+
// Remove all padding for values that take up the entire span
156+
if (Math.Abs(sizeDifference) == paddingTotal)
157+
{
158+
component.BeginPadding = 0;
159+
component.EndPadding = 0;
160+
}
161+
else
162+
{
163+
// Decrease BeginPadding for large time values that don't occupy entire span
164+
component.BeginPadding += sizeDifference;
165+
}
138166
}
139167
else
140168
{
141-
// Decrease BeginPadding for large time values that don't occupy entire span
142-
span.BeginPadding += sizeDifference;
169+
// Truncate value
170+
data = data.Substring(0, dataLimit);
143171
}
144172
}
173+
174+
if (data.Length < dataLimit)
175+
{
176+
component.EndPadding += Math.Abs(sizeDifference);
177+
}
178+
179+
var beginPadding = new string(' ', component.BeginPadding);
180+
var endPadding = new string(' ', component.EndPadding);
181+
182+
return $"{beginPadding}{data}{endPadding}";
145183
}
146184
}
147185

0 commit comments

Comments
 (0)