newrelic_rpm 6.7.0.359 → 6.8.0.360

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +41 -6
  4. data/CHANGELOG.md +76 -0
  5. data/Guardfile +7 -1
  6. data/lib/new_relic/agent.rb +31 -0
  7. data/lib/new_relic/agent/agent.rb +7 -2
  8. data/lib/new_relic/agent/agent_logger.rb +4 -0
  9. data/lib/new_relic/agent/attribute_filter.rb +7 -7
  10. data/lib/new_relic/agent/attributes.rb +152 -0
  11. data/lib/new_relic/agent/autostart.rb +19 -14
  12. data/lib/new_relic/agent/configuration/default_source.rb +72 -5
  13. data/lib/new_relic/agent/configuration/yaml_source.rb +10 -5
  14. data/lib/new_relic/agent/connect/request_builder.rb +3 -11
  15. data/lib/new_relic/agent/datastores/mongo/event_formatter.rb +2 -2
  16. data/lib/new_relic/agent/datastores/mongo/obfuscator.rb +8 -8
  17. data/lib/new_relic/agent/distributed_trace_intrinsics.rb +90 -0
  18. data/lib/new_relic/agent/distributed_trace_metrics.rb +74 -0
  19. data/lib/new_relic/agent/distributed_trace_monitor.rb +2 -12
  20. data/lib/new_relic/agent/distributed_trace_payload.rb +9 -76
  21. data/lib/new_relic/agent/distributed_trace_transport_type.rb +43 -0
  22. data/lib/new_relic/agent/guid_generator.rb +28 -0
  23. data/lib/new_relic/agent/logging.rb +4 -0
  24. data/lib/new_relic/agent/new_relic_service.rb +7 -5
  25. data/lib/new_relic/agent/span_event_aggregator.rb +1 -0
  26. data/lib/new_relic/agent/span_event_primitive.rb +29 -7
  27. data/lib/new_relic/agent/sql_sampler.rb +1 -1
  28. data/lib/new_relic/agent/trace_context.rb +244 -0
  29. data/lib/new_relic/agent/trace_context_payload.rb +134 -0
  30. data/lib/new_relic/agent/trace_context_request_monitor.rb +42 -0
  31. data/lib/new_relic/agent/transaction.rb +26 -18
  32. data/lib/new_relic/agent/transaction/abstract_segment.rb +2 -2
  33. data/lib/new_relic/agent/transaction/distributed_tracing.rb +20 -101
  34. data/lib/new_relic/agent/transaction/external_request_segment.rb +18 -5
  35. data/lib/new_relic/agent/transaction/segment.rb +7 -1
  36. data/lib/new_relic/agent/transaction/trace_context.rb +159 -0
  37. data/lib/new_relic/agent/transaction/trace_node.rb +8 -3
  38. data/lib/new_relic/agent/transaction_error_primitive.rb +4 -11
  39. data/lib/new_relic/agent/transaction_event_primitive.rb +3 -11
  40. data/lib/new_relic/coerce.rb +29 -6
  41. data/lib/new_relic/control/instance_methods.rb +10 -1
  42. data/lib/new_relic/dependency_detection.rb +4 -4
  43. data/lib/new_relic/noticed_error.rb +8 -4
  44. data/lib/new_relic/version.rb +1 -1
  45. data/newrelic_rpm.gemspec +6 -4
  46. data/test/agent_helper.rb +88 -9
  47. data/true +0 -0
  48. metadata +50 -18
  49. data/lib/new_relic/agent/transaction/attributes.rb +0 -154
  50. data/lib/tasks/versions.html.erb +0 -28
  51. data/lib/tasks/versions.postface.html +0 -8
  52. data/lib/tasks/versions.preface.html +0 -9
  53. data/lib/tasks/versions.rake +0 -65
  54. data/lib/tasks/versions.txt.erb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0981d305747247f1ac56c96ff7268cfd8b9bd7d419e8b800f2daf9193ae1dee
