lib/elastic_apm/agent.rb in elastic-apm-3.6.0 vs lib/elastic_apm/agent.rb in elastic-apm-3.7.0
- old
+ new
@@ -1,5 +1,22 @@
+# Licensed to Elasticsearch B.V. under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Elasticsearch B.V. licenses this file to you under
+# the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
# frozen_string_literal: true
require 'elastic_apm/error'
require 'elastic_apm/context_builder'
@@ -32,13 +49,13 @@
config = Config.new(config) unless config.is_a?(Config)
LOCK.synchronize do
return @instance if @instance
- unless config.active?
+ unless config.enabled?
config.logger.debug format(
- "%sAgent disabled with `active: false'",
+ "%sAgent disabled with `enabled: false'",
Logging::PREFIX
)
return
end
@@ -70,10 +87,11 @@
@instrumenter = Instrumenter.new(
config,
metrics: metrics,
stacktrace_builder: stacktrace_builder
) { |event| enqueue event }
+ @pid = Process.pid
end
attr_reader(
:central_config,
:config,
@@ -143,10 +161,13 @@
name = nil,
type = nil,
context: nil,
trace_context: nil
)
+ return unless config.recording?
+ detect_forking!
+
instrumenter.start_transaction(
name,
type,
config: config,
context: context,
@@ -168,10 +189,16 @@
context: nil,
trace_context: nil,
parent: nil,
sync: nil
)
+ detect_forking!
+
+ # We don't check config.recording? because the span
+ # will not be created if there's no transaction.
+ # We want to use the recording value from the config
+ # that existed when start_transaction was called. ~estolfo
instrumenter.start_span(
name,
type,
subtype: subtype,
action: action,
@@ -205,10 +232,12 @@
end
# errors
def report(exception, context: nil, handled: true)
+ return unless config.recording?
+ detect_forking!
return if config.filter_exception_types.include?(exception.class.to_s)
error = @error_builder.build_exception(
exception,
context: context,
@@ -217,10 +246,13 @@
enqueue error
error.id
end
def report_message(message, context: nil, backtrace: nil, **attrs)
+ return unless config.recording?
+ detect_forking!
+
error = @error_builder.build_log(
message,
context: context,
backtrace: backtrace,
**attrs
@@ -237,8 +269,22 @@
# misc
def inspect
super.split.first + '>'
+ end
+
+ def detect_forking!
+ return if @pid == Process.pid
+
+ config.logger.debug "Detected forking,
+ restarting threads in process [PID:#{Process.pid}]"
+
+ central_config.handle_forking!
+ transport.handle_forking!
+ instrumenter.handle_forking!
+ metrics.handle_forking!
+
+ @pid = Process.pid
end
end
end