Skip to content

Commit 63f8b18

Browse files
authored
Merge pull request #3780 from DataDog/backport/1.x/add_log_deprecation_limits
Backport (1.x): Add limits to #log_deprecation
2 parents dd5f9b1 + cb3418c commit 63f8b18

File tree

6 files changed

+175
-55
lines changed

6 files changed

+175
-55
lines changed

lib/datadog/core.rb

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require_relative 'core/deprecations'
34
require_relative 'core/extensions'
45

56
# We must load core extensions to make certain global APIs
@@ -9,25 +10,7 @@ module Datadog
910
# products. It is a dependency of each product. Contrast with Datadog::Kit
1011
# for higher-level features.
1112
module Core
12-
class << self
13-
# Records the occurrence of a deprecated operation in this library.
14-
#
15-
# Currently, these operations are logged to `Datadog.logger` at `warn` level.
16-
#
17-
# `disallowed_next_major` adds a message informing that the deprecated operation
18-
# won't be allowed in the next major release.
19-
#
20-
# @yieldreturn [String] a String with the lazily evaluated deprecation message.
21-
# @param [Boolean] disallowed_next_major whether this deprecation will be enforced in the next major release.
22-
def log_deprecation(disallowed_next_major: true)
23-
Datadog.logger.warn do
24-
message = yield
25-
message += ' This will be enforced in the next major release.' if disallowed_next_major
26-
message
27-
end
28-
nil
29-
end
30-
end
13+
extend Core::Deprecations
3114
end
3215

3316
extend Core::Extensions

lib/datadog/core/deprecations.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
module Datadog
4+
module Core
5+
# Contains behavior for handling deprecated functions in the codebase.
6+
module Deprecations
7+
# Records the occurrence of a deprecated operation in this library.
8+
#
9+
# Currently, these operations are logged to `Datadog.logger` at `warn` level.
10+
#
11+
# `disallowed_next_major` adds a message informing that the deprecated operation
12+
# won't be allowed in the next major release.
13+
#
14+
# @yieldreturn [String] a String with the lazily evaluated deprecation message.
15+
# @param [Boolean] disallowed_next_major whether this deprecation will be enforced in the next major release.
16+
# @param [Object] key A unique key for the deprecation. Only the first message with the same key will be logged.
17+
def log_deprecation(disallowed_next_major: true, key: nil)
18+
return unless log_deprecation?(key)
19+
20+
Datadog.logger.warn do
21+
message = yield
22+
message += ' This will be enforced in the next major release.' if disallowed_next_major
23+
message
24+
end
25+
26+
# Track the deprecation being logged.
27+
deprecation_logged!(key)
28+
29+
nil
30+
end
31+
32+
private
33+
34+
# Determines whether a deprecation message should be logged.
35+
#
36+
# Internal use only.
37+
def log_deprecation?(key)
38+
return true if key.nil?
39+
40+
# Only allow a deprecation to be logged once.
41+
!logged_deprecations.key?(key)
42+
end
43+
44+
def deprecation_logged!(key)
45+
return if key.nil?
46+
47+
logged_deprecations[key] += 1
48+
end
49+
50+
# Tracks what deprecation warnings have already been logged
51+
#
52+
# Internal use only.
53+
def logged_deprecations
54+
@logged_deprecations ||= Hash.new(0)
55+
end
56+
end
57+
end
58+
end

sig/datadog/core.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Datadog
22
module Core
33
end
44

5+
extend Core::Deprecations
56
extend Core::Configuration
67
extend Tracing::Contrib::Extensions::Helpers
78
end

