Top Level Namespace

Defined Under Namespace

Modules: Appsignal Classes: AppsignalPumaPlugin, Object

Constant Summary collapse

EXT_PATH =
File.expand_path("..", __FILE__).freeze
AGENT_PLATFORM =
Appsignal::System.agent_platform
AGENT_ARCHITECTURE =
Appsignal::System.agent_architecture
TARGET_TRIPLE =
"#{AGENT_ARCHITECTURE}-#{AGENT_PLATFORM}".freeze
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" => "dee4fcb",
  "mirrors" => [
    "https://appsignal-agent-releases.global.ssl.fastly.net",
    "https://d135dj0rjqvssy.cloudfront.net"
  ],
  "triples" => {
    "x86_64-darwin" => {
      "static" => {
        "checksum" => "d45bfc2eb38138c317b501c3156459b8dbbb15a59910f1e2ec3d2d02e461a147",
        "filename" => "appsignal-x86_64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "a126901650046ae1c40affd386ab5dfcda5cb89f9bd05cf0a3087faff1d2d77d",
        "filename" => "appsignal-x86_64-darwin-all-dynamic.tar.gz"
      }
    },
    "universal-darwin" => {
      "static" => {
        "checksum" => "d45bfc2eb38138c317b501c3156459b8dbbb15a59910f1e2ec3d2d02e461a147",
        "filename" => "appsignal-x86_64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "a126901650046ae1c40affd386ab5dfcda5cb89f9bd05cf0a3087faff1d2d77d",
        "filename" => "appsignal-x86_64-darwin-all-dynamic.tar.gz"
      }
    },
    "aarch64-darwin" => {
      "static" => {
        "checksum" => "1cd875e74ba18c2bc81533437a9eebdf08f624e0201427c13326c2be00f22907",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "d2243146bf4e5839a4cf4430870b7b87d6553e1da4dc631c9fb396bba094cea4",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "arm64-darwin" => {
      "static" => {
        "checksum" => "1cd875e74ba18c2bc81533437a9eebdf08f624e0201427c13326c2be00f22907",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "d2243146bf4e5839a4cf4430870b7b87d6553e1da4dc631c9fb396bba094cea4",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "arm-darwin" => {
      "static" => {
        "checksum" => "1cd875e74ba18c2bc81533437a9eebdf08f624e0201427c13326c2be00f22907",
        "filename" => "appsignal-aarch64-darwin-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "d2243146bf4e5839a4cf4430870b7b87d6553e1da4dc631c9fb396bba094cea4",
        "filename" => "appsignal-aarch64-darwin-all-dynamic.tar.gz"
      }
    },
    "aarch64-linux" => {
      "static" => {
        "checksum" => "72fbfaa1c6ed72defea9eca59679372f53ddeab845a9b2f75d9d165e4983d69e",
        "filename" => "appsignal-aarch64-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "bf31b4836c84ceaa87a52aa85f296d40e32cfd784a8aa5b19ec5ca3a4aa5c8eb",
        "filename" => "appsignal-aarch64-linux-all-dynamic.tar.gz"
      }
    },
    "i686-linux" => {
      "static" => {
        "checksum" => "c504a7233512c9285274a7ba0142ab91cf4637c6f5f57c58c6890137ba0f82de",
        "filename" => "appsignal-i686-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "19091f50e98afed547b73bc220770bdafce737e152ab9345dabe03e284219cc1",
        "filename" => "appsignal-i686-linux-all-dynamic.tar.gz"
      }
    },
    "x86-linux" => {
      "static" => {
        "checksum" => "c504a7233512c9285274a7ba0142ab91cf4637c6f5f57c58c6890137ba0f82de",
        "filename" => "appsignal-i686-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "19091f50e98afed547b73bc220770bdafce737e152ab9345dabe03e284219cc1",
        "filename" => "appsignal-i686-linux-all-dynamic.tar.gz"
      }
    },
    "x86_64-linux" => {
      "static" => {
        "checksum" => "f9e68dbbee7d38b255c5b9bc0cb75226764f79cd51421e4e00a9e7454b0c6ccb",
        "filename" => "appsignal-x86_64-linux-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "58e3192c87903a2abce6540b893c9d25a69f43e37fd2367df45c2c22c26068ba",
        "filename" => "appsignal-x86_64-linux-all-dynamic.tar.gz"
      }
    },
    "x86_64-linux-musl" => {
      "static" => {
        "checksum" => "8fa1d110d2544502509cc66ba8ec6685010d8c6d8373f4f14ebe721d4be157e0",
        "filename" => "appsignal-x86_64-linux-musl-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "fc46dd29864165968d96bb78fde1c8970903488d80d97056e630636c736cf86f",
        "filename" => "appsignal-x86_64-linux-musl-all-dynamic.tar.gz"
      }
    },
    "aarch64-linux-musl" => {
      "static" => {
        "checksum" => "a43895183baf017879332885a904208773befc14b9bd628efae6d1513052b782",
        "filename" => "appsignal-aarch64-linux-musl-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "7e96b29348be655066cf162ae5c15248bba7d7177663e35e60a6c5ad243937d0",
        "filename" => "appsignal-aarch64-linux-musl-all-dynamic.tar.gz"
      }
    },
    "x86_64-freebsd" => {
      "static" => {
        "checksum" => "1b992f770bdbced09f7b5f18d7aa721710218d4a76a131adc7affe44d68684f0",
        "filename" => "appsignal-x86_64-freebsd-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "300b718986c10ff65f237e1d74086e9cc0f7079d7b531e3b6f9a64752f1aa6ee",
        "filename" => "appsignal-x86_64-freebsd-all-dynamic.tar.gz"
      }
    },
    "amd64-freebsd" => {
      "static" => {
        "checksum" => "1b992f770bdbced09f7b5f18d7aa721710218d4a76a131adc7affe44d68684f0",
        "filename" => "appsignal-x86_64-freebsd-all-static.tar.gz"
      },
      "dynamic" => {
        "checksum" => "300b718986c10ff65f237e1d74086e9cc0f7079d7b531e3b6f9a64752f1aa6ee",
        "filename" => "appsignal-x86_64-freebsd-all-dynamic.tar.gz"
      }
    }
  }
}.freeze

Instance Method Summary collapse

Instance Method Details

#abort_installation(reason) ⇒ Object



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

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

#check_architectureObject



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

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 support@appsignal.com, we aim to support everything our customers run."
    )
  end
end

#create_dummy_makefileObject



70
71
72
73
74
# File 'ext/base.rb', line 70

def create_dummy_makefile
  File.open(File.join(EXT_PATH, "Makefile"), "w") do |file|
    file.write "default:\nclean:\ninstall:"
  end
end

#download_archive(type) ⇒ Object



112
113
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
# File 'ext/base.rb', line 112

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



19
20
21
# File 'ext/base.rb', line 19

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.



219
220
221
222
223
# File 'ext/base.rb', line 219

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



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

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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'ext/extconf.rb', line 77

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

def http_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["http_proxy"])
  return [proxy, error] if proxy

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

  [nil, error]
end

#installObject



9
10
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
# File 'ext/extconf.rb', line 9

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)


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

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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'ext/extconf.rb', line 61

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)


3
4
5
6
7
# File 'ext/extconf.rb', line 3

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

#reportObject



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
60
61
62
# File 'ext/base.rb', line 23

def report
  @report ||=
    begin
      rbconfig = RbConfig::CONFIG
      {
        "result" => {
          "status" => "incomplete"
        },
        "language" => {
          "name" => "ruby",
          "version" => "#{rbconfig["RUBY_PROGRAM_VERSION"]}-p#{rbconfig["PATCHLEVEL"]}"
        },
        "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



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

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

#try_http_proxy_value(value) ⇒ Object



214
215
216
# File 'ext/base.rb', line 214

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

#unarchive(archive) ⇒ Object



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

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

      File.open(ext_path(entry.full_name), "wb") do |f|
        f.write(entry.read)
      end
    end
  end
  store_download_version_on_report
  FileUtils.chmod(0o755, ext_path("appsignal-agent"))
end

#verify_archive(archive, type) ⇒ Object



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

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



64
65
66
67
68
# File 'ext/base.rb', line 64

def write_report
  File.open(File.join(EXT_PATH, "install.report"), "w") do |file|
    file.write JSON.generate(report)
  end
end