-
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
17 changed files
with
190 additions
and
37 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
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 @@ | ||
require 'ddtrace/registry' | ||
|
||
module Datadog | ||
module Contrib | ||
# Base provides features that are shared across all integrations | ||
module Base | ||
def self.included(base) | ||
base.send(:include, Registry::Registerable) | ||
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
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,42 @@ | ||
require_relative 'registry/registerable' | ||
|
||
module Datadog | ||
# Registry provides insertion/retrieval capabilities for integrations | ||
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,20 @@ | ||
module Datadog | ||
class Registry | ||
# Registerable provides a convenience method for self-registering | ||
module Registerable | ||
def self.included(base) | ||
base.singleton_class.send(:include, ClassMethods) | ||
end | ||
|
||
# ClassMethods | ||
module ClassMethods | ||
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 | ||
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 |