Skip to content

Commit

Permalink
add ruby api
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecsl committed Feb 10, 2025
1 parent 9f56940 commit 89be230
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/shy-countries-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"eppo_core": minor
"ruby-sdk": minor
---

[Unstable] Add ability for attaching context to events dispatched
2 changes: 1 addition & 1 deletion eppo_core/src/event_ingestion/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ pub(super) struct Event {
pub event_type: String,

pub payload: serde_json::Value,
}
}
38 changes: 26 additions & 12 deletions eppo_core/src/event_ingestion/event_delivery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,12 @@ impl EventDelivery {
pub fn attach_context(&mut self, key: String, value: Value) -> Result<(), ContextError> {
// ensure value is valid not object or array
return match value {
Value::Object(_) | Value::Array(_) => {
Err(ContextError::InvalidContextValueType)
}
Value::Object(_) | Value::Array(_) => Err(ContextError::InvalidContextValueType),
_ => {
self.context.insert(key, value);
Ok(())
}
}
};
}

async fn deliver_inner(
Expand Down Expand Up @@ -242,10 +240,18 @@ mod tests {
}),
};

delivery.attach_context("key1".to_string(), json!("value1")).unwrap();
delivery.attach_context("key2".to_string(), json!(42)).unwrap();
delivery.attach_context("key3".to_string(), json!(true)).unwrap();
delivery.attach_context("key4".to_string(), json!(null)).unwrap();
delivery
.attach_context("key1".to_string(), json!("value1"))
.unwrap();
delivery
.attach_context("key2".to_string(), json!(42))
.unwrap();
delivery
.attach_context("key3".to_string(), json!(true))
.unwrap();
delivery
.attach_context("key4".to_string(), json!(null))
.unwrap();

let result = delivery.deliver(vec![event.clone()]).await;

Expand All @@ -261,10 +267,18 @@ mod tests {
SdkKey::new("foobar".into()),
Url::parse("http://example.com").unwrap(),
);
assert!(delivery.attach_context("key1".to_string(), json!("value1")).is_ok());
assert!(delivery.attach_context("key2".to_string(), json!(42)).is_ok());
assert!(delivery.attach_context("key3".to_string(), json!(true)).is_ok());
assert!(delivery.attach_context("key4".to_string(), json!(null)).is_ok());
assert!(delivery
.attach_context("key1".to_string(), json!("value1"))
.is_ok());
assert!(delivery
.attach_context("key2".to_string(), json!(42))
.is_ok());
assert!(delivery
.attach_context("key3".to_string(), json!(true))
.is_ok());
assert!(delivery
.attach_context("key4".to_string(), json!(null))
.is_ok());
assert_eq!(delivery.context.len(), 4);
assert_eq!(delivery.context.get("key1").unwrap(), &json!("value1"));
assert_eq!(delivery.context.get("key2").unwrap(), &json!(42));
Expand Down
2 changes: 1 addition & 1 deletion eppo_core/src/event_ingestion/event_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,4 @@ mod tests {
// TODO: use flush instead of sleeping
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
}
1 change: 0 additions & 1 deletion ruby-sdk/ext/eppo_client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ pub struct Client {
event_ingestion: Option<MutEventIngestion>,
}


impl Client {
pub fn new(config: Config) -> Client {
// Initialize logger
Expand Down
8 changes: 7 additions & 1 deletion ruby-sdk/lib/eppo_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ def shutdown
end

# Unstable
# Enqueues an arbitrary event. Events must have a type and a payload
# Enqueues an arbitrary event. Events must have a type and a payload.
def unstable_track(event_type, payload)
@core.track(event_type, payload)
end

# Unstable
# Sets an arbitrary context key/value pair to be sent with all events.
def unstable_set_context(key, value)
@core.set_context(key, value)
end

def get_string_assignment(flag_key, subject_key, subject_attributes, default_value)
get_assignment_inner(flag_key, subject_key, subject_attributes, "STRING", default_value)
end
Expand Down

0 comments on commit 89be230

Please sign in to comment.