conrad 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb4af3e930e6793ecb9a869e52c0150ea2aa87753b3f062713c872b5042ea754
4
- data.tar.gz: 73c9bdc8e25b35527cf7064c934ef4a5762c2705c2b6c1c22bcc56c102fab547
3
+ metadata.gz: fd153974202827badd3ab0b1e74ac823ac9ad67d7ac17ae627afcd0c9c9c5f93
4
+ data.tar.gz: b549b8ac875f6b0654f693c751946192158538ba3d56fc29b3e4bb896e665467
5
5
  SHA512:
6
- metadata.gz: e16f4db32806aa47a4411a6b9dbc5964eb522f477f48d2e97995839636b0b66e73bbc6392f02756d12e4a9f8244d1f17d6693a17082c08a5ac623de2408495dd
7
- data.tar.gz: a866c01f282c5cfa5b1b71c83d9653dd697f25252f448d3df1b772d05bcc838bf127f4fb92a36f3848372ded0aac1473ca68761620a1309a0ce00456a9a18d99
6
+ metadata.gz: e304189667f80f0e6e7e8a206881d9cfa17d4289d1737ce0c7f96b655a97da5c266f8e3435586bfe9e2b3d3e8b88a112485d27bc53bf3ee5c29a72525a69d896
7
+ data.tar.gz: 3c95e0f400d386782d8dd00f1ff8203e67f46799b8fa93a538c4419ca5b58fe326d1fcbb6672939a6c12b1fc7e9358e42ddb737247dfa2cb60c725770fc4b9d8
@@ -0,0 +1,28 @@
1
+ module Conrad
2
+ # A module containing all of conrad's built in event emitters for outputting events
3
+ module Emitters
4
+ # Basic emitter for sending events to AWS's sqs.
5
+ class Sqs
6
+ attr_reader :queue_url, :region, :access_key_id, :secret_access_key
7
+
8
+ # Takes in and stores SQS region, url and creds for accessing a queue.
9
+ def initialize(queue_url:, region:, access_key_id:, secret_access_key:)
10
+ @queue_url = queue_url
11
+ @region = region
12
+ @access_key_id = access_key_id
13
+ @secret_access_key = secret_access_key
14
+
15
+ @client ||= Aws::SQS::Client.new(
16
+ region: region,
17
+ access_key_id: access_key_id,
18
+ secret_access_key: secret_access_key
19
+ )
20
+ end
21
+
22
+ # Sends an event up to SQS
23
+ def call(event)
24
+ @client.send_message(queue_url: queue_url, message_body: event)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ module Conrad
2
+ module Emitters
3
+ # Basic emitter for sending events to $stdout.
4
+ class Stdout
5
+ # Puts an event to $stdout.
6
+ def call(event)
7
+ puts event
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Conrad
6
+ # A module containing all of conrad's event formatters.
7
+ module Formatters
8
+ # Formats a given Hash into a presentable JSON format.
9
+ class JSON
10
+ # Formats a given Hash into a presentable JSON format.
11
+ #
12
+ # @param event [Hash] event to be formatted
13
+ #
14
+ # @return [String] JSON formatted string
15
+ def call(event)
16
+ event.to_json
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,54 @@
1
+ require 'conrad/errors'
2
+
3
+ module Conrad
4
+ module Processors
5
+ # Used to add timestamps to an audit event in seconds or milliseconds.
6
+ #
7
+ # @!attribute [r] generator
8
+ # object used to generate the timestamp
9
+ class AddTimestamp
10
+ # :nodoc:
11
+ class Error < Conrad::Error; end
12
+
13
+ # Types of units supported for generation.
14
+ ALLOWED_TIME_UNITS = %i[milliseconds seconds].freeze
15
+
16
+ attr_reader :generator, :timestamp_key
17
+
18
+ # Creates a new instance of AddTimestmap processor
19
+ #
20
+ # @param units [Symbol] type of time units for the timestamp generated.
21
+ # Allows :seconds or :milliseconds.
22
+ # @param timestamp_key [Symbol] key to add to the event hash.
23
+ # @raise [ArgumentError] if the given units value is not one of
24
+ # ALLOWED_TIME_UNITS
25
+ def initialize(units: :milliseconds, timestamp_key: :timestamp)
26
+ unless ALLOWED_TIME_UNITS.include? units
27
+ raise ArgumentError, "Provided units of `#{units}` must be one of #{ALLOWED_TIME_UNITS}"
28
+ end
29
+
30
+ @generator = generator_from_units(units)
31
+ @timestamp_key = timestamp_key
32
+ end
33
+
34
+ # Generates and adds a timestamp to the provided Hash.
35
+ #
36
+ # @param event [Hash]
37
+ # @return [Hash]
38
+ def call(event)
39
+ event.merge(timestamp_key => generator.call)
40
+ end
41
+
42
+ private
43
+
44
+ def generator_from_units(units)
45
+ case units
46
+ when :milliseconds then -> { (Time.now.to_f * 1000).to_i }
47
+ when :seconds then -> { Time.now.to_i }
48
+ else
49
+ raise UnrecognizedTimeUnit, units
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,27 @@
1
+ require 'securerandom'
2
+
3
+ module Conrad
4
+ # A module containing conrad's processors. They're used to modify events or add metadata before being output.
5
+ module Processors
6
+ # Generalized processor for inserting a UUID into the event. Allows
7
+ # configuring the key used for insertion.
8
+ #
9
+ # @!attribute [r] uuid_key
10
+ # The key inserted into the event hash for the generated UUID.
11
+ class AddUUID
12
+ attr_reader :uuid_key
13
+
14
+ # @param uuid_key [Symbol] key to use for the generated UUID
15
+ def initialize(uuid_key = :event_uuid)
16
+ @uuid_key = uuid_key
17
+ end
18
+
19
+ # @param event [Hash] the current event
20
+ #
21
+ # @return [Hash] the hash with the UUID inserted
22
+ def call(event)
23
+ event.merge(uuid_key => SecureRandom.uuid)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,45 @@
1
+ module Conrad
2
+ module Processors
3
+ # Processor class used to take a given event, extract a set of attributes from
4
+ # it, then wrap the remaining attributes into a payload attribute. This allows
5
+ # downstream consumers to know about the outermost structure (the envelope)
6
+ # without needing to account for all possible permutations of the event. This
7
+ # also allows routing through various systems on a small number of values. If
8
+ # a given envelope key does not exist in the original event, then it will be
9
+ # set to nil in the resulting event.
10
+ #
11
+ # When using this processor, it is highly recommended that this be the last
12
+ # processor unless you need to act on the wrapped event.
13
+ #
14
+ # @attribute envelope_keys [r]
15
+ # @attribute payload_key [r]
16
+ class Envelope
17
+ attr_reader :envelope_keys, :payload_key
18
+
19
+ # @param envelope_keys [Array<Symbol>] the keys to extract from the event.
20
+ # NOTE: These must be exact matches of both value and type (i.e. Strings
21
+ # and Symbols should not be considered interchangeable, and the event
22
+ # must be created with the attribute keys matching the types given here)
23
+ # @param payload_key [Symbol] key to wrap the remainder of the event inside
24
+ #
25
+ # @raise [TypeError] if the envelope_keys argument is not an Array
26
+ def initialize(envelope_keys, payload_key: :payload)
27
+ raise TypeError, 'envelope_keys must be an Array' unless envelope_keys.is_a? Array
28
+
29
+ @envelope_keys = envelope_keys
30
+ @payload_key = payload_key
31
+ end
32
+
33
+ # @param event [Hash] event to be wrapped in the configured envelope
34
+ #
35
+ # @return [Hash] the wrapped event
36
+ def call(event)
37
+ envelope = envelope_keys.each_with_object({}) do |key, obj|
38
+ obj[key] = event.delete(key)
39
+ end
40
+
41
+ envelope.merge(payload_key => event)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1 @@
1
+ Dir['conrad/processors/*.rb'].each { |file| require file }
@@ -1,6 +1,6 @@
1
1
  require 'conrad/errors'
