-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtrace.rb
More file actions
52 lines (41 loc) · 1.44 KB
/
trace.rb
File metadata and controls
52 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
require "braintrust"
require "opentelemetry/sdk"
# Example: Enable Braintrust tracing and send a span manually
#
# This example demonstrates how to:
# 1. Initialize Braintrust with tracing enabled (automatically configures OpenTelemetry)
# 2. Create spans manually
# 3. Send the spans to Braintrust
#
# Usage:
# bundle exec ruby examples/trace.rb
Braintrust.init(blocking_login: true)
# Get a tracer
tracer = OpenTelemetry.tracer_provider.tracer("my-app")
# Create a span manually
# Note: braintrust.parent, braintrust.org, and braintrust.app_url are automatically added!
root_span = nil
tracer.in_span("examples/trace.rb") do |span|
root_span = span
# Set custom attributes
span.set_attribute("user.id", "123")
span.set_attribute("operation.type", "manual_test")
span.set_attribute("environment", "example")
puts "Inside span - doing some work..."
sleep 0.1
# You can create nested spans - they also get Braintrust attributes automatically
tracer.in_span("nested-operation") do |nested_span|
nested_span.set_attribute("step", "1")
puts " Inside nested span..."
sleep 0.05
end
end
# Print permalink to view this trace in Braintrust
puts "\n✓ View this trace in Braintrust:"
puts " #{Braintrust::Trace.permalink(root_span)}"
# Shutdown to flush spans to Braintrust
OpenTelemetry.tracer_provider.shutdown
puts "\n✓ Success! Trace sent to Braintrust!"