Skip to content

Commit 6e5c6be

Browse files
author
mdbmes
authored
CDRIVER-3775 mongoc_structured_log (#1795)
This is an initial implementation of "structured logging" for libmongoc. * Partially addresses CDRIVER-3775 (Easier debugging with standardized logging) * Partially addresses CDRIVER-4485 (Logging Specification); prose tests and truncation TBD * Partially addresses CDRIVER-4486 (Command logging and monitoring); prose tests and truncation TBD * Addresses CDRIVER-4535 (Misc updates to logging spec) * Addresses CDRIVER-4553 (Command logging test covering unacknowledged write) * Unified test runner support for: createEntities, observeLogMessages, waitForEvent, $$matchAsDocument, $$matchAsRoot * Add serverDescriptionChangedEvent to the unified test runner and unify its two event serialization systems * Private serialization utilities, *_append_contents_to_bson * Sync unified tests from the command-logging-and-monitoring spec * bson-dsl support for oid() values * Private utilities for dealing with zero'ed oids
1 parent 66d9c5f commit 6e5c6be

File tree

104 files changed

+10789
-861
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+10789
-861
lines changed

src/common/src/bson-dsl.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ Generate a UTF-8 value from the given null-terminated character array beginning
262262
at `zstr`.
263263

264264

265+
#### `oid(const bson_oid_t* oid)`
266+
267+
Generate an ObjectId value from the given C expression that evaluates to
268+
a bson_oid_t pointer.
269+
270+
265271
#### `utf8_w_len(const char* str, int len)`
266272

267273
Generate a UTF-8 value from the character array beginning at `str`, with length

src/common/src/common-bson-dsl-private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
246246
#define _bsonArrayOperation_boolean(X) _bsonArrayAppendValue (boolean (X))
247247
#define _bsonValueOperation_boolean(b) _bsonValueOperation_bool (b)
248248

249+
#define _bsonValueOperation_oid(o) \
250+
if (!bson_append_oid (_bsonBuildAppendArgs, (o))) { \
251+
bsonBuildError = "Error while appending oid(" _bsonDSL_str (o) ")"; \
252+
} else \
253+
((void) 0)
254+
#define _bsonArrayOperation_oid(X) _bsonArrayAppendValue (oid (X))
255+
249256
#define _bsonValueOperation_null \
250257
if (!bson_append_null (_bsonBuildAppendArgs)) { \
251258
bsonBuildError = "Error while appending a null"; \

src/common/src/common-oid-private.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2009-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "common-prelude.h"
18+
19+
#ifndef MONGO_C_DRIVER_COMMON_OID_PRIVATE_H
20+
#define MONGO_C_DRIVER_COMMON_OID_PRIVATE_H
21+
22+
#include <bson/bson.h>
23+
24+
BSON_BEGIN_DECLS
25+
26+
extern const bson_oid_t kZeroObjectId;
27+
28+
void
29+
mcommon_oid_set_zero (bson_oid_t *oid);
30+
31+
bool
32+
mcommon_oid_is_zero (const bson_oid_t *oid);
33+
34+
BSON_END_DECLS
35+
36+
#endif /* MONGO_C_DRIVER_COMMON_OID_PRIVATE_H */

src/common/src/common-oid.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2009-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <common-oid-private.h>
18+
19+
const bson_oid_t kZeroObjectId = {{0}};
20+
21+
void
22+
mcommon_oid_set_zero (bson_oid_t *oid)
23+
{
24+
BSON_ASSERT (oid);
25+
memset (oid, 0, sizeof *oid);
26+
}
27+
28+
bool
29+
mcommon_oid_is_zero (const bson_oid_t *oid)
30+
{
31+
BSON_ASSERT (oid);
32+
return bson_oid_equal_unsafe (oid, &kZeroObjectId);
33+
}

src/common/tests/test-common-oid.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2009-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "TestSuite.h"
18+
19+
#include <common-oid-private.h>
20+
21+
static void
22+
test_mcommon_oid_zero (void)
23+
{
24+
bson_oid_t oid;
25+
bson_oid_init_from_string (&oid, "000000000000000000000000");
26+
BSON_ASSERT (true == bson_oid_equal (&oid, &kZeroObjectId));
27+
BSON_ASSERT (true == mcommon_oid_is_zero (&oid));
28+
bson_oid_init_from_string (&oid, "010000000000000000000000");
29+
BSON_ASSERT (false == mcommon_oid_is_zero (&oid));
30+
bson_oid_init_from_string (&oid, "000000000000000000000001");
31+
BSON_ASSERT (false == mcommon_oid_is_zero (&oid));
32+
bson_oid_init_from_string (&oid, "ffffffffffffffffffffffff");
33+
BSON_ASSERT (false == mcommon_oid_is_zero (&oid));
34+
mcommon_oid_set_zero (&oid);
35+
BSON_ASSERT (true == mcommon_oid_is_zero (&oid));
36+
}
37+
38+
void
39+
test_mcommon_oid_install (TestSuite *suite)
40+
{
41+
TestSuite_Add (suite, "/mcommon/oid/zero", test_mcommon_oid_zero);
42+
}

src/libbson/src/bson/bson-macros.h

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323

2424
#include <stdio.h>
25+
#include <stdlib.h>
2526

2627
#ifdef __cplusplus
2728
#include <algorithm>
@@ -190,25 +191,52 @@
190191
#define BSON_FUNC __func__
191192
#endif
192193

193-
#define BSON_ASSERT(test) \
194-
do { \
195-
if (!(BSON_LIKELY (test))) { \
196-
fprintf (stderr, "%s:%d %s(): precondition failed: %s\n", __FILE__, (int) (__LINE__), BSON_FUNC, #test); \
197-
abort (); \
198-
} \
194+
195+
#if defined(_MSC_VER)
196+
#define BSON_INLINE __inline
197+
#else
198+
#define BSON_INLINE __inline__
199+
#endif
200+
201+
202+
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
203+
#define BSON_NORETURN noreturn
204+
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
205+
#define BSON_NORETURN _Noreturn
206+
#elif defined(__GNUC__) && 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
207+
#define BSON_NORETURN __attribute__ ((__noreturn__))
208+
#else
209+
#define BSON_NORETURN
210+
#endif
211+
212+
213+
static BSON_INLINE BSON_NORETURN void
214+
_bson_assert_failed_on_line (const char *file, int line, const char *func, const char *test)
215+
{
216+
fprintf (stderr, "%s:%d %s(): assertion failed: %s\n", file, line, func, test);
217+
abort ();
218+
}
219+
220+
static BSON_INLINE BSON_NORETURN void
221+
_bson_assert_failed_on_param (const char *param, const char *func)
222+
{
223+
fprintf (stderr, "The parameter: %s, in function %s, cannot be NULL\n", param, func);
224+
abort ();
225+
}
226+
227+
#define BSON_ASSERT(test) \
228+
do { \
229+
if (!(BSON_LIKELY (test))) { \
230+
_bson_assert_failed_on_line (__FILE__, (int) (__LINE__), BSON_FUNC, #test); \
231+
} \
199232
} while (0)
200233

201234
/**
202235
* @brief Assert the expression `Assertion`, and evaluates to `Value` on
203236
* success.
204237
*/
205238
#define BSON_ASSERT_INLINE(Assertion, Value) \
206-
((void) ((Assertion) \
207-
? (0) \
208-
: ((fprintf ( \
209-
stderr, "%s:%d %s(): Assertion '%s' failed", __FILE__, (int) (__LINE__), BSON_FUNC, #Assertion), \
210-
abort ()), \
211-
0)), \
239+
((void) ((Assertion) ? (0) : (_bson_assert_failed_on_line (__FILE__, (int) (__LINE__), BSON_FUNC, #Assertion), 0)), \
212240
Value)
213241

214242
/**
@@ -225,12 +253,11 @@
225253
#define BSON_ASSERT_PTR_INLINE(Pointer) BSON_ASSERT_INLINE ((Pointer) != NULL, (Pointer))
226254

227255
/* Used for asserting parameters to provide a more precise error message */
228-
#define BSON_ASSERT_PARAM(param) \
229-
do { \
230-
if ((BSON_UNLIKELY (param == NULL))) { \
231-
fprintf (stderr, "The parameter: %s, in function %s, cannot be NULL\n", #param, BSON_FUNC); \
232-
abort (); \
233-
} \
256+
#define BSON_ASSERT_PARAM(param) \
257+
do { \
258+
if ((BSON_UNLIKELY (param == NULL))) { \
259+
_bson_assert_failed_on_param (#param, BSON_FUNC); \
260+
} \
234261
} while (0)
235262

236263
// `BSON_OPTIONAL_PARAM` is a documentation-only macro to document X may be NULL.
@@ -294,13 +321,6 @@
294321
#endif
295322

296323

297-
#if defined(_MSC_VER)
298-
#define BSON_INLINE __inline
299-
#else
300-
#define BSON_INLINE __inline__
301-
#endif
302-
303-
304324
#ifdef _MSC_VER
305325
#define BSON_ENSURE_ARRAY_PARAM_SIZE(_n)
306326
#define BSON_TYPEOF decltype

src/libbson/src/bson/bson-timegm.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@
3131
#define ATTRIBUTE_FORMAT(spec) /* empty */
3232
#endif
3333

34-
#if !defined _Noreturn && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112)
35-
#if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
36-
#define _Noreturn __attribute__ ((__noreturn__))
37-
#else
38-
#define _Noreturn
39-
#endif
40-
#endif
41-
4234
#if !defined(__STDC_VERSION__) && !defined restrict
4335
#define restrict /* empty */
4436
#endif

src/libmongoc/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ example-scram
3434
example-sdam-monitoring
3535
example-session
3636
example-start-at-optime
37+
example-structured-log
3738
example-transaction
3839
example-update
3940
fam

src/libmongoc/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ set (MONGOC_SOURCES
648648
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-stream-gridfs-download.c
649649
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-stream-gridfs-upload.c
650650
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-stream-socket.c
651+
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-structured-log.c
651652
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-timeout.c
652653
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-topology.c
653654
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-topology-background-monitoring.c
@@ -664,6 +665,7 @@ set (MONGOC_SOURCES
664665
${mongo-c-driver_SOURCE_DIR}/src/common/src/common-atomic.c
665666
${mongo-c-driver_SOURCE_DIR}/src/common/src/common-b64.c
666667
${mongo-c-driver_SOURCE_DIR}/src/common/src/common-md5.c
668+
${mongo-c-driver_SOURCE_DIR}/src/common/src/common-oid.c
667669
${mongo-c-driver_SOURCE_DIR}/src/common/src/common-thread.c
668670

669671
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-crypto.c
@@ -762,6 +764,7 @@ set (HEADERS
762764
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-stream-file.h
763765
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-stream-gridfs.h
764766
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-stream-socket.h
767+
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-structured-log.h
765768
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-topology-description.h
766769
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-uri.h
767770
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-version-functions.h
@@ -1031,6 +1034,7 @@ endif ()
10311034
set (test-libmongoc-sources
10321035
${mongo-c-driver_SOURCE_DIR}/src/common/tests/test-common-atomic.c
10331036
${mongo-c-driver_SOURCE_DIR}/src/common/tests/test-common-cmp.c
1037+
${mongo-c-driver_SOURCE_DIR}/src/common/tests/test-common-oid.c
10341038
${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/corpus-test.c
10351039
${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/corpus-test.h
10361040
${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/test-atomic.c
@@ -1141,6 +1145,7 @@ set (test-libmongoc-sources
11411145
${PROJECT_SOURCE_DIR}/tests/test-mongoc-ssl.c
11421146
${PROJECT_SOURCE_DIR}/tests/test-mongoc-stream.c
11431147
${PROJECT_SOURCE_DIR}/tests/test-mongoc-streamable-hello.c
1148+
${PROJECT_SOURCE_DIR}/tests/test-mongoc-structured-log.c
11441149
${PROJECT_SOURCE_DIR}/tests/test-mongoc-thread.c
11451150
${PROJECT_SOURCE_DIR}/tests/test-mongoc-timeout.c
11461151
${PROJECT_SOURCE_DIR}/tests/test-mongoc-topology-description.c
@@ -1292,7 +1297,9 @@ if (ENABLE_EXAMPLES AND ENABLE_SHARED)
12921297
mongoc_add_example (example-gridfs ${PROJECT_SOURCE_DIR}/examples/example-gridfs.c)
12931298
mongoc_add_example (example-gridfs-bucket ${PROJECT_SOURCE_DIR}/examples/example-gridfs-bucket.c)
12941299
if (NOT WIN32 AND ENABLE_EXAMPLES)
1300+
# Examples that use pthreads
12951301
mongoc_add_example (example-pool ${PROJECT_SOURCE_DIR}/examples/example-pool.c)
1302+
mongoc_add_example (example-structured-log ${PROJECT_SOURCE_DIR}/examples/example-structured-log.c)
12961303
endif ()
12971304
mongoc_add_example (example-scram ${PROJECT_SOURCE_DIR}/examples/example-scram.c)
12981305
mongoc_add_example (example-sdam-monitoring ${PROJECT_SOURCE_DIR}/examples/example-sdam-monitoring.c)

0 commit comments

Comments
 (0)