l2meter 0.0.7 → 0.0.8

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
  SHA1:
3
- metadata.gz: dd6772c37f01a4c133505ba3a9ae1f4024169e2c
4
- data.tar.gz: 21fa24020d0957dc14dd2e83c178dcc8904a436a
3
+ metadata.gz: bffe75a295c408136d1a2e4aa04bed633933381e
4
+ data.tar.gz: 5b3dbea7648cee86c9cef54334ec568e5a7a044f
5
5
  SHA512:
6
- metadata.gz: 55d30f2dab010527aea006c4ad0653f0571ce5b8c51b1ce03cde832a0b9419a0be146b17385b2ef3eb1e0b404e7fb8482e3ec09ba913fcdfa0042bfb5a6d95df
7
- data.tar.gz: aa3f76c6f75db2f899579440305ce029c61a96b2182954972f11601f9c5942fa565d225181ed70259fac6514eadcb0fe94df4767dec8faea23159dfb1eeaeb2f
6
+ metadata.gz: 9ee9b15dc8fa10b16379dcc291dd40e89aa6bd7dff8b471395ea2291493556e4cb6806b81182b023efdfc758b15603afe7c159dee86fbac15ac1747cd1b77569
7
+ data.tar.gz: 3d3537ffa3a3b8593a2236590ad4f5d0e2c4fe7181b757fc5eacd418372f81970d9e82e8602bfdff52693c3de61eede19cf7f643d7ae63f99c34752fc0393a76
data/README.md CHANGED
@@ -102,6 +102,17 @@ metrics.sample :db_query, 235, unit: :ms, # => sample#db-query.ms=235
102
102
  metrics.unique :user, "bob@example.com" # => unique#user=bob@example.com
103
103
  ```
104
104
 
105
+ L2meter also allows to append elapsed time to your log messages automatically.
106
+
107
+ ```ruby
108
+ metrics.with_elapsed do
109
+ do_work_step_1
110
+ log :step_1_done # => step-1-done elapsed=1.2345s
111
+ do_work_step_2
112
+ log :step_2_done # => step-2-done elapsed=2.3456s
113
+ end
114
+ ```
115
+
105
116
  ### Configuration
106
117
 
107
118
  L2meter supports configuration. Here's how you can configure things:
@@ -4,14 +4,12 @@ module L2meter
4
4
 
5
5
  def initialize(configuration: Configuration.new)
6
6
  @configuration = configuration
7
+ @start_times = []
7
8
  end
8
9
 
9
10
  def log(*args)
10
- params = Hash === args.last ? args.pop : {}
11
- args = args.map { |key| [ key, true ] }.to_h
12
- params = args.merge(params)
13
- params = current_context.merge(params)
14
- params = merge_source(params)
11
+ params = transform_log_args(*args)
12
+ params = merge_contexts(params)
15
13
 
16
14
  if block_given?
17
15
  wrap params, &Proc.new
@@ -20,6 +18,13 @@ module L2meter
20
18
  end
21
19
  end
22
20
 
21
+ def with_elapsed
22
+ @start_times << Time.now
23
+ yield
24
+ ensure
25
+ @start_times.pop
26
+ end
27
+
23
28
  def silence
24
29
  output = configuration.output
25
30
  configuration.output = NullObject.new
@@ -53,13 +58,31 @@ module L2meter
53
58
 
54
59
  private
55
60
 
56
- def configuration_contexts
57
- configuration.contexts
61
+ def transform_log_args(*args)
62
+ params = Hash === args.last ? args.pop : {}
63
+ args = args.map { |key| [ key, true ] }.to_h
64
+ args.merge(params)
58
65
  end
59
66
 
60
- def merge_source(params)
67
+ def merge_contexts(params)
68
+ params = current_context.merge(params)
61
69
  source = configuration.source
62
- source ? { source: source }.merge(params) : params
70
+ params = { source: source }.merge(params) if source
71
+
72
+ if start_time = @start_times.last
73
+ elapsed = Time.now - start_time
74
+ params = merge_elapsed(elapsed, params)
75
+ end
76
+
77
+ params
78
+ end
79
+
80
+ def merge_elapsed(elapsed, params)
81
+ params.merge(elapsed: "%.4fs" % elapsed)
82
+ end
83
+
84
+ def configuration_contexts
85
+ configuration.contexts
63
86
  end
64
87
 
65
88
  def current_context
@@ -99,22 +122,33 @@ module L2meter
99
122
  end
100
123
 
101
124
  def wrap(params)
102
- time_at_start = Time.now
103
125
  write params.merge(at: :start)
104
- result = yield
105
- rescue Exception => error
106
- # Rescuing Exception class is a well-known anitpattern, I know. But in
107
- # this case we're just setting a variable and re-raising immidiately,
108
- # which should probably be fine in most cases.
109
- status = { at: :exception, exception: error.class.to_s, message: error.message.strip }
110
- raise
111
- else
112
- status = { at: :finish }
126
+
127
+ result, exception, elapsed = execute_with_elapsed(&Proc.new)
128
+
129
+ if exception
130
+ status = { at: :exception, exception: exception.class.name, message: exception.message }
131
+ else
132
+ status = { at: :finish }
133
+ end
134
+
135
+ status = merge_elapsed(elapsed, status)
136
+
137
+ write params.merge(status)
138
+
139
+ raise exception if exception
140
+
113
141
  result
142
+ end
143
+
144
+ def execute_with_elapsed
145
+ time_at_start = Time.now
146
+ caught_exception = nil
147
+ result = yield
148
+ rescue Exception => exception
149
+ caught_exception = exception
114
150
  ensure
115
- elapsed = Time.now - time_at_start
116
- status.merge! elapsed: "%.4fs" % elapsed
117
- write params.merge(status)
151
+ return [ result, caught_exception, Time.now - time_at_start ]
118
152
  end
119
153
  end
120
154
  end
@@ -1,3 +1,3 @@
1
1
  module L2meter
2
- VERSION = "0.0.7".freeze
2
+ VERSION = "0.0.8".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: l2meter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Pravosud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-21 00:00:00.000000000 Z
11
+ date: 2015-09-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: