Skip to content

Commit 22af117

Browse files
authored
feat: bolero feature for profiling protobuf (#1213)
This was already present, but as a dev dependency. This switches it to be an optional dependency so that other crates can use bolero with these types.
1 parent 2a43433 commit 22af117

File tree

10 files changed

+2375
-609
lines changed

10 files changed

+2375
-609
lines changed

LICENSE-3rdparty.yml

Lines changed: 2335 additions & 584 deletions
Large diffs are not rendered by default.

datadog-profiling-protobuf/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ license.workspace = true
1212
bench = false
1313

1414
[features]
15+
bolero = ["dep:bolero"]
1516
prost_impls = ["dep:prost"]
1617

1718
[dependencies]
1819
prost = { version = "0.13", optional = true }
20+
bolero = { version = "0.13", default-features = false, optional = true }
1921

2022
[dev-dependencies]
2123
bolero = "0.13"
22-
datadog-profiling-protobuf = { path = ".", features = ["prost_impls"] }
24+
datadog-profiling-protobuf = { path = ".", features = ["bolero", "prost_impls"] }

datadog-profiling-protobuf/src/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::io::{self, Write};
88
/// useful to Datadog right now, so we save the bytes/ops.
99
#[repr(C)]
1010
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
11-
#[cfg_attr(test, derive(bolero::generator::TypeGenerator))]
11+
#[cfg_attr(feature = "bolero", derive(bolero::generator::TypeGenerator))]
1212
pub struct Function {
1313
/// Unique nonzero id for the function.
1414
pub id: Record<u64, 1, NO_OPT_ZERO>,

datadog-profiling-protobuf/src/label.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
use super::{Record, StringOffset, Value, WireType, OPT_ZERO};
55
use std::io::{self, Write};
66

7-
/// Label includes additional context for this sample. It can include things
7+
/// A label includes additional context for this sample. It can include things
88
/// like a thread id, allocation size, etc.
99
#[repr(C)]
10-
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
11-
#[cfg_attr(test, derive(bolero::generator::TypeGenerator))]
10+
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
11+
#[cfg_attr(feature = "bolero", derive(bolero::generator::TypeGenerator))]
1212
pub struct Label {
1313
/// An annotation for a sample, e.g. "allocation_size".
1414
pub key: Record<StringOffset, 1, OPT_ZERO>,

datadog-profiling-protobuf/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ use std::io::{self, Write};
8888
/// of elements in the array.
8989
///
9090
/// [Condensed Reference Card]: https://protobuf.dev/programming-guides/encoding/#cheat-sheet
91-
#[derive(Copy, Clone, Default, Eq, PartialEq)]
91+
#[derive(Copy, Clone, Default, Eq, PartialEq, Hash)]
9292
#[repr(transparent)]
93-
#[cfg_attr(test, derive(bolero::generator::TypeGenerator))]
93+
#[cfg_attr(feature = "bolero", derive(bolero::generator::TypeGenerator))]
9494
pub struct Record<P: Value, const F: u32, const O: bool> {
9595
/// The value of the record. This is pub because of a quirk in Rust's
9696
/// orphan rules which prevent implementing `From<Record<P,...> for P`.
@@ -128,7 +128,7 @@ pub unsafe trait Value: Default + Eq {
128128
/// size encoded as int32 varint
129129
/// ```
130130
///
131-
/// Calculate the number of bytes for `(message | string | packed)` only.
131+
/// Calculate the number of bytes for `(message | string | packed)` only.
132132
///
133133
/// For a varint, returns between 1 and 10 bytes for the number of bytes
134134
/// used to encode the varint.
@@ -152,7 +152,9 @@ pub const OPT_ZERO: bool = true;
152152
/// Intended to be provided to a [`Record`] to mean that it shouldn't optimize
153153
/// for a value of zero. Should be used on fields that should not be zero, such
154154
/// as `Mapping.id` and for Records which hold arrays, since that would cause
155-
/// the length of the decoded array to change, which is unexpected.
155+
/// the length of the decoded array to change, which is unexpected. Things
156+
/// like sample types shouldn't get optimized away, since they get used
157+
/// element-wise and this would screw up the pairing.
156158
pub const NO_OPT_ZERO: bool = false;
157159

158160
impl<P: Value, const F: u32, const O: bool> From<P> for Record<P, F, O> {

datadog-profiling-protobuf/src/location.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::io::{self, Write};
99
/// field is not omitted for size/CPU reasons.
1010
#[repr(C)]
1111
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
12-
#[cfg_attr(test, derive(bolero::generator::TypeGenerator))]
12+
#[cfg_attr(feature = "bolero", derive(bolero::generator::TypeGenerator))]
1313
pub struct Location {
1414
/// Unique nonzero id for the location. A profile could use instruction
1515
/// addresses or any integer sequence as ids.
@@ -27,10 +27,10 @@ pub struct Location {
2727
pub line: Record<Line, 4, OPT_ZERO>,
2828
}
2929

30-
/// Represents function and line number information. Omits column.
30+
/// Represents function and line number information. Omits column.
3131
#[repr(C)]
3232
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
33-
#[cfg_attr(test, derive(bolero::generator::TypeGenerator))]
33+
#[cfg_attr(feature = "bolero", derive(bolero::generator::TypeGenerator))]
3434
pub struct Line {
3535
/// The id of the corresponding profile.Function for this line.
3636
pub function_id: Record<u64, 1, OPT_ZERO>,

datadog-profiling-protobuf/src/mapping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::io::{self, Write};
88
/// save bytes/CPU since they are unused in Datadog.
99
#[repr(C)]
1010
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
11-
#[cfg_attr(test, derive(bolero::generator::TypeGenerator))]
11+
#[cfg_attr(feature = "bolero", derive(bolero::generator::TypeGenerator))]
1212
pub struct Mapping {
1313
/// Unique nonzero id for the mapping.
1414
pub id: Record<u64, 1, NO_OPT_ZERO>,

datadog-profiling-protobuf/src/prost_impls.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#[derive(Eq, Hash, PartialEq, ::prost::Message)]
4+
pub use ::prost::Message;
5+
6+
#[derive(Eq, Hash, PartialEq, Message)]
57
pub struct Profile {
68
#[prost(message, repeated, tag = "1")]
79
pub sample_types: Vec<ValueType>,
@@ -33,7 +35,7 @@ pub struct Profile {
3335
pub default_sample_type: i64,
3436
}
3537

36-
#[derive(Clone, Eq, Hash, PartialEq, PartialOrd, Ord, ::prost::Message)]
38+
#[derive(Clone, Eq, Hash, PartialEq, PartialOrd, Ord, Message)]
3739
pub struct Sample {
3840
#[prost(uint64, repeated, tag = "1")]
3941
pub location_ids: Vec<u64>,
@@ -43,15 +45,15 @@ pub struct Sample {
4345
pub labels: Vec<Label>,
4446
}
4547

46-
#[derive(Copy, Clone, Eq, PartialEq, Hash, ::prost::Message)]
48+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Message)]
4749
pub struct ValueType {
4850
#[prost(int64, tag = "1")]
4951
pub r#type: i64, // Index into string table
5052
#[prost(int64, tag = "2")]
5153
pub unit: i64, // Index into string table
5254
}
5355

54-
#[derive(Clone, Eq, PartialEq, Hash, PartialOrd, Ord, ::prost::Message)]
56+
#[derive(Clone, Eq, PartialEq, Hash, PartialOrd, Ord, Message)]
5557
pub struct Label {
5658
#[prost(int64, tag = "1")]
5759
pub key: i64, // Index into string table
@@ -63,7 +65,7 @@ pub struct Label {
6365
pub num_unit: i64,
6466
}
6567

66-
#[derive(Copy, Clone, Eq, PartialEq, Hash, ::prost::Message)]
68+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Message)]
6769
pub struct Mapping {
6870
#[prost(uint64, tag = "1")]
6971
pub id: u64,
@@ -87,7 +89,7 @@ pub struct Mapping {
8789
pub has_inline_frames: bool,
8890
}
8991

90-
#[derive(Clone, Eq, PartialEq, Hash, ::prost::Message)]
92+
#[derive(Clone, Eq, PartialEq, Hash, Message)]
9193
pub struct Location {
9294
#[prost(uint64, tag = "1")]
9395
pub id: u64,
@@ -101,15 +103,15 @@ pub struct Location {
101103
pub is_folded: bool,
102104
}
103105

104-
#[derive(Copy, Clone, Eq, PartialEq, Hash, ::prost::Message)]
106+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Message)]
105107
pub struct Line {
106108
#[prost(uint64, tag = "1")]
107109
pub function_id: u64,
108110
#[prost(int64, tag = "2")]
109111
pub line: i64,
110112
}
111113

112-
#[derive(Copy, Clone, Eq, PartialEq, Hash, ::prost::Message)]
114+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Message)]
113115
pub struct Function {
114116
#[prost(uint64, tag = "1")]
115117
pub id: u64,

datadog-profiling-protobuf/src/string.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::{varint, Value, WireType};
5+
use std::fmt;
56
use std::io::{self, Write};
67

78
unsafe impl Value for &str {
@@ -16,18 +17,26 @@ unsafe impl Value for &str {
1617
}
1718
}
1819

20+
// todo: for OTEL, needs to be i32::MAX rather than u32::MAX.
1921
/// Represents an offset into the Profile's string table. Note that it cannot
2022
/// exceed u32 because an entire protobuf message must not be larger than or
2123
/// equal to 2 GiB. By the time you encode the tag and length prefix for each
2224
/// string, there's no way to get this many unique-ish strings without first
2325
/// exceeding the protobuf 2 GiB limit.
2426
///
2527
/// A value of 0 means "no string" or "empty string" (they are synonymous).
28+
/// cbindgen:field-names=[offset]
2629
#[repr(C)]
27-
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
28-
#[cfg_attr(test, derive(bolero::generator::TypeGenerator))]
30+
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
31+
#[cfg_attr(feature = "bolero", derive(bolero::generator::TypeGenerator))]
2932
pub struct StringOffset(u32);
3033

34+
impl fmt::Display for StringOffset {
35+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36+
self.0.fmt(f)
37+
}
38+
}
39+
3140
/// # Safety
3241
/// The Default implementation will return all zero-representations.
3342
unsafe impl Value for StringOffset {

datadog-profiling-protobuf/src/value_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::io::{self, Write};
66

77
/// ValueType describes the semantics and measurement units of a value.
88
#[repr(C)]
9-
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
10-
#[cfg_attr(test, derive(bolero::generator::TypeGenerator))]
9+
#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq)]
10+
#[cfg_attr(feature = "bolero", derive(bolero::generator::TypeGenerator))]
1111
pub struct ValueType {
1212
pub r#type: Record<StringOffset, 1, OPT_ZERO>,
1313
pub unit: Record<StringOffset, 2, OPT_ZERO>,

0 commit comments

Comments
 (0)