Skip to content

Commit f142c61

Browse files
authored
CDRIVER-5638 Record both FaaS and container metadata when both are present (#2016)
1 parent 36f6d7f commit f142c61

File tree

3 files changed

+61
-31
lines changed

3 files changed

+61
-31
lines changed

src/libmongoc/src/mongoc/mongoc-handshake-private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ typedef struct _mongoc_handshake_t {
113113
char *compiler_info;
114114
char *flags;
115115

116+
bool docker;
117+
bool kubernetes;
118+
116119
mongoc_handshake_env_t env;
117120
optional_int32 env_timeout_sec;
118121
optional_int32 env_memory_mb;

src/libmongoc/src/mongoc/mongoc-handshake.c

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#ifdef _POSIX_VERSION
2020
#include <sys/utsname.h>
21+
#include <unistd.h>
2122
#endif
2223

2324
#ifdef _WIN32
@@ -356,39 +357,33 @@ _get_system_info (mongoc_handshake_t *handshake)
356357
handshake->os_architecture = _get_os_architecture ();
357358
}
358359

359-
static void
360-
_free_system_info (mongoc_handshake_t *handshake)
361-
{
362-
bson_free (handshake->os_type);
363-
bson_free (handshake->os_name);
364-
bson_free (handshake->os_version);
365-
bson_free (handshake->os_architecture);
366-
}
367-
368360
static void
369361
_get_driver_info (mongoc_handshake_t *handshake)
370362
{
371363
handshake->driver_name = bson_strndup ("mongoc", HANDSHAKE_DRIVER_NAME_MAX);
372364
handshake->driver_version = bson_strndup (MONGOC_VERSION_S, HANDSHAKE_DRIVER_VERSION_MAX);
373365
}
374366

375-
static void
376-
_free_driver_info (mongoc_handshake_t *handshake)
377-
{
378-
bson_free (handshake->driver_name);
379-
bson_free (handshake->driver_version);
380-
}
381-
382367
static void
383368
_set_platform_string (mongoc_handshake_t *handshake)
384369
{
385370
handshake->platform = bson_strdup ("");
386371
}
387372

388373
static void
389-
_free_env_info (mongoc_handshake_t *handshake)
374+
_get_container_info (mongoc_handshake_t *handshake)
390375
{
391-
bson_free (handshake->env_region);
376+
char *kubernetes_env = _mongoc_getenv ("KUBERNETES_SERVICE_HOST");
377+
handshake->kubernetes = kubernetes_env;
378+
379+
handshake->docker = false;
380+
#ifdef _WIN32
381+
handshake->docker = (_access_s ("C:\\.dockerenv", 0) == 0);
382+
#else
383+
handshake->docker = (access ("/.dockerenv", F_OK) == 0);
384+
#endif
385+
386+
bson_free (kubernetes_env);
392387
}
393388

394389
static void
@@ -514,21 +509,14 @@ _set_flags (mongoc_handshake_t *handshake)
514509
handshake->flags = mcommon_string_from_append_destroy_with_steal (&append);
515510
}
516511

