Skip to content

Commit

Permalink
At 764 Add user agent for AWS client config (#65)
Browse files Browse the repository at this point in the history
* test getting application name in mac

* get application name in windows

* add user agent in client config

* remove unused include

* add log msg

* add unit tests for GetUserAgent()
  • Loading branch information
yanw-bq authored May 20, 2021
1 parent 6130455 commit f7d67d4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/UnitTests/UTConn/test_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ TEST(TestDecodeHex, All_possible_hex) {
EXPECT_EQ(expected, OktaCredentialsProvider::DecodeHex(hex_encoded));
}

TEST(TestGetUserAgent, Success) {
TSCommunication conn;
std::string expected = "ts-odbc." TIMESTREAMDRIVERVERSION " [ut_conn]";
EXPECT_EQ(expected, conn.GetUserAgent());
}

// TODO: enable gmock and mock the response from timestream
//class TestTSConnConnectDBStart : public testing::Test {
// protected:
Expand Down
11 changes: 11 additions & 0 deletions src/odfesqlodbc/mylog.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include "es_odbc.h"
#include "misc.h"

#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif

#ifndef WIN32
#include <errno.h>
#include <pwd.h>
Expand Down Expand Up @@ -124,6 +128,13 @@ const char *GetExeProgramName() {

if (GetModuleFileName(NULL, pathname, sizeof(pathname)) > 0)
_splitpath(pathname, NULL, NULL, exename, NULL);
#elif __APPLE__
char pathname[PATH_MAX + 1];
uint32_t size = PATH_MAX + 1;

if (_NSGetExecutablePath(pathname, &size) == 0) {
STRCPY_FIXED(exename, po_basename(pathname));
}
#else
CSTR flist[] = {"/proc/self/exe", "/proc/curproc/file",
"/proc/curproc/exe"};
Expand Down
11 changes: 11 additions & 0 deletions src/odfesqlodbc/ts_communication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
// clang-format on

namespace {
const Aws::String UA_ID_PREFIX = Aws::String("ts-odbc.");

typedef std::function< std::unique_ptr< Aws::TimestreamQuery::TimestreamQueryClient >(
const runtime_options& options,
const Aws::Client::ClientConfiguration& config) > QueryClientCreator;
Expand Down Expand Up @@ -109,6 +111,7 @@ bool TSCommunication::Validate(const runtime_options& options) {

std::unique_ptr< Aws::TimestreamQuery::TimestreamQueryClient > TSCommunication::CreateQueryClient(const runtime_options& options) {
Aws::Client::ClientConfiguration config;
config.userAgent = GetUserAgent();
if (!options.auth.end_point_override.empty()) {
config.endpointOverride = options.auth.end_point_override;
} else {
Expand Down Expand Up @@ -205,6 +208,14 @@ void TSCommunication::StopResultRetrieval(StatementClass* stmt) {
}
}

Aws::String TSCommunication::GetUserAgent() {
Aws::String program_name(GetExeProgramName());
Aws::String name_suffix = " [" + program_name + "]";
Aws::String msg = "Name of the application using the driver: " + name_suffix;
LogMsg(LOG_INFO, msg.c_str());
return UA_ID_PREFIX + GetVersion() + name_suffix;
}

/**
* Context class for Aws::Client::AsyncCallerContext
* Only for execution
Expand Down
6 changes: 6 additions & 0 deletions src/odfesqlodbc/ts_communication.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class TSCommunication : public Communication {
*/
virtual void StopResultRetrieval(StatementClass* stmt) override;

/**
* Get the user agent for Aws::Client::ClientConfiguration.
* @return the user agent.
*/
Aws::String GetUserAgent();

private:
/**
* Create Timestream Query Client
Expand Down

0 comments on commit f7d67d4

Please sign in to comment.