trailer 0.1.4 → 0.1.5

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: 5ef9d938575a0cbb5572d94c31a83abb9defb0457a8da5490f3d32125816521f
4
- data.tar.gz: 01010af54861f7127fc144e4970d00a1356081f564d59c161fd0289148224c62
3
+ metadata.gz: '080993ef6144050d45a1c16f07593acdfa29c99fd8b9a6bf5166e7bcabecd336'
4
+ data.tar.gz: '09e3d367d1246daa91c7acfca32d37d2863ab7f758d5956f948c0f067ce3d9aa'
5
5
  SHA512:
6
- metadata.gz: 276bbfbf9e53b96c3460b902976a290845afc5e8b5b1d595d4af13537497a4bef5b0e33b0413d7c622079e8b504e350b88e9dafb9c3940b280bcfad46b34501c
7
- data.tar.gz: d774b1e6d66dbeff66de86a677624e6a8e94f1edfd9b468854f1c1621f1751d011c01123982d47b29b26d0a472c5bd38e1ad2736ad15ea92f6005c82420cf770
6
+ metadata.gz: 789c1083fedc2c1c04972d0f96c35126768ac92203460da24e2fc0734144f36af708028a87db03bfe3921a583e67a28c10695c97f5f598572035e63a53ea63cf
7
+ data.tar.gz: e93b1ad62ea77c1247ab07d27d9b324b27dd44be65515ee84eaca663bfc57e13b78c4a8bb368e0b172fcdc872da8889c5aef97f28f0ac555371c211b40f98982
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.5 - 2020-09-08
4
+
5
+ ### Features
6
+
7
+ * Attach all known data to exceptions. ([#3])
8
+ * Add tests. ([#2])
9
+
10
+ [#3]: https://github.com/shuttlerock/trailer/pull/3
11
+ [#2]: https://github.com/shuttlerock/trailer/pull/2
12
+
3
13
  ## 0.1.4 - 2020-08-27
4
14
 
5
15
  ### Features
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![CircleCI](https://circleci.com/gh/Shuttlerock/trailer/tree/master.svg?style=shield)](https://circleci.com/gh/Shuttlerock/workflows/trailer/tree/master)
2
+ [![Gem Version](https://badge.fury.io/rb/trailer.svg)](https://badge.fury.io/rb/trailer)
3
+
1
4
  # Trailer
2
5
 
3
6
  Trailer provides a Ruby framework for tracing events in the context of a request or background job. It allows you to tag and log events with metadata, so that you can search later for e.g. all events and exceptions related to a particular request.
@@ -14,11 +14,12 @@ module Trailer
14
14
  def trace_event(event, resource = nil, **tags, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
15
15
  return yield block unless Trailer.enabled?
16
16
 
17
- event = Trailer::Utility.resource_name(event) unless event.is_a?(String)
17
+ event = Trailer::Utility.resource_name(event) unless event.is_a?(String) || event.is_a?(Symbol)
18
18
 
19
19
  unless resource.nil?
20
- resource_name = resource if resource.is_a?(String)
20
+ resource_name = resource if resource.is_a?(String) || resource.is_a?(Symbol)
21
21
  resource_name ||= Trailer::Utility.resource_name(resource)
22
+ resource_name ||= 'unknown'
22
23
 
23
24
  # If column_names() is available, we are probably looking at an ActiveRecord instance.
24
25
  if resource.class.respond_to?(:column_names)
@@ -27,8 +28,8 @@ module Trailer
27
28
  end
28
29
  elsif resource.respond_to?(:to_h)
29
30
  # This handles other types of data, such as GraphQL input objects.
30
- resource.to_h.stringify_keys.each do |key, value|
31
- tags[key] ||= value if key.to_s.match?(Trailer.config.auto_tag_fields) || Trailer.config.tag_fields.include?(key)
31
+ resource.to_h.transform_keys(&:to_sym).each do |key, value|
32
+ tags[key] ||= value if key.match?(Trailer.config.auto_tag_fields) || Trailer.config.tag_fields.include?(key)
32
33
  end
33
34
  end
34
35
 
@@ -53,6 +54,7 @@ module Trailer
53
54
 
54
55
  # Put the keys in alphabetical order, with the event and resource first.
55
56
  sorted = { event: event, resource: resource_name }.merge(tags.sort_by { |key, _val| key.to_s }.to_h)
57
+ sorted.transform_keys!(&:to_sym)
56
58
  RequestStore.store[:trailer].write(sorted)
57
59
 
58
60
  result
@@ -14,8 +14,9 @@ module Trailer
14
14
  :environment,
15
15
  :storage,
16
16
  :host_name,
17
- :service_name,
18
- :tag_fields
17
+ :service_name
18
+
19
+ attr_reader :tag_fields
19
20
 
20
21
  # Constructor.
21
22
  def initialize
@@ -40,7 +41,12 @@ module Trailer
40
41
  # The storage backend class to use.
41
42
  @storage = Trailer::Storage::CloudWatch
42
43
  # Optional - When tracing ActiveRecord instances, we can tag our trace with these fields explicitly.
43
- @tag_fields = %w[name]
44
+ @tag_fields = %i[name]
45
+ end
46
+
47
+ # Make sure we store tag_fields as symbols for consistency.
48
+ def tag_fields=(fields)
49
+ @tag_fields = Array(fields).flatten.map(&:to_sym)
44
50
  end
45
51
  end
46
52
  end
@@ -13,13 +13,14 @@ module Trailer
13
13
  #
14
14
  # @param err [Exception] The exception to record.
15
15
  def add_exception(err)
16
- write({ exception: err.class.name, message: err.message, trace: err.backtrace[0..9] })
16
+ write(tags.merge(exception: err.class.name, message: err.message, trace: Array(err.backtrace)[0..9]))
17
17
  end
18
18
 
19
19
  # Finish tracing, and flush storage.
20
20
  def finish
21
21
  storage.async.flush
22
22
  @trace_id = nil
23
+ @tags = {}
23
24
  end
24
25
 
25
26
  # Create a new trace ID to link log entries.
@@ -28,6 +29,7 @@ module Trailer
28
29
 
29
30
  # See https://github.com/aws/aws-xray-sdk-ruby/blob/1869ca5/lib/aws-xray-sdk/model/segment.rb#L26-L30
30
31
  @trace_id = %(1-#{Time.now.to_i.to_s(16)}-#{SecureRandom.hex(12)})
32
+ @tags = {} # This is used to accumulate tags in case we have an exception.
31
33
  end
32
34
 
33
35
  # Write the given hash to storage.
@@ -36,18 +38,19 @@ module Trailer
36
38
  def write(data)
37
39
  raise Trailer::Error, 'start() must be called before write()' if @trace_id.nil?
38
40
  raise Trailer::Error, 'data must be an instance of Hash' unless data.is_a?(Hash)
39
- raise Trailer::Error, 'could not convert data to JSON' unless data.respond_to?(:to_json)
40
41
 
41
42
  # Include some standard tags.
42
43
  data[:environment] ||= Trailer.config.environment
43
44
  data[:host_name] ||= Trailer.config.host_name
44
45
  data[:service_name] ||= Trailer.config.service_name
46
+ data = data.compact.merge(trace_id: trace_id)
45
47
 
46
- storage.async.write(data.compact.merge(trace_id: trace_id))
48
+ storage.async.write(data)
49
+ @tags.merge!(data)
47
50
  end
48
51
 
49
52
  private
50
53
 
51
- attr_accessor :storage, :trace_id
54
+ attr_accessor :storage, :tags, :trace_id
52
55
  end
53
56
  end
@@ -16,16 +16,6 @@ module Trailer
16
16
  ensure_log_stream
17
17
  end
18
18
 
19
- # Queues the given hash for writing to CloudWatch.
20
- #
21
- # @param data [Hash] A key-value hash of trace data to write to storage.
22
- def write(data)
23
- messages << {
24
- timestamp: (Time.now.utc.to_f.round(3) * 1000).to_i,
25
- message: data&.to_json,
26
- }.compact
27
- end
28
-
29
19
  # Sends all of the queued messages to CloudWatch, and resets the messages queue.
30
20
  #
31
21
  # See https://stackoverflow.com/a/36901509
@@ -49,6 +39,18 @@ module Trailer
49
39
  retry
50
40
  end
51
41
 
42
+ # Queues the given hash for writing to CloudWatch.
43
+ #
44
+ # @param data [Hash] A key-value hash of trace data to write to storage.
45
+ def write(data)
46
+ return if data.empty?
47
+
48
+ messages << {
49
+ timestamp: (Time.now.utc.to_f.round(3) * 1000).to_i,
50
+ message: data&.to_json,
51
+ }.compact
52
+ end
53
+
52
54
  private
53
55
 
54
56
  attr_accessor :client, :messages, :sequence_token
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailer
4
+ module Storage
5
+ class Null
6
+ include Concurrent::Async
7
+
8
+ # Pretends to queue the given hash for writing.
9
+ #
10
+ # @param data [Hash] A key-value hash of trace data to write to storage.
11
+ def write(_data); end
12
+
13
+ # Pretends to flush the queued messages to the storage provider.
14
+ def flush; end
15
+ end
16
+ end
17
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Trailer
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.5'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dave Perrett
@@ -34,7 +34,7 @@ cert_chain:
34
34
  3Um/SScB6alBpv/eTa/qPXa+S84pZDU6kFDasnH7GEl6jYlVxZbzpHUIvkDIOp5J
35
35
  6O1XOcLc39AfX5etu1WZ+wx3xUzux0oqS6rXcl63HmuKe8Son7RqJQ==
36
36
  -----END CERTIFICATE-----
37
- date: 2020-08-27 00:00:00.000000000 Z
37
+ date: 2020-09-08 00:00:00.000000000 Z
38
38
  dependencies:
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: aws-sdk-cloudwatchlogs
@@ -247,6 +247,7 @@ files:
247
247
  - lib/trailer/railtie.rb
248
248
  - lib/trailer/recorder.rb
249
249
  - lib/trailer/storage/cloud_watch.rb
250
+ - lib/trailer/storage/null.rb
250
251
  - lib/trailer/utility.rb
251
252
  - lib/trailer/version.rb
252
253
  - trailer.gemspec
metadata.gz.sig CHANGED
Binary file