-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
207 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'libdatadog' | ||
|
||
require_relative 'crashtracking/tag_builder' | ||
require_relative 'crashtracking/agent_base_url' | ||
|
||
module Datadog | ||
module Core | ||
# Used to report Ruby VM crashes. | ||
# | ||
# NOTE: The crashtracker native state is a singleton; so even if you create multiple instances of `Crashtracker` | ||
# and start them, it only works as "last writer wins". Same for stop -- there's only one state, so calling stop | ||
# on it will stop the crash tracker, regardless of which instance started it. | ||
# | ||
# Methods prefixed with _native_ are implemented in `crashtracker.c` | ||
class Crashtracker | ||
LIBDATADOG_API_FAILURE = | ||
begin | ||
require "libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}" | ||
nil | ||
rescue LoadError => e | ||
e.message | ||
end | ||
|
||
def self.build(settings, agent_settings) | ||
tags = Core::Crashtracking::TagBuilder.call(settings) | ||
agent_base_url = Core::Crashtracking::AgentBaseUrl.resolve(agent_settings) | ||
unless agent_base_url | ||
Datadog.logger.warn('Missing agent base URL; cannot enable crash tracking') | ||
end | ||
|
||
ld_library_path = Libdatadog.ld_library_path | ||
unless ld_library_path | ||
Datadog.logger.warn('Missing ld_library_path; cannot enable crash tracking') | ||
end | ||
|
||
path_to_crashtracking_receiver_binary = Libdatadog.path_to_crashtracking_receiver_binary | ||
unless path_to_crashtracking_receiver_binary | ||
Datadog.logger.warn('Missing path_to_crashtracking_receiver_binary; cannot enable crash tracking') | ||
end | ||
|
||
return if [agent_base_url, ld_library_path, path_to_crashtracking_receiver_binary].any?(&:nil?) | ||
|
||
new( | ||
tags: tags, | ||
agent_base_url: agent_base_url, | ||
ld_library_path: ld_library_path, | ||
path_to_crashtracking_receiver_binary: path_to_crashtracking_receiver_binary | ||
) | ||
end | ||
|
||
def initialize(tags:, agent_base_url:, ld_library_path:, path_to_crashtracking_receiver_binary:) | ||
@tags = tags | ||
@agent_base_url = agent_base_url | ||
@ld_library_path = ld_library_path | ||
@path_to_crashtracking_receiver_binary = path_to_crashtracking_receiver_binary | ||
end | ||
|
||
def start | ||
start_or_update_on_fork(action: :start) | ||
end | ||
|
||
def reset_after_fork | ||
start_or_update_on_fork(action: :update_on_fork) | ||
end | ||
|
||
def stop | ||
begin | ||
self.class._native_stop | ||
Datadog.logger.debug('Crash tracking stopped successfully') | ||
rescue => e | ||
Datadog.logger.error("Failed to stop crash tracking: #{e.message}") | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :tags, :agent_base_url, :ld_library_path, :path_to_crashtracking_receiver_binary | ||
|
||
def start_or_update_on_fork(action:) | ||
# unless path_to_crashtracking_receiver_binary | ||
# Datadog.logger.warn( | ||
# "Cannot #{action} crash tracker as no path_to_crashtracking_receiver_binary was found" | ||
# ) | ||
# return | ||
# end | ||
|
||
# unless ld_library_path | ||
# Datadog.logger.warn( | ||
# "Cannot #{action} crash tracker as no ld_library_path was found" | ||
# ) | ||
# return | ||
# end | ||
|
||
begin | ||
self.class._native_start_or_update_on_fork( | ||
action: action, | ||
exporter_configuration: [:agent, agent_base_url], | ||
path_to_crashtracking_receiver_binary: path_to_crashtracking_receiver_binary, | ||
ld_library_path: ld_library_path, | ||
tags_as_array: tags.to_a, | ||
upload_timeout_seconds: 1 | ||
) | ||
Datadog.logger.debug("Crash tracking #{action} successful") | ||
rescue => e | ||
Datadog.logger.error("Failed to #{action} crash tracking: #{e.message}") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../core/configuration/ext' | ||
|
||
module Datadog | ||
module Core | ||
module Crashtracking | ||
module AgentBaseUrl | ||
module_function | ||
|
||
def resolve(agent_settings) | ||
case agent_settings.adapter | ||
when Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER | ||
"#{agent_settings.ssl ? 'https' : 'http'}://#{agent_settings.hostname}:#{agent_settings.port}/" | ||
when Datadog::Core::Configuration::Ext::Agent::UnixSocket::ADAPTER | ||
"unix://#{agent_settings.uds_path}" | ||
else | ||
Datadog.logger.warn("Unexpected adapter: #{agent_settings.adapter}") | ||
nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../core/utils' | ||
require_relative '../core/environment/socket' | ||
require_relative '../core/environment/identity' | ||
require_relative '../core/environment/git' | ||
|
||
module Datadog | ||
module Core | ||
module Crashtracking | ||
# Builds a hash of default plus user tags to be included in a profile | ||
module TagBuilder | ||
module_function | ||
|
||
def call(settings) | ||
# When changing or adding these, make sure they are kept in sync with | ||
# https://docs.google.com/spreadsheets/d/1LOGMf4c4Avbtn36uZ2SWvhIGKRPLM1BoWkUP4JYj7hA/ (Datadog internal link) | ||
tags = { | ||
'host' => Environment::Socket.hostname, | ||
'language' => Environment::Identity.lang, | ||
'process_id' => Process.pid.to_s, | ||
'profiler_version' => Environment::Identity.gem_datadog_version, | ||
'runtime' => Environment::Identity.lang, # This is known to be repeated from language, above | ||
'runtime_engine' => Environment::Identity.lang_engine, | ||
'runtime-id' => Environment::Identity.id, | ||
'runtime_platform' => Environment::Identity.lang_platform, | ||
'runtime_version' => Environment::Identity.lang_version, | ||
} | ||
|
||
tags['env'] = settings.env if settings.env | ||
tags['service'] = settings.service if settings.service | ||
tags['version'] = settings.version if settings.version | ||
tags['git.repository_url'] = Environment::Git.git_repository_url if Environment::Git.git_repository_url | ||
tags['git.commit.sha'] = Environment::Git.git_commit_sha if Environment::Git.git_commit_sha | ||
|
||
# Make sure everything is an utf-8 string, to avoid encoding issues in native code/libddprof/further downstream | ||
settings.tags.merge(tags).map do |key, value| | ||
[Datadog::Core::Utils.utf8_encode(key), Datadog::Core::Utils.utf8_encode(value)] | ||
end.to_h | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.