517-
static void
518-
_free_platform_string (mongoc_handshake_t *handshake)
519-
{
520-
bson_free (handshake->platform);
521-
bson_free (handshake->compiler_info);
522-
bson_free (handshake->flags);
523-
}
524-
525512
void
526513
_mongoc_handshake_init (void)
527514
{
528515
_get_system_info (_mongoc_handshake_get ());
529516
_get_driver_info (_mongoc_handshake_get ());
530517
_set_platform_string (_mongoc_handshake_get ());
531518
_get_env_info (_mongoc_handshake_get ());
519+
_get_container_info (_mongoc_handshake_get ());
532520
_set_compiler_info (_mongoc_handshake_get ());
533521
_set_flags (_mongoc_handshake_get ());
534522

@@ -540,10 +528,16 @@ void
540528
_mongoc_handshake_cleanup (void)
541529
{
542530
mongoc_handshake_t *h = _mongoc_handshake_get ();
543-
_free_system_info (h);
544-
_free_driver_info (h);
545-
_free_platform_string (h);
546-
_free_env_info (h);
531+
bson_free (h->os_type);
532+
bson_free (h->os_name);
533+
bson_free (h->os_version);
534+
bson_free (h->os_architecture);
535+
bson_free (h->driver_name);
536+
bson_free (h->driver_version);
537+
bson_free (h->platform);
538+
bson_free (h->compiler_info);
539+
bson_free (h->flags);
540+
bson_free (h->env_region);
547541
*h = (mongoc_handshake_t) {0};
548542

549543
bson_mutex_destroy (&gHandshakeLock);
@@ -701,7 +695,11 @@ _mongoc_handshake_build_doc_with_application (const char *appname)
701695
doc (kv ("name", cstr (env_name)),
702696
if (md->env_timeout_sec.set, then (kv ("timeout_sec", int32 (md->env_timeout_sec.value)))),
703697
if (md->env_memory_mb.set, then (kv ("memory_mb", int32 (md->env_memory_mb.value)))),
704-
if (md->env_region, then (kv ("region", cstr (md->env_region)))))))));
698+
if (md->env_region, then (kv ("region", cstr (md->env_region)))))))),
699+
if (md->kubernetes || md->docker,
700+
then (kv ("container",
701+
doc (if (md->docker, then (kv ("runtime", cstr ("docker")))),
702+
if (md->kubernetes, then (kv ("orchestrator", cstr ("kubernetes")))))))));
705703

706704
if (md->platform) {
707705
_append_platform_field (doc, md->platform, false);

src/libmongoc/tests/test-mongoc-handshake.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ clear_faas_env (void)
399399
ASSERT (_mongoc_setenv ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", ""));
400400
ASSERT (_mongoc_setenv ("FUNCTIONS_WORKER_RUNTIME", ""));
401401
ASSERT (_mongoc_setenv ("K_SERVICE", ""));
402+
ASSERT (_mongoc_setenv ("KUBERNETES_SERVICE_HOST", ""));
402403
ASSERT (_mongoc_setenv ("FUNCTION_MEMORY_MB", ""));
403404
ASSERT (_mongoc_setenv ("FUNCTION_TIMEOUT_SEC", ""));
404405
ASSERT (_mongoc_setenv ("FUNCTION_REGION", ""));
@@ -646,6 +647,28 @@ test_aws_not_lambda (void *test_ctx)
646647
_reset_handshake ();
647648
}
648649

650+
static void
651+
test_aws_and_container (void *test_ctx)
652+
{
653+
BSON_UNUSED (test_ctx);
654+
ASSERT (_mongoc_setenv ("AWS_EXECUTION_ENV", "AWS_Lambda_java8"));
655+
ASSERT (_mongoc_setenv ("AWS_REGION", "us-east-2"));
656+
ASSERT (_mongoc_setenv ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "1024"));
657+
ASSERT (_mongoc_setenv ("KUBERNETES_SERVICE_HOST", "1"));
658+
659+
_override_host_platform_os ();
660+
bson_t *doc = _get_handshake_document (true);
661+
_handshake_check_required_fields (doc);
662+
663+
ASSERT_CMPSTR (bson_lookup_utf8 (doc, "container.orchestrator"), "kubernetes");
664+
ASSERT_CMPSTR (bson_lookup_utf8 (doc, "env.name"), "aws.lambda");
665+
_handshake_check_env (doc, default_memory_mb, 0, "us-east-2");
666+
667+
bson_destroy (doc);
668+
clear_faas_env ();
669+
_reset_handshake ();
670+
}
671+
649672
static void
650673
test_mongoc_handshake_data_append_null_args (void)
651674
{
@@ -1399,4 +1422,10 @@ test_handshake_install (TestSuite *suite)
13991422
NULL /* dtor */,
14001423
NULL /* ctx */,
14011424
test_framework_skip_if_no_setenv);
1425+
TestSuite_AddFull (suite,
1426+
"/MongoDB/handshake/faas/aws_and_container",
1427+
test_aws_and_container,
1428+
NULL /* dtor */,
1429+
NULL /* ctx */,
1430+
test_framework_skip_if_no_setenv);
14021431
}

0 commit comments

Comments
 (0)