Skip to content

Commit 69d04ae

Browse files
authored
CDRIVER-3775 structured log: floating point durations (#1831)
* Add double() structured log item * Modify monotonic_time_duration() structured log item to use double-precision floating point milliseconds, instead of two integers See also DRIVERS-3089
1 parent 92aa4fe commit 69d04ae

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

src/libmongoc/src/mongoc/mongoc-structured-log-private.h

+18-5
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ mongoc_structured_log_instance_destroy (mongoc_structured_log_instance_t *instan
172172
#define _mongoc_structured_log_item_int64(_key_or_null, _value_int64) \
173173
{.func = _mongoc_structured_log_append_int64, .arg1.utf8 = (_key_or_null), .arg2.int64 = (_value_int64)},
174174

175+
/**
176+
* @def double(key, value)
177+
* @brief Structured log item, double precision floating point
178+
*
179+
* @param key Key as a NUL-terminated const char * expression, or NULL to skip this item.
180+
* @param value Value as a double expression.
181+
*/
182+
#define _mongoc_structured_log_item_double(_key_or_null, _value_double) \
183+
{.func = _mongoc_structured_log_append_double, .arg1.utf8 = (_key_or_null), .arg2.double_value = (_value_double)},
184+
175185
/**
176186
* @def boolean(key, value)
177187
* @brief Structured log item, boolean
@@ -337,12 +347,9 @@ typedef enum {
337347
* @def monotonic_time_duration(duration)
338348
* @brief Structured log item, standard format for a duration in monotonic time.
339349
* @param duration Duration in microseconds, as an int64_t expression.
340-
*
341-
* Includes milliseconds for consistency across drivers, and microseconds as the highest available resolution.
342350
*/
343-
#define _mongoc_structured_log_item_monotonic_time_duration(_duration) \
344-
_mongoc_structured_log_item_int32 ("durationMS", (int32_t) ((_duration) / 1000)) \
345-
_mongoc_structured_log_item_int64 ("durationMicros", (_duration))
351+
#define _mongoc_structured_log_item_monotonic_time_duration(_duration) \
352+
_mongoc_structured_log_item_double ("durationMS", (_duration) * 1e-3)
346353

347354
typedef struct mongoc_structured_log_builder_stage_t mongoc_structured_log_builder_stage_t;
348355

@@ -371,6 +378,7 @@ struct mongoc_structured_log_builder_stage_t {
371378
const char *utf8;
372379
const mongoc_read_prefs_t *read_prefs;
373380
const struct _mongoc_topology_t *topology;
381+
double double_value;
374382
int32_t int32;
375383
int64_t int64;
376384
mongoc_error_content_flags_t error_flags;
@@ -428,6 +436,11 @@ _mongoc_structured_log_append_int64 (bson_t *bson,
428436
const mongoc_structured_log_builder_stage_t *stage,
429437
const mongoc_structured_log_opts_t *opts);
430438

439+
const mongoc_structured_log_builder_stage_t *
440+
_mongoc_structured_log_append_double (bson_t *bson,
441+
const mongoc_structured_log_builder_stage_t *stage,
442+
const mongoc_structured_log_opts_t *opts);
443+
431444
const mongoc_structured_log_builder_stage_t *
432445
_mongoc_structured_log_append_boolean (bson_t *bson,
433446
const mongoc_structured_log_builder_stage_t *stage,

src/libmongoc/src/mongoc/mongoc-structured-log.c

+13
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,19 @@ _mongoc_structured_log_append_int64 (bson_t *bson,
716716
return stage + 1;
717717
}
718718

719+
const mongoc_structured_log_builder_stage_t *
720+
_mongoc_structured_log_append_double (bson_t *bson,
721+
const mongoc_structured_log_builder_stage_t *stage,
722+
const mongoc_structured_log_opts_t *opts)
723+
{
724+
BSON_UNUSED (opts);
725+
const char *key_or_null = stage->arg1.utf8;
726+
if (key_or_null) {
727+
bson_append_double (bson, key_or_null, -1, stage->arg2.double_value);
728+
}
729+
return stage + 1;
730+
}
731+
719732
const mongoc_structured_log_builder_stage_t *
720733
_mongoc_structured_log_append_boolean (bson_t *bson,
721734
const mongoc_structured_log_builder_stage_t *stage,

src/libmongoc/tests/test-mongoc-structured-log.c

+7-9
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ test_structured_log_basic_data_types (void)
221221
BCON_INT32 (-12345),
222222
"kInt64",
223223
BCON_INT64 (0x76543210aabbccdd),
224+
"kDouble",
225+
BCON_DOUBLE (3.14159265358979323846),
224226
"kTrue",
225227
BCON_BOOL (true),
226228
"kFalse",
@@ -253,6 +255,8 @@ test_structured_log_basic_data_types (void)
253255
int32 (NULL, 9999),
254256
int64 ("kInt64", 0x76543210aabbccdd),
255257
int64 (NULL, -1),
258+
double ("kDouble", 3.14159265358979323846),
259+
double (NULL, 1),
256260
boolean ("kTrue", true),
257261
boolean ("kFalse", false),
258262
boolean (NULL, true));
@@ -626,17 +630,11 @@ test_structured_log_duration (void)
626630
.expected_bson = BCON_NEW ("message",
627631
BCON_UTF8 ("Log entry with duration"),
628632
"durationMS",
629-
BCON_INT32 (1),
630-
"durationMicros",
631-
BCON_INT64 (1999),
633+
BCON_DOUBLE (1.999),
632634
"durationMS",
633-
BCON_INT32 (0),
634-
"durationMicros",
635-
BCON_INT64 (10),
635+
BCON_DOUBLE (0.01),
636636
"durationMS",
637-
BCON_INT32 (10000000),
638-
"durationMicros",
639-
BCON_INT64 (10000000999)),
637+
BCON_DOUBLE (10000000.999)),
640638
.expected_calls = 1,
641639
};
642640

0 commit comments

Comments
 (0)