-
Notifications
You must be signed in to change notification settings - Fork 51
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
[feat] langfuse tracing layer #498
Merged
Merged
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
2c4d9d1
working langfuse initial implementation
ahau-square 8cf258b
simplified tracing implementation
ahau-square 7e19d72
merged
ahau-square ed5a321
remove file logging
ahau-square 928398c
add file writing layer to tracing
ahau-square ff8238d
use generation type instead
ahau-square c67af0b
refactor observation layer
ajgray-stripe e3f5ce2
consolidate tool traces
ahau-square fe67482
consolidate tool tracing
ahau-square e96c084
migrated tracing into core goose
ahau-square 697b1e2
add langfuse starter script and update justfile
ahau-square 926e412
tracing added to server
ahau-square 6c21f7b
Merge branch 'v1.0' into ahau/1.0-tracing-langfuse
ahau-square 3ca25e7
formatting
ahau-square 7cd41f9
save cli session traces to have same session name for log file name
ahau-square 7fa0ebc
add tracing on other providers
ahau-square 6beb3f6
reformat
ahau-square c5df60a
add tests
ahau-square 375b33e
formatting
ahau-square cc509aa
merged
ahau-square 8876ae9
Merge v1.0: Resolve conflicts with tracing implementation
ahau-square 75f9719
Merge branch 'v1.0' into ahau/1.0-tracing-langfuse
ahau-square 7652e27
merged with cli working with tracing
ahau-square b1efec5
tracing added back into server after merge
ahau-square 9409351
fix tests
ahau-square a25ed46
remove agent.rs mistakenly added in merge
ahau-square f646c13
fix log levels on log file
ahau-square aa7780f
format
ahau-square File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use anyhow::{Context, Result}; | ||
use serde_json::Value; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
use tracing_appender::rolling::Rotation; | ||
use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Registry}; | ||
use tracing::dispatcher::set_global_default; | ||
|
||
use crate::tracing::{langfuse_layer, observation_layer::{BatchManager, ObservationLayer, SpanTracker}}; | ||
|
||
struct ConsoleLogger { | ||
batch: Vec<Value>, | ||
} | ||
|
||
impl ConsoleLogger { | ||
fn new() -> Self { | ||
Self { | ||
batch: Vec::new(), | ||
} | ||
} | ||
} | ||
|
||
impl BatchManager for ConsoleLogger { | ||
fn add_event(&mut self, _event_type: &str, body: Value) { | ||
self.batch.push(body); | ||
} | ||
|
||
fn send(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
self.batch.clear(); | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn get_log_directory() -> Result<PathBuf> { | ||
let home = std::env::var("HOME").context("HOME environment variable not set")?; | ||
let base_log_dir = PathBuf::from(home).join(".config").join("goose").join("logs"); | ||
|
||
// Create date-based subdirectory | ||
let now = chrono::Local::now(); | ||
let date_dir = base_log_dir.join(now.format("%Y-%m-%d").to_string()); | ||
|
||
// Ensure log directory exists | ||
fs::create_dir_all(&date_dir).context("Failed to create log directory")?; | ||
|
||
Ok(date_dir) | ||
} | ||
|
||
fn create_observation_layer() -> ObservationLayer { | ||
let batch_manager = Arc::new(Mutex::new(ConsoleLogger::new())); | ||
ObservationLayer { | ||
batch_manager, | ||
span_tracker: Arc::new(Mutex::new(SpanTracker::new())), | ||
} | ||
} | ||
|
||
pub fn setup_logging() -> Result<()> { | ||
// Set up file appender for goose module logs | ||
let log_dir = get_log_directory()?; | ||
let timestamp = chrono::Local::now().format("%Y%m%d_%H%M%S%.3f").to_string(); | ||
|
||
// Create non-rolling file appender | ||
let file_appender = tracing_appender::rolling::RollingFileAppender::new( | ||
Rotation::NEVER, | ||
log_dir, | ||
&format!("{}.log", timestamp), | ||
); | ||
|
||
// Create JSON file logging layer | ||
let file_layer = fmt::layer() | ||
.with_target(true) | ||
.with_level(true) | ||
.with_writer(file_appender) | ||
.with_ansi(false) | ||
.with_file(true); | ||
|
||
// Update filter to include debug level | ||
let filter = EnvFilter::try_from_default_env() | ||
.unwrap_or_else(|_| EnvFilter::new("goose=debug")); | ||
|
||
// Build the base subscriber | ||
let subscriber = Registry::default() | ||
.with(file_layer) | ||
.with(filter) | ||
.with(create_observation_layer()); | ||
|
||
// Set up the dispatcher | ||
let dispatcher = if let Some(langfuse) = langfuse_layer::create_langfuse_observer() { | ||
subscriber.with(langfuse).into() | ||
} else { | ||
subscriber.into() | ||
}; | ||
|
||
// Set the subscriber as the default | ||
set_global_default(dispatcher) | ||
.map_err(|e| anyhow::anyhow!("Failed to set global subscriber: {}", e))?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will have some conflicts with this file with the mcp changes, but we can forward port easily i think