Skip to content

Commit

Permalink
Instrumentation worker ffi (#27)
Browse files Browse the repository at this point in the history
* Initial telemetry ffi implementation

* Add telemetry header generation to build script

* Using the nightly toolchain when running cbindgen to expand functions in macros
  • Loading branch information
paullegranddc authored Sep 21, 2022
1 parent 7068677 commit 78969e5
Show file tree
Hide file tree
Showing 15 changed files with 538 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ jobs:
rust_version: ${{ matrix.rust_version }}
build_profile: "release"

- name: Install nightly toolchain
run: rustup install nightly

- name: Install Rust ${{ matrix.rust_version }}
if: ${{ matrix.rust_version != '' }}
run: rustup install ${{ matrix.rust_version }} && rustup default ${{ matrix.rust_version }}
Expand Down
15 changes: 15 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"ddcommon",
"ddcommon-ffi",
"ddtelemetry",
"ddtelemetry-ffi",
"tools",
]
# https://doc.rust-lang.org/cargo/reference/resolver.html#feature-resolver-version-2
Expand Down
10 changes: 9 additions & 1 deletion LICENSE-3rdparty.yml

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions build-profiling-ffi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ echo "Building tools"
cargo build --package tools --bins

echo "Generating $destdir/include/libdatadog headers..."
cbindgen --crate ddcommon-ffi \
rustup run nightly -- cbindgen --crate ddcommon-ffi \
--config ddcommon-ffi/cbindgen.toml \
--output "$destdir/include/datadog/common.h"
cbindgen --crate "${datadog_profiling_ffi}" \
rustup run nightly -- cbindgen --crate "${datadog_profiling_ffi}" \
--config profiling-ffi/cbindgen.toml \
--output "$destdir/include/datadog/profiling.h"
./target/debug/dedup_headers "$destdir/include/datadog/common.h" "$destdir/include/datadog/profiling.h"
rustup run nightly -- cbindgen --crate ddtelemetry-ffi \
--config ddtelemetry-ffi/cbindgen.toml \
--output "$destdir/include/datadog/telemetry.h"
./target/debug/dedup_headers "$destdir/include/datadog/common.h" "$destdir/include/datadog/telemetry.h" "$destdir/include/datadog/profiling.h"

echo "Done."
4 changes: 3 additions & 1 deletion ddcommon-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc.

pub mod option;
pub mod slice;
pub mod tags;
pub mod vec;

pub use slice::Slice;
pub use option::Option;
pub use slice::{CharSlice, Slice};
pub use vec::Vec;
24 changes: 24 additions & 0 deletions ddcommon-ffi/src/option.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc.

#[repr(C)]
#[derive(Debug, PartialEq, Eq)]
pub enum Option<T> {
Some(T),
None,
}

impl<T> Option<T> {
pub fn to_std(self) -> std::option::Option<T> {
self.into()
}
}

impl<T> From<Option<T>> for std::option::Option<T> {
fn from(o: Option<T>) -> Self {
match o {
Option::Some(s) => Some(s),
Option::None => None,
}
}
}
9 changes: 9 additions & 0 deletions ddcommon-ffi/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@ use std::mem::ManuallyDrop;
/// The names ptr and len were chosen to minimize conversion from a previous
/// Buffer type which this has replaced to become more general.
#[repr(C)]
#[derive(Debug)]
pub struct Vec<T: Sized> {
ptr: *const T,
len: usize,
capacity: usize,
_marker: PhantomData<T>,
}

impl<T: PartialEq> PartialEq for Vec<T> {
fn eq(&self, other: &Self) -> bool {
self.len() == other.len() && self.into_iter().zip(other.into_iter()).all(|(s, o)| s == o)
}
}

impl<T: Eq> Eq for Vec<T> {}

impl<T> Drop for Vec<T> {
fn drop(&mut self) {
let vec =
Expand Down
17 changes: 17 additions & 0 deletions ddtelemetry-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc.

[package]
name = "ddtelemetry-ffi"
version = "0.8.0"
edition = "2021"

[lib]
# LTO is ignored if "lib" is added as crate type
# cf. https://github.com/rust-lang/rust/issues/51009
crate-type = ["staticlib", "cdylib"]

[dependencies]
ddtelemetry = { path = "../ddtelemetry", version = "0.8.0" }
ddcommon-ffi = { path = "../ddcommon-ffi", version = "0.8.0" }
paste = "1"
32 changes: 32 additions & 0 deletions ddtelemetry-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc.

language = "C"
tab_width = 2
header = """// Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc.
"""
include_guard = "DDOG_TELEMETRY_H"
style = "both"

no_includes = true
sys_includes = ["stdbool.h", "stddef.h", "stdint.h"]
includes = ["datadog/common.h"]

[export]
prefix = "ddog_"

[export.mangle]
rename_types="SnakeCase"

[enum]
prefix_with_name = true
rename_variants = "ScreamingSnakeCase"

[fn]
must_use = "DDOG_CHECK_RETURN"

[parse]
expand = ["ddtelemetry-ffi"]
parse_deps = true
include = ["ddcommon", "ddtelemetry", "ddcommon-ffi"]
Loading

0 comments on commit 78969e5

Please sign in to comment.