Skip to content

Commit 3461d6a

Browse files
authored
Add User-Agent information (#11)
* Add User-Agent information Send a user-agent http header when talking to Lambda's http server. This is important for collecting usage metrics.
1 parent 8b44c7d commit 3461d6a

File tree

7 files changed

+143
-14
lines changed

7 files changed

+143
-14
lines changed

CMakeLists.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.5)
22
set(CMAKE_CXX_STANDARD 11)
33
project(aws-lambda-runtime
4-
VERSION 0.1
4+
VERSION 0.1.0
55
LANGUAGES CXX)
66

77
option(ENABLE_TESTS "Enables building the test project, requires AWS C++ SDK." OFF)
@@ -26,6 +26,7 @@ check_cxx_compiler_flag("-Wl,-flto" LTO_CAPABLE)
2626
add_library(${PROJECT_NAME}
2727
"src/logging.cpp"
2828
"src/runtime.cpp"
29+
"${CMAKE_CURRENT_BINARY_DIR}/version.cpp"
2930
)
3031

3132
target_include_directories(${PROJECT_NAME} PUBLIC
@@ -51,11 +52,11 @@ target_compile_options(${PROJECT_NAME} PRIVATE
5152
"-Wno-sign-conversion")
5253

5354
if (LOG_VERBOSITY)
54-
target_compile_definitions(${PROJECT_NAME} PRIVATE "-DAWS_LAMBDA_LOG=${LOG_VERBOSITY}")
55+
target_compile_definitions(${PROJECT_NAME} PRIVATE "AWS_LAMBDA_LOG=${LOG_VERBOSITY}")
5556
elseif(CMAKE_BUILD_TYPE STREQUAL Debug)
56-
target_compile_definitions(${PROJECT_NAME} PRIVATE "-DAWS_LAMBDA_LOG=3")
57+
target_compile_definitions(${PROJECT_NAME} PRIVATE "AWS_LAMBDA_LOG=3")
5758
else ()
58-
target_compile_definitions(${PROJECT_NAME} PRIVATE "-DAWS_LAMBDA_LOG=0")
59+
target_compile_definitions(${PROJECT_NAME} PRIVATE "AWS_LAMBDA_LOG=0")
5960
endif()
6061

6162
if ((BUILD_SHARED_LIBS) AND (LTO_CAPABLE))
@@ -69,8 +70,14 @@ if (ENABLE_TESTS)
6970
add_subdirectory(tests)
7071
endif()
7172

73+
#versioning
74+
configure_file(
75+
"${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp.in"
76+
"${CMAKE_CURRENT_BINARY_DIR}/version.cpp"
77+
NEWLINE_STYLE LF)
78+
7279
# installation
73-
install(FILES "include/aws/lambda-runtime/runtime.h"
80+
install(FILES "include/aws/lambda-runtime/runtime.h" "include/aws/lambda-runtime/version.h"
7481
DESTINATION "include/aws/lambda-runtime")
7582

7683
install(FILES "include/aws/logging/logging.h"

ci/codebuild/build-cpp-sdk.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ cmake .. -GNinja -DBUILD_ONLY="lambda" \
1111
-DCMAKE_BUILD_TYPE=Release \
1212
-DENABLE_UNITY_BUILD=ON \
1313
-DBUILD_SHARED_LIBS=ON \
14-
-DAUTORUN_UNIT_TESTS=OFF \
14+
-DENABLE_TESTING=OFF \
1515
-DCMAKE_INSTALL_PREFIX=/install $@
1616
ninja
1717
ninja install

include/aws/lambda-runtime/version.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
/*
3+
* Copyright 2018-present Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License").
6+
* You may not use this file except in compliance with the License.
7+
* A copy of the License is located at
8+
*
9+
* http://aws.amazon.com/apache2.0
10+
*
11+
* or in the "license" file accompanying this file. This file is distributed
12+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
* express or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
17+
namespace aws {
18+
namespace lambda_runtime {
19+
20+
/**
21+
* Returns the major component of the library version.
22+
*/
23+
unsigned get_version_major();
24+
25+
/**
26+
* Returns the minor component of the library version.
27+
*/
28+
unsigned get_version_minor();
29+
30+
/**
31+
* Returns the patch component of the library version.
32+
*/
33+
unsigned get_version_patch();
34+
35+
/**
36+
* Returns the semantic version of the library in the form Major.Minor.Patch
37+
*/
38+
char const* get_version();
39+
40+
} // namespace lambda_runtime
41+
} // namespace aws

src/runtime.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#include "aws/lambda-runtime/runtime.h"
17+
#include "aws/lambda-runtime/version.h"
1718
#include "aws/lambda-runtime/outcome.h"
1819
#include "aws/logging/logging.h"
1920
#include "aws/http/response.h"
@@ -26,7 +27,7 @@
2627
#include <array>
2728
#include <cstdlib> // for strtoul
2829

29-
#define LAMBDA_RUNTIME_API __attribute__((visibility("default")))
30+
#define AWS_LAMBDA_RUNTIME_API __attribute__((visibility("default")))
3031

3132
namespace aws {
3233
namespace lambda_runtime {
@@ -105,6 +106,12 @@ static size_t write_header(char* ptr, size_t size, size_t nmemb, void* userdata)
105106
return size * nmemb;
106107
}
107108

109+
static std::string const& get_user_agent_header()
110+
{
111+
static std::string user_agent = std::string("User-Agent: AWS_Lambda_Cpp/") + get_version();
112+
return user_agent;
113+
}
114+
108115
static size_t read_data(char* buffer, size_t size, size_t nitems, void* userdata)
109116
{
110117
auto const limit = size * nitems;
@@ -243,9 +250,13 @@ runtime::next_outcome runtime::get_next()
243250
curl_easy_setopt(m_curl_handle, CURLOPT_WRITEDATA, &resp);
244251
curl_easy_setopt(m_curl_handle, CURLOPT_HEADERDATA, &resp);
245252

253+
curl_slist* headers = nullptr;
254+
headers = curl_slist_append(headers, get_user_agent_header().c_str());
255+
246256
logging::log_debug(LOG_TAG, "Making request to %s", m_endpoints[Endpoints::NEXT].c_str());
247257
CURLcode curl_code = curl_easy_perform(m_curl_handle);
248258
logging::log_debug(LOG_TAG, "Completed request to %s", m_endpoints[Endpoints::NEXT].c_str());
259+
curl_slist_free_all(headers);
249260

250261
if (curl_code != CURLE_OK) {
251262
logging::log_debug(LOG_TAG, "CURL returned error code %d - %s", curl_code, curl_easy_strerror(curl_code));
@@ -343,6 +354,7 @@ runtime::post_outcome runtime::do_post(
343354

344355
headers = curl_slist_append(headers, "Expect:");
345356
headers = curl_slist_append(headers, "transfer-encoding:");
357+
headers = curl_slist_append(headers, get_user_agent_header().c_str());
346358
auto const& payload = handler_response.get_payload();
347359
logging::log_debug(
348360
LOG_TAG, "calculating content length... %s", ("content-length: " + std::to_string(payload.length())).c_str());
@@ -398,7 +410,7 @@ static bool handle_post_outcome(runtime::post_outcome const& o, std::string cons
398410
return false;
399411
}
400412

401-
LAMBDA_RUNTIME_API
413+
AWS_LAMBDA_RUNTIME_API
402414
void run_handler(std::function<invocation_response(invocation_request const&)> const& handler)
403415
{
404416
logging::log_info(LOG_TAG, "Initializing the C++ Lambda Runtime.");
@@ -501,7 +513,7 @@ static std::string json_escape(std::string const& in)
501513
return out;
502514
}
503515

504-
LAMBDA_RUNTIME_API
516+
AWS_LAMBDA_RUNTIME_API
505517
invocation_response invocation_response::success(std::string const& payload, std::string const& content_type)
506518
{
507519
invocation_response r;
@@ -511,7 +523,7 @@ invocation_response invocation_response::success(std::string const& payload, std
511523
return r;
512524
}
513525

514-
LAMBDA_RUNTIME_API
526+
AWS_LAMBDA_RUNTIME_API
515527
invocation_response invocation_response::failure(std::string const& error_message, std::string const& error_type)
516528
{
517529
invocation_response r;

src/version.cpp.in

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2018-present Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
#define AWS_LAMBDA_RUNTIME_API __attribute__((visibility("default")))
17+
18+
namespace aws {
19+
namespace lambda_runtime {
20+
21+
/* clang-format off */
22+
AWS_LAMBDA_RUNTIME_API
23+
unsigned get_version_major()
24+
{
25+
return @PROJECT_VERSION_MAJOR@;
26+
}
27+
28+
AWS_LAMBDA_RUNTIME_API
29+
unsigned get_version_minor()
30+
{
31+
return @PROJECT_VERSION_MINOR@;
32+
}
33+
34+
AWS_LAMBDA_RUNTIME_API
35+
unsigned get_version_patch()
36+
{
37+
return @PROJECT_VERSION_PATCH@;
38+
}
39+
/* clang-format on */
40+
41+
AWS_LAMBDA_RUNTIME_API
42+
char const* get_version()
43+
{
44+
return "@PROJECT_VERSION@";
45+
}
46+
47+
} // namespace lambda_runtime
48+
} // namespace aws

tests/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ find_package(GTest REQUIRED)
44

55
add_executable(${PROJECT_NAME}
66
main.cpp
7-
runtime_tests.cpp)
7+
runtime_tests.cpp
8+
version_tests.cpp)
89

910
target_link_libraries(${PROJECT_NAME} PRIVATE ${AWSSDK_LINK_LIBRARIES} aws-lambda-runtime GTest::GTest)
1011

11-
# gtest_discover_tests(${PROJECT_NAME}) Can't use this until CMake 3.10 :(
12-
# gtest_add_tests(TARGET ${PROJECT_NAME} runtime_tests.cpp)
13-
GTEST_ADD_TESTS(${PROJECT_NAME} "" AUTO)
12+
gtest_discover_tests(${PROJECT_NAME}) # requires CMake 3.10 or later
1413

1514
add_subdirectory(resources)
1615

tests/version_tests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <gtest/gtest.h>
2+
#include <aws/lambda-runtime/version.h>
3+
4+
using namespace aws::lambda_runtime;
5+
6+
TEST(VersionTests, get_version_major)
7+
{
8+
auto version = get_version_major();
9+
ASSERT_EQ(0, version);
10+
}
11+
12+
TEST(VersionTests, get_version_minor)
13+
{
14+
auto version = get_version_minor();
15+
ASSERT_GE(version, 1);
16+
}
17+
18+
TEST(VersionTests, get_version_patch)
19+
{
20+
auto version = get_version_patch();
21+
ASSERT_GE(version, 0);
22+
}

0 commit comments

Comments
 (0)