Skip to content

Commit 30d7999

Browse files
jaggederestestolfo
authored andcommitted
Racecar instrumentation (#1284)
* WIP racecar instrumentation * Change to subscriber for racecar instrumentation * Update to use transactions rather than spans * Racecar WIP * Updated Racecar instrumentation and specs * Ignore racecar spec if ActiveSupport is not available * Remove extra requires from spec_helper * Add additional require to Racecar spy * Update changelog for #1284
1 parent 19dbead commit 30d7999

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ endif::[]
4343
4444
- Added transaction_name to reported error to allow grouping by transaction name {pull}1267[#1267]
4545
- Added ability to query server for version (useful in the future) {pull}1278[#1278]
46+
- Added instrumentation for https://github.com/zendesk/racecar/ Racecar Kafka library {pull}1284[#1284]
4647
4748
===== Changed
4849

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ gem 'json-schema', require: nil
5151
gem 'mongo', require: nil
5252
gem 'opentracing', require: nil
5353
gem 'rake', '>= 13.0', require: nil
54+
gem 'racecar', require: nil
5455
gem 'resque', require: nil
5556
gem 'sequel', require: nil
5657
gem 'shoryuken', require: nil

lib/elastic_apm/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def available_instrumentations
159159
mongo
160160
net_http
161161
rake
162+
racecar
162163
redis
163164
resque
164165
s3

lib/elastic_apm/spies/racecar.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
begin
19+
require 'active_support/notifications'
20+
require 'active_support/subscriber'
21+
22+
# frozen_string_literal: true
23+
module ElasticAPM
24+
# @api private
25+
module Spies
26+
# @api private
27+
class RacecarSpy
28+
TYPE = 'kafka'
29+
SUBTYPE = 'racecar'
30+
31+
# @api private
32+
class ConsumerSubscriber < ActiveSupport::Subscriber
33+
def start_process_message(event)
34+
start_process_transaction(event: event, kind: 'process_message')
35+
end
36+
def process_message(_event)
37+
ElasticAPM.end_transaction
38+
end
39+
40+
def start_process_batch(event)
41+
start_process_transaction(event: event, kind: 'process_batch')
42+
end
43+
def process_batch(_event)
44+
ElasticAPM.end_transaction
45+
end
46+
47+
private # only public methods will be subscribed
48+
49+
def start_process_transaction(event:, kind:)
50+
ElasticAPM.start_transaction(kind, TYPE)
51+
ElasticAPM.current_transaction.context.set_service(framework_name: 'racecar', framework_version: Racecar::VERSION)
52+
end
53+
end
54+
55+
class ProducerSubscriber < ActiveSupport::Subscriber
56+
def start_deliver_message(event)
57+
ElasticAPM.start_transaction('deliver_message',TYPE)
58+
ElasticAPM.current_transaction.context.set_service(framework_name: 'racecar', framework_version: Racecar::VERSION)
59+
end
60+
61+
def deliver_message(_event)
62+
ElasticAPM.end_transaction
63+
end
64+
end
65+
66+
def install
67+
ConsumerSubscriber.attach_to(:racecar)
68+
ProducerSubscriber.attach_to(:racecar)
69+
end
70+
end
71+
register 'Racecar', 'racecar', RacecarSpy.new
72+
end
73+
end
74+
75+
rescue LoadError
76+
# no active support available
77+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# frozen_string_literal: true
19+
20+
require 'spec_helper'
21+
22+
begin
23+
require 'active_support/notifications'
24+
require "active_support/subscriber"
25+
require 'racecar'
26+
27+
module ElasticAPM
28+
RSpec.describe 'Spy: Racecar', :intercept do
29+
it 'captures the instrumentation' do
30+
with_agent do
31+
ActiveSupport::Notifications.instrument('start_process_message.racecar')
32+
ActiveSupport::Notifications.instrument('process_message.racecar') do
33+
# this is the body of the racecar consumer #process method
34+
end
35+
first_transaction = @intercepted.transactions.first
36+
expect(first_transaction).not_to be_nil
37+
expect(first_transaction.name).to eq('process_message')
38+
expect(first_transaction.type).to eq('kafka')
39+
end
40+
end
41+
end
42+
end
43+
44+
rescue LoadError # in case we don't have ActiveSupport
45+
end

0 commit comments

Comments
 (0)