Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dynamic Instrumentation] Check specific path for diagnostics upload #5461

Merged
merged 2 commits into from
May 27, 2024
Merged
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 @@ -16,6 +16,7 @@ namespace Datadog.Trace.Debugger.Upload
{
internal class DiagnosticsUploadApi : DebuggerUploadApiBase
{
private const string LegacyEndpoint = "debugger/v1/input";
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor<DiagnosticsUploadApi>();

private readonly IApiRequestFactory _apiRequestFactory;
Expand Down Expand Up @@ -60,20 +61,15 @@ public override async Task<bool> SendBatchAsync(ArraySegment<byte> data)

private Task<IApiResponse> PostAsync(string uri, ArraySegment<byte> data)
{
if (uri.Contains("diagnostics"))
{
var multipart = _apiRequestFactory.Create(new Uri(uri));

return
multipart
.PostAsync(new MultipartFormItem[]
{
new("event", MimeTypes.Json, "event.json", data)
});
}

var request = _apiRequestFactory.Create(new Uri(uri));
return request.PostAsync(data, MimeTypes.Json);
var isLegacy = uri.Contains(LegacyEndpoint);
shurivich marked this conversation as resolved.
Show resolved Hide resolved

return isLegacy ?
request.PostAsync(data, MimeTypes.Json) :
request.PostAsync(new MultipartFormItem[]
{
new("event", MimeTypes.Json, "event.json", data)
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// <copyright file="TestApiRequest.cs" company="Datadog">
// <copyright file="TestApiRequest.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>

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Datadog.Trace.Agent;
using Datadog.Trace.Agent.Transports;
Expand All @@ -32,6 +33,8 @@ public TestApiRequest(

public Uri Endpoint { get; }

public string ContentType { get; private set; }

public Dictionary<string, string> ExtraHeaders { get; } = new();

public List<TestApiResponse> Responses { get; } = new();
Expand All @@ -56,6 +59,7 @@ public virtual Task<IApiResponse> PostAsync(ArraySegment<byte> bytes, string con
{
var response = new TestApiResponse(_statusCode, _responseContent, _responseContentType);
Responses.Add(response);
ContentType = contentType;

return Task.FromResult((IApiResponse)response);
}
Expand All @@ -69,8 +73,19 @@ public async Task<IApiResponse> PostAsync(Func<Stream, Task> writeToRequestStrea
}
}

public Task<IApiResponse> PostAsync(MultipartFormItem[] items, MultipartCompression multipartCompression = MultipartCompression.None)
public async Task<IApiResponse> PostAsync(MultipartFormItem[] items, MultipartCompression multipartCompression = MultipartCompression.None)
{
throw new NotImplementedException();
var boundary = "----not implemented" + Guid.NewGuid().ToString("N");
var contentType = ContentTypeHelper.GetContentType("multipart/form-data", boundary);

return await PostAsync(
async stream =>
{
using var writer = new StreamWriter(stream, Encoding.UTF8);
await writer.WriteAsync("not implemented \r\n");
},
contentType,
"utf-8",
boundary);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// <copyright file="DiagnosticsUploadApiTests.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>

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Datadog.Trace.Configuration;
using Datadog.Trace.Debugger.Upload;
using Datadog.Trace.TestHelpers.TransportHelpers;
using Datadog.Trace.Tests.Agent;
using FluentAssertions;
using Xunit;

namespace Datadog.Trace.Tests.Debugger;

public class DiagnosticsUploadApiTests
{
private readonly TestRequestFactory _testRequestFactory;
private readonly DiscoveryServiceMock _discoveryServiceMock;
private readonly NullGitMetadataProvider _nullGitMetadataProvider;
private readonly ArraySegment<byte> _data;

public DiagnosticsUploadApiTests()
{
_testRequestFactory = new TestRequestFactory();
_discoveryServiceMock = new DiscoveryServiceMock();
_nullGitMetadataProvider = new NullGitMetadataProvider();
_data = new ArraySegment<byte>(Encoding.UTF8.GetBytes("str"));
}

[Fact]
public async Task DiagnosticsLegacy_RequestSentAsJson()
{
var api = DiagnosticsUploadApi.Create(_testRequestFactory, _discoveryServiceMock, _nullGitMetadataProvider);
_discoveryServiceMock.TriggerChange(diagnosticsEndpoint: "debugger/v1/input");

await api.SendBatchAsync(_data);
_testRequestFactory.RequestsSent.First().ContentType.Should().StartWith("application/json");
}

[Fact]
public async Task DiagnosticsUpToDat_RequestSentAsMultipart()
{
var api = DiagnosticsUploadApi.Create(_testRequestFactory, _discoveryServiceMock, _nullGitMetadataProvider);
_discoveryServiceMock.TriggerChange(diagnosticsEndpoint: "debugger/v1/diagnostics");

await api.SendBatchAsync(_data);
_testRequestFactory.RequestsSent.First().ContentType.Should().StartWith("multipart/form-data");
}
}
Loading