Top Level Namespace

Defined Under Namespace

Modules: Appsignal Classes: AppsignalPumaPlugin, 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 ship family of tasks in the appsignal-agent repository. Modifications to this file will be overwritten with the next agent release.

{
  "version" => "0.35.5",
  "mirrors" => [
    "https://appsignal-agent-releases.global.ssl.fastly.net",
    "https://d135dj0rjqvssy.cloudfront.net"
  ],
  "triples" => {
    "x86_64-darwin" => {
      "static" => {
        "checksum" => "3985b53b2a2814d44737182890e6fbe31b4b88361025140477a598e0e41fd948",
        "filename" => "appsignal-x86_64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "d135e2daf5c041a4ce7a7ab720cfd977fbae8375a007995e33a96558d3635792",
        "filename" => "appsignal-x86_64-darwin-all-dynamic.tar.gz"
      }
    },
    "universal-darwin" => {
      "static" => {
        "checksum" => "3985b53b2a2814d44737182890e6fbe31b4b88361025140477a598e0e41fd948",
        "filename" => "appsignal-x86_64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "d135e2daf5c041a4ce7a7ab720cfd977fbae8375a007995e33a96558d3635792",
        "filename" => "appsignal-x86_64-darwin-all-dynamic.tar.gz"
      }
    },
    "aarch64-darwin" => {
      "static" => {
        "checksum" => "79d22436bfe32dab4661f2a4149c6ab34c71e810c5d808ee3e60cd9f8a1395e4",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "2c49c8f24a9dd8f9d55c28015b3e1096180807e7b13d4a9cd8ed30f58bc6612f",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "arm64-darwin" => {
      "static" => {
        "checksum" => "79d22436bfe32dab4661f2a4149c6ab34c71e810c5d808ee3e60cd9f8a1395e4",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "2c49c8f24a9dd8f9d55c28015b3e1096180807e7b13d4a9cd8ed30f58bc6612f",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "arm-darwin" => {
      "static" => {
        "checksum" => "79d22436bfe32dab4661f2a4149c6ab34c71e810c5d808ee3e60cd9f8a1395e4",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "2c49c8f24a9dd8f9d55c28015b3e1096180807e7b13d4a9cd8ed30f58bc6612f",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "aarch64-linux" => {
      "static" => {
        "checksum" => "4a54587bb61f59d0b60032b2e0c1d14d2e726e20af049353c2ff279d07dd3028",
        "filename" => "appsignal-aarch64-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "aa2d011c538ce547c87110e7c8e49a24597d60ea2d1418032fc3db06267efba2",
        "filename" => "appsignal-aarch64-linux-all-dynamic.tar.gz"
      }
    },
    "i686-linux" => {
      "static" => {
        "checksum" => "01e2237029c3af23cc6f348fb63f65a92b8caf8f4731b78d014bb4001559aad5",
        "filename" => "appsignal-i686-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "c1043ac92b000a406d9afb77b6ec1d3ab6cfffd63c218a720c11daa3edb7fd1f",
        "filename" => "appsignal-i686-linux-all-dynamic.tar.gz"
      }
    },
    "x86-linux" => {
      "static" => {
        "checksum" => "01e2237029c3af23cc6f348fb63f65a92b8caf8f4731b78d014bb4001559aad5",
        "filename" => "appsignal-i686-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "c1043ac92b000a406d9afb77b6ec1d3ab6cfffd63c218a720c11daa3edb7fd1f",
        "filename" => "appsignal-i686-linux-all-dynamic.tar.gz"
      }
    },
    "x86_64-linux" => {
      "static" => {
        "checksum" => "beae1db2c122eb579f1ccb450177b0c64d65569d774efc7a1ee72c42d3382d39",
        "filename" => "appsignal-x86_64-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "67650f59d2ea9c99cde7dfe44da65a506522a3cdccabe697931d89ebaea96d18",
        "filename" => "appsignal-x86_64-linux-all-dynamic.tar.gz"
      }
    },
    "x86_64-linux-musl" => {
      "static" => {
        "checksum" => "521c4486d10cdafa1f72103cc439d6f0e4f549f1522c4473bf43dc487ec42436",
        "filename" => "appsignal-x86_64-linux-musl-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "0a94fcee8eb1f8fd06fce3eacffbcf9f2a52f5cde040796e19a589176525db4b",
        "filename" => "appsignal-x86_64-linux-musl-all-dynamic.tar.gz"
      }
    },
    "aarch64-linux-musl" => {
      "static" => {
        "checksum" => "c29aab31f4ca59efb1483f48c0cb3c27d799347b81655a28f24c146b55aa7db6",
        "filename" => "appsignal-aarch64-linux-musl-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "4b77ea0c1f50ada6e5f6ed693002e56db1e34e0e9d5fdb4ac80fc52fd51099b8",
        "filename" => "appsignal-aarch64-linux-musl-all-dynamic.tar.gz"
      }
    },
    "x86_64-freebsd" => {
      "static" => {
        "checksum" => "a3598e9df1b6b5970aabbbccbe4e77c2372d2320cd87bfa20f32fca53b8505e4",
        "filename" => "appsignal-x86_64-freebsd-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "4cbfd5e16776c8ea4308ee7f3a096c5cc137f2506f8a05d9c8504e3483fcd8ac",
        "filename" => "appsignal-x86_64-freebsd-all-dynamic.tar.gz"
      }
    },
    "amd64-freebsd" => {
      "static" => {
        "checksum" => "a3598e9df1b6b5970aabbbccbe4e77c2372d2320cd87bfa20f32fca53b8505e4",
        "filename" => "appsignal-x86_64-freebsd-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "4cbfd5e16776c8ea4308ee7f3a096c5cc137f2506f8a05d9c8504e3483fcd8ac",
        "filename" => "appsignal-x86_64-freebsd-all-dynamic.tar.gz"
      }
    }
  }
}.freeze

Instance Method Summary collapse

Instance Method Details

#abort_installation(reason) ⇒ Object



81
82
83
84
85
86
87
# File 'ext/base.rb', line 81

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

#check_architectureObject



102
103
104
105
106
107
108
109
110
111
112
# File 'ext/base.rb', line 102

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

#create_dummy_makefileObject



73
74
75
# File 'ext/base.rb', line 73

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

#download_archive(type) ⇒ Object



114
115
116
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
# File 'ext/base.rb', line 114

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_CERT_PATH,
          :proxy => proxy }
      ]
      if URI.respond_to?(:open) # rubocop:disable Style/GuardClause
        return URI.open(*args)
      else
        return open(*args)
      end
    rescue => error
      download_errors << "- URL: #{download_url}\n  Error: #{error.class}: #{error.message}"
      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.



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

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



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

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/PredicateName



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/PredicateName
  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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'ext/base.rb', line 192

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)


98
99
100
# File 'ext/base.rb', line 98

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



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
60
61
62
63
64
65
66
67
# File 'ext/base.rb', line 25

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



187
188
189
190
# File 'ext/base.rb', line 187

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



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

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

#try_http_proxy_value(value) ⇒ Object



217
218
219
# File 'ext/base.rb', line 217

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

#unarchive(archive) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
# File 'ext/base.rb', line 175

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/base.rb', line 160

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



69
70
71
# File 'ext/base.rb', line 69

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