Module: Braintrust::Internal::Env

Defined in:
lib/braintrust/internal/env.rb

Overview

Environment variable utilities.

Constant Summary collapse

ENV_AUTO_INSTRUMENT =
"BRAINTRUST_AUTO_INSTRUMENT"
ENV_ENVIRONMENT_NAME =
"BRAINTRUST_ENVIRONMENT_NAME"
ENV_ENVIRONMENT_TYPE =
"BRAINTRUST_ENVIRONMENT_TYPE"
ENV_INSTRUMENT_EXCEPT =
"BRAINTRUST_INSTRUMENT_EXCEPT"
ENV_INSTRUMENT_ONLY =
"BRAINTRUST_INSTRUMENT_ONLY"
ENV_FLUSH_ON_EXIT =
"BRAINTRUST_FLUSH_ON_EXIT"

Class Method Summary collapse

Class Method Details

.auto_instrumentObject



14
15
16
# File 'lib/braintrust/internal/env.rb', line 14

def self.auto_instrument
  ENV[ENV_AUTO_INSTRUMENT] != "false"
end

.detect_environmentObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/braintrust/internal/env.rb', line 31

def self.detect_environment
  env_type = env_value(ENV_ENVIRONMENT_TYPE)
  env_name = env_value(ENV_ENVIRONMENT_NAME)
  if present?(env_type) || present?(env_name)
    return {type: env_type, name: env_name}.compact
  end

  {
    "GITHUB_ACTIONS" => "github_actions",
    "GITLAB_CI" => "gitlab_ci",
    "CIRCLECI" => "circleci",
    "BUILDKITE" => "buildkite",
    "JENKINS_URL" => "jenkins",
    "JENKINS_HOME" => "jenkins",
    "TF_BUILD" => "azure_pipelines",
    "TEAMCITY_VERSION" => "teamcity",
    "TRAVIS" => "travis",
    "BITBUCKET_BUILD_NUMBER" => "bitbucket"
  }.each do |key, name|
    return {type: "ci", name: name} if present?(ENV[key])
  end
  return {type: "ci", name: "ci"} if present?(ENV["CI"])

  server_name = detect_server_environment_name
  return {type: "server", name: server_name} if server_name

  deployment_mode_environment(ENV["RAILS_ENV"]) ||
    deployment_mode_environment(ENV["RACK_ENV"])
end

.flush_on_exitObject

Whether to automatically flush spans on program exit. Default: true



19
20
21
# File 'lib/braintrust/internal/env.rb', line 19

def self.flush_on_exit
  ENV[ENV_FLUSH_ON_EXIT] != "false"
end

.instrument_exceptObject



23
24
25
# File 'lib/braintrust/internal/env.rb', line 23

def self.instrument_except
  parse_list(ENV_INSTRUMENT_EXCEPT)
end

.instrument_onlyObject



27
28
29
# File 'lib/braintrust/internal/env.rb', line 27

def self.instrument_only
  parse_list(ENV_INSTRUMENT_ONLY)
end

.parse_list(key) ⇒ Array<Symbol>?

Parse a comma-separated environment variable into an array of symbols.

Parameters:

  • key (String)

    The environment variable name

Returns:

  • (Array<Symbol>, nil)

    Array of symbols, or nil if not set



64
65
66
67
68
# File 'lib/braintrust/internal/env.rb', line 64

def self.parse_list(key)
  value = ENV[key]
  return nil unless value
  value.split(",").map(&:strip).map(&:to_sym)
end