Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#nullable enable

using System;
using Datadog.Trace.Agent.MessagePack;
using Datadog.Trace.Ci.Agent.Payloads;
using Datadog.Trace.Ci.Tags;
using Datadog.Trace.Configuration;
Expand All @@ -15,27 +16,12 @@ namespace Datadog.Trace.Ci.Agent.MessagePack;

internal sealed class CIEventMessagePackFormatter : EventMessagePackFormatter<CIVisibilityProtocolPayload>
{
private readonly byte[] _metadataBytes = StringEncoding.UTF8.GetBytes("metadata");

private readonly byte[] _asteriskBytes = StringEncoding.UTF8.GetBytes("*");
private readonly byte[] _runtimeIdBytes = StringEncoding.UTF8.GetBytes(Trace.Tags.RuntimeId);
// Runtime values
private readonly byte[] _runtimeIdValueBytes = StringEncoding.UTF8.GetBytes(Tracer.RuntimeId);
private readonly byte[] _languageNameBytes = StringEncoding.UTF8.GetBytes("language");
private readonly byte[] _languageNameValueBytes = StringEncoding.UTF8.GetBytes("dotnet");
private readonly byte[] _libraryVersionBytes = StringEncoding.UTF8.GetBytes(CommonTags.LibraryVersion);
private readonly byte[] _libraryVersionValueBytes = StringEncoding.UTF8.GetBytes(TracerConstants.AssemblyVersion);
private readonly byte[] _environmentBytes = StringEncoding.UTF8.GetBytes("env");
private readonly byte[]? _environmentValueBytes;

private readonly byte[] _testBytes = StringEncoding.UTF8.GetBytes(SpanTypes.Test);
private readonly byte[] _testSuiteEndBytes = StringEncoding.UTF8.GetBytes(SpanTypes.TestSuite);
private readonly byte[] _testModuleEndBytes = StringEncoding.UTF8.GetBytes(SpanTypes.TestModule);
private readonly byte[] _testSessionEndBytes = StringEncoding.UTF8.GetBytes(SpanTypes.TestSession);
private readonly byte[] _testSessionNameBytes = StringEncoding.UTF8.GetBytes("test_session.name");
private readonly byte[]? _testSessionNameValueBytes;

private readonly byte[] _eventsBytes = StringEncoding.UTF8.GetBytes("events");

private readonly ArraySegment<byte> _envelopBytes;

public CIEventMessagePackFormatter(TracerSettings tracerSettings)
Expand Down Expand Up @@ -104,7 +90,7 @@ private ArraySegment<byte> GetEnvelopeArraySegment()
// # Metadata

// Key
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _metadataBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.MetadataBytes);

// Value
var metadataValuesCount = _testSessionNameValueBytes is not null ? 5 : 1;
Expand All @@ -113,7 +99,7 @@ private ArraySegment<byte> GetEnvelopeArraySegment()
// -> * : {}

// Key
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _asteriskBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.AsteriskBytes);

// Value (RuntimeId, Language, library_version, Env?)
int valuesCount = 3;
Expand All @@ -124,56 +110,56 @@ private ArraySegment<byte> GetEnvelopeArraySegment()

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

offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _runtimeIdBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.RuntimeIdBytes);
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _runtimeIdValueBytes);

offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _languageNameBytes);
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _languageNameValueBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.LanguageBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.DotnetLanguageValueBytes);

offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _libraryVersionBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.LibraryVersionBytes);
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _libraryVersionValueBytes);

if (_environmentValueBytes is not null)
{
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _environmentBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.EnvBytes);
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _environmentValueBytes);
}

if (_testSessionNameValueBytes is not null)
{
// -> test : {}
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.TestBytes);
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 1);
// -> test_session.name : "value"
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.TestSessionNameBytes);
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameValueBytes);

// -> test_suite_end : {}
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSuiteEndBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.TestSuiteBytes);
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 1);
// -> test_session.name : "value"
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.TestSessionNameBytes);
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameValueBytes);

// -> test_module_end : {}
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testModuleEndBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.TestModuleBytes);
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 1);
// -> test_session.name : "value"
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.TestSessionNameBytes);
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameValueBytes);

