Skip to content

Commit

Permalink
metric specs
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Feb 28, 2024
1 parent 3f51c15 commit c1217cd
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 21 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
# increment a simple counter
Sentry::Metrics.incr('button_click')
# set a value, unit and tags
Sentry::Metrics.incr('time', 5, 'second', tags: { browser:' firefox' })
Sentry::Metrics.incr('time', 5, unit: 'second', tags: { browser:' firefox' })
# distribution - get statistical aggregates from an array of observations
Sentry::Metrics.distribution('page_load', 15.0, 'millisecond')
Sentry::Metrics.distribution('page_load', 15.0, unit: 'millisecond')
# gauge - record statistical aggregates directly on the SDK, more space efficient
Sentry::Metrics.gauge('page_load', 15.0, 'millisecond')
Sentry::Metrics.gauge('page_load', 15.0, unit: 'millisecond')
# set - get unique counts of elements
Sentry::Metrics.set('user_view', 'jane')
Expand Down
18 changes: 8 additions & 10 deletions sentry-ruby/lib/sentry/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@
module Sentry
module Metrics
class << self
# TODO-neel-metrics define units, maybe symbols

def incr(key, value = 1.0, unit = 'none', tags: {}, timestamp: nil)
Sentry.metrics_aggregator&.add(:c, key, value, unit, tags: tags, timestamp: timestamp)
def incr(key, value = 1.0, unit: 'none', tags: {}, timestamp: nil)
Sentry.metrics_aggregator&.add(:c, key, value, unit: unit, tags: tags, timestamp: timestamp)
end

def distribution(key, value, unit = 'none', tags: {}, timestamp: nil)
Sentry.metrics_aggregator&.add(:d, key, value, unit, tags: tags, timestamp: timestamp)
def distribution(key, value, unit: 'none', tags: {}, timestamp: nil)
Sentry.metrics_aggregator&.add(:d, key, value, unit: unit, tags: tags, timestamp: timestamp)
end

def set(key, value, unit = 'none', tags: {}, timestamp: nil)
Sentry.metrics_aggregator&.add(:s, key, value, unit, tags: tags, timestamp: timestamp)
def set(key, value, unit: 'none', tags: {}, timestamp: nil)
Sentry.metrics_aggregator&.add(:s, key, value, unit: unit, tags: tags, timestamp: timestamp)
end

def gauge(key, value, unit = 'none', tags: {}, timestamp: nil)
Sentry.metrics_aggregator&.add(:g, key, value, unit, tags: tags, timestamp: timestamp)
def gauge(key, value, unit: 'none', tags: {}, timestamp: nil)
Sentry.metrics_aggregator&.add(:g, key, value, unit: unit, tags: tags, timestamp: timestamp)
end
end
end
Expand Down
18 changes: 10 additions & 8 deletions sentry-ruby/lib/sentry/metrics/aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ class Aggregator
s: SetMetric
}

# exposed only for testing
attr_reader :thread, :buckets, :flush_shift

def initialize(configuration, client)
@client = client
@logger = configuration.logger
@default_tags = { 'release' => configuration.release, 'environment' => configuration.environment }

@default_tags = {}
@default_tags['release'] = configuration.release if configuration.release
@default_tags['environment'] = configuration.environment if configuration.environment

@thread = nil
@exited = false
Expand All @@ -37,10 +43,11 @@ def initialize(configuration, client)
def add(type,
key,
value,
unit,
unit: 'none',
tags: {},
timestamp: nil)
return unless ensure_thread
return unless METRIC_TYPES.keys.include?(type)

timestamp = timestamp.to_i if timestamp.is_a?(Time)
timestamp ||= Sentry.utc_now.to_i
Expand All @@ -64,8 +71,6 @@ def add(type,
end

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

flushable_buckets = get_flushable_buckets!(force)
return if flushable_buckets.empty?

Expand All @@ -77,9 +82,6 @@ def flush(force: false)
is_json: false
)

log_debug("[Metrics::Aggregator] flushing buckets: #{flushable_buckets}")
log_debug("[Metrics::Aggregator] payload: #{payload}")

Sentry.background_worker.perform do
@client.transport.send_envelope(envelope)
end
Expand All @@ -100,7 +102,7 @@ def ensure_thread

@thread = Thread.new do
loop do
# TODO use event for force flush later
# TODO-neel-metrics use event for force flush later
sleep(FLUSH_INTERVAL)
flush

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L107 was not covered by tests
end
Expand Down
Loading

0 comments on commit c1217cd

Please sign in to comment.