Skip to content

Commit c57dd03

Browse files
committed
Convert Encoding.Utf8.GetBytes calls to static constants
1 parent fa83d95 commit c57dd03

10 files changed

Lines changed: 333 additions & 357 deletions

File tree

tracer/src/Datadog.Trace/Agent/MessagePack/SpanMessagePackFormatter.cs

Lines changed: 150 additions & 174 deletions
Large diffs are not rendered by default.

tracer/src/Datadog.Trace/Ci/Agent/MessagePack/CIEventMessagePackFormatter.cs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,16 @@
77

88
using System;
99
using Datadog.Trace.Ci.Agent.Payloads;
10-
using Datadog.Trace.Ci.Tags;
1110
using Datadog.Trace.Configuration;
1211
using Datadog.Trace.Vendors.MessagePack;
1312

1413
namespace Datadog.Trace.Ci.Agent.MessagePack;
1514

1615
internal sealed class CIEventMessagePackFormatter : EventMessagePackFormatter<CIVisibilityProtocolPayload>
1716
{
18-
private readonly byte[] _metadataBytes = StringEncoding.UTF8.GetBytes("metadata");
19-
20-
private readonly byte[] _asteriskBytes = StringEncoding.UTF8.GetBytes("*");
21-
private readonly byte[] _runtimeIdBytes = StringEncoding.UTF8.GetBytes(Trace.Tags.RuntimeId);
2217
private readonly byte[] _runtimeIdValueBytes = StringEncoding.UTF8.GetBytes(Tracer.RuntimeId);
23-
private readonly byte[] _languageNameBytes = StringEncoding.UTF8.GetBytes("language");
24-
private readonly byte[] _languageNameValueBytes = StringEncoding.UTF8.GetBytes("dotnet");
25-
private readonly byte[] _libraryVersionBytes = StringEncoding.UTF8.GetBytes(CommonTags.LibraryVersion);
26-
private readonly byte[] _libraryVersionValueBytes = StringEncoding.UTF8.GetBytes(TracerConstants.AssemblyVersion);
27-
private readonly byte[] _environmentBytes = StringEncoding.UTF8.GetBytes("env");
2818
private readonly byte[]? _environmentValueBytes;
29-
30-
private readonly byte[] _testBytes = StringEncoding.UTF8.GetBytes(SpanTypes.Test);
31-
private readonly byte[] _testSuiteEndBytes = StringEncoding.UTF8.GetBytes(SpanTypes.TestSuite);
32-
private readonly byte[] _testModuleEndBytes = StringEncoding.UTF8.GetBytes(SpanTypes.TestModule);
33-
private readonly byte[] _testSessionEndBytes = StringEncoding.UTF8.GetBytes(SpanTypes.TestSession);
34-
private readonly byte[] _testSessionNameBytes = StringEncoding.UTF8.GetBytes("test_session.name");
3519
private readonly byte[]? _testSessionNameValueBytes;
36-
37-
private readonly byte[] _eventsBytes = StringEncoding.UTF8.GetBytes("events");
38-
3920
private readonly ArraySegment<byte> _envelopBytes;
4021

4122
public CIEventMessagePackFormatter(TracerSettings tracerSettings)
@@ -55,6 +36,23 @@ public CIEventMessagePackFormatter(TracerSettings tracerSettings)
5536
_envelopBytes = GetEnvelopeArraySegment();
5637
}
5738

39+
#pragma warning disable SA1516 // Elements should be separated by blank line
40+
private static ReadOnlySpan<byte> MetadataBytes => "metadata"u8;
41+
private static ReadOnlySpan<byte> AsteriskBytes => "*"u8;
42+
private static ReadOnlySpan<byte> RuntimeIdBytes => "runtime-id"u8;
43+
private static ReadOnlySpan<byte> LanguageNameBytes => "language"u8;
44+
private static ReadOnlySpan<byte> LanguageNameValueBytes => "dotnet"u8;
45+
private static ReadOnlySpan<byte> LibraryVersionBytes => "library_version"u8;
46+
private static ReadOnlySpan<byte> LibraryVersionValueBytes => TracerConstants.AssemblyVersionBytes;
47+
private static ReadOnlySpan<byte> EnvironmentBytes => "env"u8;
48+
private static ReadOnlySpan<byte> TestBytes => "test"u8;
49+
private static ReadOnlySpan<byte> TestSuiteEndBytes => "test_suite_end"u8;
50+
private static ReadOnlySpan<byte> TestModuleEndBytes => "test_module_end"u8;
51+
private static ReadOnlySpan<byte> TestSessionEndBytes => "test_session_end"u8;
52+
private static ReadOnlySpan<byte> TestSessionNameBytes => "test_session.name"u8;
53+
private static ReadOnlySpan<byte> EventsBytes => "events"u8;
54+
#pragma warning restore SA1516
55+
5856
public override int Serialize(ref byte[] bytes, int offset, CIVisibilityProtocolPayload? value, IFormatterResolver formatterResolver)
5957
{
6058
if (value is null)
@@ -104,7 +102,7 @@ private ArraySegment<byte> GetEnvelopeArraySegment()
104102
// # Metadata
105103

106104
// Key
107-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _metadataBytes);
105+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, MetadataBytes);
108106

