use crate::interceptor::{
AfterResponseContext, BeforeRequestContext, ErrorContext, Interceptor, StreamChunkContext,
StreamEndContext, StreamTerminalContext,
};
use crate::langfuse_context::LangfuseContext;
use crate::semantic_conventions::{
GEN_AI_OPERATION_NAME, GEN_AI_PROVIDER_NAME, GEN_AI_REQUEST_MAX_TOKENS, GEN_AI_REQUEST_MODEL,
GEN_AI_REQUEST_TEMPERATURE, GEN_AI_RESPONSE_ID, GEN_AI_USAGE_INPUT_TOKENS,
GEN_AI_USAGE_OUTPUT_TOKENS,
};
use crate::Result;
use opentelemetry::{
trace::{SpanKind, Status, Tracer},
KeyValue,
};
use serde_json::Value;
use std::future::Future;
use std::sync::{Arc, Mutex};
use tracing::{debug, error, info};
pub struct LangfuseState<S = opentelemetry::global::BoxedSpan> {
pub(crate) span: Mutex<Option<S>>,
}
impl<S> Default for LangfuseState<S> {
fn default() -> Self {
Self {
span: Mutex::new(None),
}
}
}
#[derive(Debug, Clone)]
pub struct LangfuseConfig {
pub debug: bool,
}
impl Default for LangfuseConfig {
fn default() -> Self {
Self {
debug: std::env::var("LANGFUSE_DEBUG")
.unwrap_or_else(|_| "false".to_string())
.parse()
.unwrap_or(false),
}
}
}
impl LangfuseConfig {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_debug(mut self, debug: bool) -> Self {
self.debug = debug;
self
}
}
pub struct LangfuseInterceptor<T: Tracer + Send + Sync> {
config: LangfuseConfig,
tracer: Arc<T>,
context: Arc<LangfuseContext>,
}
impl<T: Tracer + Send + Sync> LangfuseInterceptor<T>
where
T::Span: Send + Sync + 'static,
{
pub fn new(tracer: T, config: LangfuseConfig) -> Self {
if config.debug {
info!("Langfuse interceptor initialized");
}
Self {
config,
tracer: Arc::new(tracer),
context: Arc::new(LangfuseContext::new()),
}
}
pub fn set_session_id(&self, session_id: impl Into<String>) {
self.context.set_session_id(session_id);
}
pub fn set_user_id(&self, user_id: impl Into<String>) {
self.context.set_user_id(user_id);
}
pub fn add_tags(&self, tags: Vec<String>) {
self.context.add_tags(tags);
}
pub fn add_tag(&self, tag: impl Into<String>) {
self.context.add_tag(tag);
}
pub fn set_metadata(&self, metadata: serde_json::Value) {
self.context.set_metadata(metadata);
}
pub fn clear_context(&self) {
self.context.clear();
}
pub fn context(&self) -> &Arc<LangfuseContext> {
&self.context
}
fn extract_request_params(request_json: &str) -> serde_json::Result<Value> {
serde_json::from_str(request_json)
}
}
#[allow(
clippy::unnecessary_wraps,
clippy::unused_self,
clippy::needless_pass_by_ref_mut
)]
impl<T: Tracer + Send + Sync> LangfuseInterceptor<T>
where
T::Span: Send + Sync + 'static,
{
fn record_before_request(
&self,
ctx: &mut BeforeRequestContext<'_, LangfuseState<T::Span>>,
) -> Result<()> {
let tracer = self.tracer.as_ref();
let mut attributes = vec![
KeyValue::new(GEN_AI_PROVIDER_NAME, "openai"),
KeyValue::new("gen_ai.system", "openai"),
KeyValue::new(GEN_AI_OPERATION_NAME, ctx.operation.to_string()),
KeyValue::new(GEN_AI_REQUEST_MODEL, ctx.model.to_string()),
];
attributes.extend(self.context.get_attributes());
if let Ok(params) = Self::extract_request_params(ctx.request_json) {
if let Some(temperature) = params
.get("temperature")
.and_then(serde_json::Value::as_f64)
{
attributes.push(KeyValue::new(GEN_AI_REQUEST_TEMPERATURE, temperature));
}
if let Some(max_tokens) = params
.get("max_tokens")
.or_else(|| params.get("max_output_tokens"))
.and_then(serde_json::Value::as_i64)
{
attributes.push(KeyValue::new(GEN_AI_REQUEST_MAX_TOKENS, max_tokens));
}
for (i, (role, content)) in prompt_messages(¶ms).into_iter().enumerate() {
attributes.push(KeyValue::new(format!("gen_ai.prompt.{i}.role"), role));
attributes.push(KeyValue::new(format!("gen_ai.prompt.{i}.content"), content));
}
}
let span = tracer
.span_builder(ctx.operation.to_string())
.with_kind(SpanKind::Client)
.with_attributes(attributes)
.start(tracer);
*ctx.state
.span
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(span);
if self.config.debug {
debug!("Started Langfuse span for operation: {}", ctx.operation);
}
Ok(())
}
fn record_after_response(
&self,
ctx: &AfterResponseContext<'_, LangfuseState<T::Span>>,
) -> Result<()> {
use opentelemetry::trace::Span;
let Some(mut span) = ctx
.state
.span
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take()
else {
if self.config.debug {
debug!("No span found in state for operation: {}", ctx.operation);
}
return Ok(());
};
#[allow(clippy::cast_possible_truncation)]
{
span.set_attribute(KeyValue::new(
"duration_ms",
ctx.duration.as_millis() as i64,
));
}
if let Some(input_tokens) = ctx.input_tokens {
span.set_attribute(KeyValue::new(GEN_AI_USAGE_INPUT_TOKENS, input_tokens));
}
if let Some(output_tokens) = ctx.output_tokens {
span.set_attribute(KeyValue::new(GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens));
}
if let Ok(response) = Self::extract_request_params(ctx.response_json) {
if let Some(id) = response.get("id").and_then(serde_json::Value::as_str) {
span.set_attribute(KeyValue::new(GEN_AI_RESPONSE_ID, id.to_string()));
}
if let Some(output) = response.get("output").and_then(serde_json::Value::as_array) {
let messages = output.iter().filter(|item| {
item.get("type").and_then(serde_json::Value::as_str) == Some("message")
});
for (i, message) in messages.enumerate() {
span.set_attribute(KeyValue::new(
format!("gen_ai.completion.{i}.role"),
"assistant",
));
span.set_attribute(KeyValue::new(
format!("gen_ai.completion.{i}.content"),
content_text(message.get("content")),
));
}
}
if let Some(choices) = response
.get("choices")
.and_then(serde_json::Value::as_array)
{
for (i, choice) in choices.iter().enumerate() {
if let Some(message) = choice.get("message") {
if let Some(role) = message.get("role").and_then(serde_json::Value::as_str)
{
span.set_attribute(KeyValue::new(
format!("gen_ai.completion.{i}.role"),
role.to_string(),
));
}
if let Some(content) =
message.get("content").and_then(serde_json::Value::as_str)
{
span.set_attribute(KeyValue::new(
format!("gen_ai.completion.{i}.content"),
content.to_string(),
));
}
}
}
}
}
span.end();
if self.config.debug {
debug!("Completed Langfuse span for operation: {}", ctx.operation);
}
Ok(())
}
fn record_on_stream_chunk(
&self,
_ctx: &StreamChunkContext<'_, LangfuseState<T::Span>>,
) -> Result<()> {
Ok(())
}
fn record_on_stream_end(
&self,
ctx: &StreamEndContext<'_, LangfuseState<T::Span>>,
) -> Result<()> {
use opentelemetry::trace::Span;
let Some(mut span) = ctx
.state
.span
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take()
else {
if self.config.debug {
debug!(
"No span found in state for stream operation: {}",
ctx.operation
);
}
return Ok(());
};
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
{
span.set_attribute(KeyValue::new(
"stream.total_chunks",
ctx.total_chunks as i64,
));
span.set_attribute(KeyValue::new(
"stream.duration_ms",
ctx.duration.as_millis() as i64,
));
}
if let Some(input_tokens) = ctx.input_tokens {
span.set_attribute(KeyValue::new(GEN_AI_USAGE_INPUT_TOKENS, input_tokens));
}
if let Some(output_tokens) = ctx.output_tokens {
span.set_attribute(KeyValue::new(GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens));
}
span.end();
if self.config.debug {
info!(
"Completed streaming span for operation: {} with {} chunks",
ctx.operation, ctx.total_chunks
);
}
Ok(())
}
fn record_on_stream_terminal(
&self,
ctx: &StreamTerminalContext<'_, LangfuseState<T::Span>>,
) -> Result<()> {
use opentelemetry::trace::Span;
if matches!(ctx.outcome, crate::streaming::StreamOutcome::Completed) {
return self.record_on_stream_end(&ctx.summary);
}
let Some(mut span) = ctx
.summary
.state
.span
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take()
else {
if self.config.debug {
debug!(
"No span found for terminal stream event: {}",
ctx.summary.operation
);
}
return Ok(());
};
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
{
span.set_attribute(KeyValue::new(
"stream.total_chunks",
ctx.summary.total_chunks as i64,
));
span.set_attribute(KeyValue::new(
"stream.duration_ms",
ctx.summary.duration.as_millis() as i64,
));
}
span.set_attribute(KeyValue::new(
"stream.outcome",
match ctx.outcome {
crate::streaming::StreamOutcome::Completed => "completed",
crate::streaming::StreamOutcome::StreamFailed(_) => "failed",
crate::streaming::StreamOutcome::Cancelled => "cancelled",
},
));
if let Some(input_tokens) = ctx.summary.input_tokens {
span.set_attribute(KeyValue::new(GEN_AI_USAGE_INPUT_TOKENS, input_tokens));
}
if let Some(output_tokens) = ctx.summary.output_tokens {
span.set_attribute(KeyValue::new(GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens));
}
let description = match ctx.outcome {
crate::streaming::StreamOutcome::StreamFailed(failure) => {
format!("stream failed ({:?})", failure.kind)
}
crate::streaming::StreamOutcome::Cancelled => "stream cancelled".to_string(),
crate::streaming::StreamOutcome::Completed => unreachable!(),
};
span.set_status(Status::error(description));
span.end();
if self.config.debug {
info!(
"Finished streaming span for operation {} with outcome {:?}",
ctx.summary.operation, ctx.outcome
);
}
Ok(())
}
fn record_on_error(&self, ctx: &ErrorContext<'_, LangfuseState<T::Span>>) {
use opentelemetry::trace::{Span, Status};
let Some(state) = ctx.state else {
if self.config.debug {
debug!(
"No state available for error in operation: {}",
ctx.operation
);
}
return;
};
let Some(mut span) = state
.span
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take()
else {
if self.config.debug {
debug!(
"No span found in state for error in operation: {}",
ctx.operation
);
}
return;
};
span.set_status(Status::error(ctx.error.to_string()));
span.set_attribute(KeyValue::new("error.type", format!("{:?}", ctx.error)));
span.set_attribute(KeyValue::new("error.message", ctx.error.to_string()));
if let Some(model) = ctx.model {
span.set_attribute(KeyValue::new(GEN_AI_REQUEST_MODEL, model.to_string()));
}
span.end();
if self.config.debug {
error!(
"Recorded error for operation {}: {}",
ctx.operation, ctx.error
);
}
}
}
impl<T: Tracer + Send + Sync> Interceptor<LangfuseState<T::Span>> for LangfuseInterceptor<T>
where
T::Span: Send + Sync + 'static,
{
fn before_request(
&self,
ctx: &mut BeforeRequestContext<'_, LangfuseState<T::Span>>,
) -> impl Future<Output = Result<()>> + Send {
std::future::ready(self.record_before_request(ctx))
}
fn after_response(
&self,
ctx: &AfterResponseContext<'_, LangfuseState<T::Span>>,
) -> impl Future<Output = Result<()>> + Send {
std::future::ready(self.record_after_response(ctx))
}
fn on_stream_chunk(
&self,
ctx: &StreamChunkContext<'_, LangfuseState<T::Span>>,
) -> impl Future<Output = Result<()>> + Send {
std::future::ready(self.record_on_stream_chunk(ctx))
}
fn on_stream_end(
&self,
ctx: &StreamEndContext<'_, LangfuseState<T::Span>>,
) -> impl Future<Output = Result<()>> + Send {
std::future::ready(self.record_on_stream_end(ctx))
}
fn on_stream_terminal(
&self,
ctx: &StreamTerminalContext<'_, LangfuseState<T::Span>>,
) -> impl Future<Output = Result<()>> + Send {
std::future::ready(self.record_on_stream_terminal(ctx))
}
fn on_error(
&self,
ctx: &ErrorContext<'_, LangfuseState<T::Span>>,
) -> impl Future<Output = ()> + Send {
self.record_on_error(ctx);
std::future::ready(())
}
}
fn prompt_messages(params: &serde_json::Value) -> Vec<(String, String)> {
let mut prompt = Vec::new();
if let Some(messages) = params.get("messages").and_then(serde_json::Value::as_array) {
for message in messages.iter().filter(|message| message.is_object()) {
prompt.push(role_and_content(message, "unknown"));
}
return prompt;
}
if let Some(instructions) = params
.get("instructions")
.and_then(serde_json::Value::as_str)
{
prompt.push(("system".to_string(), instructions.to_string()));
}
match params.get("input") {
Some(serde_json::Value::String(text)) => prompt.push(("user".to_string(), text.clone())),
Some(serde_json::Value::Array(items)) => {
for item in items.iter().filter(|item| item.get("role").is_some()) {
prompt.push(role_and_content(item, "user"));
}
}
_ => {}
}
prompt
}
fn role_and_content(message: &serde_json::Value, default_role: &str) -> (String, String) {
let role = message
.get("role")
.and_then(serde_json::Value::as_str)
.unwrap_or(default_role)
.to_string();
(role, content_text(message.get("content")))
}
fn content_text(content: Option<&serde_json::Value>) -> String {
match content {
Some(serde_json::Value::String(text)) => text.clone(),
Some(serde_json::Value::Array(parts)) => parts
.iter()
.filter_map(|part| part.get("text").and_then(serde_json::Value::as_str))
.collect(),
_ => String::new(),
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::streaming::StreamOutcome;
use opentelemetry::trace::noop::NoopTracer;
use std::time::Duration;
#[test]
fn test_config_from_env() {
std::env::set_var("LANGFUSE_DEBUG", "true");
let config = LangfuseConfig::default();
assert!(config.debug);
std::env::remove_var("LANGFUSE_DEBUG");
}
#[test]
fn prompt_messages_of_chat_and_responses_requests() {
let chat = serde_json::json!({"messages": [
{"role": "system", "content": "Be brief"},
{"role": "user", "content": [{"type": "text", "text": "Hi"}]}
]});
assert_eq!(
prompt_messages(&chat),
[
("system".to_string(), "Be brief".to_string()),
("user".to_string(), "Hi".to_string())
]
);
let responses = serde_json::json!({
"instructions": "Be brief",
"input": [
{"type": "message", "role": "user", "content": "Hi"},
{"type": "function_call_output", "call_id": "c", "output": "x"}
]
});
assert_eq!(
prompt_messages(&responses),
[
("system".to_string(), "Be brief".to_string()),
("user".to_string(), "Hi".to_string())
]
);
let text = serde_json::json!({"input": "Hello"});
assert_eq!(
prompt_messages(&text),
[("user".to_string(), "Hello".to_string())]
);
}
#[test]
fn test_interceptor_creation() {
let tracer = NoopTracer::new();
let config = LangfuseConfig::new().with_debug(true);
let _interceptor = LangfuseInterceptor::new(tracer, config);
}
#[tokio::test]
async fn terminal_failure_removes_active_langfuse_span() {
let interceptor = LangfuseInterceptor::new(NoopTracer::new(), LangfuseConfig::new());
let mut state = LangfuseState::default();
let mut before = BeforeRequestContext {
operation: "chat",
model: "mock-model",
request_json: "{}",
state: &mut state,
};
interceptor.before_request(&mut before).await.unwrap();
let summary = StreamEndContext {
operation: "chat",
model: "mock-model",
request_json: "{}",
total_chunks: 2,
duration: Duration::from_millis(12),
input_tokens: Some(3),
output_tokens: Some(4),
state: &state,
};
let terminal = StreamTerminalContext {
summary,
outcome: StreamOutcome::Cancelled,
};
interceptor.on_stream_terminal(&terminal).await.unwrap();
assert!(state.span.lock().unwrap().is_none());
}
#[tokio::test]
async fn span_names_the_provider_in_both_genai_attributes() {
use opentelemetry::trace::TracerProvider as _;
use opentelemetry_sdk::trace::{InMemorySpanExporter, SdkTracerProvider};
let exporter = InMemorySpanExporter::default();
let provider = SdkTracerProvider::builder()
.with_simple_exporter(exporter.clone())
.build();
let interceptor = LangfuseInterceptor::new(provider.tracer("test"), LangfuseConfig::new());
let mut state = LangfuseState::default();
let mut before = BeforeRequestContext {
operation: "chat",
model: "mock-model",
request_json: "{}",
state: &mut state,
};
interceptor.before_request(&mut before).await.unwrap();
let summary = StreamEndContext {
operation: "chat",
model: "mock-model",
request_json: "{}",
total_chunks: 1,
duration: Duration::from_millis(1),
input_tokens: Some(3),
output_tokens: Some(4),
state: &state,
};
let terminal = StreamTerminalContext {
summary,
outcome: StreamOutcome::Completed,
};
interceptor.on_stream_terminal(&terminal).await.unwrap();
let spans = exporter.get_finished_spans().unwrap();
let span = spans.first().expect("one span");
let value = |key: &str| {
span.attributes
.iter()
.find(|kv| kv.key.as_str() == key)
.map(|kv| kv.value.to_string())
};
assert_eq!(value("gen_ai.provider.name").as_deref(), Some("openai"));
assert_eq!(value("gen_ai.system").as_deref(), Some("openai"));
assert_eq!(value("gen_ai.request.model").as_deref(), Some("mock-model"));
assert_eq!(value("gen_ai.usage.input_tokens").as_deref(), Some("3"));
assert_eq!(value("gen_ai.usage.output_tokens").as_deref(), Some("4"));
}
}