2
- require 'conrad/stdout_emitter'
3
- require 'conrad/json_formatter'
2
+ require 'conrad/emitters/stdout'
3
+ require 'conrad/formatters/json'
4
4
 
5
5
  module Conrad
6
6
  # Provides the ability to record an event took place.
@@ -10,12 +10,12 @@ module Conrad
10
10
  #
11
11
  # @!attribute [r] formatter
12
12
  # Configured formatter for creating the final event. Defaults to
13
- # JSONFormatter.
14
- # @see Conrad::JSONFormatter
13
+ # JSON.
14
+ # @see Conrad::JSON
15
15
  # @!attribute [r] emitter
16
16
  # Configured emitter for sending the final event. Defaults to
17
- # StdoutEmitter.
18
- # @see Conrad::StdoutEmitter
17
+ # Stdout.
18
+ # @see Conrad::Stdout
19
19
  # @!attribute [r] processors
20
20
  # Configured processors for processing the event pre-formatting and
21
21
  # emission. Defaults to an empty array.
@@ -31,7 +31,11 @@ module Conrad
31
31
  #
32
32
  # @raise [ArgumentError] if the formatter, emitter, or any of the
33
33
  # processors do not respond_to? `call` with a truthy value.
34
- def initialize(formatter: JSONFormatter.new, emitter: StdoutEmitter.new, processors: [])
34
+ def initialize(
35
+ formatter: Conrad::Formatters::JSON.new,
36
+ emitter: Conrad::Emitters::Stdout.new,
37
+ processors: []
38
+ )
35
39
  check_callability(formatter: formatter, emitter: emitter, processors: processors)
36
40
 
37
41
  @formatter = formatter
@@ -1,5 +1,5 @@
1
1
  # :nodoc:
2
2
  module Conrad
3
3
  # :nodoc:
