Skip to content
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
24 changes: 20 additions & 4 deletions examples/ffi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@ endfunction()
# Uncomment to debug build commands
# set(CMAKE_VERBOSE_MAKEFILE ON)

if (MSVC)
# warning level 4 and treat warnings as errors
# /wd4201: suppress nameless struct/union warning used in generated common.h
add_compile_options(/W4 /WX /wd4201)
else()
add_compile_options(-Wall -Wextra -Werror)
endif()

add_executable(exporter exporter.cpp)
# needed for designated initializers
target_compile_features(exporter PRIVATE cxx_std_20)
target_link_libraries(exporter PRIVATE Datadog::Profiling)
set_vcruntime_link_type(exporter ${VCRUNTIME_LINK_TYPE})

if(MSVC)
target_compile_definitions(exporter PUBLIC _CRT_SECURE_NO_WARNINGS)
endif()

if(NOT WIN32)
add_executable(exporter_manager exporter_manager.c)
target_link_libraries(exporter_manager PRIVATE Datadog::Profiling)
Expand All @@ -40,10 +52,6 @@ add_executable(profile_intern profile_intern.cpp)
target_compile_features(profile_intern PRIVATE cxx_std_20)
target_link_libraries(profile_intern PRIVATE Datadog::Profiling)

if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_compile_definitions(exporter PUBLIC _CRT_SECURE_NO_WARNINGS)
endif()

add_executable(profiles profiles.c)
target_link_libraries(profiles PRIVATE Datadog::Profiling)
set_vcruntime_link_type(profiles ${VCRUNTIME_LINK_TYPE})
Expand Down Expand Up @@ -83,10 +91,18 @@ add_executable(library_config library_config.c)
target_link_libraries(library_config PRIVATE Datadog::Profiling)
set_vcruntime_link_type(library_config ${VCRUNTIME_LINK_TYPE})

