Skip to content

Commit

Permalink
fix: Change appsignal info method based presence
Browse files Browse the repository at this point in the history
  • Loading branch information
KoenSengers committed Feb 2, 2024
1 parent 116e699 commit dd17fd7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ jobs:
fail-fast: false
matrix:
ruby:
- 3.0
- 3.1
- 3.2
- 3.3

runs-on: ubuntu-latest
steps:
Expand Down
16 changes: 16 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require:
- rubocop-performance

AllCops:
NewCops: enable
TargetRubyVersion: 3.1

Style/Documentation:
Enabled: false

Gemspec/DevelopmentDependencies:
Enabled: false

Naming/FileName:
Exclude:
- 'lib/appsignal-sourcemap.rb'
3 changes: 2 additions & 1 deletion appsignal-sourcemap.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Gem::Specification.new do |spec|

spec.metadata["allowed_push_host"] = "https://rubygems.pkg.github.com/Drieam"

spec.required_ruby_version = ">= 2.7"
spec.required_ruby_version = ">= 3.1"

spec.files = Dir["lib/**/*", "README.md"]

spec.add_dependency "appsignal", "~> 3.0"
spec.add_dependency "parallel", "~> 1.0"

spec.add_development_dependency "standard"
spec.metadata["rubygems_mfa_required"] = "true"
end
14 changes: 14 additions & 0 deletions lib/appsignal/sourcemap/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Appsignal
module Sourcemap
class Base
private

def log_appsignal(message, type = "info")
log_method = Appsignal.respond_to?(:internal_logger) ? "internal_logger" : "logger"
Appsignal.send(log_method).send(type, message)
end
end
end
end
14 changes: 6 additions & 8 deletions lib/appsignal/sourcemap/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Appsignal
module Sourcemap
class Supervisor
class Supervisor < Base
PARALLEL_THREADS = 10

def self.start
Expand All @@ -15,27 +15,25 @@ def self.start
def start
return if invalid_preconditions

Appsignal.logger.info("Starting sourcemaps upload")
log_appsignal("Starting sourcemaps upload")

Parallel.each(source_map_paths, in_threads: PARALLEL_THREADS) do |source_map_path|
Uploader.upload(source_map_path)
end

Appsignal.logger.info("Finished sourcemaps upload")
log_appsignal("Finished sourcemaps upload")
end

private

def invalid_preconditions
unless Appsignal.config.valid?
return Appsignal.logger.error("Skipping sourcemaps upload since Appsignal config is invalid")
return log_appsignal("Skipping sourcemaps upload since Appsignal config is invalid", "error")
end
if asset_host.blank?
return Appsignal.logger.error("Skipping sourcemaps upload since Rails asset_host is not set")
end
if source_map_paths.empty?
return Appsignal.logger.info("Skipping sourcemaps upload since no javascript maps are found")
return log_appsignal("Skipping sourcemaps upload since Rails asset_host is not set", "error")
end
return log_appsignal("Skipping sourcemaps upload since no javascript maps are found") if source_map_paths.empty?

false
end
Expand Down
19 changes: 12 additions & 7 deletions lib/appsignal/sourcemap/uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Appsignal
module Sourcemap
class Uploader
class Uploader < Base
UPLOAD_URI = URI("https://appsignal.com/api/sourcemaps")

def self.upload(sourcemap_path)
Expand All @@ -11,31 +11,36 @@ def self.upload(sourcemap_path)

def initialize(sourcemap_path)
@sourcemap_path = sourcemap_path
super
end

def upload # rubocop:disable Metrics/AbcSize
Appsignal.logger.debug "Starting sourcemap upload '#{@sourcemap_path}' with parameters: #{request_form_data}"
def upload
log_appsignal("Starting sourcemap upload '#{@sourcemap_path}' with parameters: #{request_form_data}", "debug")

response = Net::HTTP.start(UPLOAD_URI.hostname, UPLOAD_URI.port, use_ssl: true) do |http|
http.request(request)
end

if response.is_a?(Net::HTTPSuccess)
Appsignal.logger.debug("Finished sourcemap upload '#{@sourcemap_path}'")
log_appsignal("Finished sourcemap upload '#{@sourcemap_path}'", "debug")
File.delete(sourcemap_full_path)
return
end

Appsignal.logger.error <<~MESSAGE
log_appsignal(error_message(response), "error")
end

private

def error_message(response)
<<~MESSAGE
Uploading sourcemap #{@sourcemap_path} failed with message '#{response.message}'.
Response: #{response.body}
MESSAGE
end

private

def sourcemap_full_path
Rails.public_path.join(@sourcemap_path)
end
Expand Down

0 comments on commit dd17fd7

Please sign in to comment.