Skip to content

Commit

Permalink
Support running in situations where Gem.loaded_specs is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoanjo committed Oct 1, 2024
1 parent 19646f2 commit 6b67c03
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/datadog/profiling/collectors/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "set"
require "time"
require "libdatadog"

module Datadog
module Profiling
Expand Down Expand Up @@ -62,11 +63,19 @@ def collect_application_info(settings)
def collect_profiler_info(settings)
unless @profiler_info
lib_datadog_gem = ::Gem.loaded_specs["libdatadog"]

libdatadog_version =
if lib_datadog_gem
"#{lib_datadog_gem.version}-#{lib_datadog_gem.platform}"
else
# In some cases, Gem.loaded_specs may not be available, as in
# https://github.com/DataDog/dd-trace-rb/pull/1506; let's use the version directly
"#{Libdatadog::VERSION}-(unknown)"
end

@profiler_info = {
# TODO: If profiling is extracted and its version diverges from the datadog gem, this is inaccurate.
# Update if this ever occurs.
version: Datadog::Core::Environment::Identity.gem_datadog_version,
libdatadog: "#{lib_datadog_gem.version}-#{lib_datadog_gem.platform}",
libdatadog: libdatadog_version,
settings: collect_settings_recursively(settings.profiling),
}.freeze
end
Expand Down
17 changes: 17 additions & 0 deletions spec/datadog/profiling/collectors/info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@
)
end
end

describe "libdatadog version reporting" do
it "reports the libdatadog version, including platform" do
libdatadog_gem = Gem.loaded_specs["libdatadog"]

expect(info.dig(:profiler, :libdatadog)).to eq("#{libdatadog_gem.version}-#{libdatadog_gem.platform}")
end

context "when libdatadog is not available in loaded_specs" do
it "reports the libdatadog version, with an unknown platform" do
expect(Gem.loaded_specs).to receive(:[]).with("libdatadog").and_return(nil)
allow(Gem.loaded_specs).to receive(:[]).and_call_original

expect(info.dig(:profiler, :libdatadog)).to eq("#{Libdatadog::VERSION}-(unknown)")
end
end
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions vendor/rbs/libdatadog/0/libdatadog.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ module Libdatadog
def self.path_to_crashtracking_receiver_binary: () -> ::String?
def self.ld_library_path: () -> ::String?
def self.pkgconfig_folder: () -> ::String?

VERSION: ::String
end

0 comments on commit 6b67c03

Please sign in to comment.