-
Notifications
You must be signed in to change notification settings - Fork 373
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
12 changed files
with
168 additions
and
31 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,14 @@ | ||
require 'ddtrace/registry' | ||
|
||
module Datadog | ||
module Contrib | ||
module Base | ||
def self.included(base) | ||
base.class_eval do | ||
include Registry::Registerable | ||
module_function :register_as | ||
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
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
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,41 @@ | ||
require_relative 'registry/registerable' | ||
|
||
module Datadog | ||
class Registry | ||
include Enumerable | ||
|
||
Entry = Struct.new(:name, :klass, :auto_patch?) | ||
|
||
def initialize | ||
@data = {} | ||
@mutex = Mutex.new | ||
end | ||
|
||
def add(name, klass, auto_patch = false) | ||
@mutex.synchronize do | ||
@data[name] = Entry.new(name, klass, auto_patch).freeze | ||
end | ||
end | ||
|
||
def each | ||
@mutex.synchronize do | ||
@data.each { |_, entry| yield(entry) } | ||
end | ||
end | ||
|
||
def [](name) | ||
@mutex.synchronize do | ||
entry = @data[name] | ||
entry.klass if entry | ||
end | ||
end | ||
|
||
def to_h | ||
@mutex.synchronize do | ||
@data.each_with_object({}) do |(_, entry), hash| | ||
hash[entry.name] = entry.auto_patch? | ||
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,12 @@ | ||
module Datadog | ||
class Registry | ||
module Registerable | ||
def register_as(name, options = {}) | ||
registry = options.fetch(:registry, Datadog.registry) | ||
auto_patch = options.fetch(:auto_patch, false) | ||
|
||
registry.add(name, self, auto_patch) | ||
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,60 @@ | ||
require 'minitest/autorun' | ||
require 'ddtrace' | ||
require 'ddtrace/registry' | ||
|
||
module Datadog | ||
class RegistryTest < Minitest::Test | ||
def test_object_retrieval | ||
registry = Registry.new | ||
|
||
object1 = Object.new | ||
object2 = Object.new | ||
|
||
registry.add(:object1, object1) | ||
registry.add(:object2, object2) | ||
|
||
assert_same(object1, registry[:object1]) | ||
assert_same(object2, registry[:object2]) | ||
end | ||
|
||
def test_hash_coercion | ||
registry = Registry.new | ||
|
||
object1 = Object.new | ||
object2 = Object.new | ||
|
||
registry.add(:object1, object1, true) | ||
registry.add(:object2, object2, false) | ||
|
||
assert_equal({ :object1 => true, :object2 => false }, registry.to_h) | ||
end | ||
|
||
def test_enumeration | ||
registry = Registry.new | ||
|
||
object1 = Object.new | ||
object2 = Object.new | ||
|
||
registry.add(:object1, object1, true) | ||
registry.add(:object2, object2, false) | ||
|
||
assert(registry.respond_to?(:each)) | ||
assert_kind_of(Enumerable, registry) | ||
|
||
# Enumerable#map | ||
objects = registry.map(&:klass) | ||
assert_kind_of(Array, objects) | ||
assert_equal(2, objects.size) | ||
assert_includes(objects, object1) | ||
assert_includes(objects, object2) | ||
end | ||
|
||
def test_registry_entry | ||
entry = Registry::Entry.new(:array, Array, true) | ||
|
||
assert_equal(:array, entry.name) | ||
assert_equal(Array, entry.klass) | ||
assert_equal(true, entry.auto_patch?) | ||
end | ||
end | ||
end |