|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'libdatadog' |
| 4 | + |
| 5 | +require_relative 'tag_builder' |
| 6 | +require_relative 'agent_base_url' |
| 7 | +require_relative '../utils/only_once' |
| 8 | +require_relative '../utils/at_fork_monkey_patch' |
| 9 | + |
| 10 | +module Datadog |
| 11 | + module Core |
| 12 | + module Crashtracking |
| 13 | + # Used to report Ruby VM crashes. |
| 14 | + # |
| 15 | + # NOTE: The crashtracker native state is a singleton; |
| 16 | + # so even if you create multiple instances of `Crashtracking::Component` and start them, |
| 17 | + # it only works as "last writer wins". Same for stop -- there's only one state, so calling stop |
| 18 | + # on it will stop the crash tracker, regardless of which instance started it. |
| 19 | + # |
| 20 | + # Methods prefixed with _native_ are implemented in `crashtracker.c` |
| 21 | + class Component |
| 22 | + LIBDATADOG_API_FAILURE = |
| 23 | + begin |
| 24 | + require "libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}" |
| 25 | + nil |
| 26 | + rescue LoadError => e |
| 27 | + e.message |
| 28 | + end |
| 29 | + |
| 30 | + ONLY_ONCE = Core::Utils::OnlyOnce.new |
| 31 | + |
| 32 | + def self.build(settings, agent_settings, logger:) |
| 33 | + tags = TagBuilder.call(settings) |
| 34 | + agent_base_url = AgentBaseUrl.resolve(agent_settings) |
| 35 | + logger.warn('Missing agent base URL; cannot enable crash tracking') unless agent_base_url |
| 36 | + |
| 37 | + ld_library_path = ::Libdatadog.ld_library_path |
| 38 | + logger.warn('Missing ld_library_path; cannot enable crash tracking') unless ld_library_path |
| 39 | + |
| 40 | + path_to_crashtracking_receiver_binary = ::Libdatadog.path_to_crashtracking_receiver_binary |
| 41 | + unless path_to_crashtracking_receiver_binary |
| 42 | + logger.warn('Missing path_to_crashtracking_receiver_binary; cannot enable crash tracking') |
| 43 | + end |
| 44 | + |
| 45 | + return unless agent_base_url |
| 46 | + return unless ld_library_path |
| 47 | + return unless path_to_crashtracking_receiver_binary |
| 48 | + |
| 49 | + new( |
| 50 | + tags: tags, |
| 51 | + agent_base_url: agent_base_url, |
| 52 | + ld_library_path: ld_library_path, |
| 53 | + path_to_crashtracking_receiver_binary: path_to_crashtracking_receiver_binary, |
| 54 | + logger: logger |
| 55 | + ).tap(&:start) |
| 56 | + end |
| 57 | + |
| 58 | + def initialize(tags:, agent_base_url:, ld_library_path:, path_to_crashtracking_receiver_binary:, logger:) |
| 59 | + @tags = tags |
| 60 | + @agent_base_url = agent_base_url |
| 61 | + @ld_library_path = ld_library_path |
| 62 | + @path_to_crashtracking_receiver_binary = path_to_crashtracking_receiver_binary |
| 63 | + @logger = logger |
| 64 | + end |
| 65 | + |
| 66 | + def start |
| 67 | + Utils::AtForkMonkeyPatch.apply! |
| 68 | + |
| 69 | + start_or_update_on_fork(action: :start) |
| 70 | + ONLY_ONCE.run do |
| 71 | + Utils::AtForkMonkeyPatch.at_fork(:child) do |
| 72 | + # Must NOT reference `self` here, as only the first instance will |
| 73 | + # be captured by the ONLY_ONCE and we want to pick the latest active one |
| 74 | + # (which may have different tags or agent config) |
| 75 | + Datadog.send(:components).crashtracker&.update_on_fork |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + def update_on_fork |
| 81 | + start_or_update_on_fork(action: :update_on_fork) |
| 82 | + end |
| 83 | + |
| 84 | + def stop |
| 85 | + self.class._native_stop |
| 86 | + logger.debug('Crash tracking stopped successfully') |
| 87 | + rescue => e |
| 88 | + logger.error("Failed to stop crash tracking: #{e.message}") |
| 89 | + end |
| 90 | + |
| 91 | + private |
| 92 | + |
| 93 | + attr_reader :tags, :agent_base_url, :ld_library_path, :path_to_crashtracking_receiver_binary, :logger |
| 94 | + |
| 95 | + def start_or_update_on_fork(action:) |
| 96 | + self.class._native_start_or_update_on_fork( |
| 97 | + action: action, |
| 98 | + exporter_configuration: [:agent, agent_base_url], |
| 99 | + path_to_crashtracking_receiver_binary: path_to_crashtracking_receiver_binary, |
| 100 | + ld_library_path: ld_library_path, |
| 101 | + tags_as_array: tags.to_a, |
| 102 | + upload_timeout_seconds: 1 |
| 103 | + ) |
| 104 | + logger.debug("Crash tracking #{action} successfully") |
| 105 | + rescue => e |
| 106 | + logger.error("Failed to #{action} crash tracking: #{e.message}") |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments