Skip to content
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

add AI SDK metadata to the trace level #277

Merged
merged 1 commit into from
Dec 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion app-server/src/traces/span_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub const GEN_AI_INPUT_COST: &str = "gen_ai.usage.input_cost";
pub const GEN_AI_OUTPUT_COST: &str = "gen_ai.usage.output_cost";

// Custom lmnr attributes
pub const ASSOCIATION_PROPERTIES_PREFIX: &str = "lmnr.association.properties.";
pub const ASSOCIATION_PROPERTIES_PREFIX: &str = "lmnr.association.properties";
pub const SPAN_TYPE: &str = "lmnr.span.type";
pub const SPAN_PATH: &str = "lmnr.span.path";
pub const LLM_NODE_RENDERED_PROMPT: &str = "lmnr.span.prompt";
27 changes: 22 additions & 5 deletions app-server/src/traces/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,32 @@ impl SpanAttributes {
}

pub fn metadata(&self) -> Option<HashMap<String, String>> {
let res = self.get_flattened_association_properties("metadata");
if res.is_empty() {
let mut metadata = self.get_flattened_association_properties("metadata");
let ai_sdk_metadata = self.get_flattened_properties("ai", "telemetry.metadata");
metadata.extend(ai_sdk_metadata);
if metadata.is_empty() {
None
} else {
Some(
res.into_iter()
metadata
.into_iter()
.map(|(k, v)| (k, json_value_to_string(v)))
.collect(),
)
}
}

fn get_flattened_association_properties(&self, prefix: &str) -> HashMap<String, Value> {
fn get_flattened_association_properties(&self, entity: &str) -> HashMap<String, Value> {
self.get_flattened_properties(ASSOCIATION_PROPERTIES_PREFIX, entity)
}

fn get_flattened_properties(
&self,
attribute_prefix: &str,
entity: &str,
) -> HashMap<String, Value> {
let mut res = HashMap::new();
let prefix = format!("{ASSOCIATION_PROPERTIES_PREFIX}{prefix}.");
let prefix = format!("{attribute_prefix}.{entity}.");
for (key, value) in self.attributes.iter() {
if key.starts_with(&prefix) {
res.insert(
Expand Down Expand Up @@ -614,6 +625,12 @@ fn should_keep_attribute(attribute: &str) -> bool {
return false;
}

// AI SDK
// remove ai.prompt.messages as it is stored in LLM span's input
if attribute == "ai.prompt.messages" {
return false;
}

true
}

Expand Down