@@ -5,17 +5,18 @@ module Braintrust
55 # and allows overriding with explicit options
66 class Config
77 attr_reader :api_key , :org_name , :default_project , :app_url , :api_url ,
8- :filter_ai_spans , :span_filter_funcs
8+ :filter_ai_spans , :span_filter_funcs , :compress_otel_payload
99
1010 def initialize ( api_key : nil , org_name : nil , default_project : nil , app_url : nil , api_url : nil ,
11- filter_ai_spans : nil , span_filter_funcs : nil )
11+ filter_ai_spans : nil , span_filter_funcs : nil , compress_otel_payload : true )
1212 @api_key = api_key
1313 @org_name = org_name
1414 @default_project = default_project
1515 @app_url = app_url
1616 @api_url = api_url
1717 @filter_ai_spans = filter_ai_spans
1818 @span_filter_funcs = span_filter_funcs || [ ]
19+ @compress_otel_payload = compress_otel_payload
1920 end
2021
2122 # Create a Config from environment variables, with option overrides
@@ -27,9 +28,10 @@ def initialize(api_key: nil, org_name: nil, default_project: nil, app_url: nil,
2728 # @param api_url [String, nil] API URL (overrides BRAINTRUST_API_URL env var)
2829 # @param filter_ai_spans [Boolean, nil] Enable AI span filtering (overrides BRAINTRUST_OTEL_FILTER_AI_SPANS env var)
2930 # @param span_filter_funcs [Array<Proc>, nil] Custom span filter functions
31+ # @param compress_otel_payload [Boolean, nil] Gzip OTEL export payloads (overrides BRAINTRUST_COMPRESS_OTEL_PAYLOAD env var). Default: true
3032 # @return [Config] the created config
3133 def self . from_env ( api_key : nil , org_name : nil , default_project : nil , app_url : nil , api_url : nil ,
32- filter_ai_spans : nil , span_filter_funcs : nil )
34+ filter_ai_spans : nil , span_filter_funcs : nil , compress_otel_payload : nil )
3335 # Parse filter_ai_spans from ENV if not explicitly provided
3436 env_filter_ai_spans = ENV [ "BRAINTRUST_OTEL_FILTER_AI_SPANS" ]
3537 filter_ai_spans_value = if filter_ai_spans . nil?
@@ -38,15 +40,31 @@ def self.from_env(api_key: nil, org_name: nil, default_project: nil, app_url: ni
3840 filter_ai_spans
3941 end
4042
43+ # Gzip OTEL payloads by default; disable via env var if not explicitly provided
44+ compress_otel_payload_value = if compress_otel_payload . nil?
45+ parse_bool ( ENV [ "BRAINTRUST_COMPRESS_OTEL_PAYLOAD" ] , default : true )
46+ else
47+ compress_otel_payload
48+ end
49+
4150 new (
4251 api_key : api_key || ( ( ENV [ "BRAINTRUST_API_KEY" ] && ENV [ "BRAINTRUST_API_KEY" ] . empty? ) ? nil : ENV [ "BRAINTRUST_API_KEY" ] ) ,
4352 org_name : org_name || ENV [ "BRAINTRUST_ORG_NAME" ] ,
4453 default_project : default_project || ENV [ "BRAINTRUST_DEFAULT_PROJECT" ] ,
4554 app_url : app_url || ENV [ "BRAINTRUST_APP_URL" ] || "https://www.braintrust.dev" ,
4655 api_url : api_url || ENV [ "BRAINTRUST_API_URL" ] || "https://api.braintrust.dev" ,
4756 filter_ai_spans : filter_ai_spans_value ,
48- span_filter_funcs : span_filter_funcs
57+ span_filter_funcs : span_filter_funcs ,
58+ compress_otel_payload : compress_otel_payload_value
4959 )
5060 end
61+
62+ # Parse a boolean-ish env var value. Falsy values: "false", "0", "no", "off"
63+ # (case-insensitive). nil/empty falls back to the default.
64+ def self . parse_bool ( value , default :)
65+ return default if value . nil? || value . empty?
66+
67+ !%w[ false 0 no off ] . include? ( value . strip . downcase )
68+ end
5169 end
5270end
0 commit comments