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 publish` family of tasks in the appsignal-agent repository. Modifications to this file will be overwritten with the next agent release.

{
  "version" => "0.35.29",
  "mirrors" => [
    "https://d135dj0rjqvssy.cloudfront.net",
    "https://appsignal-agent-releases.global.ssl.fastly.net"
  ],
  "triples" => {
    "x86_64-darwin" => {
      "static" => {
        "checksum" => "880b317fc23d3cfa11ba88c80d11129bd02742b8b9c100bb038b66e73f85b723",
        "filename" => "appsignal-x86_64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "0b9503e12862fb4824d78b1388bc4f51a9821f5c45ff78ae0782c2200632eac9",
        "filename" => "appsignal-x86_64-darwin-all-dynamic.tar.gz"
      }
    },
    "universal-darwin" => {
      "static" => {
        "checksum" => "880b317fc23d3cfa11ba88c80d11129bd02742b8b9c100bb038b66e73f85b723",
        "filename" => "appsignal-x86_64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "0b9503e12862fb4824d78b1388bc4f51a9821f5c45ff78ae0782c2200632eac9",
        "filename" => "appsignal-x86_64-darwin-all-dynamic.tar.gz"
      }
    },
    "aarch64-darwin" => {
      "static" => {
        "checksum" => "64b0107722401f5ee39eebec03b9c5a68a14967e8aa8806848df930f85a8afaf",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "3c1e8da7ce62cfd72b3f83c212baf8ddcf3998b8c37fdcb990d1fc9397488428",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "arm64-darwin" => {
      "static" => {
        "checksum" => "64b0107722401f5ee39eebec03b9c5a68a14967e8aa8806848df930f85a8afaf",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "3c1e8da7ce62cfd72b3f83c212baf8ddcf3998b8c37fdcb990d1fc9397488428",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "arm-darwin" => {
      "static" => {
        "checksum" => "64b0107722401f5ee39eebec03b9c5a68a14967e8aa8806848df930f85a8afaf",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "3c1e8da7ce62cfd72b3f83c212baf8ddcf3998b8c37fdcb990d1fc9397488428",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "aarch64-linux" => {
      "static" => {
        "checksum" => "02dd40769daa5cde64dfee9e0931d0432c4ccffeb6c08197cccc454234fdae2c",
        "filename" => "appsignal-aarch64-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "1eaeed41ec98cb930454944cf27d45e5bb35805d303503ea00f59dbaccce7c37",
        "filename" => "appsignal-aarch64-linux-all-dynamic.tar.gz"
      }
    },
    "i686-linux" => {
      "static" => {
        "checksum" => "4e19e1db93add17a71aaf2fd14ddf4cbd6913338f8ebeb0569baa59e154e8999",
        "filename" => "appsignal-i686-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "df508739e7f67824566e6388966c1f88df1b835abc519cb1f3c8297dd6ff1fe2",
        "filename" => "appsignal-i686-linux-all-dynamic.tar.gz"
      }
    },
    "x86-linux" => {
      "static" => {
        "checksum" => "4e19e1db93add17a71aaf2fd14ddf4cbd6913338f8ebeb0569baa59e154e8999",
        "filename" => "appsignal-i686-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "df508739e7f67824566e6388966c1f88df1b835abc519cb1f3c8297dd6ff1fe2",
        "filename" => "appsignal-i686-linux-all-dynamic.tar.gz"
      }
    },
    "x86_64-linux" => {
      "static" => {
        "checksum" => "1150865e4a9b6d773a10702414b9b0b7cc69a72c0cbb17f5a01cebf40cabbcc4",
        "filename" => "appsignal-x86_64-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "b17ed8c85171638f14d0ff3bca9d83894a9d2966da0a4554ba9b6cd1197a0a54",
        "filename" => "appsignal-x86_64-linux-all-dynamic.tar.gz"
      }
    },
    "x86_64-linux-musl" => {
      "static" => {
        "checksum" => "86fe06ca6dcc93e68a9c603c9087f15b0cef213f4df0eab6c0b495034045cde0",
        "filename" => "appsignal-x86_64-linux-musl-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "6b68164b768fda589390d34f3c20ac7fe19c7a64a864af723fad52abf981492b",
        "filename" => "appsignal-x86_64-linux-musl-all-dynamic.tar.gz"
      }
    },
    "aarch64-linux-musl" => {
      "static" => {
        "checksum" => "a3b2e4eb3a32408cbbc5b0a12b1d61322378ce0dc30edfb1c541a43d0c5306ff",
        "filename" => "appsignal-aarch64-linux-musl-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "72163a268416a7171e69bd68f3cf4b732989107eb25b9a910618cc66079a1c87",
        "filename" => "appsignal-aarch64-linux-musl-all-dynamic.tar.gz"
      }
    },
    "x86_64-freebsd" => {
      "static" => {
        "checksum" => "a4982fab5a7b4a4292bd0002e3bc571cbeced167c6a3e36f6c26e7898b3d38a7",
        "filename" => "appsignal-x86_64-freebsd-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "0dd1afc8e0896b87ce76b65358806e0b89213ffb8727a50d926c987978c5e1ee",
        "filename" => "appsignal-x86_64-freebsd-all-dynamic.tar.gz"
      }
    },
    "amd64-freebsd" => {
      "static" => {
        "checksum" => "a4982fab5a7b4a4292bd0002e3bc571cbeced167c6a3e36f6c26e7898b3d38a7",
        "filename" => "appsignal-x86_64-freebsd-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "0dd1afc8e0896b87ce76b65358806e0b89213ffb8727a50d926c987978c5e1ee",
        "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