Skip to content

Commit 2cdcdcb

Browse files
authored
chore(ci): fix trace exporter FFI examples (#1839)
# What does this PR do? cleanup the FFI examples for the Trace Exporter and stop skipping them in the GH action that runs the examples. # Motivation Test coverage for FFI examples # Additional Notes Anything else we should know when reviewing? # How to test the change? Describe here in detail how the change can be validated. Co-authored-by: edmund.kump <edmund.kump@datadoghq.com>
1 parent 5beaea5 commit 2cdcdcb

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

examples/ffi/trace_exporter.c

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#include <assert.h>
4+
#include <stdint.h>
55
#include <stdio.h>
66
#include <string.h>
77
#include <datadog/common.h>
88
#include <datadog/data-pipeline.h>
99
#include <datadog/log.h>
1010

11-
#define UNUSED(x) (void)(x)
1211
#define SUCCESS 0
1312

1413
void handle_error(ddog_TraceExporterError *err) {
@@ -81,20 +80,20 @@ int main(int argc, char** argv)
8180
ddog_CharSlice version = DDOG_CHARSLICE_C("1.0");
8281
ddog_CharSlice service = DDOG_CHARSLICE_C("test_app");
8382

84-
UNUSED(language_version);
85-
UNUSED(language_interpreter);
86-
UNUSED(hostname);
87-
UNUSED(env);
88-
UNUSED(version);
89-
UNUSED(service);
90-
9183
ddog_TraceExporterError *ret = NULL;
9284
ddog_TraceExporterConfig *config = NULL;
9385

9486
ddog_trace_exporter_config_new(&config);
9587
ddog_trace_exporter_config_set_url(config, url);
9688
ddog_trace_exporter_config_set_tracer_version(config, tracer_version);
9789
ddog_trace_exporter_config_set_language(config, language);
90+
ddog_trace_exporter_config_set_lang_version(config, language_version);
91+
ddog_trace_exporter_config_set_lang_interpreter(config, language_interpreter);
92+
ddog_trace_exporter_config_set_hostname(config, hostname);
93+
ddog_trace_exporter_config_set_env(config, env);
94+
ddog_trace_exporter_config_set_version(config, version);
95+
ddog_trace_exporter_config_set_service(config, service);
96+
ddog_trace_exporter_config_set_connection_timeout(config, 1000);
9897

9998
ddog_TelemetryClientConfig telemetry_config = {
10099
.interval = 60000,
@@ -110,25 +109,59 @@ int main(int argc, char** argv)
110109
}
111110

112111
ret = ddog_trace_exporter_new(&trace_exporter, config);
113-
114-
assert(ret == NULL);
115-
assert(trace_exporter != NULL);
116-
117-
ddog_ByteSlice buffer = { .ptr = NULL, .len=0 };
118-
ddog_TraceExporterResponse *response;
119-
120-
ret = ddog_trace_exporter_send(trace_exporter, buffer, &response);
121-
122-
assert(ret->code == DDOG_TRACE_EXPORTER_ERROR_CODE_SERDE);
123112
if (ret) {
124113
error = ret->code;
125114
handle_error(ret);
126115
goto error;
127116
}
128117

129-
ddog_trace_exporter_response_free(response);
118+
printf("TraceExporter created successfully\n");
119+
120+
// Construct a minimal valid msgpack V04 trace payload: [[{span}]]
121+
// One trace containing one span with the 7 required fields:
122+
// service, name, resource, trace_id, span_id, start, duration
123+
// Hand rolling a payload like this is not scalable. If we need to
124+
// do this more than once, please write a helper function.
125+
static const uint8_t trace_payload[] = {
126+
0x91, // array(1): one trace
127+
0x91, // array(1): one span
128+
0x87, // map(7): span fields
129+
0xa7, 's','e','r','v','i','c','e', // key: "service"
130+
0xa8, 't','e','s','t','_','a','p','p', // val: "test_app"
131+
0xa4, 'n','a','m','e', // key: "name"
132+
0xab, 'w','e','b','.','r','e','q','u','e','s','t', // val: "web.request"
133+
0xa8, 'r','e','s','o','u','r','c','e', // key: "resource"
134+
0xaa, 'G','E','T',' ','/','h','e','l','l','o', // val: "GET /hello"
135+
0xa8, 't','r','a','c','e','_','i','d', // key: "trace_id"
136+
0x01, // val: 1
137+
0xa7, 's','p','a','n','_','i','d', // key: "span_id"
138+
0x01, // val: 1
139+
0xa5, 's','t','a','r','t', // key: "start"
140+
0xce, 0x3b, 0x9a, 0xca, 0x00, // val: 1000000000
141+
0xa8, 'd','u','r','a','t','i','o','n', // key: "duration"
142+
0xce, 0x1d, 0xcd, 0x65, 0x00, // val: 500000000
143+
};
144+
ddog_ByteSlice buffer = {
145+
.ptr = trace_payload,
146+
.len = sizeof(trace_payload)
147+
};
148+
ddog_TraceExporterResponse *response = NULL;
149+
150+
// Send will deserialize the payload and attempt to forward it to the agent.
151+
// Without a running agent, this will fail with a network error.
152+
ret = ddog_trace_exporter_send(trace_exporter, buffer, &response);
153+
if (ret) {
154+
printf("Send returned expected error (no agent running): %s\n", ret->msg);
155+
ddog_trace_exporter_error_free(ret);
156+
} else {
157+
printf("Send succeeded\n");
158+
ddog_trace_exporter_response_free(response);
159+
}
160+
130161
ddog_trace_exporter_free(trace_exporter);
162+
trace_exporter = NULL;
131163
ddog_trace_exporter_config_free(config);
164+
config = NULL;
132165

133166
// Disable file logging if it was enabled
134167
if (log_path != NULL) {

tools/src/bin/ffi_test.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,7 @@ fn per_test_env(name: &str, project_root: &Path, work_dir: &Path) -> Vec<(String
225225

226226
fn expected_failures() -> &'static HashMap<&'static str, &'static str> {
227227
static MAP: OnceLock<HashMap<&'static str, &'static str>> = OnceLock::new();
228-
MAP.get_or_init(|| {
229-
HashMap::from([(
230-
"trace_exporter",
231-
"expects trace data, fails on empty buffer",
232-
)])
233-
})
228+
MAP.get_or_init(HashMap::new)
234229
}
235230

236231
// Test data directories to symlink into work directory

0 commit comments

Comments
 (0)