4
- data.tar.gz: 9b4ca44ed6641fe4ea5f71296d8042f3ed5024a9d8d1dc3230241f02cab529fd
3
+ metadata.gz: 07ac800f099e868f4ac7fb9e86972f5533c48d87bd28cb54b1015b5dfe4af601
4
+ data.tar.gz: 0e1daf547dc945b5b57c8ff09f4866b26f907796ae654d9ebdf4c409cde8cfdf
5
5
  SHA512:
6
- metadata.gz: 8596846993859508c5acf8fc54155572332a75258f1970b01b9d89974ec7a4f935d5f728f2826bbac8190e49f368e51d961b6754f2742248a228458fa225763d
7
- data.tar.gz: b07bc5d7dbd98faec43a76514ac6314f5f9c8667941db20b68ac4f13fddbf69656c7b75d1d3b02f4c1d46873aecf528bd5062c96f4895bf2a4234bd48da5999d
6
+ metadata.gz: 32b818ece7b0ac768d5ee8c8615633bc45ce6d7f3bc8e1a98ecc5632cc927c8dfa74266334f7fd6c55a468b6ff533deaed2773d90c1e32288598ea984f3e9aa0
7
+ data.tar.gz: a562491c09abeaaaf7de0fdb7b03e13706528144c004320b497bf214877831f221128bf92545ccac60bc02d4d696c2a6ecff5a3ca4091c3fba3bad8cba3ad287
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  Gemfile.lock
2
+ Guard*
2
3
  .DS\_Store
3
4
  .svn/
4
5
  *~
@@ -16,14 +16,13 @@ before_install:
16
16
  - jdk_switcher use oraclejdk8
17
17
  - sudo rm -f /etc/apt/sources.list.d/travis_ci_zeromq3.list
18
18
  - gem --version
19
- - ./test/script/before_install/update_rubygems.sh
20
- - rvm @global do gem uninstall bundler --all --executables || true
21
- - gem uninstall bundler --all --executables || true
22
- - gem install bundler -v=1.17.3
19
+ - "echo 'gem: --no-rdoc --no-ri --no-document' > ~/.gemrc"
20
+ - ./test/script/before_install/conditionally_upgrade_rubygems.sh
23
21
  - ./test/script/before_install/gemstash_mirror.sh
22
+ - "echo GEMSTASH_MIRROR: $GEMSTASH_MIRROR"
24
23
  - bundle --version
25
24
 
26
- install: bundle install
25
+ install: bundle install --jobs 4
27
26
 
28
27
  addons:
29
28
  apt:
@@ -43,10 +42,12 @@ branches:
43
42
  - master
44
43
  - dev
45
44
  - release
45
+ - RUBY-2227_w3c_trace_context_poc
46
46
 
47
47
  rvm:
48
48
  # Run slowest builds first to try and optimize overall cycle time.
49
49
  - jruby-9.2.6.0
50
+ - 2.7.0-preview1
50
51
  - 2.6.1
51
52
  - 2.5.3
52
53
  - 2.4.2
@@ -62,7 +63,7 @@ env:
62
63
  global:
63
64
  - BUNDLE_PATH=/home/travis/bundle
64
65
  - MONGODB=2.6.11
65
- - JAVA_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Xmx640m"
66
+ - JAVA_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Xmx1024m -XX:CICompilerCount=1 -Xshare:auto"
66
67
  - JRUBY_OPTS="-Xcompile.invokedynamic=false -J-Djruby.compile.mode=OFF"
67
68
  - RBXOPT="-Xcompiler.no_rbc -Xint"
68
69
  - SERIALIZE=1
@@ -98,6 +99,40 @@ matrix:
98
99
  fast_finish: true
99
100
  exclude:
100
101
  # Unsupported Rails/Ruby combinations
