Skip to content

Commit

Permalink
config and apis
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Feb 21, 2024
1 parent ee59473 commit e59667e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
13 changes: 13 additions & 0 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require "sentry/session_flusher"
require "sentry/backpressure_monitor"
require "sentry/cron/monitor_check_ins"
require "sentry/metrics"

[
"sentry/rake",
Expand Down Expand Up @@ -77,6 +78,10 @@ def exception_locals_tp
# @return [BackpressureMonitor, nil]
attr_reader :backpressure_monitor

# @!attribute [r] metrics_aggregator
# @return [Metrics::Aggregator, nil]
attr_reader :metrics_aggregator

##### Patch Registration #####

# @!visibility private
Expand Down Expand Up @@ -224,6 +229,7 @@ def init(&block)
@background_worker = Sentry::BackgroundWorker.new(config)
@session_flusher = config.session_tracking? ? Sentry::SessionFlusher.new(config, client) : nil
@backpressure_monitor = config.enable_backpressure_handling ? Sentry::BackpressureMonitor.new(config, client) : nil
@metrics_aggregator = config.enable_metrics ? Sentry::Metrics::Aggregator.new(config, client) : nil
exception_locals_tp.enable if config.include_local_variables
at_exit { close }
end
Expand All @@ -244,6 +250,13 @@ def close
@backpressure_monitor = nil
end

if @metrics_aggregator
# TODO-neel-metrics force flush?
@metrics_aggregator.flush(force: true)
@metrics_aggregator.kill
@metrics_aggregator = nil

Check warning on line 257 in sentry-ruby/lib/sentry-ruby.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry-ruby.rb#L255-L257

Added lines #L255 - L257 were not covered by tests
end

if client = get_current_client
client.transport.flush

Expand Down
7 changes: 7 additions & 0 deletions sentry-ruby/lib/sentry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ def capture_exception_frame_locals=(value)
# @return [Boolean, nil]
attr_reader :enable_tracing

# Enable metrics usage
# Starts a new {Sentry::Metrics::Aggregator} instance to aggregate metrics
# and a thread to aggregate flush every 5 seconds.
# @return [Boolean]
attr_accessor :enable_metrics

# Send diagnostic client reports about dropped events, true by default
# tries to attach to an existing envelope max once every 30s
# @return [Boolean]
Expand Down Expand Up @@ -383,6 +389,7 @@ def initialize
self.rack_env_whitelist = RACK_ENV_WHITELIST_DEFAULT
self.traces_sampler = nil
self.enable_tracing = nil
self.enable_metrics = false

@transport = Transport::Configuration.new
@cron = Cron::Configuration.new
Expand Down
25 changes: 24 additions & 1 deletion sentry-ruby/lib/sentry/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,35 @@
require 'sentry/metrics/distribution_metric'
require 'sentry/metrics/gauge_metric'
require 'sentry/metrics/set_metric'
require 'sentry/metrics/aggregator'

module Sentry
module Metrics
class << self
# TODO-neel-metrics define units, maybe symbols
def incr(key, value: 1.0, unit: 'none', tags: nil, timestamp: nil)
#
def incr(key, value = 1.0, unit = 'none', tags: {}, timestamp: nil)
return unless Sentry.metrics_aggregator

Check warning on line 16 in sentry-ruby/lib/sentry/metrics.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics.rb#L16

Added line #L16 was not covered by tests

Sentry.metrics_aggregator.add(:c, key, value, unit, tags: tags, timestamp: timestamp)

Check warning on line 18 in sentry-ruby/lib/sentry/metrics.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics.rb#L18

Added line #L18 was not covered by tests
end

def distribution(key, value, unit = 'none', tags: {}, timestamp: nil)
return unless Sentry.metrics_aggregator

Check warning on line 22 in sentry-ruby/lib/sentry/metrics.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics.rb#L22

Added line #L22 was not covered by tests

Sentry.metrics_aggregator.add(:d, key, value, unit, tags: tags, timestamp: timestamp)

Check warning on line 24 in sentry-ruby/lib/sentry/metrics.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics.rb#L24

Added line #L24 was not covered by tests
end

def set(key, value, unit = 'none', tags: {}, timestamp: nil)
return unless Sentry.metrics_aggregator

Check warning on line 28 in sentry-ruby/lib/sentry/metrics.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics.rb#L28

Added line #L28 was not covered by tests

Sentry.metrics_aggregator.add(:s, key, value, unit, tags: tags, timestamp: timestamp)

Check warning on line 30 in sentry-ruby/lib/sentry/metrics.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics.rb#L30

Added line #L30 was not covered by tests
end

def gauge(key, value, unit = 'none', tags: {}, timestamp: nil)
return unless Sentry.metrics_aggregator

Check warning on line 34 in sentry-ruby/lib/sentry/metrics.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics.rb#L34

Added line #L34 was not covered by tests

Sentry.metrics_aggregator.add(:g, key, value, unit, tags: tags, timestamp: timestamp)

Check warning on line 36 in sentry-ruby/lib/sentry/metrics.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics.rb#L36

Added line #L36 was not covered by tests
end
end
end
Expand Down
13 changes: 5 additions & 8 deletions sentry-ruby/lib/sentry/metrics/aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class Aggregator
def initialize(configuration, client)
@client = client
@logger = configuration.logger
@release = configuration.release
@environment = configuration.environment
@default_tags = { 'release' => @release, 'environment' => @environment }

Check warning on line 21 in sentry-ruby/lib/sentry/metrics/aggregator.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics/aggregator.rb#L19-L21

Added lines #L19 - L21 were not covered by tests

@thread = nil
@exited = false
Expand All @@ -36,7 +35,7 @@ def add(type,
key,
value,
unit,
tags: nil,
tags: {},
timestamp: nil)
return unless ensure_thread

Check warning on line 40 in sentry-ruby/lib/sentry/metrics/aggregator.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics/aggregator.rb#L40

Added line #L40 was not covered by tests

Expand All @@ -47,7 +46,7 @@ def add(type,
# and buckets into 10 second intervals
bucket_timestamp = (timestamp / ROLLUP_IN_SECONDS) * ROLLUP_IN_SECONDS

Check warning on line 47 in sentry-ruby/lib/sentry/metrics/aggregator.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics/aggregator.rb#L47

Added line #L47 was not covered by tests

serialized_tags = serialize_tags(tags)
serialized_tags = serialize_tags(tags.merge(@default_tags))
bucket_key = [type, key, unit, serialized_tags]

Check warning on line 50 in sentry-ruby/lib/sentry/metrics/aggregator.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics/aggregator.rb#L49-L50

Added lines #L49 - L50 were not covered by tests

@mutex.synchronize do
Expand All @@ -61,7 +60,7 @@ def add(type,
end
end

def flush
def flush(force: false)
@mutex.synchronize do
log_debug("[Metrics::Aggregator] current bucket state: #{@buckets}")

Check warning on line 65 in sentry-ruby/lib/sentry/metrics/aggregator.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics/aggregator.rb#L64-L65

Added lines #L64 - L65 were not covered by tests
# TODO
Expand Down Expand Up @@ -96,10 +95,8 @@ def ensure_thread
false

Check warning on line 95 in sentry-ruby/lib/sentry/metrics/aggregator.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics/aggregator.rb#L93-L95

Added lines #L93 - L95 were not covered by tests
end

# important to sort for key consistency
def serialize_tags(tags)
return [] unless tags

# important to sort for key consistency
tags.map do |k, v|
if v.is_a?(Array)
v.map { |x| [k.to_s, x.to_s] }

Check warning on line 102 in sentry-ruby/lib/sentry/metrics/aggregator.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/metrics/aggregator.rb#L100-L102

Added lines #L100 - L102 were not covered by tests
Expand Down

0 comments on commit e59667e

Please sign in to comment.