Skip to content

Commit cdbbaec

Browse files
stefanbotezStefan Botez
and
Stefan Botez
authored
Added User-Agent header (#9)
Co-authored-by: Stefan Botez <[email protected]>
1 parent f744341 commit cdbbaec

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

lib/aws_lambda_ric.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class LambdaRunner
2626

2727
ENV_VAR_RUNTIME_API = 'AWS_LAMBDA_RUNTIME_API'
2828

29-
def initialize(runtime_server_addr)
30-
@lambda_server = LambdaServer.new(runtime_server_addr)
29+
def initialize(runtime_server_addr, user_agent)
30+
@lambda_server = LambdaServer.new(runtime_server_addr, user_agent)
3131
@runtime_loop_active = true # if false, we will exit the program
3232
@exit_code = 0
3333
end

lib/aws_lambda_ric/bootstrap.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@ def self.bootstrap_handler
3030
end
3131
app_root = Dir.pwd
3232
handler = ARGV[0]
33-
lambda_runner = AwsLambdaRuntimeInterfaceClient::LambdaRunner.new(fetch_runtime_server)
33+
lambda_runner = AwsLambdaRuntimeInterfaceClient::LambdaRunner.new(fetch_runtime_server, get_user_agent)
3434
puts "Executing '#{handler}' in function directory '#{app_root}'"
3535
lambda_runner.run(app_root, handler)
3636
end
3737

38+
def self.get_user_agent
39+
ruby_version = RUBY_VERSION.to_s
40+
version = AwsLambdaRuntimeInterfaceClient::VERSION
41+
42+
"aws-lambda-ruby/#{ruby_version}-#{version}"
43+
end
44+
3845
end

lib/aws_lambda_ric/lambda_server.rb

+11-10
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ class LambdaServer
1010
LAMBDA_DEFAULT_SERVER_ADDRESS = '127.0.0.1:9001'
1111
LAMBDA_RUNTIME_API_VERSION = '2018-06-01'
1212

13-
MAX_HEADER_SIZE = 1024 * 1024
14-
LONG_TIMEOUT = 1_000_000
13+
MAX_HEADER_SIZE_BYTES = 1024 * 1024
14+
LONG_TIMEOUT_MS = 1_000_000
1515

16-
def initialize(server_address)
16+
def initialize(server_address, user_agent)
1717
server_address ||= LAMBDA_DEFAULT_SERVER_ADDRESS
18-
@server_address = 'http://' + server_address + '/' + LAMBDA_RUNTIME_API_VERSION
18+
@server_address = "http://#{server_address}/#{LAMBDA_RUNTIME_API_VERSION}"
19+
@user_agent = user_agent
1920
end
2021

2122
def next_invocation
2223
next_invocation_uri = URI(@server_address + '/runtime/invocation/next')
2324
begin
2425
http = Net::HTTP.new(next_invocation_uri.host, next_invocation_uri.port)
25-
http.read_timeout = LONG_TIMEOUT
26+
http.read_timeout = LONG_TIMEOUT_MS
2627
resp = http.start do |connection|
27-
connection.get(next_invocation_uri.path)
28+
connection.get(next_invocation_uri.path, { 'User-Agent' => @user_agent })
2829
end
2930
if resp.is_a?(Net::HTTPSuccess)
3031
request_id = resp['Lambda-Runtime-Aws-Request-Id']
@@ -51,7 +52,7 @@ def send_response(request_id:, response_object:, content_type: 'application/json
5152
Net::HTTP.post(
5253
response_uri,
5354
response_object,
54-
{ 'Content-Type' => content_type }
55+
{ 'Content-Type' => content_type, 'User-Agent' => @user_agent }
5556
)
5657
rescue StandardError => e
5758
raise LambdaErrors::LambdaRuntimeError.new(e)
@@ -61,8 +62,8 @@ def send_response(request_id:, response_object:, content_type: 'application/json
6162
def send_error_response(request_id:, error_object:, error:, xray_cause:)
6263
response_uri = URI(@server_address + "/runtime/invocation/#{request_id}/error")
6364
begin
64-
headers = { 'Lambda-Runtime-Function-Error-Type' => error.runtime_error_type }
65-
headers['Lambda-Runtime-Function-XRay-Error-Cause'] = xray_cause if xray_cause.bytesize < MAX_HEADER_SIZE
65+
headers = { 'Lambda-Runtime-Function-Error-Type' => error.runtime_error_type, 'User-Agent' => @user_agent }
66+
headers['Lambda-Runtime-Function-XRay-Error-Cause'] = xray_cause if xray_cause.bytesize < MAX_HEADER_SIZE_BYTES
6667
Net::HTTP.post(
6768
response_uri,
6869
error_object.to_json,
@@ -79,7 +80,7 @@ def send_init_error(error_object:, error:)
7980
Net::HTTP.post(
8081
uri,
8182
error_object.to_json,
82-
{ 'Lambda-Runtime-Function-Error-Type' => error.runtime_error_type }
83+
{ 'Lambda-Runtime-Function-Error-Type' => error.runtime_error_type, 'User-Agent' => @user_agent }
8384
)
8485
rescue StandardError => e
8586
raise LambdaErrors::LambdaRuntimeInitError.new(e)

test/unit/lambda_server_test.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ def setup
1111
@request_id = 'test_id'
1212
@error = LambdaErrors::LambdaRuntimeError.new(StandardError.new('User error, replace user'))
1313
@error_uri = URI("http://#{@server_address}/2018-06-01/runtime/invocation/#{@request_id}/error")
14-
@under_test = LambdaServer.new(@server_address)
14+
@mock_user_agent = 'mock-user-agent'
15+
@under_test = LambdaServer.new(@server_address, @mock_user_agent)
1516
end
1617

1718
def test_post_invocation_error_with_large_xray_cause
1819
large_xray_cause = ('a' * 1024 * 1024)[0..-2]
1920
headers = {'Lambda-Runtime-Function-Error-Type' => @error.runtime_error_type,
20-
'Lambda-Runtime-Function-XRay-Error-Cause' => large_xray_cause}
21+
'Lambda-Runtime-Function-XRay-Error-Cause' => large_xray_cause,
22+
'User-Agent' => @mock_user_agent}
2123
post_mock = Minitest::Mock.new
2224
post_mock.expect :call, nil, [@error_uri, @error.to_lambda_response.to_json, headers]
2325

@@ -35,7 +37,8 @@ def test_post_invocation_error_with_large_xray_cause
3537

3638
def test_post_invocation_error_with_too_large_xray_cause
3739
too_large_xray_cause = 'a' * 1024 * 1024
38-
headers = {'Lambda-Runtime-Function-Error-Type' => @error.runtime_error_type}
40+
headers = {'Lambda-Runtime-Function-Error-Type' => @error.runtime_error_type,
41+
'User-Agent' => @mock_user_agent}
3942
post_mock = Minitest::Mock.new
4043
post_mock.expect :call, nil, [@error_uri, @error.to_lambda_response.to_json, headers]
4144

0 commit comments

Comments
 (0)