Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kinesis instrumentation #2974

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/new_relic/agent/configuration/default_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,15 @@ def self.notify
:allowed_from_server => false,
:description => 'Controls auto-instrumentation of the aws_sdk_lambda library at start-up. May be one of `auto`, `prepend`, `chain`, `disabled`.'
},
:'instrumentation.kinesis' => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

James had a question about the consistency of our aws sdk config options. For most of our instrumentation.* configs, the snake cased name of the gem is used. However, we're not consistent with our AWS configs. Some are snake case, others are just smaller portions of the name, like instrumentation.dynamodb. Do we want to establish more consistency? What do you think about using the gem name vs. the AWS service name?

:default => 'auto',
:documentation_default => 'auto',
:public => true,
:type => String,
:dynamic_name => true,
:allowed_from_server => false,
:description => 'Controls auto-instrumentation of the kinesis library at start-up. May be one of `auto`, `prepend`, `chain`, `disabled`.'
},
:'instrumentation.ruby_kafka' => {
:default => 'auto',
:public => true,
Expand Down
22 changes: 22 additions & 0 deletions lib/new_relic/agent/instrumentation/kinesis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

require_relative 'kinesis/instrumentation'
require_relative 'kinesis/chain'
require_relative 'kinesis/prepend'

DependencyDetection.defer do
named :kinesis

depends_on do
defined?(Aws::Kinesis::Client)
end
executes do
if use_prepend?
prepend_instrument Aws::Kinesis::Client, NewRelic::Agent::Instrumentation::Kinesis::Prepend
else
chain_instrument NewRelic::Agent::Instrumentation::Kinesis::Chain
end
end
end
21 changes: 21 additions & 0 deletions lib/new_relic/agent/instrumentation/kinesis/chain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic::Agent::Instrumentation
module Kinesis::Chain
def self.instrument!
::Aws::Kinesis::Client.class_eval do
include NewRelic::Agent::Instrumentation::Kinesis

NewRelic::Agent::Instrumentation::Kinesis::INSTRUMENTED_METHODS.each do |method_name|
alias_method("#{method_name}_without_new_relic".to_sym, method_name.to_sym)

define_method(method_name) do |*args|
instrument_method_with_new_relic(method_name, *args) { send("#{method_name}_without_new_relic".to_sym, *args) }
end
end
end
end
end
end
71 changes: 71 additions & 0 deletions lib/new_relic/agent/instrumentation/kinesis/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic::Agent::Instrumentation
module Kinesis
INSTRUMENTED_METHODS = %w[
add_tags_to_stream
create_stream
decrease_stream_retention_period
delete_stream
describe_limits
describe_stream
disable_enhanced_monitoring
enable_enhanced_monitoring
get_records
get_shard_iterator
increase_stream_retention_period
list_streams
list_tags_for_stream
merge_shards
put_record
put_records
remove_tags_from_stream
split_shard
update_shard_count
].freeze

KINESIS = 'Kinesis'
AWS_KINESIS_DATA_STREAMS = 'aws_kinesis_data_streams'

def instrument_method_with_new_relic(method_name, *args)
return yield unless NewRelic::Agent::Tracer.tracing_enabled?

NewRelic::Agent.record_instrumentation_invocation(KINESIS)

params = args[0]
segment = NewRelic::Agent::Tracer.start_segment(name: segment_name(method_name, params))
arn = get_arn(params) if params
segment&.add_agent_attribute('cloud.resource_id', arn) if arn

begin
NewRelic::Agent::Tracer.capture_segment_error(segment) { yield }
ensure
segment&.add_agent_attribute('cloud.platform', AWS_KINESIS_DATA_STREAMS)
segment&.add_agent_attribute('name', segment_name(method_name, params))
segment&.finish
end
end

def segment_name(method_name, params)
return "#{KINESIS}/#{method_name}/#{params[:stream_name]}" if params&.dig(:stream_name)

"#{KINESIS}/#{method_name}"
rescue => e
NewRelic::Agent.logger.warn("Failed to create segment name: #{e}")
end

def nr_account_id
return @nr_account_id if defined?(@nr_account_id)

@nr_account_id = NewRelic::Agent::Aws.get_account_id(config)
end

def get_arn(params)
return params[:stream_arn] if params[:stream_arn]

NewRelic::Agent::Aws.create_arn(KINESIS.downcase, "stream/#{params[:stream_name]}", config&.region, nr_account_id) if params[:stream_name]
end
end
end
15 changes: 15 additions & 0 deletions lib/new_relic/agent/instrumentation/kinesis/prepend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic::Agent::Instrumentation
module Kinesis::Prepend
include NewRelic::Agent::Instrumentation::Kinesis

INSTRUMENTED_METHODS.each do |method_name|
define_method(method_name) do |*args|
instrument_method_with_new_relic(method_name, *args) { super(*args) }
end
end
end
end
9 changes: 9 additions & 0 deletions test/multiverse/suites/kinesis/Envfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

instrumentation_methods :chain, :prepend

gemfile <<~RB
gem 'aws-sdk-kinesis'
RB
19 changes: 19 additions & 0 deletions test/multiverse/suites/kinesis/config/newrelic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
development:
error_collector:
enabled: true
apdex_t: 0.5
monitor_mode: true
license_key: bootstrap_newrelic_admin_license_key_000
instrumentation:
kinesis: <%= $instrumentation_method %>
app_name: test
log_level: debug
host: 127.0.0.1
api_host: 127.0.0.1
transaction_trace:
record_sql: obfuscated
enabled: true
stack_trace_threshold: 0.5
transaction_threshold: 1.0
capture_params: false
Loading
Loading