Skip to content
Merged
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
22 changes: 15 additions & 7 deletions tracer/src/Datadog.Trace/FeatureFlags/Exposure/ExposureApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Datadog.Trace.HttpOverStreams;
using Datadog.Trace.Logging;
using Datadog.Trace.Vendors.Newtonsoft.Json;
using Datadog.Trace.Vendors.Newtonsoft.Json.Serialization;

namespace Datadog.Trace.FeatureFlags.Exposure;

Expand All @@ -26,6 +27,15 @@ internal sealed class ExposureApi : IDisposable

private const int DefaultCapacity = 1 << 16; // 65536 elements
public const string ExposurePath = "evp_proxy/v2/api/v2/exposure";
private static readonly JsonSerializerSettings SerializerSettings = new()
{
NullValueHandling = NullValueHandling.Include,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy(),
}
};

private readonly TaskCompletionSource<bool> _processExit = new();
private readonly TimeSpan _sendInterval = TimeSpan.FromSeconds(10);
private readonly Queue<ExposureEvent> _exposures = new Queue<ExposureEvent>();
Expand Down Expand Up @@ -106,10 +116,10 @@ private async Task SendLoopAsync()
var apiRequestFactory = _apiRequestFactory;
var uri = apiRequestFactory.GetEndpoint(ExposurePath);
var payload = TryGetPayload();
if (payload.Count != 0)
if (payload is not null)
{
var request = apiRequestFactory.Create(uri);
using var response = await request.PostAsync(payload, MimeTypes.Json).ConfigureAwait(false);
using var response = await request.PostAsJsonAsync(payload, MultipartCompression.None, SerializerSettings).ConfigureAwait(false);
}
}
catch (Exception ex)
Expand All @@ -130,24 +140,22 @@ private async Task SendLoopAsync()
}
}

private ArraySegment<byte> TryGetPayload()
private ExposuresRequest? TryGetPayload()
{
List<ExposureEvent> exposures;
lock (_exposures)
{
if (_exposures.Count == 0)
{
// nothing to do, skip send
return default;
return null;
}

exposures = [.. _exposures];
_exposures.Clear();
}

var request = new ExposuresRequest(_context, exposures);
var json = JsonConvert.SerializeObject(request);
return new ArraySegment<byte>(Encoding.UTF8.GetBytes(json));
return new ExposuresRequest(_context, exposures);
}

public void SendExposure(in ExposureEvent exposure)
Expand Down
Loading