-
Notifications
You must be signed in to change notification settings - Fork 20
feat!: Align tracer FFI error and response types with common conventions #2029
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
Open
lloeki
wants to merge
13
commits into
main
Choose a base branch
from
lloeki/ffi-for-trace-exporter-improved
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−65
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b723a3e
Use `ddog_MaybeError` for tracer construction function errors
lloeki 09935fb
Return `ByteSlice` from `ddog_trace_exporter_response_get_body`
lloeki 1265e15
Accept nullable `fields` pointer in `ddog_tracer_span_new`
lloeki c4a81ab
Format `tracer.rs` with stable rustfmt
lloeki 5eef52c
Update third-party license inventory
lloeki 3a8ea78
address clippy must_use errors in tests
ekump 39b4e34
Add `anyhow` to Cargo.lock for libdd-data-pipeline-ffi
ekump ce54277
regen license file after cargo.lock fix
ekump 0318655
fix: restore Option<Box<ExporterError>>
hoolioh f936486
fix: treat nullable parameters
hoolioh 53aa944
fix: address PR comment to simplyfy get_body return
hoolioh d5ca9d3
chore: remove dependency
hoolioh 561cf8d
chore: improve test coverage
hoolioh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,10 +77,10 @@ pub struct TracerSpanFields<'a> { | |
| #[no_mangle] | ||
| pub unsafe extern "C" fn ddog_tracer_span_new( | ||
| out_handle: NonNull<Box<TracerSpan>>, | ||
| fields: &TracerSpanFields, | ||
| fields: Option<&TracerSpanFields>, | ||
| ) -> Option<Box<ExporterError>> { | ||
| catch_panic!( | ||
| { | ||
| if let Some(fields) = fields { | ||
| let inner = || -> Result<(), Box<ExporterError>> { | ||
| let service = charslice_to_bytesstring(fields.service)?; | ||
| let name = charslice_to_bytesstring(fields.name)?; | ||
|
|
@@ -108,6 +108,8 @@ pub unsafe extern "C" fn ddog_tracer_span_new( | |
| Ok(()) | ||
| }; | ||
| inner().err() | ||
| } else { | ||
| gen_error!(ErrorCode::InvalidArgument) | ||
| }, | ||
| gen_error!(ErrorCode::Panic) | ||
| ) | ||
|
|
@@ -273,18 +275,19 @@ pub unsafe extern "C" fn ddog_tracer_trace_chunks_begin_chunk( | |
| #[no_mangle] | ||
| pub unsafe extern "C" fn ddog_tracer_trace_chunks_push_span( | ||
| handle: Option<&mut TracerTraceChunks>, | ||
| span: Box<TracerSpan>, | ||
| span: Option<Box<TracerSpan>>, | ||
| ) -> Option<Box<ExporterError>> { | ||
| catch_panic!( | ||
| if let Some(chunks) = handle { | ||
| if let Some(chunk) = chunks.0.last_mut() { | ||
| chunk.push(span.0); | ||
| None | ||
| } else { | ||
| gen_error!(ErrorCode::InvalidArgument) | ||
| match (handle, span) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nit: why the change from the |
||
| (Some(chunks), Some(span)) => { | ||
| if let Some(chunk) = chunks.0.last_mut() { | ||
| chunk.push(span.0); | ||
| None | ||
| } else { | ||
| gen_error!(ErrorCode::InvalidArgument) | ||
| } | ||
| } | ||
| } else { | ||
| gen_error!(ErrorCode::InvalidArgument) | ||
| _ => gen_error!(ErrorCode::InvalidArgument), | ||
| }, | ||
| gen_error!(ErrorCode::Panic) | ||
| ) | ||
|
|
@@ -313,12 +316,15 @@ pub unsafe extern "C" fn ddog_tracer_trace_chunks_push_span( | |
| #[no_mangle] | ||
| pub unsafe extern "C" fn ddog_trace_exporter_send_trace_chunks( | ||
| exporter: Option<&TraceExporter>, | ||
| chunks: Box<TracerTraceChunks>, | ||
| chunks: Option<Box<TracerTraceChunks>>, | ||
| response_out: Option<NonNull<Box<ExporterResponse>>>, | ||
| ) -> Option<Box<ExporterError>> { | ||
| let Some(exporter) = exporter else { | ||
| return gen_error!(ErrorCode::InvalidArgument); | ||
| }; | ||
| let Some(chunks) = chunks else { | ||
| return gen_error!(ErrorCode::InvalidArgument); | ||
| }; | ||
|
|
||
| catch_panic!( | ||
| match exporter.send_trace_chunks(chunks.0) { | ||
|
|
@@ -361,7 +367,7 @@ mod tests { | |
| duration: 0, | ||
| error: 0, | ||
| }; | ||
| let err = ddog_tracer_span_new(out, &fields); | ||
| let err = ddog_tracer_span_new(out, Some(&fields)); | ||
| assert!(err.is_none()); | ||
| handle.assume_init() | ||
| } | ||
|
|
@@ -386,7 +392,7 @@ mod tests { | |
| duration: 25_000_000, | ||
| error: 0, | ||
| }; | ||
| let err = ddog_tracer_span_new(out, &fields); | ||
| let err = ddog_tracer_span_new(out, Some(&fields)); | ||
| assert!(err.is_none()); | ||
|
|
||
| let span = handle.assume_init(); | ||
|
|
@@ -500,7 +506,7 @@ mod tests { | |
| duration: 0, | ||
| error: 0, | ||
| }; | ||
| let err = ddog_tracer_span_new(out, &fields); | ||
| let err = ddog_tracer_span_new(out, Some(&fields)); | ||
| assert!(err.is_none()); | ||
|
|
||
| let span = handle.assume_init(); | ||
|
|
@@ -533,18 +539,18 @@ mod tests { | |
| assert!(err.is_none()); | ||
|
|
||
| let s1 = make_minimal_span(); | ||
| let err = ddog_tracer_trace_chunks_push_span(Some(&mut *chunks), s1); | ||
| let err = ddog_tracer_trace_chunks_push_span(Some(&mut *chunks), Some(s1)); | ||
| assert!(err.is_none()); | ||
|
|
||
| let s2 = make_minimal_span(); | ||
| let err = ddog_tracer_trace_chunks_push_span(Some(&mut *chunks), s2); | ||
| let err = ddog_tracer_trace_chunks_push_span(Some(&mut *chunks), Some(s2)); | ||
| assert!(err.is_none()); | ||
|
|
||
| // Chunk 2: one span | ||
| let err = ddog_tracer_trace_chunks_begin_chunk(Some(&mut *chunks), 1); | ||
| assert!(err.is_none()); | ||
| let s3 = make_minimal_span(); | ||
| let err = ddog_tracer_trace_chunks_push_span(Some(&mut *chunks), s3); | ||
| let err = ddog_tracer_trace_chunks_push_span(Some(&mut *chunks), Some(s3)); | ||
| assert!(err.is_none()); | ||
|
|
||
| assert_eq!(chunks.0.len(), 2); | ||
|
|
@@ -571,14 +577,39 @@ mod tests { | |
|
|
||
| // No begin_chunk — push should fail | ||
| let s = make_minimal_span(); | ||
| let err = ddog_tracer_trace_chunks_push_span(Some(&mut *chunks), s); | ||
| let err = ddog_tracer_trace_chunks_push_span(Some(&mut *chunks), Some(s)); | ||
| assert!(err.is_some()); | ||
| ddog_trace_exporter_error_free(err); | ||
|
|
||
| ddog_tracer_trace_chunks_free(chunks); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn push_span_null_span_returns_error() { | ||
| unsafe { | ||
| let mut chunks = make_chunks(1); | ||
| let err = ddog_tracer_trace_chunks_begin_chunk(Some(&mut *chunks), 0); | ||
| assert!(err.is_none()); | ||
|
|
||
| let err = ddog_tracer_trace_chunks_push_span(Some(&mut *chunks), None); | ||
| assert!(err.is_some()); | ||
| ddog_trace_exporter_error_free(err); | ||
|
|
||
| ddog_tracer_trace_chunks_free(chunks); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn push_span_null_handle_returns_error() { | ||
| unsafe { | ||
| let s = make_minimal_span(); | ||
| let err = ddog_tracer_trace_chunks_push_span(None, Some(s)); | ||
| assert!(err.is_some()); | ||
| ddog_trace_exporter_error_free(err); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn trace_chunks_empty_is_valid() { | ||
| unsafe { | ||
|
|
@@ -601,4 +632,57 @@ mod tests { | |
| ddog_tracer_trace_chunks_free(chunks); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn span_new_null_fields_returns_error() { | ||
| unsafe { | ||
| let mut handle = MaybeUninit::<Box<TracerSpan>>::uninit(); | ||
| let out = NonNull::new(handle.as_mut_ptr()).unwrap(); | ||
| let err = ddog_tracer_span_new(out, None); | ||
| assert!(err.is_some()); | ||
| assert_eq!(err.as_ref().unwrap().code, ErrorCode::InvalidArgument); | ||
| ddog_trace_exporter_error_free(err); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn send_trace_chunks_null_exporter_returns_error() { | ||
| unsafe { | ||
| let chunks = make_chunks(0); | ||
| let err = ddog_trace_exporter_send_trace_chunks(None, Some(chunks), None); | ||
| assert!(err.is_some()); | ||
| assert_eq!(err.as_ref().unwrap().code, ErrorCode::InvalidArgument); | ||
| ddog_trace_exporter_error_free(err); | ||
| } | ||
| } | ||
|
|
||
| // Capacity-overflow tests: a C caller passing `usize::MAX` would make | ||
| // `Vec::with_capacity` panic with "capacity overflow"; the `catch_panic!` | ||
| // guard must convert that into `ErrorCode::Panic` instead of aborting | ||
| // the process. | ||
| #[cfg(all(feature = "catch_panic", panic = "unwind"))] | ||
| #[test] | ||
| fn trace_chunks_new_with_overflow_capacity_returns_panic_error() { | ||
| unsafe { | ||
| let mut handle = MaybeUninit::<Box<TracerTraceChunks>>::uninit(); | ||
| let out = NonNull::new(handle.as_mut_ptr()).unwrap(); | ||
| let err = ddog_tracer_trace_chunks_new(usize::MAX, out); | ||
| assert!(err.is_some()); | ||
| assert_eq!(err.as_ref().unwrap().code, ErrorCode::Panic); | ||
| ddog_trace_exporter_error_free(err); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(feature = "catch_panic", panic = "unwind"))] | ||
| #[test] | ||
| fn begin_chunk_with_overflow_capacity_returns_panic_error() { | ||
| unsafe { | ||
| let mut chunks = make_chunks(0); | ||
| let err = ddog_tracer_trace_chunks_begin_chunk(Some(&mut *chunks), usize::MAX); | ||
| assert!(err.is_some()); | ||
| assert_eq!(err.as_ref().unwrap().code, ErrorCode::Panic); | ||
| ddog_trace_exporter_error_free(err); | ||
| ddog_tracer_trace_chunks_free(chunks); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.