Skip to content
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions examples/ffi/trace_exporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,81 @@ int test_error_on_panic(void) {
return 0;
}

int verify_structured_value_encoder(void) {
uint8_t text[] = "stable";
uint8_t binary[] = {0x00, 0xff};
ddog_TracerValueToken tokens[] = {
{.kind = DDOG_TRACER_VALUE_ARRAY, .child_count = 2},
{
.kind = DDOG_TRACER_VALUE_STRING,
.bytes = {.ptr = text, .len = sizeof(text) - 1},
},
{
.kind = DDOG_TRACER_VALUE_BINARY,
.bytes = {.ptr = binary, .len = sizeof(binary)},
},
};
ddog_TracerEncodedValue *blob = NULL;
ddog_TraceExporterError *err = ddog_tracer_encode_value(
(ddog_Slice_TracerValueToken){
.ptr = tokens,
.len = sizeof(tokens) / sizeof(tokens[0]),
},
&blob);
if (err) {
handle_error(err);
return 1;
}

memset(text, 'x', sizeof(text) - 1);
memset(binary, 'x', sizeof(binary));
static const uint8_t expected[] = {
0x92, 0xa6, 's','t','a','b','l','e', 0xc4, 0x02, 0x00, 0xff,
};
ddog_ByteSlice encoded = ddog_tracer_encoded_value_as_slice(blob);
int matches = encoded.len == sizeof(expected) &&
memcmp(encoded.ptr, expected, sizeof(expected)) == 0;
ddog_tracer_encoded_value_free(blob);
if (!matches) {
fprintf(stderr, "Structured value encoder did not return an owned blob\n");
return 1;
}
return 0;
}

int verify_structured_value_encoder_rejects_invalid(void) {
ddog_TracerValueToken tokens[] = {
{.kind = 255},
};
ddog_TracerEncodedValue *blob = NULL;
ddog_TraceExporterError *err = ddog_tracer_encode_value(
(ddog_Slice_TracerValueToken){
.ptr = tokens,
.len = sizeof(tokens) / sizeof(tokens[0]),
},
&blob);
if (err == NULL) {
fprintf(stderr, "Structured value encoder accepted an unknown token kind\n");
ddog_tracer_encoded_value_free(blob);
return 1;
}
ddog_trace_exporter_error_free(err);

// The encoder writes the out-handle only on success, so a rejected input
// leaves the caller's NULL blob untouched and there is nothing to free.
if (blob != NULL) {
fprintf(stderr, "Structured value encoder wrote a blob on the error path\n");
ddog_tracer_encoded_value_free(blob);
return 1;
}
return 0;
}

int main(int argc, char** argv)
{
if (verify_structured_value_encoder() != 0) return 1;
if (verify_structured_value_encoder_rejects_invalid() != 0) return 1;

// Initialize logger with optional path from command line
const char* log_path = (argc > 1) ? argv[1] : NULL;
if (log_init(log_path) != 0) {
Expand Down
1 change: 1 addition & 0 deletions libdd-data-pipeline-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ libdd-shared-runtime = { version = "2.0.0", path = "../libdd-shared-runtime" }
libdd-common-ffi = { path = "../libdd-common-ffi", default-features = false }
libdd-tinybytes = { path = "../libdd-tinybytes" }
libdd-trace-utils = { path = "../libdd-trace-utils" }
rmp = { version = "0.8.14", default-features = false }
tokio-util = "0.7.11"
tracing.workspace = true
10 changes: 10 additions & 0 deletions libdd-data-pipeline-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef struct ddog_TracerSpan ddog_TracerSpan;
typedef struct ddog_TracerSpanEvent ddog_TracerSpanEvent;
typedef struct ddog_TracerTraceChunks ddog_TracerTraceChunks;
typedef struct ddog_TraceExporterCancelToken ddog_TraceExporterCancelToken;
typedef uint8_t ddog_TracerValueKind;
"""

[export]
Expand All @@ -38,6 +39,15 @@ exclude = ["TraceExporter", "TracerSpan", "TracerSpanEvent", "TracerTraceChunks"
"TracerSpanLinkAttribute" = "ddog_TracerSpanLinkAttribute"
"TracerTraceChunks" = "ddog_TracerTraceChunks"
"TokioCancellationToken" = "ddog_TraceExporterCancelToken"
"DDOG_TRACER_VALUE_NIL" = "DDOG_TRACER_VALUE_NIL"
"DDOG_TRACER_VALUE_BOOL" = "DDOG_TRACER_VALUE_BOOL"
"DDOG_TRACER_VALUE_I64" = "DDOG_TRACER_VALUE_I64"
"DDOG_TRACER_VALUE_U64" = "DDOG_TRACER_VALUE_U64"
"DDOG_TRACER_VALUE_F64" = "DDOG_TRACER_VALUE_F64"
"DDOG_TRACER_VALUE_STRING" = "DDOG_TRACER_VALUE_STRING"
"DDOG_TRACER_VALUE_BINARY" = "DDOG_TRACER_VALUE_BINARY"
"DDOG_TRACER_VALUE_ARRAY" = "DDOG_TRACER_VALUE_ARRAY"
"DDOG_TRACER_VALUE_MAP" = "DDOG_TRACER_VALUE_MAP"

[export.mangle]
rename_types = "PascalCase"
Expand Down
1 change: 1 addition & 0 deletions libdd-data-pipeline-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

mod error;
mod response;
mod structured_value;
mod trace_exporter;
mod tracer;

Expand Down
Loading
Loading