Skip to content

Commit 20667ee

Browse files
authored
Deprecate metrics (#2726)
1 parent 2c8c2c1 commit 20667ee

File tree

6 files changed

+108
-6
lines changed

6 files changed

+108
-6
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@
44

55
- Auto-enable Rails structured logging when `enable_logs` is true ([#2721](https://github.com/getsentry/sentry-ruby/pull/2721))
66

7+
### Miscellaneous
8+
9+
- Deprecate all Metrics related APIs [#2726](https://github.com/getsentry/sentry-ruby/pull/2726)
10+
11+
Sentry [no longer has the Metrics Beta offering](https://sentry.zendesk.com/hc/en-us/articles/26369339769883-Metrics-Beta-Ended-on-October-7th) so
12+
all the following APIs linked to Metrics have been deprecated and will be removed in the next major.
13+
14+
```ruby
15+
Sentry.init do |config|
16+
# ...
17+
config.metrics.enabled = true
18+
config.metrics.enable_code_locations = true
19+
config.metrics.before_emit = lambda {}
20+
end
21+
22+
Sentry::Metrics.increment('button_click')
23+
Sentry::Metrics.distribution('page_load', 15.0, unit: 'millisecond')
24+
Sentry::Metrics.gauge('page_load', 15.0, unit: 'millisecond')
25+
Sentry::Metrics.set('user_view', 'jane')
26+
Sentry::Metrics.timing('how_long') { sleep(1) }
27+
```
28+
729
### Internal
830

931
- Fix leftover `config.logger` call in `graphql` patch ([#2722](https://github.com/getsentry/sentry-ruby/2722)

sentry-ruby/lib/sentry/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def initialize
512512

513513
@transport = Transport::Configuration.new
514514
@cron = Cron::Configuration.new
515-
@metrics = Metrics::Configuration.new
515+
@metrics = Metrics::Configuration.new(self.sdk_logger)
516516
@structured_logging = StructuredLoggingConfiguration.new
517517
@gem_specs = Hash[Gem::Specification.map { |spec| [spec.name, spec.version.to_s] }] if Gem::Specification.respond_to?(:map)
518518

sentry-ruby/lib/sentry/metrics.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,28 @@ module Metrics
1919

2020
class << self
2121
def increment(key, value = 1.0, unit: "none", tags: {}, timestamp: nil)
22+
log_deprecation
2223
Sentry.metrics_aggregator&.add(:c, key, value, unit: unit, tags: tags, timestamp: timestamp)
2324
end
2425

2526
def distribution(key, value, unit: "none", tags: {}, timestamp: nil)
27+
log_deprecation
2628
Sentry.metrics_aggregator&.add(:d, key, value, unit: unit, tags: tags, timestamp: timestamp)
2729
end
2830

2931
def set(key, value, unit: "none", tags: {}, timestamp: nil)
32+
log_deprecation
3033
Sentry.metrics_aggregator&.add(:s, key, value, unit: unit, tags: tags, timestamp: timestamp)
3134
end
3235

3336
def gauge(key, value, unit: "none", tags: {}, timestamp: nil)
37+
log_deprecation
3438
Sentry.metrics_aggregator&.add(:g, key, value, unit: unit, tags: tags, timestamp: timestamp)
3539
end
3640

3741
def timing(key, unit: "second", tags: {}, timestamp: nil, &block)
42+
log_deprecation
43+
3844
return unless block_given?
3945
return yield unless DURATION_UNITS.include?(unit)
4046

@@ -51,6 +57,12 @@ def timing(key, unit: "second", tags: {}, timestamp: nil, &block)
5157
Sentry.metrics_aggregator&.add(:d, key, value, unit: unit, tags: tags, timestamp: timestamp)
5258
result
5359
end
60+
61+
def log_deprecation
62+
Sentry.sdk_logger.warn(LOGGER_PROGNAME) do
63+
"`Sentry::Metrics` is now deprecated and will be removed in the next major."
64+
end
65+
end
5466
end
5567
end
5668
end

sentry-ruby/lib/sentry/metrics/configuration.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ module Sentry
44
module Metrics
55
class Configuration
66
include ArgumentCheckingHelper
7+
include LoggingHelper
78

89
# Enable metrics usage.
910
# Starts a new {Sentry::Metrics::Aggregator} instance to aggregate metrics
1011
# and a thread to aggregate flush every 5 seconds.
1112
# @return [Boolean]
12-
attr_accessor :enabled
13+
attr_reader :enabled
1314

1415
# Enable code location reporting.
1516
# Will be sent once per day.
@@ -32,11 +33,20 @@ class Configuration
3233
# @return [Proc, nil]
3334
attr_reader :before_emit
3435

35-
def initialize
36+
def initialize(sdk_logger)
37+
@sdk_logger = sdk_logger
3638
@enabled = false
3739
@enable_code_locations = true
3840
end
3941

42+
def enabled=(value)
43+
log_warn <<~MSG
44+
`config.metrics` is now deprecated and will be removed in the next major.
45+
MSG
46+
47+
@enabled = value
48+
end
49+
4050
def before_emit=(value)
4151
check_callable!("metrics.before_emit", value)
4252

sentry-ruby/spec/sentry/metrics/configuration_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Sentry::Metrics::Configuration do
4+
let(:string_io) { StringIO.new }
5+
let(:sdk_logger) { Logger.new(string_io) }
6+
7+
let(:subject) { described_class.new(sdk_logger) }
8+
9+
describe '#enabled=' do
10+
it 'logs deprecation warning' do
11+
subject.enabled = true
12+
13+
expect(string_io.string).to include(
14+
"WARN -- sentry: `config.metrics` is now deprecated and will be removed in the next major."
15+
)
16+
end
17+
end
18+
419
describe '#before_emit=' do
520
it 'raises error when setting before_emit to anything other than callable or nil' do
621
subject.before_emit = -> { }

sentry-ruby/spec/sentry/metrics_spec.rb

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Sentry::Metrics do
4+
let(:aggregator) { Sentry.metrics_aggregator }
5+
let(:fake_time) { Time.new(2024, 1, 1, 1, 1, 3) }
6+
let(:string_io) { StringIO.new }
7+
let(:sdk_logger) { Logger.new(string_io) }
8+
49
before do
510
perform_basic_setup do |config|
611
config.metrics.enabled = true
12+
config.sdk_logger = sdk_logger
713
end
814
end
915

10-
let(:aggregator) { Sentry.metrics_aggregator }
11-
let(:fake_time) { Time.new(2024, 1, 1, 1, 1, 3) }
12-
1316
describe '.increment' do
1417
it 'passes default value of 1.0 with only key' do
1518
expect(aggregator).to receive(:add).with(
@@ -36,6 +39,14 @@
3639

3740
described_class.increment('foo', 5.0, unit: 'second', tags: { fortytwo: 42 }, timestamp: fake_time)
3841
end
42+
43+
it 'logs deprecation warning' do
44+
described_class.increment('foo')
45+
46+
expect(string_io.string).to include(
47+
"WARN -- sentry: `Sentry::Metrics` is now deprecated and will be removed in the next major."
48+
)
49+
end
3950
end
4051

4152
describe '.distribution' do
@@ -51,6 +62,14 @@
5162

5263
described_class.distribution('foo', 5.0, unit: 'second', tags: { fortytwo: 42 }, timestamp: fake_time)
5364
end
65+
66+
it 'logs deprecation warning' do
67+
described_class.distribution('foo', 5.0, unit: 'second', tags: { fortytwo: 42 }, timestamp: fake_time)
68+
69+
expect(string_io.string).to include(
70+
"WARN -- sentry: `Sentry::Metrics` is now deprecated and will be removed in the next major."
71+
)
72+
end
5473
end
5574

5675
describe '.set' do
@@ -66,6 +85,14 @@
6685

6786
described_class.set('foo', 'jane', tags: { fortytwo: 42 }, timestamp: fake_time)
6887
end
88+
89+
it 'logs deprecation warning' do
90+
described_class.set('foo', 'jane', tags: { fortytwo: 42 }, timestamp: fake_time)
91+
92+
expect(string_io.string).to include(
93+
"WARN -- sentry: `Sentry::Metrics` is now deprecated and will be removed in the next major."
94+
)
95+
end
6996
end
7097

7198
describe '.gauge' do
@@ -81,6 +108,14 @@
81108

82109
described_class.gauge('foo', 5.0, unit: 'second', tags: { fortytwo: 42 }, timestamp: fake_time)
83110
end
111+
112+
it 'logs deprecation warning' do
113+
described_class.gauge('foo', 5.0, unit: 'second', tags: { fortytwo: 42 }, timestamp: fake_time)
114+
115+
expect(string_io.string).to include(
116+
"WARN -- sentry: `Sentry::Metrics` is now deprecated and will be removed in the next major."
117+
)
118+
end
84119
end
85120

86121
describe '.timing' do
@@ -109,6 +144,14 @@
109144
expect(result).to eq(42)
110145
end
111146

147+
it 'logs deprecation warning' do
148+
described_class.timing('foo', unit: 'millisecond', tags: { fortytwo: 42 }, timestamp: fake_time) { sleep(0.1); 42 }
149+
150+
expect(string_io.string).to include(
151+
"WARN -- sentry: `Sentry::Metrics` is now deprecated and will be removed in the next major."
152+
)
153+
end
154+
112155
context 'with running transaction' do
113156
let(:transaction) { transaction = Sentry.start_transaction(name: 'metrics') }
114157

0 commit comments

Comments
 (0)