4
- VERSION = '1.0.0'.freeze
4
+ VERSION = '2.0.0'.freeze
5
5
  end
data/lib/conrad.rb CHANGED
@@ -1,4 +1,7 @@
1
- require 'conrad/recorder'
1
+ Gem.find_files('conrad/processors/*.rb').each { |file| require file }
2
+ Gem.find_files('conrad/formatters/*.rb').each { |file| require file }
3
+ Gem.find_files('conrad/emitters/*.rb').each { |file| require file }
4
+ Gem.find_files('conrad/*.rb').each { |file| require file }
2
5
 
3
6
  # :nodoc:
4
7
  module Conrad
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conrad
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathon Anderson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-21 00:00:00.000000000 Z
11
+ date: 2019-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: minitest
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -60,12 +74,15 @@ extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
76
  - lib/conrad.rb
63
- - lib/conrad/add_timestamp.rb
64
- - lib/conrad/add_uuid.rb
77
+ - lib/conrad/emitters/sqs.rb
78
+ - lib/conrad/emitters/stdout.rb
65
79
  - lib/conrad/errors.rb
66
- - lib/conrad/json_formatter.rb
80
+ - lib/conrad/formatters/json.rb
81
+ - lib/conrad/processors.rb
82
+ - lib/conrad/processors/add_timestamp.rb
83
+ - lib/conrad/processors/add_uuid.rb
84
+ - lib/conrad/processors/envelope.rb
67
85
  - lib/conrad/recorder.rb
68
- - lib/conrad/stdout_emitter.rb
69
86
  - lib/conrad/version.rb
70
87
  homepage: https://github.com/getoutreach/conrad
71
88
  licenses:
@@ -1,52 +0,0 @@
1
- require 'conrad/errors'
2
-
3
- module Conrad
4
- # Used to add timestamps to an audit event in seconds or milliseconds.
5
- #
6
- # @!attribute [r] generator
7
- # object used to generate the timestamp
8
- class AddTimestamp
9
- # :nodoc:
10
- class Error < Conrad::Error; end
11
-
12
- # Types of units supported for generation.
13
- ALLOWED_TIME_UNITS = %i[milliseconds seconds].freeze
14
-
15
- attr_reader :generator, :timestamp_key
16
-
17
- # Creates a new instance of AddTimestmap processor
18
- #
19
- # @param units [Symbol] type of time units for the timestamp generated.
20
- # Allows :seconds or :milliseconds.
21
- # @param timestamp_key [Symbol] key to add to the event hash.
22
- # @raise [ArgumentError] if the given units value is not one of
23
- # ALLOWED_TIME_UNITS
24
- def initialize(units: :milliseconds, timestamp_key: :timestamp)
25
- unless ALLOWED_TIME_UNITS.include? units
26
- raise ArgumentError, "Provided units of `#{units}` must be one of #{ALLOWED_TIME_UNITS}"
27
- end
28
-
29
- @generator = generator_from_units(units)
30
- @timestamp_key = timestamp_key
31
- end
32
-
33
- # Generates and adds a timestamp to the provided Hash.
34
- #
35
- # @param event [Hash]
36
- # @return [Hash]
37
- def call(event)
38
- event.merge(timestamp_key => generator.call)
39
- end
40
-
41
- private
42
-
43
- def generator_from_units(units)
44
- case units
45
- when :milliseconds then -> { (Time.now.to_f * 1000).to_i }
46
- when :seconds then -> { Time.now.to_i }
47
- else
48
- raise UnrecognizedTimeUnit, units
49
- end
50
- end
51
- end
52
- end
@@ -1,24 +0,0 @@
1
- require 'securerandom'
2
-
3
- module Conrad
4
- # Generalized processor for inserting a UUID into the event. Allows
5
- # configuring the key used for insertion.
6
- #
7
- # @!attribute [r] uuid_key
8
- # The key inserted into the event hash for the generated UUID.
9
- class AddUUID
10
- attr_reader :uuid_key
11
-
12
- # @param uuid_key [Symbol] key to use for the generated UUID
13
- def initialize(uuid_key = :event_uuid)
14
- @uuid_key = uuid_key
15
- end
16
-
17
- # @param event [Hash] the current event
18
- #
19
- # @return [Hash] the hash with the UUID inserted
20
- def call(event)
21
- event.merge(uuid_key => SecureRandom.uuid)
22
- end
23
- end
24
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
-
5
- module Conrad
6
- # Formats a given Hash into a presentable JSON format.
7
- class JSONFormatter
8
- # Formats a given Hash into a presentable JSON format.
9
- #
10
- # @param event [Hash] event to be formatted
11
- #
12
- # @return [String] JSON formatted string
13
- def call(event)
14
- event.to_json
15
- end
16
- end
17
- end
@@ -1,9 +0,0 @@
1
- module Conrad
2
- # Basic emitter for sending events to $stdout.
3
- class StdoutEmitter
4
- # Puts an event to $stdout.
5
- def call(event)
6
- puts event
7
- end
8
- end
9
- end