traceable 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/traceable/args.rb +55 -0
- data/lib/traceable/class_methods.rb +9 -7
- data/lib/traceable/version.rb +1 -1
- data/lib/traceable.rb +1 -0
- data/spec/traceable_args_spec.rb +46 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88b2d98b02d74da456bfdd4cd8e778e32b59e726
|
4
|
+
data.tar.gz: c1b4c9f98b6681d6221c7445b30f98932d3b87dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a796c8e3c1fee4476dc61bca60c74dbbc8fbd70ada3212e9092cb87b4bb3c911d461fc7e98141ea4384a46b9992b19d3549f198cb2876cc198955cdb909dc8d
|
7
|
+
data.tar.gz: 354c89710e81545b518ccf44358405bbe2faa37cdee58cce1eae40020423e07fc557a844a68177b46b1986a6229f0635a55cdba2c37269809ea7997ad5755404
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Traceable
|
4
|
+
class Args
|
5
|
+
# parameters comes from calling method(:foo).parameters,
|
6
|
+
# so it is an array of parameter descriptors e.g.
|
7
|
+
# [ [:req, :a], [:keyreq, :b] ]
|
8
|
+
# values is an array of values from an actual method invocation,
|
9
|
+
# so the two arrays are expected to match in length
|
10
|
+
def self.args_to_tags(parameters, values)
|
11
|
+
tags = {}
|
12
|
+
parameters.each_with_index do |param, i|
|
13
|
+
tags[param[1]] = format_value(values[i])
|
14
|
+
end
|
15
|
+
tags
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.format_value(val)
|
19
|
+
return val.to_trace if val.respond_to? :to_trace
|
20
|
+
return format_array_of_values(val) if val.is_a? Array
|
21
|
+
return format_hash_of_values(val) if val.is_a? Hash
|
22
|
+
val
|
23
|
+
end
|
24
|
+
|
25
|
+
MAX_ARRAY_VALUES = 10
|
26
|
+
|
27
|
+
def self.format_array_of_values(val_array)
|
28
|
+
return format_array_of_values(truncated_array(val_array)) \
|
29
|
+
if val_array.size > MAX_ARRAY_VALUES
|
30
|
+
val_array.map { |v| format_value(v) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.truncated_array(val_array)
|
34
|
+
subset = val_array[0..MAX_ARRAY_VALUES - 2]
|
35
|
+
subset << "...(#{val_array.size - subset.size})"
|
36
|
+
subset
|
37
|
+
end
|
38
|
+
|
39
|
+
MAX_HASH_KEYS = 10
|
40
|
+
|
41
|
+
def self.format_hash_of_values(val_hash)
|
42
|
+
return format_hash_of_values(truncated_hash(val_hash)) \
|
43
|
+
if val_hash.size > MAX_HASH_KEYS
|
44
|
+
Hash[val_hash.map { |k, v| [k, format_value(v)] }]
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.truncated_hash(val_hash)
|
48
|
+
first_keys = val_hash.keys[0..MAX_HASH_KEYS - 2]
|
49
|
+
first_vals = first_keys.map { |k| val_hash[k] }
|
50
|
+
first_keys << :___
|
51
|
+
first_vals << "...(#{val_hash.size - first_vals.size})"
|
52
|
+
Hash[first_keys.zip(first_vals)]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -29,13 +29,15 @@ module Traceable
|
|
29
29
|
# end
|
30
30
|
#
|
31
31
|
# The generated log message(s) will include the class name and method name
|
32
|
-
def traced_method(method_name)
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
def traced_method(method_name, include_args: true)
|
33
|
+
trace_name = "#{name}##{method_name}"
|
34
|
+
orig_method = instance_method(method_name)
|
35
|
+
params = orig_method.parameters
|
36
|
+
include_args = false if params.empty?
|
36
37
|
|
37
|
-
|
38
|
-
|
38
|
+
send(:define_method, method_name) do |*args, &block|
|
39
|
+
trace_tags = include_args ? Traceable::Args.args_to_tags(params, args) : {}
|
40
|
+
trace(trace_name, **trace_tags) do
|
39
41
|
if block
|
40
42
|
orig_method.bind(self).call(*args) { |*block_args| block.call(*block_args) }
|
41
43
|
else
|
@@ -47,7 +49,7 @@ module Traceable
|
|
47
49
|
method_name
|
48
50
|
end
|
49
51
|
|
50
|
-
# For the (relatively rare) case of calling trace() in a class method
|
52
|
+
# For the (relatively rare?) case of calling trace() in a class method
|
51
53
|
# of a Traceable class - creates a new Tracer instance
|
52
54
|
def trace(msg = nil, **tags)
|
53
55
|
tracer = Tracer.new(Tracer.default_parent)
|
data/lib/traceable/version.rb
CHANGED
data/lib/traceable.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Traceable::Args do
|
4
|
+
describe '#args_to_tags' do
|
5
|
+
class Foo
|
6
|
+
class << self
|
7
|
+
def m1(a, b); end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:m1_params) { Foo.method(:m1).parameters }
|
12
|
+
|
13
|
+
it 'converts simple params' do
|
14
|
+
expect(Traceable::Args.args_to_tags(m1_params, [1, 2]))
|
15
|
+
.to eq(a: 1, b: 2)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'converts array params' do
|
19
|
+
expect(Traceable::Args.args_to_tags(m1_params, [[1, 2], %i[a b c]]))
|
20
|
+
.to eq(a: [1, 2], b: %i[a b c])
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'converts hash params' do
|
24
|
+
expect(Traceable::Args.args_to_tags(m1_params, [{ x: 1, y: 2 }, { 'z' => 3 }]))
|
25
|
+
.to eq(a: { x: 1, y: 2 }, b: { 'z' => 3 })
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:letters) { %w[a b c d e f g h i j k l m n o p q r s t u v w x y z] }
|
30
|
+
|
31
|
+
describe '#format_array_of_values' do
|
32
|
+
it 'truncates long arrays' do
|
33
|
+
expect(Traceable::Args.format_array_of_values(letters))
|
34
|
+
.to eq(%w[a b c d e f g h i ...(17)])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#format_hash_of_values' do
|
39
|
+
it 'truncates long hashes' do
|
40
|
+
h = Hash[letters.map { |l| [l.to_sym, l] }]
|
41
|
+
expect(Traceable::Args.format_hash_of_values(h))
|
42
|
+
.to eq(a: 'a', b: 'b', c: 'c', d: 'd', e: 'e',
|
43
|
+
f: 'f', g: 'g', h: 'h', i: 'i', ___: '...(17)')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: traceable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Slade
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-02-
|
11
|
+
date: 2018-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,11 +116,13 @@ extensions: []
|
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
118
|
- lib/traceable.rb
|
119
|
+
- lib/traceable/args.rb
|
119
120
|
- lib/traceable/class_methods.rb
|
120
121
|
- lib/traceable/config.rb
|
121
122
|
- lib/traceable/tracer.rb
|
122
123
|
- lib/traceable/version.rb
|
123
124
|
- spec/spec_helper.rb
|
125
|
+
- spec/traceable_args_spec.rb
|
124
126
|
- spec/traceable_spec.rb
|
125
127
|
homepage: https://github.com/instructure/inst-jobs-statsd
|
126
128
|
licenses:
|
@@ -148,4 +150,5 @@ specification_version: 4
|
|
148
150
|
summary: Instrument code with logging
|
149
151
|
test_files:
|
150
152
|
- spec/spec_helper.rb
|
153
|
+
- spec/traceable_args_spec.rb
|
151
154
|
- spec/traceable_spec.rb
|