102
+ # 2.7
103
+ - rvm: 2.7.0-preview1
104
+ env: TYPE=UNIT ENVIRONMENT=rails30
105
+ - rvm: 2.7.0-preview1
106
+ env: TYPE=UNIT ENVIRONMENT=rails31
107
+ - rvm: 2.7.0-preview1
108
+ env: TYPE=UNIT ENVIRONMENT=rails32
109
+ - rvm: 2.7.0-preview1
110
+ env: TYPE=UNIT ENVIRONMENT=rails40
111
+ - rvm: 2.7.0-preview1
112
+ env: TYPE=UNIT ENVIRONMENT=rails41
113
+
114
+ - rvm: 2.7.0-preview1
115
+ env: TYPE=UNIT ENVIRONMENT=rails42 # rails (= 4.2.11) depends on bundler (< 2.0, >= 1.3.0)
116
+ - rvm: 2.7.0-preview1
117
+ env: TYPE=UNIT ENVIRONMENT=rails50 # rails (= 5.0.0) depends on bundler (< 2.0, >= 1.3.0)
118
+ - rvm: 2.7.0-preview1
119
+ env: TYPE=UNIT ENVIRONMENT=rails51 # rails (= 5.1.0) depends on bundler (< 2.0, >= 1.3.0)
120
+ - rvm: 2.7.0-preview1
121
+ env: TYPE=FUNCTIONAL GROUP=api # blocked: https://github.com/solnic/coercible/issues/27
122
+ - rvm: 2.7.0-preview1
123
+ env: TYPE=FUNCTIONAL GROUP=sinatra # padrino (~> 0.14.0) depends on bundler (~> 1.0)
124
+
125
+ - rvm: 2.7.0-preview1
126
+ env: TYPE=UNIT ENVIRONMENT=rails42 # rails (= 4.2.11) depends on bundler (< 2.0, >= 1.3.0)
127
+ - rvm: 2.7.0-preview1
128
+ env: TYPE=UNIT ENVIRONMENT=rails50 # rails (= 5.0.0) depends on bundler (< 2.0, >= 1.3.0)
129
+ - rvm: 2.7.0-preview1
130
+ env: TYPE=UNIT ENVIRONMENT=rails51 # rails (= 5.1.0) depends on bundler (< 2.0, >= 1.3.0)
131
+ - rvm: 2.7.0-preview1
132
+ env: TYPE=FUNCTIONAL GROUP=api # blocked: https://github.com/solnic/coercible/issues/27
133
+ - rvm: 2.7.0-preview1
134
+ env: TYPE=FUNCTIONAL GROUP=sinatra # padrino (~> 0.14.0) depends on bundler (~> 1.0)
135
+
101
136
  # 2.6
102
137
  - rvm: 2.6.1
103
138
  env: TYPE=UNIT ENVIRONMENT=rails30
@@ -1,5 +1,81 @@
1
1
  # New Relic Ruby Agent Release Notes #
2
2
 
