|
17 | 17 | before do |
18 | 18 | Datadog.configure do |c| |
19 | 19 | c.tracing.instrument :karafka, configuration_options |
| 20 | + c.tracing.instrument :karafka, describes: /special_/, distributed_tracing: false |
20 | 21 | end |
21 | 22 | end |
22 | 23 |
|
|
31 | 32 | let(:span_name) { Datadog::Tracing::Contrib::Karafka::Ext::SPAN_MESSAGE_CONSUME } |
32 | 33 |
|
33 | 34 | it 'is expected to send a span' do |
34 | | - metadata = ::Karafka::Messages::Metadata.new |
35 | | - metadata['offset'] = 412 |
| 35 | + metadata = ::Karafka::Messages::Metadata.new(offset: 412, timestamp: Time.now, topic: 'topic_a') |
36 | 36 | raw_payload = rand.to_s |
37 | 37 |
|
38 | 38 | message = ::Karafka::Messages::Message.new(raw_payload, metadata) |
39 | | - allow(message).to receive(:timestamp).and_return(Time.now) |
40 | | - allow(message).to receive(:topic).and_return('topic_a') |
41 | | - |
42 | | - topic = ::Karafka::Routing::Topic.new('topic_a', double(id: 0)) |
43 | 39 |
|
| 40 | + topic = ::Karafka::Routing::Topic.new(message.topic, double(id: 0)) |
44 | 41 | messages = ::Karafka::Messages::Builders::Messages.call([message], topic, 0, Time.now) |
45 | 42 |
|
46 | 43 | expect(messages).to all(be_a(::Karafka::Messages::Message)) |
|
55 | 52 | end |
56 | 53 |
|
57 | 54 | context 'when the message has tracing headers' do |
| 55 | + let(:topic_name) { 'topic_a' } |
58 | 56 | let(:message) do |
59 | 57 | headers = {} |
60 | 58 | Datadog::Tracing.trace('producer') do |span, trace| |
61 | 59 | Datadog::Tracing::Contrib::Karafka.inject(trace.to_digest, headers) |
62 | 60 | end |
63 | | - metadata = ::Karafka::Messages::Metadata.new |
64 | | - metadata['offset'] = 412 |
65 | | - metadata[headers_accessor] = headers |
| 61 | + metadata = ::Karafka::Messages::Metadata.new( |
| 62 | + :offset => 412, |
| 63 | + headers_accessor => headers, |
| 64 | + :topic => topic_name, |
| 65 | + :timestamp => Time.now |
| 66 | + ) |
66 | 67 | raw_payload = rand.to_s |
67 | 68 |
|
68 | | - message = ::Karafka::Messages::Message.new(raw_payload, metadata) |
69 | | - allow(message).to receive(:timestamp).and_return(Time.now) |
70 | | - allow(message).to receive(:topic).and_return('topic_a') |
71 | | - message |
| 69 | + ::Karafka::Messages::Message.new(raw_payload, metadata) |
72 | 70 | end |
73 | 71 | let(:headers_accessor) do |
74 | 72 | ::Karafka::Messages::Metadata.members.include?(:raw_headers) ? 'raw_headers' : 'headers' |
|
85 | 83 | consumer_span = Datadog::Tracing.active_span |
86 | 84 | consumer_trace = Datadog::Tracing.active_trace |
87 | 85 |
|
88 | | - topic = ::Karafka::Routing::Topic.new('topic_a', double(id: 0)) |
| 86 | + topic = ::Karafka::Routing::Topic.new(topic_name, double(id: 0)) |
89 | 87 | messages = ::Karafka::Messages::Builders::Messages.call([message], topic, 0, Time.now) |
90 | 88 | # NOTE: The following will iterate through the messages and create a new span representing |
91 | 89 | # the individual message processing (and `span` will refer to that particular span) |
|
108 | 106 | end |
109 | 107 | end |
110 | 108 |
|
| 109 | + context 'when distributed tracing is disabled for the topic in particular' do |
| 110 | + let(:topic_name) { 'special_topic' } |
| 111 | + |
| 112 | + it 'does not continue the span that produced the message' do |
| 113 | + consumer_span = nil |
| 114 | + consumer_trace = nil |
| 115 | + |
| 116 | + Datadog::Tracing.trace('consumer') do |
| 117 | + consumer_span = Datadog::Tracing.active_span |
| 118 | + consumer_trace = Datadog::Tracing.active_trace |
| 119 | + |
| 120 | + topic = ::Karafka::Routing::Topic.new(topic_name, double(id: 0)) |
| 121 | + messages = ::Karafka::Messages::Builders::Messages.call([message], topic, 0, Time.now) |
| 122 | + expect(messages).to all(be_a(::Karafka::Messages::Message)) |
| 123 | + |
| 124 | + # assert that the current trace re-set to the original trace after iterating the messages |
| 125 | + expect(Datadog::Tracing.active_trace).to eq(consumer_trace) |
| 126 | + expect(Datadog::Tracing.active_span).to eq(consumer_span) |
| 127 | + end |
| 128 | + |
| 129 | + expect(spans).to have(3).items |
| 130 | + |
| 131 | + # assert that the message span is not continuation of the producer span |
| 132 | + expect(span.parent_id).to eq(consumer_span.id) |
| 133 | + expect(span.trace_id).to eq(consumer_trace.id) |
| 134 | + end |
| 135 | + end |
| 136 | + |
111 | 137 | context 'when distributed tracing is not enabled' do |
112 | 138 | let(:configuration_options) { {distributed_tracing: false} } |
113 | 139 |
|
|
119 | 145 | consumer_span = Datadog::Tracing.active_span |
120 | 146 | consumer_trace = Datadog::Tracing.active_trace |
121 | 147 |
|
122 | | - topic = ::Karafka::Routing::Topic.new('topic_a', double(id: 0)) |
| 148 | + topic = ::Karafka::Routing::Topic.new(topic_name, double(id: 0)) |
| 149 | + |
123 | 150 | messages = ::Karafka::Messages::Builders::Messages.call([message], topic, 0, Time.now) |
124 | 151 | # NOTE: The following will iterate through the messages and create a new span representing |
125 | 152 | # the individual message processing (and `span` will refer to that particular span) |
|
148 | 175 | let(:span_name) { Datadog::Tracing::Contrib::Karafka::Ext::SPAN_WORKER_PROCESS } |
149 | 176 |
|
150 | 177 | it 'is expected to send a span' do |
151 | | - metadata = ::Karafka::Messages::Metadata.new |
152 | | - metadata['offset'] = 412 |
| 178 | + metadata = ::Karafka::Messages::Metadata.new(offset: 412, topic: 'topic_a') |
153 | 179 | raw_payload = rand.to_s |
154 | 180 |
|
155 | 181 | message = ::Karafka::Messages::Message.new(raw_payload, metadata) |
156 | | - job = double(executor: double(topic: double(name: 'topic_a', consumer: 'ABC'), partition: 0), messages: [message]) |
| 182 | + job = double(executor: double(topic: double(name: message.topic, consumer: 'ABC'), partition: 0), messages: [message]) |
157 | 183 |
|
158 | 184 | Karafka.monitor.instrument('worker.processed', {job: job}) do |
159 | 185 | # Noop |
|
169 | 195 | expect(span.resource).to eq 'ABC#consume' |
170 | 196 | end |
171 | 197 | end |
| 198 | + |
| 199 | + describe 'framework auto-instrumentation' do |
| 200 | + around do |example| |
| 201 | + # Reset before and after each example; don't allow global state to linger. |
| 202 | + Datadog.registry[:waterdrop].reset_configuration! |
| 203 | + example.run |
| 204 | + Datadog.registry[:waterdrop].reset_configuration! |
| 205 | + |
| 206 | + # reset Karafka internal state as well |
| 207 | + Karafka::App.config.internal.status.reset! |
| 208 | + Karafka.refresh! |
| 209 | + end |
| 210 | + |
| 211 | + it 'automatically enables waterdrop instrumentation' do |
| 212 | + Karafka::App.setup do |c| |
| 213 | + c.kafka = {"bootstrap.servers": '127.0.0.1:9092'} |
| 214 | + end |
| 215 | + |
| 216 | + expect(Datadog.configuration.tracing[:karafka][:enabled]).to be true |
| 217 | + expect(Datadog.configuration.tracing[:karafka][:distributed_tracing]).to be true |
| 218 | + expect(Datadog.configuration.tracing[:karafka, 'special_topic'][:enabled]).to be true |
| 219 | + expect(Datadog.configuration.tracing[:karafka, 'special_topic'][:distributed_tracing]).to be false |
| 220 | + |
| 221 | + expect(Datadog.configuration.tracing[:waterdrop][:enabled]).to be true |
| 222 | + expect(Datadog.configuration.tracing[:waterdrop][:distributed_tracing]).to be true |
| 223 | + expect(Datadog.configuration.tracing[:waterdrop, 'special_topic'][:enabled]).to be true |
| 224 | + expect(Datadog.configuration.tracing[:waterdrop, 'special_topic'][:distributed_tracing]).to be false |
| 225 | + end |
| 226 | + end |
172 | 227 | end |
0 commit comments