Skip to content

Commit dc5795e

Browse files
authored
[Tracer] Service discovery integration linux (#7014)
## Summary of changes Process or service discovery dotnet implementation using libdatadog Will only work on linux for now as it's creating an memfd behind the scenes. Implementation only valid on arch x64 ## Reason for change https://docs.google.com/document/d/1WCOAcu0S-pQ4lt8jVtkkOKJt-pUJNdI8V60lARXo77Y/edit?tab=t.0#heading=h.eq6l6vmm63m2 ## Implementation details Calling libdatadog via interop to create the memfd. ## Test coverage System test is passing in CI ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. -->
1 parent 2be900e commit dc5795e

File tree

13 files changed

+224
-5
lines changed

13 files changed

+224
-5
lines changed

tracer/build/_build/Projects.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public static class Projects
4646
AppSecUnitTests,
4747
ClrProfilerManagedTests,
4848
TraceIntegrationTests,
49-
TraceTests
49+
TraceTests,
50+
DdTraceIntegrationTests
5051
};
5152
}
5253

tracer/src/Datadog.Trace/LibDatadog/ByteSlice.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Datadog.Trace.LibDatadog;
1212

1313
/// <summary>
1414
/// Represents a slice of a byte array in memory.
15+
/// Do not change the values of this enum unless you really need to update the interop mapping.
16+
/// Libdatadog interop mapping of https://github.com/DataDog/libdatadog/blob/60583218a8de6768f67d04fcd5bc6443f67f516b/ddcommon-ffi/src/slice.rs#L54
1517
/// </summary>
1618
[StructLayout(LayoutKind.Sequential)]
1719
internal struct ByteSlice

tracer/src/Datadog.Trace/LibDatadog/CharSlice.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace Datadog.Trace.LibDatadog;
1212

1313
/// <summary>
1414
/// Represents a slice of a UTF-8 encoded string in memory.
15+
/// Do not change the values of this enum unless you really need to update the interop mapping.
16+
/// Libdatadog interop mapping of https://github.com/DataDog/libdatadog/blob/60583218a8de6768f67d04fcd5bc6443f67f516b/ddcommon-ffi/src/slice.rs#L51
1517
/// </summary>
1618
[StructLayout(LayoutKind.Sequential)]
1719
internal struct CharSlice : IDisposable
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// <copyright file="Error.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
using System;
8+
using System.Runtime.InteropServices;
9+
using System.Text;
10+
using Datadog.Trace.Logging;
11+
12+
namespace Datadog.Trace.LibDatadog;
13+
14+
/// <summary>
15+
/// Do not change the values of this enum unless you really need to update the interop mapping.
16+
/// Libdatadog interop mapping of https://github.com/DataDog/libdatadog/blob/60583218a8de6768f67d04fcd5bc6443f67f516b/ddcommon-ffi/src/error.rs#L14
17+
/// </summary>
18+
[StructLayout(LayoutKind.Sequential)]
19+
internal struct Error
20+
{
21+
public VecU8 ErrorMessage;
22+
23+
internal static string Read(ref Error resultErr)
24+
{
25+
var message = resultErr.ErrorMessage;
26+
if (message.Length == 0)
27+
{
28+
return string.Empty;
29+
}
30+
31+
var errorMessage = NativeStringHelpers.ReadUtf8NativeString(resultErr.ErrorMessage);
32+
return errorMessage;
33+
}
34+
}

tracer/src/Datadog.Trace/LibDatadog/NativeInterop.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System;
99
using System.Runtime.InteropServices;
10+
using Datadog.Trace.LibDatadog.ServiceDiscovery;
1011

1112
namespace Datadog.Trace.LibDatadog;
1213