3
+ ## v6.8.0
4
+
5
+ * **Initial Ruby 2.7 support**
6
+
7
+ The Ruby agent has been verified to run with Ruby 2.7.0-preview1.
8
+
9
+ * **New API method to add custom attributes to Spans**
10
+
11
+ New API method for adding custom attributes to spans. Previously, custom
12
+ attributes were only available at the Transaction level. Now, with Span
13
+ level custom attributes, more granular tagging of events is possible for
14
+ easier isolation and review of trace events. For more information:
15
+
16
+ * [`Agent#add_custom_span_attributes`](https://www.rubydoc.info/github/newrelic/rpm/NewRelic/Agent#add_custom_span_attributes)
17
+
18
+ * **Enables ability to migrate to Configurable Security Policies (CSP) on a per agent
19
+ basis for accounts already using High Security Mode (HSM).**
20
+
21
+ When both [HSM](https://docs.newrelic.com/docs/agents/manage-apm-agents/configuration/high-security-mode) and [CSP](https://docs.newrelic.com/docs/agents/manage-apm-agents/configuration/enable-configurable-security-policies) are enabled for an account, an agent (this version or later)
22
+ can successfully connect with either `high_security: true` or the appropriate
23
+ `security_policies_token` configured. `high_security` has been added as part of
24
+ the preconnect payload.
25
+
26
+ * **Bugfix for Logs in Context combined with act-fluent-logger-rails**
27
+
28
+ Previously, when using the Ruby agent's Logs in Context logger
29
+ to link logging data with trace and entity metadata for an
30
+ improved experience in the UI, customers who were also using
31
+ the `act-fluent-logger-rails` gem would see a `NoMethodError`
32
+ for `clear_tags!` that would interfere with the use of this
33
+ feature. This error no longer appears, allowing customers to
34
+ combine the use of Logs in Context with the use of this gem.
35
+
36
+ Please note that the Logs in Context logger does not support
37
+ tagged logging; if you are initializing your logger with a
38
+ `log_tags` argument, your custom tags may not appear on the
39
+ final version of your logs.
40
+
41
+ * **Bugfix for parsing invalid newrelic.yml**
42
+
43
+ Previously, if the newrelic.yml configuration file was invalid, and the agent
44
+ could not start as a result, the agent would not log any indication of
45
+ the problem.
46
+
47
+ This version of the agent will emit a FATAL message to STDOUT when this scenario
48
+ occurs so that customers can address issues with newrelic.yml that prevent startup.
49
+
50
+ * **Configuration options containing the terms "whitelist" and "blacklist" deprecated**
51
+
52
+ The following local configuration settings have been deprecated:
53
+
54
+ * `autostart.blacklisted_constants`: use `autostart.denylisted_constants` instead.
55
+ * `autostart.blacklisted_executables`: use `autostart.denylisted_executables` instead.
56
+ * `autostart.blacklisted_rake_tasks`: use `autostart.denylisted_rake_tasks` instead.
57
+ * `strip_exception_messages.whitelist`: use `strip_exception_messages.allowed_classes` instead.
58
+
59
+ * **Bugfix for module loading and constant resolution in Rails**
60
+
61
+ Starting in version 6.3, the Ruby agent has caused module loading and constant
62
+ resolution to sometimes fail, which caused errors in some Rails applications.
63
+ These errors were generally `NoMethodError` exceptions or I18n errors
64
+ `translation missing` or `invalid locale`. These errors would not appear if the agent
65
+ was removed from the application's Gemfile.
66
+ This version of the agent fixes these issues with module loading and constant
67
+ resolution, so these errors no longer occur.
68
+
69
+ * **Bugfix: failed to get urandom**
70
+
71
+ Previous versions of the agent would fail unexpectedly when the Ruby process used
72
+ every available file descriptor. The failures would include this message:
73
+ ```
74
+ ERROR : RuntimeError: failed to get urandom
75
+ ```
76
+ This version of the agent uses a different strategy for generating random IDs, and
77
+ will not fail in the same way when no file descriptors are available.
78
+
3
79
  ## v6.7.0
4
80
 
5
81
  * **Trace and Entity Metadata API**
data/Guardfile CHANGED
@@ -1,4 +1,10 @@
1
- guard :minitest, :test_folders => ['test/new_relic'], :all_after_pass => false do
1
+ guard_options = {
2
+ spring: "bundle exec ruby",
3
+ test_folders: ['test/new_relic', 'test'],
4
+ all_after_pass: false
5
+ }
6
+
7
+ guard :minitest, guard_options do
2
8
  watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
3
9
  watch(%r{^test/.+_test\.rb$})
4
10
  watch(%r{^test/rum/.*}) { "test/new_relic/rack/browser_monitoring_test.rb" }
@@ -568,6 +568,12 @@ module NewRelic
568
568
  # these custom attributes will also be present in the script injected into
569
569
  # the response body, making them available on Insights PageView events.
570
570
  #
571
+ #
572
+ # @param [Hash] params A Hash of attributes to be attached to the transaction event.
573
+ # Keys should be strings or symbols, and values
574
+ # may be strings, symbols, numeric values or
575
+ # booleans.
576
+ #
571
577
  # @api public
572
578
  #
573
579
  def add_custom_attributes(params) #THREAD_LOCAL_ACCESS
@@ -581,6 +587,31 @@ module NewRelic
581
587
  end
582
588
  end
583
589
 
590
+ # Add custom attributes to the span event for the current span. Attributes will be visible on spans in the
591
+ # New Relic Distributed Tracing UI and on span events in New Relic Insights.
592
+ #
593
+ # Custom attributes will not be transmitted when +high_security+ setting is enabled or
594
+ # +custom_attributes+ setting is disabled.
595
+ #
596
+ # @param [Hash] params A Hash of attributes to be attached to the span event.
597
+ # Keys should be strings or symbols, and values
598
+ # may be strings, symbols, numeric values or
599
+ # booleans.
600
+ #
601
+ # @see https://docs.newrelic.com/docs/using-new-relic/welcome-new-relic/get-started/glossary#span
602
+ # @api public
603
+ def add_custom_span_attributes params
604
+ record_api_supportability_metric :add_custom_span_attributes
605
+
606
+ if params.is_a? Hash
607
+ if segment = NewRelic::Agent::Tracer.current_segment
608
+ segment.add_custom_attributes params
609
+ end
610
+ else
611
+ ::NewRelic::Agent.logger.warn "Bad argument passed to #add_custom_span_attributes. Expected Hash but got #{params.class}"
612
+ end
613
+ end
614
+
584
615
  # @!endgroup
585
616
 
586
617
  # @!group Transaction naming
@@ -19,6 +19,7 @@ require 'new_relic/agent/commands/agent_command_router'
19
19
  require 'new_relic/agent/event_listener'
20
20
  require 'new_relic/agent/cross_app_monitor'
21
21
  require 'new_relic/agent/distributed_trace_monitor'
22
+ require 'new_relic/agent/trace_context_request_monitor'
22
23
  require 'new_relic/agent/synthetics_monitor'
23
24
  require 'new_relic/agent/transaction_event_recorder'
24
25
  require 'new_relic/agent/custom_event_aggregator'
@@ -58,6 +59,7 @@ module NewRelic
58
59
  @agent_command_router = NewRelic::Agent::Commands::AgentCommandRouter.new(@events)
59
60
  @cross_app_monitor = NewRelic::Agent::CrossAppMonitor.new(@events)
60
61
  @distributed_trace_monitor = NewRelic::Agent::DistributedTraceMonitor.new(@events)
62
+ @trace_context_monitor = NewRelic::Agent::TraceContextRequestMonitor.new(@events)
61
63
  @synthetics_monitor = NewRelic::Agent::SyntheticsMonitor.new(@events)
62
64
  @error_collector = NewRelic::Agent::ErrorCollector.new @events
63
65
  @transaction_rules = NewRelic::Agent::RulesEngine.new
@@ -147,6 +149,7 @@ module NewRelic
147
149
  attr_reader :transaction_event_recorder
148
150
  attr_reader :attribute_filter
149
151
  attr_reader :adaptive_sampler
152
+ attr_reader :environment_report
150
153
 
151
154
  def transaction_event_aggregator
152
155
  @transaction_event_recorder.transaction_event_aggregator
@@ -482,6 +485,7 @@ module NewRelic
482
485
 
483
486
  unless in_resque_child_process?
484
487
  install_exit_handler
488
+ environment_for_connect
485
489
  @harvest_samplers.load_samplers unless Agent.config[:disable_samplers]
486
490
  end
487
491
 
@@ -789,7 +793,7 @@ module NewRelic
789
793
  # require calls in Rails environments, so this method should only
790
794
  # be called synchronously from on the main thread.
791
795
  def environment_for_connect
792
- Agent.config[:send_environment_info] ? Array(EnvironmentReport.new) : []
796
+ @environment_report ||= Agent.config[:send_environment_info] ? Array(EnvironmentReport.new) : []
793
797
  end
794
798
 
795
799
  # Constructs and memoizes an event_harvest_config hash to be used in
@@ -805,7 +809,8 @@ module NewRelic
805
809
  request_builder = ::NewRelic::Agent::Connect::RequestBuilder.new \
806
810
  @service,
807
811
  Agent.config,
808
- event_harvest_config
812
+ event_harvest_config,
813
+ environment_for_connect
809
814
  connect_response = @service.connect request_builder.connect_payload
810
815
 
811
816
  response_handler = ::NewRelic::Agent::Connect::ResponseHandler.new(self, Agent.config)
@@ -173,6 +173,10 @@ module NewRelic
173
173
  StartupLogger.instance.dump(self)
174
174
  end
175
175
 
176
+ def self.format_fatal_error message
177
+ "** [NewRelic] FATAL : #{message}\n"
178
+ end
179
+
176
180
  end
177
181
 
178
182
  # In an effort to not lose messages during startup, we trap them in memory
@@ -121,7 +121,7 @@ module NewRelic
121
121
  @high_security = config[:high_security]
122
122
 
123
123
  setup_key_cache
124
- cache_prefix_blacklist
124
+ cache_prefix_denylist
125
125
  end
126
126
 
127
127
  # Note the key_cache is a global cache, accessible by multiple threads,
@@ -222,16 +222,16 @@ module NewRelic
222
222
  # arguments for Sidekiq and Resque in the common case, since none of
223
223
  # these attributes are captured by default.
224
224
  #
225
- def cache_prefix_blacklist
226
- @prefix_blacklist = {}
227
- @prefix_blacklist[:'request.parameters'] = true unless might_allow_prefix_uncached?(:'request.parameters')
228
- @prefix_blacklist[:'job.sidekiq.args'] = true unless might_allow_prefix_uncached?(:'job.sidekiq.args')
229
- @prefix_blacklist[:'job.resque.args'] = true unless might_allow_prefix_uncached?(:'job.resque.args')
225
+ def cache_prefix_denylist
226
+ @prefix_denylist = {}
227
+ @prefix_denylist[:'request.parameters'] = true unless might_allow_prefix_uncached?(:'request.parameters')
228
+ @prefix_denylist[:'job.sidekiq.args'] = true unless might_allow_prefix_uncached?(:'job.sidekiq.args')
229
+ @prefix_denylist[:'job.resque.args'] = true unless might_allow_prefix_uncached?(:'job.resque.args')
230
230
  end
231
231
 
232
232
  # Note that the given prefix *must* be a Symbol
233
233
  def might_allow_prefix?(prefix)
234
- !@prefix_blacklist.include?(prefix)
234
+ !@prefix_denylist.include?(prefix)
235
235
  end
236
236
 
237
237
  def might_allow_prefix_uncached?(prefix)
@@ -0,0 +1,152 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'new_relic/agent/attribute_processing'
6
+
7
+ module NewRelic
8
+ module Agent
9
+ class Attributes
10
+ KEY_LIMIT = 255
11
+ VALUE_LIMIT = 255
12
+ COUNT_LIMIT = 64
13
+
14
+ EMPTY_HASH = {}.freeze
15
+
16
+ def initialize(filter)
17
+ @filter = filter
18
+
19
+ @custom_attributes = {}
20
+ @agent_attributes = {}
21
+ @intrinsic_attributes = {}
22
+
23
+ @custom_destinations = {}
24
+ @agent_destinations = {}
25
+ @already_warned_count_limit = nil
26
+ end
27
+
28
+ def add_agent_attribute(key, value, default_destinations)
29
+ destinations = @filter.apply(key, default_destinations)
30
+ return if destinations == AttributeFilter::DST_NONE
31
+
32
+ @agent_destinations[key] = destinations
33
+ add(@agent_attributes, key, value)
34
+ end
35
+
36
+ def add_agent_attribute_with_key_check(key, value, default_destinations)
37
+ if exceeds_bytesize_limit? key, KEY_LIMIT
38
+ NewRelic::Agent.logger.debug("Agent attribute #{key} was dropped for exceeding key length limit #{KEY_LIMIT}")
39
+ return
40
+ end
41
+
42
+ add_agent_attribute(key, value, default_destinations)
43
+ end
44
+
45
+ def add_intrinsic_attribute(key, value)
46
+ add(@intrinsic_attributes, key, value)
47
+ end
48
+
49
+ def merge_untrusted_agent_attributes(attributes, prefix, default_destinations)
50
+ return if @filter.high_security?
51
+ return if !@filter.might_allow_prefix?(prefix)
52
+
53
+ AttributeProcessing.flatten_and_coerce(attributes, prefix) do |k, v|
54
+ add_agent_attribute_with_key_check(k, v, AttributeFilter::DST_NONE)
55
+ end
56
+ end
57
+
58
+ def merge_custom_attributes(other)
59
+ return unless Agent.config[:'custom_attributes.enabled']
60
+ return if other.empty?
61
+ AttributeProcessing.flatten_and_coerce(other) do |k, v|
62
+ add_custom_attribute(k, v)
63
+ end
64
+ end
65
+
66
+ def custom_attributes_for(destination)
67
+ for_destination(@custom_attributes, @custom_destinations, destination)
68
+ end
69
+
70
+ def agent_attributes_for(destination)
71
+ for_destination(@agent_attributes, @agent_destinations, destination)
72
+ end
73
+
74
+ def intrinsic_attributes_for(destination)
75
+ if destination == NewRelic::Agent::AttributeFilter::DST_TRANSACTION_TRACER ||
76
+ destination == NewRelic::Agent::AttributeFilter::DST_ERROR_COLLECTOR
77
+ @intrinsic_attributes
78
+ else
79
+ EMPTY_HASH
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def add_custom_attribute(key, value)
86
+ if @custom_attributes.size >= COUNT_LIMIT
87
+ unless @already_warned_count_limit
88
+ NewRelic::Agent.logger.warn("Custom attributes count exceeded limit of #{COUNT_LIMIT}. Any additional custom attributes during this transaction will be dropped.")
89
+ @already_warned_count_limit = true
90
+ end
91
+ return
92
+ end
93
+
94
+ if @filter.high_security?
95
+ NewRelic::Agent.logger.debug("Unable to add custom attribute #{key} while in high security mode.")
96
+ return
97
+ end
98
+
99
+ if exceeds_bytesize_limit?(key, KEY_LIMIT)
100
+ NewRelic::Agent.logger.warn("Custom attribute key '#{key}' was longer than limit of #{KEY_LIMIT} bytes. This attribute will be dropped.")
101
+ return
102
+ end
103
+
104
+ destinations = @filter.apply(key, AttributeFilter::DST_ALL)
105
+ return if destinations == AttributeFilter::DST_NONE
106
+
107
+ @custom_destinations[key] = destinations
108
+ add(@custom_attributes, key, value)
109
+ end
110
+
111
+ def add(attributes, key, value)
112
+ return if value.nil?
113
+
114
+ if exceeds_bytesize_limit?(value, VALUE_LIMIT)
115
+ value = slice(value)
116
+ end
117
+
118
+ attributes[key] = value
119
+ end
120
+
121
+ def for_destination(attributes, calculated_destinations, destination)
122
+ # Avoid allocating anything if there are no attrs at all
123
+ return EMPTY_HASH if attributes.empty?
124
+
125
+ attributes.inject({}) do |memo, (key, value)|
126
+ if @filter.allows?(calculated_destinations[key], destination)
127
+ memo[key] = value
128
+ end
129
+ memo
130
+ end
131
+ end
132
+
133
+ def exceeds_bytesize_limit?(value, limit)
134
+ if value.respond_to?(:bytesize)
135
+ value.bytesize > limit
136
+ elsif value.is_a?(Symbol)
137
+ value.to_s.bytesize > limit
138
+ else
139
+ false
140
+ end
141
+ end
142
+
143
+ # Take one byte past our limit. Why? This lets us unconditionally chop!
144
+ # the end. It'll either remove the one-character-too-many we have, or
145
+ # peel off the partial, mangled character left by the byteslice.
146
+ def slice(incoming)
147
+ result = incoming.to_s.byteslice(0, VALUE_LIMIT + 1)
148
+ result.chop!
149
+ end
150
+ end
151
+ end
152
+ end