sig/datadog/core/deprecations.rbs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Datadog
2+
module Core
3+
module Deprecations
4+
interface _Hashing
5+
def hash: () -> ::Integer
6+
def eql?: (untyped) -> bool
7+
def nil?: () -> bool
8+
end
9+
10+
type key = _Hashing
11+
12+
@logged_deprecations: ::Hash[key, ::Integer]
13+
def log_deprecation: (?disallowed_next_major: bool, ?key: key?) { () -> String } -> void
14+
15+
private
16+
def log_deprecation?: (key key) -> bool
17+
18+
def deprecation_logged!: (key key) -> void
19+
def logged_deprecations: () -> ::Hash[key, ::Integer]
20+
end
21+
end
22+
end
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
require 'spec_helper'
2+
3+
require 'datadog/core/deprecations'
4+
5+
RSpec.describe Datadog::Core::Deprecations do
6+
context 'when extended' do
7+
subject(:test_class) { Class.new { extend Datadog::Core::Deprecations } }
8+
9+
describe '.log_deprecation' do
10+
subject(:log_deprecation) { call_log_deprecation }
11+
12+
let(:options) { {} }
13+
let(:message) { 'Longer allowed.' }
14+
15+
def call_log_deprecation
16+
test_class.log_deprecation(**options) { message }
17+
end
18+
19+
context 'by default' do
20+
it 'warns with enforcement message' do
21+
expect(Datadog.logger).to receive(:warn) do |&block|
22+
expect(block.call).to eq('Longer allowed. This will be enforced in the next major release.')
23+
end
24+
log_deprecation
25+
end
26+
27+
it 'does not limit messages' do
28+
expect(Datadog.logger).to receive(:warn).twice
29+
2.times { call_log_deprecation }
30+
end
31+
end
32+
33+
context 'with disallowed_next_major:' do
34+
let(:options) { { disallowed_next_major: disallowed_next_major } }
35+
36+
context 'true' do
37+
let(:disallowed_next_major) { true }
38+
39+
it 'warns with enforcement message' do
40+
expect(Datadog.logger).to receive(:warn) do |&block|
41+
expect(block.call).to eq('Longer allowed. This will be enforced in the next major release.')
42+
end
43+
log_deprecation
44+
end
45+
end
46+
47+
context 'false' do
48+
let(:disallowed_next_major) { false }
49+
50+
it 'warns with enforcement message' do
51+
expect(Datadog.logger).to receive(:warn) do |&block|
52+
expect(block.call).to eq('Longer allowed.')
53+
end
54+
log_deprecation
55+
end
56+
end
57+
end
58+
59+
context 'with key:' do
60+
let(:options) { { key: key } }
61+
62+
context 'nil' do
63+
let(:key) { nil }
64+
65+
it 'does not limit messages' do
66+
expect(Datadog.logger).to receive(:warn).twice
67+
2.times { call_log_deprecation }
68+
end
69+
end
70+
71+
context 'Symbol' do
72+
let(:key) { :deprecated_setting }
73+
74+
it 'limits messages' do
75+
expect(Datadog.logger).to receive(:warn).once
76+
2.times { call_log_deprecation }
77+
end
78+
end
79+
80+
context 'String' do
81+
let(:key) { 'deprecated_setting' }
82+
83+
it 'limits messages' do
84+
expect(Datadog.logger).to receive(:warn).once
85+
2.times { call_log_deprecation }
86+
end
87+
end
88+
end
89+
end
90+
end
91+
end

spec/datadog/core_spec.rb

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,7 @@
33
require 'datadog/core'
44

55
RSpec.describe Datadog::Core do
6-
describe '.log_deprecation' do
7-
subject(:log_deprecation) { described_class.log_deprecation(**options) { message } }
8-
let(:options) { {} }
9-
let(:message) { 'Longer allowed.' }
10-
11-
context 'by default' do
12-
it 'warns with enforcement message' do
13-
expect(Datadog.logger).to receive(:warn) do |&block|
14-
expect(block.call).to eq('Longer allowed. This will be enforced in the next major release.')
15-
end
16-
log_deprecation
17-
end
18-
end
19-
20-
context 'with disallowed_next_major true' do
21-
let(:options) { { disallowed_next_major: true } }
22-
23-
it 'warns with enforcement message' do
24-
expect(Datadog.logger).to receive(:warn) do |&block|
25-
expect(block.call).to eq('Longer allowed. This will be enforced in the next major release.')
26-
end
27-
log_deprecation
28-
end
29-
end
30-
31-
context 'with disallowed_next_major false' do
32-
let(:options) { { disallowed_next_major: false } }
33-
34-
it 'warns with enforcement message' do
35-
expect(Datadog.logger).to receive(:warn) do |&block|
36-
expect(block.call).to eq('Longer allowed.')
37-
end
38-
log_deprecation
39-
end
40-
end
41-
end
6+
it { expect(described_class).to be_a_kind_of(Datadog::Core::Deprecations) }
427
end
438

449
RSpec.describe Datadog do

0 commit comments

Comments
 (0)