77
88using System ;
99using Datadog . Trace . Ci . Agent . Payloads ;
10- using Datadog . Trace . Ci . Tags ;
1110using Datadog . Trace . Configuration ;
1211using Datadog . Trace . Vendors . MessagePack ;
1312
1413namespace Datadog . Trace . Ci . Agent . MessagePack ;
1514
1615internal 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 }
0 commit comments