Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"lib/datadog-agent/config",
"lib/datadog-agent/config-overlay-model",
"lib/datadog-agent/config-testing",
"lib/datadog-metrics-v3",
"lib/ddsketch",
"lib/ottl",
"lib/process-memory",
Expand Down Expand Up @@ -43,6 +44,7 @@ license = "Apache-2.0"
repository = "https://github.com/DataDog/saluki"

[workspace.dependencies]
datadog-metrics-v3 = { path = "lib/datadog-metrics-v3" }
datadog-agent-commons = { path = "lib/datadog-agent/commons" }
datadog-agent-config = { path = "lib/datadog-agent/config" }
datadog-agent-config-overlay-model = { path = "lib/datadog-agent/config-overlay-model" }
Expand Down
9 changes: 9 additions & 0 deletions lib/datadog-metrics-v3/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "datadog-metrics-v3"
version = "0.1.0"
edition = "2021"
description = "V3 columnar protobuf codec for Datadog metrics — zero external dependencies"
license = "Apache-2.0"

[dependencies]
# intentionally empty — only std required
94 changes: 94 additions & 0 deletions lib/datadog-metrics-v3/src/interner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//! Generic interning for dictionary deduplication.

use std::{collections::HashMap, hash::Hash};

/// Generic interning structure for dictionary deduplication.
///
/// Assigns unique 1-based IDs to values, returning the same ID for duplicate values.
/// ID 0 is reserved for "empty/none" in the V3 format.
#[derive(Debug)]
pub struct Interner<K: Eq + Hash> {
index: HashMap<K, i64>,
last_id: i64,
}

impl<K: Eq + Hash> Default for Interner<K> {
fn default() -> Self {
Self::new()
}
}

impl<K: Eq + Hash> Interner<K> {
/// Creates a new empty interner.
pub fn new() -> Self {
Self {
index: HashMap::new(),
last_id: 0,
}
}

/// Gets the ID for a key, inserting it if not present.
///
/// Returns `(id, is_new)` where `is_new` is true if the key was newly inserted.
/// IDs are 1-based (0 is reserved for empty/none values).
pub fn get_or_insert(&mut self, key: K) -> (i64, bool) {
if let Some(&id) = self.index.get(&key) {
(id, false)
} else {
self.last_id += 1;
self.index.insert(key, self.last_id);
(self.last_id, true)
}
}

/// Returns the number of interned values.
#[allow(dead_code)]
pub fn len(&self) -> usize {
self.index.len()
}

/// Returns true if no values have been interned.
#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.index.is_empty()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_interner_basic() {
let mut interner: Interner<String> = Interner::new();

// First insertion returns ID 1 and is_new=true
let (id1, is_new1) = interner.get_or_insert("hello".to_string());
assert_eq!(id1, 1);
assert!(is_new1);

// Second insertion of same value returns same ID and is_new=false
let (id2, is_new2) = interner.get_or_insert("hello".to_string());
assert_eq!(id2, 1);
assert!(!is_new2);

// New value gets next ID
let (id3, is_new3) = interner.get_or_insert("world".to_string());
assert_eq!(id3, 2);
assert!(is_new3);

assert_eq!(interner.len(), 2);
}

#[test]
fn test_interner_tuples() {
let mut interner: Interner<(i32, i32, i32)> = Interner::new();

let (id1, _) = interner.get_or_insert((1, 2, 3));
let (id2, _) = interner.get_or_insert((1, 2, 3));
let (id3, _) = interner.get_or_insert((4, 5, 6));

assert_eq!(id1, id2);
assert_ne!(id1, id3);
}
}
8 changes: 8 additions & 0 deletions lib/datadog-metrics-v3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod interner;
mod serializer;
mod types;
mod writer;

pub use serializer::serialize_v3_payload;
pub use types::{V3MetricType, V3ValueType};
pub use writer::{V3EncodedData, V3MetricBuilder, V3Writer};
Loading
Loading