Skip to content

Commit 01c8171

Browse files
authored
feat: Allow disabling of redis root spans (#777)
1 parent b4644b7 commit 01c8171

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

instrumentation/redis/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release History: opentelemetry-instrumentation-redis
22

3+
### Unreleased
4+
5+
* ADDED: Configuration option to enable or disable redis root spans [#777](https://github.com/open-telemetry/opentelemetry-ruby/pull/777)
6+
37
### v0.18.0 / 2021-05-21
48

59
* ADDED: Updated API depedency for 1.0.0.rc1

instrumentation/redis/lib/opentelemetry/instrumentation/redis/instrumentation.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
1919
defined?(::Redis)
2020
end
2121

22-
option :peer_service, default: nil, validate: :string
23-
option :enable_statement_obfuscation, default: true, validate: :boolean
22+
option :peer_service, default: nil, validate: :string
23+
option :trace_root_spans, default: true, validate: :boolean
24+
option :enable_statement_obfuscation, default: true, validate: :boolean
2425

2526
private
2627

instrumentation/redis/lib/opentelemetry/instrumentation/redis/patches/client.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ module Client
1313
MAX_STATEMENT_LENGTH = 500
1414
private_constant :MAX_STATEMENT_LENGTH
1515

16-
def process(commands) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
16+
def process(commands) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
17+
return super unless config[:trace_root_spans] || OpenTelemetry::Trace.current_span.context.valid?
18+
1719
host = options[:host]
1820
port = options[:port]
1921

instrumentation/redis/test/opentelemetry/instrumentation/redis/patches/client_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,31 @@ def redis_with_auth(redis_options = {})
260260
_(last_span.attributes['db.statement']).must_equal 'SET K x'
261261
end
262262

263+
describe 'when trace_root_spans is disabled' do
264+
before do
265+
instrumentation.instance_variable_set(:@installed, false)
266+
instrumentation.install(trace_root_spans: false)
267+
end
268+
269+
it 'traces redis spans with a parent' do
270+
redis = redis_with_auth
271+
OpenTelemetry.tracer_provider.tracer('tester').in_span('a root!') do
272+
redis.set('a', 'b')
273+
end
274+
275+
redis_span = exporter.finished_spans.find { |s| s.name == 'SET' }
276+
_(redis_span.name).must_equal 'SET'
277+
_(redis_span.attributes['db.statement']).must_equal 'SET ? ?'
278+
end
279+
280+
it 'does not trace redis spans without a parent' do
281+
redis = redis_with_auth
282+
redis.set('a', 'b')
283+
284+
_(exporter.finished_spans.size).must_equal 0
285+
end
286+
end
287+
263288
describe 'when enable_statement_obfuscation is enabled' do
264289
before do
265290
instrumentation.instance_variable_set(:@installed, false)

0 commit comments

Comments
 (0)