Skip to content

CDRIVER-5638 Record both FaaS and container metadata when both are present #2016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-handshake-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ typedef struct _mongoc_handshake_t {
char *compiler_info;
char *flags;

bool docker;
bool kubernetes;

mongoc_handshake_env_t env;
optional_int32 env_timeout_sec;
optional_int32 env_memory_mb;
Expand Down
59 changes: 27 additions & 32 deletions src/libmongoc/src/mongoc/mongoc-handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#ifdef _POSIX_VERSION
#include <sys/utsname.h>
#include <unistd.h>
#endif

#ifdef _WIN32
Expand Down Expand Up @@ -351,39 +352,30 @@ _get_system_info (mongoc_handshake_t *handshake)
handshake->os_architecture = _get_os_architecture ();
}

static void
_free_system_info (mongoc_handshake_t *handshake)
{
bson_free (handshake->os_type);
bson_free (handshake->os_name);
bson_free (handshake->os_version);
bson_free (handshake->os_architecture);
}

static void
_get_driver_info (mongoc_handshake_t *handshake)
{
handshake->driver_name = bson_strndup ("mongoc", HANDSHAKE_DRIVER_NAME_MAX);
handshake->driver_version = bson_strndup (MONGOC_VERSION_S, HANDSHAKE_DRIVER_VERSION_MAX);
}

static void
_free_driver_info (mongoc_handshake_t *handshake)
{
bson_free (handshake->driver_name);
bson_free (handshake->driver_version);
}

static void
_set_platform_string (mongoc_handshake_t *handshake)
{
handshake->platform = bson_strdup ("");
}

static void
_free_env_info (mongoc_handshake_t *handshake)
{
bson_free (handshake->env_region);
_get_container_info (mongoc_handshake_t *handshake) {
char* kubernetes_env = _mongoc_getenv("KUBERNETES_SERVICE_HOST");
handshake->kubernetes = kubernetes_env;

handshake->docker = false;
#ifdef _POSIX_VERSION
handshake->docker = (access("/.dockerenv", F_OK) == 0);
#endif

bson_free (kubernetes_env);
}

static void
Expand Down Expand Up @@ -509,21 +501,14 @@ _set_flags (mongoc_handshake_t *handshake)
handshake->flags = mcommon_string_from_append_destroy_with_steal (&append);
}

static void
_free_platform_string (mongoc_handshake_t *handshake)
{
bson_free (handshake->platform);
bson_free (handshake->compiler_info);
bson_free (handshake->flags);
}

void
_mongoc_handshake_init (void)
{
_get_system_info (_mongoc_handshake_get ());
_get_driver_info (_mongoc_handshake_get ());
_set_platform_string (_mongoc_handshake_get ());
_get_env_info (_mongoc_handshake_get ());
_get_container_info (_mongoc_handshake_get());
_set_compiler_info (_mongoc_handshake_get ());
_set_flags (_mongoc_handshake_get ());

Expand All @@ -535,10 +520,16 @@ void
_mongoc_handshake_cleanup (void)
{
mongoc_handshake_t *h = _mongoc_handshake_get ();
_free_system_info (h);
_free_driver_info (h);
_free_platform_string (h);
_free_env_info (h);
bson_free (h->os_type);
bson_free (h->os_name);
bson_free (h->os_version);
bson_free (h->os_architecture);
bson_free (h->driver_name);
bson_free (h->driver_version);
bson_free (h->platform);
bson_free (h->compiler_info);
bson_free (h->flags);
bson_free (h->env_region);
*h = (mongoc_handshake_t){0};

bson_mutex_destroy (&gHandshakeLock);
Expand Down Expand Up @@ -696,7 +687,11 @@ _mongoc_handshake_build_doc_with_application (const char *appname)
doc (kv ("name", cstr (env_name)),
if (md->env_timeout_sec.set, then (kv ("timeout_sec", int32 (md->env_timeout_sec.value)))),
if (md->env_memory_mb.set, then (kv ("memory_mb", int32 (md->env_memory_mb.value)))),
if (md->env_region, then (kv ("region", cstr (md->env_region)))))))));
if (md->env_region, then (kv ("region", cstr (md->env_region)))))))),
if (md->kubernetes || md->docker,
then (kv ("container",
doc (if (md->docker, then (kv ("runtime", cstr ("docker")))),
if (md->kubernetes, then (kv ("orchestrator", cstr ("kubernetes")))))))));

if (md->platform) {
_append_platform_field (doc, md->platform, false);
Expand Down
32 changes: 32 additions & 0 deletions src/libmongoc/tests/test-mongoc-handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ clear_faas_env (void)
ASSERT (_mongoc_setenv ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", ""));
ASSERT (_mongoc_setenv ("FUNCTIONS_WORKER_RUNTIME", ""));
ASSERT (_mongoc_setenv ("K_SERVICE", ""));
ASSERT (_mongoc_setenv ("KUBERNETES_SERVICE_HOST", ""));
ASSERT (_mongoc_setenv ("FUNCTION_MEMORY_MB", ""));
ASSERT (_mongoc_setenv ("FUNCTION_TIMEOUT_SEC", ""));
ASSERT (_mongoc_setenv ("FUNCTION_REGION", ""));
Expand Down Expand Up @@ -643,6 +644,31 @@ test_aws_not_lambda (void *test_ctx)
_reset_handshake ();
}

static void
test_aws_and_container (void *test_ctx)
{
BSON_UNUSED (test_ctx);
ASSERT (_mongoc_setenv ("AWS_EXECUTION_ENV", "AWS_Lambda_java8"));
ASSERT (_mongoc_setenv ("AWS_REGION", "us-east-2"));
ASSERT (_mongoc_setenv ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "1024"));
ASSERT (_mongoc_setenv ("KUBERNETES_SERVICE_HOST", "1"));

_override_host_platform_os ();
bson_t *doc = _get_handshake_document (true);
_handshake_check_required_fields (doc);

bson_iter_t iter;
ASSERT (bson_iter_init(&iter, doc));
ASSERT (bson_iter_find_descendant(&iter, "container.orchestrator", &iter));
ASSERT (BSON_ITER_HOLDS_UTF8(&iter));
ASSERT (strcmp("kubernetes", bson_iter_utf8(&iter, NULL)) == 0);
_handshake_check_env (doc, default_memory_mb, 0, "us-east-2");

bson_destroy (doc);
clear_faas_env ();
_reset_handshake ();
}

static void
test_mongoc_handshake_data_append_null_args (void)
{
Expand Down Expand Up @@ -1396,4 +1422,10 @@ test_handshake_install (TestSuite *suite)
NULL /* dtor */,
NULL /* ctx */,
test_framework_skip_if_no_setenv);
TestSuite_AddFull (suite,
"/MongoDB/handshake/faas/aws_and_container",
test_aws_and_container,
NULL /* dtor */,
NULL /* ctx */,
test_framework_skip_if_no_setenv);
}