if(MSVC)
target_compile_definitions(library_config PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()

add_executable(ddsketch ddsketch.c)
target_link_libraries(ddsketch PRIVATE Datadog::Profiling)
set_vcruntime_link_type(ddsketch ${VCRUNTIME_LINK_TYPE})

add_executable(ffe ffe.c)
target_link_libraries(ffe PRIVATE Datadog::Profiling)
set_vcruntime_link_type(ffe ${VCRUNTIME_LINK_TYPE})

if(MSVC)
target_compile_definitions(ffe PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()
15 changes: 8 additions & 7 deletions examples/ffi/array_queue.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

#include <cstdint>
extern "C" {
#include <datadog/common.h>
}
Expand All @@ -12,8 +13,8 @@ extern "C" {
#include <vector>

struct Sample {
int x;
int y;
uint32_t x;
uint32_t y;
};

void delete_fn(void *sample) { delete (Sample *)sample; }
Expand All @@ -36,15 +37,15 @@ int main(void) {
}
std::unique_ptr<ddog_ArrayQueue, Deleter> array_queue(array_queue_new_result.ok);

size_t num_threads = 4;
size_t num_elements = 50;
uint32_t num_threads = 4;
uint32_t num_elements = 50;
std::vector<std::atomic<size_t>> counts(num_elements);
for (size_t i = 0; i < num_elements; ++i) {
for (uint32_t i = 0; i < num_elements; ++i) {
counts[i].store(0);
}

auto consumer = [&array_queue, &counts, num_elements]() {
for (size_t i = 0; i < num_elements; ++i) {
for (uint32_t i = 0; i < num_elements; ++i) {
while (true) {
ddog_ArrayQueue_PopResult pop_result = ddog_ArrayQueue_pop(array_queue.get());
if (pop_result.tag == DDOG_ARRAY_QUEUE_POP_RESULT_OK) {
Expand All @@ -64,7 +65,7 @@ int main(void) {
};

auto producer = [&array_queue, num_elements]() {
for (size_t i = 0; i < num_elements; ++i) {
for (uint32_t i = 0; i < num_elements; ++i) {
Sample *sample = new Sample();
sample->x = i;
sample->y = i;
Expand Down
11 changes: 5 additions & 6 deletions examples/ffi/crashinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ extern "C" {
#include <memory>
#include <optional>
#include <string>
#include <thread>
#include <vector>

static ddog_CharSlice to_slice_c_char(const char *s) { return {.ptr = s, .len = strlen(s)}; }
[[maybe_unused]]
static ddog_CharSlice to_slice_c_char(const char *s, std::size_t size) {
return {.ptr = s, .len = size};
}
Expand Down Expand Up @@ -97,9 +96,9 @@ void add_random_frames(ddog_crasht_Handle_StackTrace* stacktrace) {
std::string filename = "/path/to/code/file_" + std::to_string(i);
check_result(ddog_crasht_StackFrame_with_file(new_frame.get(), to_slice_string(filename)),
"failed to add filename");
check_result(ddog_crasht_StackFrame_with_line(new_frame.get(), i * 4 + 3),
check_result(ddog_crasht_StackFrame_with_line(new_frame.get(), static_cast<uint32_t>(i * 4 + 3)),
"failed to add line");
check_result(ddog_crasht_StackFrame_with_column(new_frame.get(), i * 3 + 7),
check_result(ddog_crasht_StackFrame_with_column(new_frame.get(), static_cast<uint32_t>(i * 3 + 7)),
"failed to add line");

// This operation consumes the frame, so use .release here
Expand Down Expand Up @@ -239,7 +238,7 @@ int main(void) {
check_result(ddog_crasht_CrashInfoBuilder_with_timestamp(builder.get(), timestamp),
"Failed to set timestamp");

ddog_crasht_ProcInfo procinfo = {.pid = 42};
ddog_crasht_ProcInfo procinfo = { .pid = 42, .tid = 0 };
check_result(ddog_crasht_CrashInfoBuilder_with_proc_info(builder.get(), procinfo),
"Failed to set procinfo");

Expand All @@ -255,7 +254,7 @@ int main(void) {
#endif

auto sigInfo = ddog_crasht_SigInfo {
.addr = "0xBABEF00D",
.addr = to_slice_c_char("0xBABEF00D"),
.code = 16,
.code_human_readable = DDOG_CRASHT_SI_CODES_UNKNOWN,
.signo = -1,
Expand Down
4 changes: 3 additions & 1 deletion examples/ffi/crashtracking_unhandled_exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ int main(int argc, char **argv) {
struct ddog_crasht_Slice_CInt signals = ddog_crasht_default_signals();
ddog_crasht_Config config = {
.create_alt_stack = false,
.endpoint = slice(endpoint),
.endpoint = {
.url = slice(endpoint),
},
.resolve_frames = DDOG_CRASHT_STACKTRACE_COLLECTION_DISABLED,
.signals = {.ptr = signals.ptr, .len = signals.len},
};
Expand Down
11 changes: 11 additions & 0 deletions examples/ffi/exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,25 @@ int main(int argc, char *argv[]) {
.function =
{
.name = DDOG_CHARSLICE_C_BARE("{main}"),
.name_id = { .value = 0 },
.system_name = DDOG_CHARSLICE_C_BARE(""),
.system_name_id = { .value = 0 },
.filename = DDOG_CHARSLICE_C_BARE("/srv/example/index.php"),
.filename_id = { .value = 0 },
},
.address = 0,
.line = 0,
};

int64_t value = 10;
const ddog_prof_Label label = {
.key = DDOG_CHARSLICE_C_BARE("language"),
.key_id = { .value = 0 },
.str = DDOG_CHARSLICE_C_BARE("php"),
.str_id = { .value = 0 },
.num = 0,
.num_unit = DDOG_CHARSLICE_C_BARE(""),
.num_unit_id = { .value = 0 },
};
ddog_prof_Sample sample = {
.locations = {&root_location, 1},
Expand Down
7 changes: 4 additions & 3 deletions examples/ffi/library_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#ifdef _WIN32
// Define setenv for Windows
Expand Down Expand Up @@ -74,14 +75,14 @@ int main(int argc, const char *const *argv) {
if (args.infer) {
ddog_library_configurator_with_detect_process_info(configurator);
} else {
ddog_CharSlice args[] = {
ddog_CharSlice cmd_args[] = {
DDOG_CHARSLICE_C("/bin/true"),
};
ddog_CharSlice envp[] = {
DDOG_CHARSLICE_C("FOO=BAR"),
};
ddog_library_configurator_with_process_info(
configurator, (ddog_ProcessInfo){.args = DDOG_SLICE_CHARSLICE(args),
configurator, (ddog_ProcessInfo){.args = DDOG_SLICE_CHARSLICE(cmd_args),
.envp = DDOG_SLICE_CHARSLICE(envp),
.language = language});
}
Expand All @@ -104,7 +105,7 @@ int main(int argc, const char *const *argv) {
}

ddog_Vec_LibraryConfig configs = config_result.ok.value;
for (int i = 0; i < configs.len; i++) {
for (uintptr_t i = 0; i < configs.len; i++) {
const ddog_LibraryConfig *cfg = &configs.ptr[i];

printf("Setting env variable: %s=%s from origin %s\n", cfg->name.ptr, cfg->value.ptr,
Expand Down
7 changes: 3 additions & 4 deletions examples/ffi/profile_intern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ extern "C" {
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <thread>
#include <vector>

static ddog_CharSlice to_slice_c_char(const char *s) { return {.ptr = s, .len = strlen(s)}; }
[[maybe_unused]]
static ddog_CharSlice to_slice_c_char(const char *s, std::size_t size) {
return {.ptr = s, .len = size};
}
[[maybe_unused]]
static ddog_CharSlice to_slice_string(std::string const &s) {
return {.ptr = s.data(), .len = s.length()};
}

[[maybe_unused]]
static std::string to_string(ddog_CharSlice s) { return std::string(s.ptr, s.len); }

void print_error(const ddog_Error &err) {
Expand Down
3 changes: 1 addition & 2 deletions examples/ffi/telemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <datadog/common.h>
#include <datadog/telemetry.h>
#include <stdio.h>
#include <stdlib.h>

#define TRY(expr) \
{ \
Expand Down Expand Up @@ -44,4 +43,4 @@ int main(void) {
ddog_telemetry_handle_wait_for_shutdown(handle);

return 0;
}
}
9 changes: 6 additions & 3 deletions examples/ffi/telemetry_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <datadog/common.h>
#include <datadog/telemetry.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
Expand Down Expand Up @@ -32,6 +31,8 @@ unsigned int sleep(unsigned int seconds) {
}
#endif

#define UNUSED(x) (void)(x)


ddog_CharSlice charslice_from_ptr(char *str) {
return (ddog_CharSlice){
Expand Down Expand Up @@ -67,7 +68,8 @@ int main(void) {

ddog_CharSlice metric_name = DDOG_CHARSLICE_C("test.telemetry");
ddog_Vec_Tag tags = ddog_Vec_Tag_new();
ddog_Vec_Tag_push(&tags, charslice_from_ptr("foo"), charslice_from_ptr("bar"));
struct ddog_Vec_Tag_PushResult res = ddog_Vec_Tag_push(&tags, charslice_from_ptr("foo"), charslice_from_ptr("bar"));
UNUSED(res);
// tags is consummed
struct ddog_ContextKey test_temetry = ddog_telemetry_handle_register_metric_context(
handle, metric_name, DDOG_METRIC_TYPE_COUNT, tags, true, DDOG_METRIC_NAMESPACE_TELEMETRY);
Expand All @@ -76,7 +78,8 @@ int main(void) {
TRY(ddog_telemetry_handle_add_point(handle, &test_temetry, 1.0));

ddog_Vec_Tag extra_tags = ddog_Vec_Tag_new();
ddog_Vec_Tag_push(&tags, charslice_from_ptr("baz"), charslice_from_ptr("bat"));
res = ddog_Vec_Tag_push(&tags, charslice_from_ptr("baz"), charslice_from_ptr("bat"));
UNUSED(res);
TRY(ddog_telemetry_handle_add_point_with_tags(handle, &test_temetry, 1.0, extra_tags));
for (int i = 0; i < 10; i++) {
TRY(ddog_telemetry_handle_add_log(
Expand Down
23 changes: 14 additions & 9 deletions examples/ffi/trace_exporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <datadog/common.h>
#include <datadog/data-pipeline.h>
#include <datadog/log.h>

enum {
SUCCESS,
ERROR_SEND,
};
#define UNUSED(x) (void)(x)
#define SUCCESS 0

void handle_error(ddog_TraceExporterError *err) {
fprintf(stderr, "Operation failed with error: %d, reason: %s\n", err->code, err->msg);
Expand Down Expand Up @@ -73,7 +70,7 @@ int main(int argc, char** argv)

int error;

ddog_TraceExporter* trace_exporter;
ddog_TraceExporter* trace_exporter = NULL;
ddog_CharSlice url = DDOG_CHARSLICE_C("http://localhost:8126/");
ddog_CharSlice tracer_version = DDOG_CHARSLICE_C("v0.1");
ddog_CharSlice language = DDOG_CHARSLICE_C("dotnet");
Expand All @@ -84,8 +81,15 @@ int main(int argc, char** argv)
ddog_CharSlice version = DDOG_CHARSLICE_C("1.0");
ddog_CharSlice service = DDOG_CHARSLICE_C("test_app");

ddog_TraceExporterError *ret;
ddog_TraceExporterConfig *config;
UNUSED(language_version);
UNUSED(language_interpreter);
UNUSED(hostname);
UNUSED(env);
UNUSED(version);
UNUSED(service);

ddog_TraceExporterError *ret = NULL;
ddog_TraceExporterConfig *config = NULL;

ddog_trace_exporter_config_new(&config);
ddog_trace_exporter_config_set_url(config, url);
Expand All @@ -100,6 +104,7 @@ int main(int argc, char** argv)

ret = ddog_trace_exporter_config_enable_telemetry(config, &telemetry_config);
if (ret) {
error = ret->code;
handle_error(ret);
goto error;
}
Expand All @@ -116,7 +121,7 @@ int main(int argc, char** argv)

assert(ret->code == DDOG_TRACE_EXPORTER_ERROR_CODE_SERDE);
if (ret) {
error = ERROR_SEND;
error = ret->code;
handle_error(ret);
goto error;
}
Expand Down
Loading