18
18
19
19
#ifdef _POSIX_VERSION
20
20
#include <sys/utsname.h>
21
+ #include <unistd.h>
21
22
#endif
22
23
23
24
#ifdef _WIN32
@@ -356,39 +357,33 @@ _get_system_info (mongoc_handshake_t *handshake)
356
357
handshake -> os_architecture = _get_os_architecture ();
357
358
}
358
359
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
-
368
360
static void
369
361
_get_driver_info (mongoc_handshake_t * handshake )
370
362
{
371
363
handshake -> driver_name = bson_strndup ("mongoc" , HANDSHAKE_DRIVER_NAME_MAX );
372
364
handshake -> driver_version = bson_strndup (MONGOC_VERSION_S , HANDSHAKE_DRIVER_VERSION_MAX );
373
365
}
374
366
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
-
382
367
static void
383
368
_set_platform_string (mongoc_handshake_t * handshake )
384
369
{
385
370
handshake -> platform = bson_strdup ("" );
386
371
}
387
372
388
373
static void
389
- _free_env_info (mongoc_handshake_t * handshake )
374
+ _get_container_info (mongoc_handshake_t * handshake )
390
375
{
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 );
392
387
}
393
388
394
389
static void
@@ -514,21 +509,14 @@ _set_flags (mongoc_handshake_t *handshake)
514
509
handshake -> flags = mcommon_string_from_append_destroy_with_steal (& append );
515
510
}
516
511
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
-
525
512
void
526
513
_mongoc_handshake_init (void )
527
514
{
528
515
_get_system_info (_mongoc_handshake_get ());
529
516
_get_driver_info (_mongoc_handshake_get ());
530
517
_set_platform_string (_mongoc_handshake_get ());
531
518
_get_env_info (_mongoc_handshake_get ());
519
+ _get_container_info (_mongoc_handshake_get ());
532
520
_set_compiler_info (_mongoc_handshake_get ());
533
521
_set_flags (_mongoc_handshake_get ());
534
522
@@ -540,10 +528,16 @@ void
540
528
_mongoc_handshake_cleanup (void )
541
529
{
542
530
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 );
547
541
* h = (mongoc_handshake_t ) {0 };
548
542
549
543
bson_mutex_destroy (& gHandshakeLock );
@@ -701,7 +695,11 @@ _mongoc_handshake_build_doc_with_application (const char *appname)
701
695
doc (kv ("name" , cstr (env_name )),
702
696
if (md -> env_timeout_sec .set , then (kv ("timeout_sec" , int32 (md -> env_timeout_sec .value )))),
703
697
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" )))))))));
705
703
706
704
if (md -> platform ) {
707
705
_append_platform_field (doc , md -> platform , false);
0 commit comments