Top Level Namespace

Defined Under Namespace

Modules: Appsignal Classes: Object

Constant Summary collapse

EXT_PATH =
File.expand_path(__dir__).freeze
AGENT_PLATFORM =
Appsignal::System.agent_platform
AGENT_ARCHITECTURE =
Appsignal::System.agent_architecture
TARGET_TRIPLE =
"#{AGENT_ARCHITECTURE}-#{AGENT_PLATFORM}"
ARCH_CONFIG =
APPSIGNAL_AGENT_CONFIG["triples"][TARGET_TRIPLE].freeze
CA_CERT_PATH =
File.join(EXT_PATH, "../resources/cacert.pem").freeze
APPSIGNAL_AGENT_CONFIG =

DO NOT EDIT This is a generated file by the rake publish family of tasks in the appsignal-agent repository. Modifications to this file will be overwritten with the next agent release.

{
  "version" => "0.36.12",
  "mirrors" => [
    "https://d135dj0rjqvssy.cloudfront.net",
    "https://appsignal-agent-releases.global.ssl.fastly.net"
  ],
  "triples" => {
    "x86_64-darwin" => {
      "static" => {
        "checksum" => "a63189a2ba6b500e038e5658f97478ceae27a956baa2f89a4e79ee5a1dadace6",
        "filename" => "appsignal-x86_64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "ecfb108dcc4d10f442415debaceed6d2e568b44ab938cf0ed6fee3ccfbdc5379",
        "filename" => "appsignal-x86_64-darwin-all-dynamic.tar.gz"
      }
    },
    "universal-darwin" => {
      "static" => {
        "checksum" => "a63189a2ba6b500e038e5658f97478ceae27a956baa2f89a4e79ee5a1dadace6",
        "filename" => "appsignal-x86_64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "ecfb108dcc4d10f442415debaceed6d2e568b44ab938cf0ed6fee3ccfbdc5379",
        "filename" => "appsignal-x86_64-darwin-all-dynamic.tar.gz"
      }
    },
    "aarch64-darwin" => {
      "static" => {
        "checksum" => "d23d6cc15e7df1c810b55dc33905002c5a1d6bc3f7cb10690d7e21398aabdac0",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "b4f9bf20f87fc7ddd8cc307646a956f734a2153323cc01487ced322263f32c2b",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "arm64-darwin" => {
      "static" => {
        "checksum" => "d23d6cc15e7df1c810b55dc33905002c5a1d6bc3f7cb10690d7e21398aabdac0",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "b4f9bf20f87fc7ddd8cc307646a956f734a2153323cc01487ced322263f32c2b",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "arm-darwin" => {
      "static" => {
        "checksum" => "d23d6cc15e7df1c810b55dc33905002c5a1d6bc3f7cb10690d7e21398aabdac0",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "b4f9bf20f87fc7ddd8cc307646a956f734a2153323cc01487ced322263f32c2b",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "aarch64-linux" => {
      "static" => {
        "checksum" => "0a277e682f1568fbc54df849a46f21f155643b40c285a5bd30294c9d046e49c2",
        "filename" => "appsignal-aarch64-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "94e0193908955e7a3a274a37947e234c5b038aa26c0ea9b371a08fd63441a940",
        "filename" => "appsignal-aarch64-linux-all-dynamic.tar.gz"
      }
    },
    "i686-linux" => {
      "static" => {
        "checksum" => "0d4536a2b1c9b6915f0f8597bd858988e284e7dbc4a14488332ffa7e401a1134",
        "filename" => "appsignal-i686-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "c5df66876e4611d5ef8798013f0188469cbdea6753a9b9d6cf54bd858e6331e9",
        "filename" => "appsignal-i686-linux-all-dynamic.tar.gz"
      }
    },
    "x86-linux" => {
      "static" => {
        "checksum" => "0d4536a2b1c9b6915f0f8597bd858988e284e7dbc4a14488332ffa7e401a1134",
        "filename" => "appsignal-i686-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "c5df66876e4611d5ef8798013f0188469cbdea6753a9b9d6cf54bd858e6331e9",
        "filename" => "appsignal-i686-linux-all-dynamic.tar.gz"
      }
    },
    "x86_64-linux" => {
      "static" => {
        "checksum" => "eec59a3e46dd5d5baff044465b4e314f21edf6270500bf24e3bc50afcee5f2e2",
        "filename" => "appsignal-x86_64-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "37471e17c5cb9f252d039cfbc0c01d570698c2245f6c8783fd2778e640b59a54",
        "filename" => "appsignal-x86_64-linux-all-dynamic.tar.gz"
      }
    },
    "x86_64-linux-musl" => {
      "static" => {
        "checksum" => "4454ec095ad14813bedea20a110a26235dbc6e8e33538219fad325792c0d888e",
        "filename" => "appsignal-x86_64-linux-musl-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "4c21039b077b0c292d530210e077d942694f4c533d3761ed11e27ca932215131",
        "filename" => "appsignal-x86_64-linux-musl-all-dynamic.tar.gz"
      }
    },
    "aarch64-linux-musl" => {
      "static" => {
        "checksum" => "048a55f0f10087e7954559d6cd09b371a0b532673f7ca769bcfde5729e297a72",
        "filename" => "appsignal-aarch64-linux-musl-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "8dc336cffcb2cffe85daa8c70b02b0af7e626ed81c8873cdbb627216a411d59a",
        "filename" => "appsignal-aarch64-linux-musl-all-dynamic.tar.gz"
      }
    },
    "x86_64-freebsd" => {
      "static" => {
        "checksum" => "d81d039a3c1936bd393a6140b46f48092da31600b0311eab819acd777521a588",
        "filename" => "appsignal-x86_64-freebsd-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "cc345d4a791200e5af53c533e7ad83d278e98ee0becb5b54e502ba8934094372",
        "filename" => "appsignal-x86_64-freebsd-all-dynamic.tar.gz"
      }
    },
    "amd64-freebsd" => {
      "static" => {
        "checksum" => "d81d039a3c1936bd393a6140b46f48092da31600b0311eab819acd777521a588",
        "filename" => "appsignal-x86_64-freebsd-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "cc345d4a791200e5af53c533e7ad83d278e98ee0becb5b54e502ba8934094372",
        "filename" => "appsignal-x86_64-freebsd-all-dynamic.tar.gz"
      }
    }
  }
}.freeze

Instance Method Summary collapse

Instance Method Details

#abort_installation(reason) ⇒ Object



88
89
90
91
92
93
94
# File 'ext/base.rb', line 88

def abort_installation(reason)
  report["result"] = {
    "status" => "failed",
    "message" => reason
  }
  false
end

#ca_file_pathObject



25
26
27
28
29
30
# File 'ext/base.rb', line 25

def ca_file_path
  configured_path = ENV.fetch("APPSIGNAL_CA_FILE_PATH", nil)
  return configured_path if configured_path && !configured_path.strip.empty?

  CA_CERT_PATH
end

#check_architectureObject



109
110
111
112
113
114
115
# File 'ext/base.rb', line 109

def check_architecture
  APPSIGNAL_AGENT_CONFIG["triples"].key?(TARGET_TRIPLE) || abort_installation(
      "AppSignal currently does not support your system architecture (#{TARGET_TRIPLE})." \
        "Please let us know at support@appsignal.com, we aim to support everything " \
        "our customers run."
    )
end

#create_dummy_makefileObject



80
81
82
# File 'ext/base.rb', line 80

def create_dummy_makefile
  File.write(File.join(EXT_PATH, "Makefile"), "default:\nclean:\ninstall:")
end

#download_archive(type) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'ext/base.rb', line 117

def download_archive(type)
  report["build"]["source"] = "remote"

  unless ARCH_CONFIG.key?(type)
    abort_installation(
      "AppSignal currently does not support your system. " \
        "Expected config for architecture '#{TARGET_TRIPLE}' and package type '#{type}', " \
        "but none found. For a full list of supported systems visit: " \
        "https://docs.appsignal.com/support/operating-systems.html"
    )
    return
  end

  version = APPSIGNAL_AGENT_CONFIG["version"]
  filename = ARCH_CONFIG[type]["filename"]
  download_errors = []

  APPSIGNAL_AGENT_CONFIG["mirrors"].each do |mirror|
    download_url = [mirror, version, filename].join("/")
    report["download"]["download_url"] = download_url

    begin
      proxy, _error = http_proxy
      args = [
        download_url,
        { :ssl_ca_cert => ca_file_path,
          :proxy => proxy }
      ]
      if URI.respond_to?(:open) # rubocop:disable Style/GuardClause
        return URI.open(*args) # rubocop:disable Security/Open
      else
        return open(*args) # rubocop:disable Security/Open
      end
    rescue => error
      backtrace = error.backtrace.join("\n")
      download_errors <<
        "- URL: #{download_url}\n  Error: #{error.class}: #{error.message}\n#{backtrace}"
      next
    end
  end

  abort_installation(
    "Could not download archive from any of our mirrors. " \
      "Please make sure your network allows access to any of these mirrors.\n" \
      "Attempted to download the archive from the following urls:\n#{download_errors.join("\n")}"
  )
end

#ext_path(path) ⇒ Object



21
22
23
# File 'ext/base.rb', line 21

def ext_path(path)
  File.join(EXT_PATH, path)
end

#fail_install_on_purpose_in_test!Object

Fail the installation on purpose in a specific test environment.



227
228
229
230
231
# File 'ext/base.rb', line 227

def fail_install_on_purpose_in_test!
  return unless ENV["_TEST_APPSIGNAL_EXTENSION_FAILURE"]

  raise "AppSignal internal test failure"
end

#fail_installation_with_error(error) ⇒ Object



96
97
98
99
100
101
102
103
# File 'ext/base.rb', line 96

def fail_installation_with_error(error)
  report["result"] = {
    "status" => "error",
    "error" => "#{error.class}: #{error}",
    "backtrace" => error.backtrace
  }
  false
end

#have_required_function(library, func) ⇒ Object

rubocop:disable Naming/PredicatePrefix



80
81
82
83
84
85
86
87
88
89
90
91
# File 'ext/extconf.rb', line 80

def have_required_function(library, func) # rubocop:disable Naming/PredicatePrefix
  if have_func(func)
    report["build"]["dependencies"][library] = "linked"
    return
  end

  report["build"]["dependencies"][library] = "not linked"
  abort_installation("Missing function '#{func}'")
  # Exit with true/0/success because the AppSignal installation should never
  # break a build
  exit
end

#http_proxyObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'ext/base.rb', line 197

def http_proxy
  proxy = try_http_proxy_value(ENV.fetch("APPSIGNAL_HTTP_PROXY", nil))
  return [proxy, nil] if proxy

  proxy, error =
    begin
      [try_http_proxy_value(Gem.configuration[:http_proxy]), nil]
    rescue => error
      # Ignore this setting if the `.gemrc` file can't be read. This raises an
      # error on Rubies with psych 4 in the standard library, but also have
      # psych 5 installed: Ruby < 3.2.
      # https://github.com/appsignal/appsignal-ruby/issues/904
      [nil, error]
    end
  return [proxy, error] if proxy

  proxy = try_http_proxy_value(ENV.fetch("http_proxy", nil))
  return [proxy, error] if proxy

  proxy = try_http_proxy_value(ENV.fetch("HTTP_PROXY", nil))
  return [proxy, error] if proxy

  [nil, error]
end

#installObject



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
53
54
55
56
57
58
59
# File 'ext/extconf.rb', line 11

def install
  fail_install_on_purpose_in_test!

  library_type = "static"
  report["language"]["implementation"] = "ruby"
  report["build"]["library_type"] = library_type
  return unless check_architecture

  if local_build?
    report["build"]["source"] = "local"
  else
    archive = download_archive(library_type)
    return unless archive
    return unless verify_archive(archive, library_type)

    unarchive(archive)
  end

  is_linux_system = [
    Appsignal::System::LINUX_TARGET,
    Appsignal::System::MUSL_TARGET
  ].include?(AGENT_PLATFORM)

  require "mkmf"
  link_libraries if is_linux_system

  if !have_library("appsignal", "appsignal_start", "appsignal.h")
    abort_installation("Library libappsignal.a or appsignal.h not found")
  elsif !find_executable("appsignal-agent", EXT_PATH)
    abort_installation("File appsignal-agent not found")
  else
    if is_linux_system
      # Statically link libgcc and libgcc_s libraries.
      # Dependencies of the libappsignal extension library.
      # If the gem is installed on a host with build tools installed, but is
      # run on one that isn't the missing libraries will cause the extension
      # to fail on start.
      $LDFLAGS += " -static-libgcc" # rubocop:disable Style/GlobalVars
      report["build"]["flags"]["LDFLAGS"] = $LDFLAGS # rubocop:disable Style/GlobalVars
    end
    create_makefile "appsignal_extension"
    successful_installation
  end
rescue => error
  fail_installation_with_error(error)
ensure
  create_dummy_makefile unless installation_succeeded?
  write_report
end

#installation_succeeded?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'ext/base.rb', line 105

def installation_succeeded?
  report["result"]["status"] == "success"
end

Ruby 2.6 requires us to statically link more libraries we use in our extension library than previous versions. Needed for normal Linux libc and musl builds.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'ext/extconf.rb', line 64

def link_libraries
  if RbConfig::CONFIG["THREAD_MODEL"] == "pthread"
    # Link gem extension against pthread library
    have_library "pthread"
    have_required_function "pthread", "pthread_create"
  end

  # Links gem extension against the `dl` library. This is needed when Ruby is
  # not linked against `dl` itself, so link it on the gem extension.
  have_library "dl"
  # Check if functions are available now from the linked library
  %w[dlopen dlclose dlsym].each do |func|
    have_required_function "dl", func
  end
end

#local_build?Boolean

Returns:

  • (Boolean)


5
6
7
8
9
# File 'ext/extconf.rb', line 5

def local_build?
  File.exist?(ext_path("appsignal-agent")) &&
    File.exist?(ext_path("libappsignal.a")) &&
    File.exist?(ext_path("appsignal.h"))
end

#reportObject



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/base.rb', line 32

def report
  @report ||=
    begin
      rbconfig = RbConfig::CONFIG
      patchlevel = rbconfig["PATCHLEVEL"]
      patchlevel_label = "-p#{patchlevel}" if patchlevel
      ruby_version = "#{RUBY_VERSION}#{patchlevel_label}"
      {
        "result" => {
          "status" => "incomplete"
        },
        "language" => {
          "name" => "ruby",
          "version" => ruby_version
        },
        "download" => {
          "checksum" => "unverified"
        },
        "build" => {
          "time" => Time.now.utc,
          "package_path" => File.dirname(EXT_PATH),
          "architecture" => AGENT_ARCHITECTURE,
          "target" => AGENT_PLATFORM,
          "musl_override" => Appsignal::System.force_musl_build?,
          "linux_arm_override" => Appsignal::System.force_linux_arm_build?,
          "dependencies" => {},
          "flags" => {}
        },
        "host" => {
          "root_user" => Process.uid.zero?,
          "dependencies" => {}.tap do |d|
            ldd_output = Appsignal::System.ldd_version_output
            ldd_version = Appsignal::System.extract_ldd_version(ldd_output)
            d["libc"] = ldd_version if ldd_version
          end
        }
      }.tap do |r|
        proxy, error = http_proxy
        r["download"]["http_proxy"] = proxy
        r["download"]["http_proxy_error"] = error if error
      end
    end
end

#store_download_version_on_reportObject



192
193
194
195
# File 'ext/base.rb', line 192

def store_download_version_on_report
  path = File.expand_path(File.join(File.dirname(__FILE__), "appsignal.version"))
  report["build"]["agent_version"] = File.read(path).strip
end

#successful_installationObject



84
85
86
# File 'ext/base.rb', line 84

def successful_installation
  report["result"] = { "status" => "success" }
end

#try_http_proxy_value(value) ⇒ Object



222
223
224
# File 'ext/base.rb', line 222

def try_http_proxy_value(value)
  value if value.respond_to?(:empty?) && !value.strip.empty?
end

#unarchive(archive) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'ext/base.rb', line 180

def unarchive(archive)
  Gem::Package::TarReader.new(Zlib::GzipReader.open(archive)) do |tar|
    tar.each do |entry|
      next unless entry.file?

      File.binwrite(ext_path(entry.full_name), entry.read)
    end
  end
  store_download_version_on_report
  FileUtils.chmod(0o755, ext_path("appsignal-agent"))
end

#verify_archive(archive, type) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'ext/base.rb', line 165

def verify_archive(archive, type)
  expected_checksum = ARCH_CONFIG[type]["checksum"]
  actual_checksum = Digest::SHA256.hexdigest(archive.read)
  if actual_checksum == expected_checksum
    report["download"]["checksum"] = "verified"
    true
  else
    report["download"]["checksum"] = "invalid"
    abort_installation(
      "Checksum of downloaded archive could not be verified: " \
        "Expected '#{expected_checksum}', got '#{actual_checksum}'."
    )
  end
end

#write_reportObject



76
77
78
# File 'ext/base.rb', line 76

def write_report
  File.write(File.join(EXT_PATH, "install.report"), JSON.generate(report))
end