datadog-lambda 0.6.0 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/datadog/lambda.rb +44 -10
- data/lib/datadog/lambda/trace/listener.rb +26 -9
- data/lib/datadog/lambda/trace/patch_http.rb +13 -7
- data/lib/datadog/lambda/version.rb +2 -2
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8e991dcdf4df850e5a9a56fe9f402f94fd1a1c4d9e5558cf9d1eff868331472
|
4
|
+
data.tar.gz: 7e5d7ea704e3ee327496b9568e10ec620e77e48ad56eddf4059b59b16b7fcbd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz: '
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '081dccfc9c8e566e63d9a29f8cc28d8db0f66f8b5bdd0137bcadf618e277be28408225c5d874400db1cd20173e5623f95619c82bea703ea7c9f2adb6cef2f0a5'
|
7
|
+
data.tar.gz: 60ea065a6e8237182ff24872904c7ebb97262604763cbeea8977f7648e375f6304aab816bc1f2ad25ea7d298ee5dcfd280e5abfef8af09e6c1634c1055e12609
|
data/lib/datadog/lambda.rb
CHANGED
@@ -7,12 +7,14 @@
|
|
7
7
|
# This product includes software developed at Datadog (https://www.datadoghq.com/).
|
8
8
|
# Copyright 2019 Datadog, Inc.
|
9
9
|
#
|
10
|
+
# rubocop:disable Metrics/ModuleLength
|
10
11
|
|
11
12
|
require 'datadog/lambda/trace/listener'
|
12
13
|
require 'datadog/lambda/utils/logger'
|
13
14
|
require 'datadog/lambda/trace/patch_http'
|
14
15
|
require 'json'
|
15
16
|
require 'time'
|
17
|
+
require 'datadog/lambda/version'
|
16
18
|
|
17
19
|
module Datadog
|
18
20
|
# Instruments AWS Lambda functions with Datadog distributed tracing and
|
@@ -36,6 +38,7 @@ module Datadog
|
|
36
38
|
c.tracer writer: Datadog::SyncWriter.new(
|
37
39
|
transport: Datadog::Transport::IO.default
|
38
40
|
)
|
41
|
+
c.tags = { "_dd.origin": 'lambda' }
|
39
42
|
yield(c) if block_given?
|
40
43
|
end
|
41
44
|
end
|
@@ -67,7 +70,6 @@ module Datadog
|
|
67
70
|
# Gets the current tracing context
|
68
71
|
def self.trace_context
|
69
72
|
context = Hash[Datadog::Trace.trace_context]
|
70
|
-
context.delete(:source)
|
71
73
|
context
|
72
74
|
end
|
73
75
|
|
@@ -81,34 +83,65 @@ module Datadog
|
|
81
83
|
raise 'value must be a number' unless value.is_a?(Numeric)
|
82
84
|
|
83
85
|
time ||= Time.now
|
84
|
-
|
86
|
+
time_ms = time.to_f.to_i
|
87
|
+
|
88
|
+
tag_list = ["dd_lambda_layer:datadog-ruby#{dd_lambda_layer_tag}"]
|
85
89
|
tags.each do |tag|
|
86
90
|
tag_list.push("#{tag[0]}:#{tag[1]}")
|
87
91
|
end
|
88
|
-
|
89
|
-
metric
|
90
|
-
|
92
|
+
metric = { e: time_ms, m: name, t: tag_list, v: value }
|
93
|
+
puts metric.to_json
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.dd_lambda_layer_tag
|
97
|
+
RUBY_VERSION[0, 3].tr('.', '')
|
91
98
|
end
|
92
99
|
|
93
100
|
# Generate tags for enhanced metrics
|
94
101
|
# @param context [Object] https://docs.aws.amazon.com/lambda/latest/dg/ruby-context.html
|
95
102
|
# @return [hash] a hash of the enhanced metrics tags
|
103
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
96
104
|
def self.gen_enhanced_tags(context)
|
97
105
|
arn_parts = context.invoked_function_arn.split(':')
|
98
|
-
|
106
|
+
# Check if we have an alias or version
|
107
|
+
function_alias = arn_parts[7].nil? ? nil : arn_parts[7]
|
108
|
+
|
109
|
+
tags = {
|
99
110
|
functionname: context.function_name,
|
100
111
|
region: arn_parts[3],
|
101
112
|
account_id: arn_parts[4],
|
102
113
|
memorysize: context.memory_limit_in_mb,
|
103
114
|
cold_start: @is_cold_start,
|
104
|
-
runtime: "Ruby #{RUBY_VERSION}"
|
115
|
+
runtime: "Ruby #{RUBY_VERSION}",
|
116
|
+
resource: context.function_name,
|
117
|
+
datadog_lambda: Datadog::Lambda::VERSION::STRING.to_sym
|
105
118
|
}
|
119
|
+
begin
|
120
|
+
tags[:dd_trace] = Gem.loaded_specs['ddtrace'].version
|
121
|
+
rescue StandardError
|
122
|
+
Datadog::Utils.logger.debug 'dd-trace unavailable'
|
123
|
+
end
|
124
|
+
# If we have an alias...
|
125
|
+
unless function_alias.nil?
|
126
|
+
# If the alis version is $Latest, drop the $ for ddog tag convention.
|
127
|
+
if function_alias.start_with?('$')
|
128
|
+
function_alias[0] = ''
|
129
|
+
# If the alias is not a version number add the executed version tag
|
130
|
+
elsif !/\A\d+\z/.match(function_alias)
|
131
|
+
tags[:executedversion] = context.function_version
|
132
|
+
end
|
133
|
+
# Append the alias to the resource tag
|
134
|
+
tags[:resource] = context.function_name + ':' + function_alias
|
135
|
+
end
|
136
|
+
|
137
|
+
tags
|
106
138
|
rescue StandardError => e
|
107
139
|
Datadog::Utils.logger.error 'Unable to parse Lambda context' \
|
108
140
|
"#{context}: #{e}"
|
109
141
|
{}
|
110
142
|
end
|
111
143
|
|
144
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
112
145
|
# Format and add tags to enhanced metrics
|
113
146
|
# This method wraps the metric method, checking the DD_ENHANCED_METRICS
|
114
147
|
# environment variable, adding 'aws.lambda.enhanced' to the metric name,
|
@@ -116,13 +149,13 @@ module Datadog
|
|
116
149
|
# @param metric_name [String] basic name of the metric
|
117
150
|
# @param context [Object] AWS Ruby Lambda Context
|
118
151
|
# @return [boolean] false if the metric was not added for some reason,
|
119
|
-
# true otherwise (for ease of testing
|
152
|
+
# true otherwise (for ease of testing
|
120
153
|
|
121
154
|
def self.record_enhanced(metric_name, context)
|
122
155
|
return false unless do_enhanced_metrics?
|
123
156
|
|
124
157
|
etags = gen_enhanced_tags(context)
|
125
|
-
metric("aws.lambda.enhanced.#{metric_name}", 1, etags)
|
158
|
+
metric("aws.lambda.enhanced.#{metric_name}", 1, **etags)
|
126
159
|
true
|
127
160
|
end
|
128
161
|
|
@@ -131,7 +164,7 @@ module Datadog
|
|
131
164
|
# enhanced metrics
|
132
165
|
def self.do_enhanced_metrics?
|
133
166
|
dd_enhanced_metrics = ENV['DD_ENHANCED_METRICS']
|
134
|
-
return
|
167
|
+
return true if dd_enhanced_metrics.nil?
|
135
168
|
|
136
169
|
dd_enhanced_metrics.downcase == 'true'
|
137
170
|
end
|
@@ -153,3 +186,4 @@ module Datadog
|
|
153
186
|
end
|
154
187
|
end
|
155
188
|
end
|
189
|
+
# rubocop:enable Metrics/ModuleLength
|
@@ -37,22 +37,39 @@ module Datadog
|
|
37
37
|
def on_end; end
|
38
38
|
|
39
39
|
def on_wrap(request_context:, cold_start:, &block)
|
40
|
+
options = get_option_tags(
|
41
|
+
request_context: request_context,
|
42
|
+
cold_start: cold_start
|
43
|
+
)
|
44
|
+
context = Datadog::Trace.trace_context
|
45
|
+
source = context[:source] if context
|
46
|
+
options[:tags]['_dd.parent_source'] = source if source && source != 'ddtrace'
|
47
|
+
options[:resource] = @function_name
|
48
|
+
options[:service] = 'aws.lambda'
|
49
|
+
options[:span_type] = 'serverless'
|
50
|
+
Datadog::Trace.apply_datadog_trace_context(Datadog::Trace.trace_context)
|
51
|
+
Datadog::Trace.wrap_datadog(options) do
|
52
|
+
block.call
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def get_option_tags(request_context:, cold_start:)
|
59
|
+
function_arn = request_context.invoked_function_arn.downcase
|
60
|
+
tk = function_arn.split(':')
|
61
|
+
function_arn = tk.length > 7 ? tk[0, 7].join(':') : function_arn
|
62
|
+
function_version = tk.length > 7 ? tk[7] : '$LATEST'
|
40
63
|
options = {
|
41
64
|
tags: {
|
42
65
|
cold_start: cold_start,
|
43
|
-
function_arn:
|
66
|
+
function_arn: function_arn,
|
67
|
+
function_version: function_version,
|
44
68
|
request_id: request_context.aws_request_id,
|
45
69
|
resource_names: request_context.function_name
|
46
70
|
}
|
47
71
|
}
|
48
|
-
|
49
|
-
options[:resource] = @handler_name
|
50
|
-
options[:service] = @function_name
|
51
|
-
options[:span_type] = 'serverless'
|
52
|
-
Datadog::Trace.apply_datadog_trace_context(Datadog::Trace.trace_context)
|
53
|
-
Datadog::Trace.wrap_datadog(options) do
|
54
|
-
block.call
|
55
|
-
end
|
72
|
+
options
|
56
73
|
end
|
57
74
|
end
|
58
75
|
end
|
@@ -30,25 +30,31 @@ module Datadog
|
|
30
30
|
# NetExtensions contains patches which add tracing context to http calls
|
31
31
|
module NetExtensions
|
32
32
|
def request(req, body = nil, &block)
|
33
|
-
logger = Datadog::Utils.logger
|
34
33
|
begin
|
35
34
|
context = Datadog::Trace.current_trace_context(
|
36
35
|
Datadog::Trace.trace_context
|
37
36
|
)
|
38
37
|
|
39
|
-
req
|
40
|
-
context[:sample_mode]
|
41
|
-
req[Datadog::Trace::DD_PARENT_ID_HEADER.to_sym] = context[:parent_id]
|
42
|
-
req[Datadog::Trace::DD_TRACE_ID_HEADER.to_sym] = context[:trace_id]
|
43
|
-
logger.debug("added context #{context} to request")
|
38
|
+
req = add_ctx_to_req(req, context)
|
44
39
|
rescue StandardError => e
|
45
40
|
trace = e.backtrace.join("\n ")
|
46
|
-
logger.debug(
|
41
|
+
Datadog::Utils.logger.debug(
|
47
42
|
"couldn't add tracing context #{context} to request #{e}:\n#{trace}"
|
48
43
|
)
|
49
44
|
end
|
50
45
|
super(req, body, &block)
|
51
46
|
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def add_ctx_to_req(req, context)
|
51
|
+
req[Datadog::Trace::DD_SAMPLING_PRIORITY_HEADER.to_sym] =
|
52
|
+
context[:sample_mode]
|
53
|
+
req[Datadog::Trace::DD_PARENT_ID_HEADER.to_sym] = context[:parent_id]
|
54
|
+
req[Datadog::Trace::DD_TRACE_ID_HEADER.to_sym] = context[:trace_id]
|
55
|
+
Datadog::Utils.logger.debug("added context #{context} to request")
|
56
|
+
req
|
57
|
+
end
|
52
58
|
end
|
53
59
|
end
|
54
60
|
end
|
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:
|
4
|
+
version: 1.11.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: 2020-
|
11
|
+
date: 2020-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-xray-sdk
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.4'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
40
|
+
version: '0.4'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -113,7 +113,7 @@ files:
|
|
113
113
|
- lib/datadog/lambda/trace/xray.rb
|
114
114
|
- lib/datadog/lambda/utils/logger.rb
|
115
115
|
- lib/datadog/lambda/version.rb
|
116
|
-
homepage: https://github.com/DataDog/
|
116
|
+
homepage: https://github.com/DataDog/datadog-lambda-rb
|
117
117
|
licenses:
|
118
118
|
- Apache-2.0
|
119
119
|
metadata:
|