feat(testing): add Langfuse::Testing module for in-memory span capture - #100
Open
Alan-Marx wants to merge 4 commits into
Open
feat(testing): add Langfuse::Testing module for in-memory span capture#100Alan-Marx wants to merge 4 commits into
Alan-Marx wants to merge 4 commits into
Conversation
Introduces `require "langfuse/testing"` + `include Langfuse::Testing` as a supported, public way to assert against emitted spans in tests without real network calls or reaching into private SDK internals. - `OtelSetup.test_mode` flag causes `build_exporter` to return a fresh `InMemorySpanExporter` on each provider build; survives `shutdown` cycles - `Langfuse::Testing#emitted_langfuse_spans` — flushes and returns recorded spans - `Langfuse::Testing#reset_langfuse` — flushes and clears the span buffer - `docs/TESTING.md` covers RSpec and Minitest integration patterns Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 8d28b91. Configure here.
@span_exporter was set in build_tracer_provider before the provider won the setup mutex. On a lost race the losing provider is shut down (stopping its exporter) while @span_exporter still pointed at it, causing emitted_langfuse_spans and reset_langfuse to read a stopped exporter. build_exporter is now called in setup before build_tracer_provider, and @span_exporter is assigned inside publish_provider under the mutex only when the provider wins. rollback_provider also clears it. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Alan-Marx
force-pushed
the
feat/test-exporter-config
branch
from
August 1, 2026 00:47
c56a700 to
880b375
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Summary
We recently added the gem to our application and have set up an in-memory span exporter in our test suite so that we can test our observed code without needing to mock Langfuse network requests or stub Langfuse methods. We think this approach provides a pretty good DX and gets us pretty close to what is happening in production so thought we would offer it up to you guys for review.
require "langfuse/testing"+include Langfuse::Testingas a supported public API for asserting against emitted spans in tests without real network calls or private ivar accessOtelSetup.test_modeflag that causesbuild_exporterto return a freshInMemorySpanExporteron each provider build, survivingshutdown/reset!cycles transparentlyemitted_langfuse_spans(flush + return recorded spans) andreset_langfuse(flush + clear buffer between tests); seedocs/TESTING.mdfor RSpec and Minitest integration examplesTest plan
bundle exec rspec spec/langfuse/testing_spec.rb— all examples passbundle exec rspec— full suite passes (1347 examples, 0 failures)bundle exec rubocop— no offensesrequire "langfuse/testing",include Langfuse::Testing, callLangfuse.observe, assert onemitted_langfuse_spans— spans captured without network callsreset_langfuseclears spans between tests andemitted_langfuse_spansreturns[]on a clean slatetest_modesurvivesLangfuse.reset!(no OTLP exporter used after reset)Note
Medium Risk
Changes the tracer provider initialization path and exporter selection, but production behavior stays on OTLP unless
langfuse/testingis required; mis-loading that file in a non-test process would silently drop real exports.Overview
Adds a test-only entry point (
require "langfuse/testing") so apps can run the real Langfuse tracing pipeline and assert on exported spans without OTLP network calls or stubbing internal APIs.Langfuse::Testingturns onOtelSetup.test_modeat load time and exposesemitted_langfuse_spans(flush + read finished spans) andreset_langfuse(flush + clear the in-memory buffer between examples).docs/TESTING.mddocuments RSpec/Minitest setup patterns.OtelSetupnow tracks the activespan_exporter, wires the exporter throughpublish_provider(including rollback/shutdown cleanup), andbuild_exporterreturnsInMemorySpanExporterwhentest_modeis set—so in-memory export survivesshutdown/reset!and each provider rebuild gets a fresh buffer. Specs cover publish-race behavior forspan_exporterand the test-mode exporter path.Reviewed by Cursor Bugbot for commit 880b375. Bugbot is set up for automated code reviews on this repo. Configure here.