|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Sentry |
| 4 | + module Metrics |
| 5 | + class Aggregator |
| 6 | + include LoggingHelper |
| 7 | + |
| 8 | + FLUSH_INTERVAL = 5 |
| 9 | + ROLLUP_IN_SECONDS = 10 |
| 10 | + |
| 11 | + KEY_SANITIZATION_REGEX = /[^a-zA-Z0-9_\/.-]+/ |
| 12 | + VALUE_SANITIZATION_REGEX = /[^[[:word:]][[:digit:]][[:space:]]_:\/@\.{}\[\]$-]+/ |
| 13 | + |
| 14 | + METRIC_TYPES = { |
| 15 | + c: CounterMetric, |
| 16 | + d: DistributionMetric, |
| 17 | + g: GaugeMetric, |
| 18 | + s: SetMetric |
| 19 | + } |
| 20 | + |
| 21 | + # exposed only for testing |
| 22 | + attr_reader :thread, :buckets, :flush_shift |
| 23 | + |
| 24 | + def initialize(configuration, client) |
| 25 | + @client = client |
| 26 | + @logger = configuration.logger |
| 27 | + |
| 28 | + @default_tags = {} |
| 29 | + @default_tags['release'] = configuration.release if configuration.release |
| 30 | + @default_tags['environment'] = configuration.environment if configuration.environment |
| 31 | + |
| 32 | + @thread = nil |
| 33 | + @exited = false |
| 34 | + @mutex = Mutex.new |
| 35 | + |
| 36 | + # buckets are a nested hash of timestamp -> bucket keys -> Metric instance |
| 37 | + @buckets = {} |
| 38 | + |
| 39 | + # the flush interval needs to be shifted once per startup to create jittering |
| 40 | + @flush_shift = Random.rand * ROLLUP_IN_SECONDS |
| 41 | + end |
| 42 | + |
| 43 | + def add(type, |
| 44 | + key, |
| 45 | + value, |
| 46 | + unit: 'none', |
| 47 | + tags: {}, |
| 48 | + timestamp: nil) |
| 49 | + return unless ensure_thread |
| 50 | + return unless METRIC_TYPES.keys.include?(type) |
| 51 | + |
| 52 | + timestamp = timestamp.to_i if timestamp.is_a?(Time) |
| 53 | + timestamp ||= Sentry.utc_now.to_i |
| 54 | + |
| 55 | + # this is integer division and thus takes the floor of the division |
| 56 | + # and buckets into 10 second intervals |
| 57 | + bucket_timestamp = (timestamp / ROLLUP_IN_SECONDS) * ROLLUP_IN_SECONDS |
| 58 | + |
| 59 | + serialized_tags = serialize_tags(get_updated_tags(tags)) |
| 60 | + bucket_key = [type, key, unit, serialized_tags] |
| 61 | + |
| 62 | + @mutex.synchronize do |
| 63 | + @buckets[bucket_timestamp] ||= {} |
| 64 | + |
| 65 | + if @buckets[bucket_timestamp][bucket_key] |
| 66 | + @buckets[bucket_timestamp][bucket_key].add(value) |
| 67 | + else |
| 68 | + @buckets[bucket_timestamp][bucket_key] = METRIC_TYPES[type].new(value) |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + def flush(force: false) |
| 74 | + flushable_buckets = get_flushable_buckets!(force) |
| 75 | + return if flushable_buckets.empty? |
| 76 | + |
| 77 | + payload = serialize_buckets(flushable_buckets) |
| 78 | + envelope = Envelope.new |
| 79 | + envelope.add_item( |
| 80 | + { type: 'statsd', length: payload.bytesize }, |
| 81 | + payload |
| 82 | + ) |
| 83 | + |
| 84 | + Sentry.background_worker.perform do |
| 85 | + @client.transport.send_envelope(envelope) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + def kill |
| 90 | + log_debug('[Metrics::Aggregator] killing thread') |
| 91 | + |
| 92 | + @exited = true |
| 93 | + @thread&.kill |
| 94 | + end |
| 95 | + |
| 96 | + private |
| 97 | + |
| 98 | + def ensure_thread |
| 99 | + return false if @exited |
| 100 | + return true if @thread&.alive? |
| 101 | + |
| 102 | + @thread = Thread.new do |
| 103 | + loop do |
| 104 | + # TODO-neel-metrics use event for force flush later |
| 105 | + sleep(FLUSH_INTERVAL) |
| 106 | + flush |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + true |
| 111 | + rescue ThreadError |
| 112 | + log_debug('[Metrics::Aggregator] thread creation failed') |
| 113 | + @exited = true |
| 114 | + false |
| 115 | + end |
| 116 | + |
| 117 | + # important to sort for key consistency |
| 118 | + def serialize_tags(tags) |
| 119 | + tags.flat_map do |k, v| |
| 120 | + if v.is_a?(Array) |
| 121 | + v.map { |x| [k.to_s, x.to_s] } |
| 122 | + else |
| 123 | + [[k.to_s, v.to_s]] |
| 124 | + end |
| 125 | + end.sort |
| 126 | + end |
| 127 | + |
| 128 | + def get_flushable_buckets!(force) |
| 129 | + @mutex.synchronize do |
| 130 | + flushable_buckets = {} |
| 131 | + |
| 132 | + if force |
| 133 | + flushable_buckets = @buckets |
| 134 | + @buckets = {} |
| 135 | + else |
| 136 | + cutoff = Sentry.utc_now.to_i - ROLLUP_IN_SECONDS - @flush_shift |
| 137 | + flushable_buckets = @buckets.select { |k, _| k <= cutoff } |
| 138 | + @buckets.reject! { |k, _| k <= cutoff } |
| 139 | + end |
| 140 | + |
| 141 | + flushable_buckets |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + # serialize buckets to statsd format |
| 146 | + def serialize_buckets(buckets) |
| 147 | + buckets.map do |timestamp, timestamp_buckets| |
| 148 | + timestamp_buckets.map do |metric_key, metric| |
| 149 | + type, key, unit, tags = metric_key |
| 150 | + values = metric.serialize.join(':') |
| 151 | + sanitized_tags = tags.map { |k, v| "#{sanitize_key(k)}:#{sanitize_value(v)}" }.join(',') |
| 152 | + |
| 153 | + "#{sanitize_key(key)}@#{unit}:#{values}|#{type}|\##{sanitized_tags}|T#{timestamp}" |
| 154 | + end |
| 155 | + end.flatten.join("\n") |
| 156 | + end |
| 157 | + |
| 158 | + def sanitize_key(key) |
| 159 | + key.gsub(KEY_SANITIZATION_REGEX, '_') |
| 160 | + end |
| 161 | + |
| 162 | + def sanitize_value(value) |
| 163 | + value.gsub(VALUE_SANITIZATION_REGEX, '') |
| 164 | + end |
| 165 | + |
| 166 | + def get_transaction_name |
| 167 | + scope = Sentry.get_current_scope |
| 168 | + return nil unless scope && scope.transaction_name |
| 169 | + return nil if scope.transaction_source_low_quality? |
| 170 | + |
| 171 | + scope.transaction_name |
| 172 | + end |
| 173 | + |
| 174 | + def get_updated_tags(tags) |
| 175 | + updated_tags = @default_tags.merge(tags) |
| 176 | + |
| 177 | + transaction_name = get_transaction_name |
| 178 | + updated_tags['transaction'] = transaction_name if transaction_name |
| 179 | + |
| 180 | + updated_tags |
| 181 | + end |
| 182 | + end |
| 183 | + end |
| 184 | +end |
0 commit comments