// -> test_session_end : {}
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionEndBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.TestSessionBytes);
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 1);
// -> test_session.name : "value"
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.TestSessionNameBytes);
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _testSessionNameValueBytes);
}

// # Events

// Key
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _eventsBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.EventsBytes);

return new ArraySegment<byte>(bytes, 0, offset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public override int Serialize(ref byte[] bytes, int offset, CIVisibilityEvent<T>

offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, 3);

offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, TypeBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, TypeBytes);
offset += MessagePackBinary.WriteString(ref bytes, offset, value.Type);

offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, VersionBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, VersionBytes);
offset += MessagePackBinary.WriteInt32(ref bytes, offset, value.Version);

offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, ContentBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, ContentBytes);
offset += formatterResolver.GetFormatter<T>().Serialize(ref bytes, offset, value.Content, formatterResolver);

return offset - originalOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@
#nullable enable

using System;
using Datadog.Trace.Agent.MessagePack;
using Datadog.Trace.Ci.Agent.Payloads;
using Datadog.Trace.Vendors.MessagePack;

namespace Datadog.Trace.Ci.Agent.MessagePack;

internal sealed class CoveragePayloadMessagePackFormatter : EventMessagePackFormatter<CICodeCoveragePayload.CoveragePayload>
{
private readonly byte[] _versionBytes = StringEncoding.UTF8.GetBytes("version");
private readonly byte[] _coveragesBytes = StringEncoding.UTF8.GetBytes("coverages");

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

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

offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _versionBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.VersionBytes);
offset += MessagePackBinary.WriteInt32(ref bytes, offset, 2);

offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, _coveragesBytes);
offset += MessagePackBinary.WriteRaw(ref bytes, offset, MessagePackConstants.CoveragesBytes);

// Write events
if (value.TestCoverageData.Lock())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma warning disable SA1402 // disable check to only have one class per file

using System;
using Datadog.Trace.Agent.MessagePack;
using Datadog.Trace.Logging;
using Datadog.Trace.Vendors.MessagePack;
using Datadog.Trace.Vendors.MessagePack.Formatters;
Expand All @@ -16,15 +17,18 @@ internal abstract class EventMessagePackFormatter
{
private readonly IDatadogLogger _log;

protected static readonly byte[] TypeBytes = StringEncoding.UTF8.GetBytes("type");
protected static readonly byte[] VersionBytes = StringEncoding.UTF8.GetBytes("version");
protected static readonly byte[] ContentBytes = StringEncoding.UTF8.GetBytes("content");

protected EventMessagePackFormatter()
{
_log = DatadogLogging.GetLoggerFor(GetType());
}

// Use generated MessagePack constants
protected static ReadOnlySpan<byte> TypeBytes => MessagePackConstants.TypeBytes;

protected static ReadOnlySpan<byte> VersionBytes => MessagePackConstants.VersionBytes;

protected static ReadOnlySpan<byte> ContentBytes => MessagePackConstants.ContentBytes;

protected IDatadogLogger Log => _log;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <copyright file="MessagePackFieldNames.CIVisibility.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

#nullable enable

using Datadog.Trace.SourceGenerators;

namespace Datadog.Trace.Agent.MessagePack
{
/// <summary>
/// MessagePack field names for CI Visibility serialization (CI-specific part).
/// These constants are marked with [MessagePackField] to generate pre-serialized byte arrays.
/// </summary>
internal static partial class MessagePackFieldNames
{
[MessagePackField]
public const string Content = "content";

// CIEventMessagePackFormatter fields
[MessagePackField]
public const string Metadata = "metadata";

[MessagePackField]
public const string Asterisk = "*";

[MessagePackField]
public const string TestSessionName = "test_session.name";

// CoveragePayloadMessagePackFormatter fields
[MessagePackField]
public const string Coverages = "coverages";

// CI SpanMessagePackFormatter fields
[MessagePackField]
public const string ItrCorrelationId = "itr_correlation_id";

// Span types (duplicated from SpanTypes.cs which is shared with Manual)
[MessagePackField]
public const string Test = "test";

[MessagePackField]
public const string TestSuite = "test_suite_end";

[MessagePackField]
public const string TestModule = "test_module_end";

[MessagePackField]
public const string TestSession = "test_session_end";
}
}
Loading
Loading