datadog-lambda 0.4.0 → 0.5.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: d37e92d79f90e4049e303719d3639560e7b025e20a62c2199342603e9f76f71d
4
- data.tar.gz: c82eb5b84734b2621220eb402e57ba1f88855f39ba94e6567bdde4a6bc6ea80c
3
+ metadata.gz: 8037783498999fb1a3291a4b6c72d13933efc9c4056b08f96e560debf804797d
4
+ data.tar.gz: 8f1f8794b9fd7a7886715afe3d41e9398e0d110ace2279c5bec2e2a34d2d60d7
5
5
  SHA512:
6
- metadata.gz: d6154dce5ec1743195799df476f5b0da7f13db96015822ecfcc4b42e560ba042b9b403355b33a060417c05efa2b9a99975e8fedc540cfea73c1f8e872b8d3985
7
- data.tar.gz: 649a76d46ce8fc2ab5f22590137448ec77d518f7f27cb7518300a90ee00fae638250de6ebb486bcf8c3346fa7074257f901dab00b643b30b32e8a2611cf919e1
6
+ metadata.gz: 98545e61a901be1b3441ba52d197ea69dfe6eee9469d571b2a77e979d4f73c56e133cc0d9502c003342a5a78ee50fea4ca4fb0af6ad7bd3a47d9bd8d5aede373
7
+ data.tar.gz: d27d0e7a1a9774347fa5865ce508b09f91137b56e400614ad15c06ba3492a977e1734b2015d380f1d14c36b896ace3bfc64e4e4839b2533280c8d411c30503c7
@@ -18,19 +18,24 @@ module Datadog
18
18
  # Instruments AWS Lambda functions with Datadog distributed tracing and
19
19
  # custom metrics
20
20
  module Lambda
21
+ @is_cold_start = true
21
22
  # Wrap the body of a lambda invocation
22
23
  # @param event [Object] event sent to lambda
23
24
  # @param context [Object] lambda context
24
25
  # @param block [Proc] implementation of the handler function.
25
- def self.wrap(event, _context, &block)
26
+ def self.wrap(event, context, &block)
26
27
  Datadog::Utils.update_log_level
27
-
28
28
  @listener ||= Trace::Listener.new
29
29
  @listener.on_start(event: event)
30
+ record_enhanced('invocations', context)
30
31
  begin
31
32
  res = block.call
33
+ rescue StandardError => e
34
+ record_enhanced('errors', context)
35
+ raise e
32
36
  ensure
33
37
  @listener.on_end
38
+ @is_cold_start = false
34
39
  end
35
40
  res
36
41
  end
@@ -58,5 +63,51 @@ module Datadog
58
63
  metric = { e: time_ms, m: name, t: tag_list, v: value }.to_json
59
64
  puts metric
60
65
  end
66
+
67
+ # Generate tags for enhanced metrics
68
+ # @param context [Object] https://docs.aws.amazon.com/lambda/latest/dg/ruby-context.html
69
+ # @return [hash] a hash of the enhanced metrics tags
70
+ def self.gen_enhanced_tags(context)
71
+ arn_parts = context.invoked_function_arn.split(':')
72
+ {
73
+ functionname: context.function_name,
74
+ region: arn_parts[3],
75
+ account_id: arn_parts[4],
76
+ memorysize: context.memory_limit_in_mb,
77
+ cold_start: @is_cold_start,
78
+ runtime: "Ruby #{RUBY_VERSION}"
79
+ }
80
+ rescue StandardError => e
81
+ Datadog::Utils.logger.error 'Unable to parse Lambda context' \
82
+ "#{context}: #{e}"
83
+ {}
84
+ end
85
+
86
+ # Format and add tags to enhanced metrics
87
+ # This method wraps the metric method, checking the DD_ENHANCED_METRICS
88
+ # environment variable, adding 'aws.lambda.enhanced' to the metric name,
89
+ # and adding the enhanced metric tags to the enhanced metrics.
90
+ # @param metric_name [String] basic name of the metric
91
+ # @param context [Object] AWS Ruby Lambda Context
92
+ # @return [boolean] false if the metric was not added for some reason,
93
+ # true otherwise (for ease of testing)
94
+
95
+ def self.record_enhanced(metric_name, context)
96
+ return false unless do_enhanced_metrics?
97
+
98
+ etags = gen_enhanced_tags(context)
99
+ metric("aws.lambda.enhanced.#{metric_name}", 1, etags)
100
+ true
101
+ end
102
+
103
+ # Check the DD_ENHANCED_METRICS environment variable
104
+ # @reurn [boolean] true if this lambda should have
105
+ # enhanced metrics
106
+ def self.do_enhanced_metrics?
107
+ dd_enhanced_metrics = ENV['DD_ENHANCED_METRICS']
108
+ return false if dd_enhanced_metrics.nil?
109
+
110
+ dd_enhanced_metrics.downcase == 'true'
111
+ end
61
112
  end
62
113
  end
@@ -12,7 +12,7 @@ module Datadog
12
12
  module Lambda
13
13
  module VERSION
14
14
  MAJOR = 0
15
- MINOR = 4
15
+ MINOR = 5
16
16
  PATCH = 0
17
17
  PRE = nil
18
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datadog-lambda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Datadog, Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-30 00:00:00.000000000 Z
11
+ date: 2020-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-xray-sdk