109107
// Value
110108
var metadataValuesCount = _testSessionNameValueBytes is not null ? 5 : 1;
@@ -113,7 +111,7 @@ private ArraySegment<byte> GetEnvelopeArraySegment()
113111
// -> * : {}
114112

115113
// Key
116-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _asteriskBytes);
114+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, AsteriskBytes);
117115

118116
// Value (RuntimeId, Language, library_version, Env?)
119117
int valuesCount = 3;
@@ -124,56 +122,56 @@ private ArraySegment<byte> GetEnvelopeArraySegment()
124122

125123
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, valuesCount);
126124

127-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _runtimeIdBytes);
125+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, RuntimeIdBytes);
128126
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _runtimeIdValueBytes);
129127

130-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _languageNameBytes);
131-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _languageNameValueBytes);
128+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, LanguageNameBytes);
129+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, LanguageNameValueBytes);
132130

133-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _libraryVersionBytes);
134-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _libraryVersionValueBytes);
131+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, LibraryVersionBytes);
132+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, LibraryVersionValueBytes);
135133

136134
if (_environmentValueBytes is not null)
137135
{
138-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _environmentBytes);
136+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, EnvironmentBytes);
139137
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _environmentValueBytes);
140138
}
141139

142140
if (_testSessionNameValueBytes is not null)
143141
{
144142
// -> test : {}
145-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testBytes);
143+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, TestBytes);
146144
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 1);
147145
// -> test_session.name : "value"
148-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameBytes);
146+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, TestSessionNameBytes);
149147
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameValueBytes);
150148

151149
// -> test_suite_end : {}
152-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSuiteEndBytes);
150+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, TestSuiteEndBytes);
153151
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 1);
154152
// -> test_session.name : "value"
155-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameBytes);
153+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, TestSessionNameBytes);
156154
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameValueBytes);
157155

158156
// -> test_module_end : {}
159-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testModuleEndBytes);
157+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, TestModuleEndBytes);
160158
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 1);
161159
// -> test_session.name : "value"
162-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameBytes);
160+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, TestSessionNameBytes);
163161
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameValueBytes);
164162

165163
// -> test_session_end : {}
166-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionEndBytes);
164+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, TestSessionEndBytes);
167165
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 1);
168166
// -> test_session.name : "value"
169-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameBytes);
167+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, TestSessionNameBytes);
170168
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameValueBytes);
171169
}
172170

173171
// # Events
174172

175173
// Key
176-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _eventsBytes);
174+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, EventsBytes);
177175

178176
return new ArraySegment<byte>(bytes, 0, offset);
179177
}

tracer/src/Datadog.Trace/Ci/Agent/MessagePack/CoveragePayloadMessagePackFormatter.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@ namespace Datadog.Trace.Ci.Agent.MessagePack;
1212

1313
internal sealed class CoveragePayloadMessagePackFormatter : EventMessagePackFormatter<CICodeCoveragePayload.CoveragePayload>
1414
{
15-
private readonly byte[] _versionBytes = StringEncoding.UTF8.GetBytes("version");
16-
private readonly byte[] _coveragesBytes = StringEncoding.UTF8.GetBytes("coverages");
15+
private static ReadOnlySpan<byte> CoveragesBytes => "coverages"u8;
1716

1817
public override int Serialize(ref byte[] bytes, int offset, CICodeCoveragePayload.CoveragePayload value, IFormatterResolver formatterResolver)
1918
{
2019
var originalOffset = offset;
2120

2221
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 2);
2322

24-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _versionBytes);
23+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, VersionBytes);
2524
offset += MessagePackBinary.WriteInt32(ref bytes, offset, 2);
2625

27-
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _coveragesBytes);
26+
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, CoveragesBytes);
2827

2928
// Write events
3029
if (value.TestCoverageData.Lock())

tracer/src/Datadog.Trace/Ci/Agent/MessagePack/EventMessagePackFormatter.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ internal abstract class EventMessagePackFormatter
1616
{
1717
private readonly IDatadogLogger _log;
1818

19-
protected static readonly byte[] TypeBytes = StringEncoding.UTF8.GetBytes("type");
20-
protected static readonly byte[] VersionBytes = StringEncoding.UTF8.GetBytes("version");
21-
protected static readonly byte[] ContentBytes = StringEncoding.UTF8.GetBytes("content");
22-
2319
protected EventMessagePackFormatter()
2420
{
2521
_log = DatadogLogging.GetLoggerFor(GetType());
2622
}
2723

24+
#pragma warning disable SA1516 // Elements should be separated by blank line
25+
protected static ReadOnlySpan<byte> TypeBytes => "type"u8;
26+
protected static ReadOnlySpan<byte> VersionBytes => "version"u8;
27+
protected static ReadOnlySpan<byte> ContentBytes => "content"u8;
28+
#pragma warning restore SA1516
29+
2830
protected IDatadogLogger Log => _log;
2931
}
3032

0 commit comments

Comments
 (0)