@@ -73,4 +74,21 @@ internal static class Config
7374
[DllImport(DllName, EntryPoint = "ddog_trace_exporter_config_set_client_computed_stats")]
7475
internal static extern ErrorHandle SetClientComputedStats(SafeHandle config, bool clientComputedStats);
7576
}
77+
78+
internal static class Common
79+
{
80+
[DllImport(DllName, EntryPoint = "ddog_store_tracer_metadata")]
81+
internal static extern TracerMemfdHandleResult StoreTracerMetadata(
82+
byte schemaVersion,
83+
CharSlice runtimeId,
84+
CharSlice tracerLanguage,
85+
CharSlice tracerVersion,
86+
CharSlice hostname,
87+
CharSlice serviceName,
88+
CharSlice serviceEnv,
89+
CharSlice serviceVersion);
90+
91+
[DllImport(DllName, EntryPoint = "ddog_Error_drop")]
92+
internal static extern void DropError(ref Error errorHandle);
93+
}
7694
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// <copyright file="NativeStringHelpers.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
#nullable enable
6+
7+
using System;
8+
using System.Text;
9+
10+
namespace Datadog.Trace.LibDatadog;
11+
12+
internal class NativeStringHelpers
13+
{
14+
public static string ReadUtf8NativeString(VecU8 nativeMessage)
15+
{
16+
string message;
17+
unsafe
18+
{
19+
#if NETCOREAPP
20+
var messageBytes = new ReadOnlySpan<byte>((void*)nativeMessage.Ptr, (int)nativeMessage.Length);
21+
message = Encoding.UTF8.GetString(messageBytes);
22+
#else
23+
message = Encoding.UTF8.GetString((byte*)nativeMessage.Ptr, (int)nativeMessage.Length);
24+
#endif
25+
}
26+
27+
return message;
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// <copyright file="ResultTag.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
#nullable enable
6+
namespace Datadog.Trace.LibDatadog.ServiceDiscovery;
7+
8+
/// <summary>
9+
/// Do not change the values of this enum unless you really need to update the interop mapping.
10+
/// Libdatadog interop mapping of https://github.com/DataDog/libdatadog/blob/60583218a8de6768f67d04fcd5bc6443f67f516b/ddcommon-ffi/src/result.rs#L44
11+
/// Cf type ddog_Result_TracerMemfdHandle_Tag in common.h headers.
12+
/// typedef enum ddog_Result_TracerMemfdHandle_Tag {
13+
/// DDOG_RESULT_TRACER_MEMFD_HANDLE_OK_TRACER_MEMFD_HANDLE,
14+
/// DDOG_RESULT_TRACER_MEMFD_HANDLE_ERR_TRACER_MEMFD_HANDLE,
15+
/// } ddog_Result_TracerMemfdHandle_Tag;
16+
/// </summary>
17+
internal enum ResultTag
18+
{
19+
Ok = 0,
20+
Error = 1,
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// <copyright file="TracerMemfdHandleResult.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
8+
using System.Runtime.InteropServices;
9+
10+
namespace Datadog.Trace.LibDatadog.ServiceDiscovery;
11+
12+
/// <summary>
13+
/// **DO NOT USE THIS TYPE in x86** to map with Libdatadog. OK and Err fields needs a 4 offset instead.
14+
/// Do not change the values of this enum unless you really need to update the interop mapping.
15+
/// Libdatadog interop mapping of the generic type: https://github.com/DataDog/libdatadog/blob/60583218a8de6768f67d04fcd5bc6443f67f516b/ddcommon-ffi/src/result.rs#L44
16+
/// Cf also ddog_Result_TracerMemfdHandle in common.h headers.
17+
/// </summary>
18+
[StructLayout(LayoutKind.Explicit)]
19+
internal struct TracerMemfdHandleResult
20+
{
21+
[FieldOffset(0)]
22+
public ResultTag Tag;
23+
24+
// beware that offset 8 is only valid on x64 and would cause a crash if read on x86.
25+
[FieldOffset(8)]
26+
public int TracerMemfdHandle;
27+
28+
[FieldOffset(8)]
29+
public Error Error;
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// <copyright file="Utils.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
namespace Datadog.Trace.LibDatadog.ServiceDiscovery;
8+
9+
internal class Utils
10+
{
11+
internal static TracerMemfdHandleResult StoreTracerMetadata(
12+
byte schemaVersion,
13+
string runtimeId,
14+
string tracerLanguage,
15+
string tracerVersion,
16+
string? hostname,
17+
string? serviceName,
18+
string? serviceEnv,
19+
string? serviceVersion)
20+
{
21+
var runtimeIdCharSlice = new CharSlice(runtimeId);
22+
var tracerLanguageCharSlice = new CharSlice(tracerLanguage);
23+
var tracerVersionCharSlice = new CharSlice(tracerVersion);
24+
var hostnameCharSlice = new CharSlice(hostname);
25+
var serviceNameCharSlice = new CharSlice(serviceName);
26+
var serviceEnvCharSlice = new CharSlice(serviceEnv);
27+
var serviceVersionCharSlice = new CharSlice(serviceVersion);
28+
29+
var result = NativeInterop.Common.StoreTracerMetadata(1, runtimeIdCharSlice, tracerLanguageCharSlice, tracerVersionCharSlice, hostnameCharSlice, serviceNameCharSlice, serviceEnvCharSlice, serviceVersionCharSlice);
30+
runtimeIdCharSlice.Dispose();
31+
tracerLanguageCharSlice.Dispose();
32+
tracerVersionCharSlice.Dispose();
33+
hostnameCharSlice.Dispose();
34+
serviceNameCharSlice.Dispose();
35+
serviceEnvCharSlice.Dispose();
36+
serviceVersionCharSlice.Dispose();
37+
return result;
38+
}
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// <copyright file="VecU8.cs" company="Datadog">
2+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
4+
// </copyright>
5+
6+
#nullable enable
7+
using System.Runtime.InteropServices;
8+
9+
namespace Datadog.Trace.LibDatadog;
10+
11+
/// <summary>
12+
/// Do not change the values of this enum unless you really need to update the interop mapping.
13+
/// Libdatadog interop mapping of https://github.com/DataDog/libdatadog/blob/60583218a8de6768f67d04fcd5bc6443f67f516b/ddcommon-ffi/src/vec.rs#L19
14+
/// </summary>
15+
[StructLayout(LayoutKind.Sequential)]
16+
internal struct VecU8
17+
{
18+
public nint Ptr; // const uint8_t*
19+
20+
public nuint Length; // size_t
21+
22+
public nuint Capacity; // size_t
23+
}

0 commit comments

Comments
 (0)