Skip to content

Commit fd84da8

Browse files
author
Jesse Williamson
authored
CXX-2342 add serviceid to events (#823)
* Set up bsoncxx spuport for catch, etc.. Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com> * Update event commands. Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com> * Import rewritten mongoc_symbols Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com> * Add unit test for serviceId availability. Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com> * Various cleanup; fix nullopt_t usage; remove is_load_balanced() Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com> * Tests for mocked load-balanced or non-load-balanced service_id. Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com> * Apply clang-format. Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com> * Cleanups in response to review comments. Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com> * Small fixups in response to review. Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com> * Update test, move function object to lambda. Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com>
1 parent 3b27f8d commit fd84da8

File tree

11 files changed

+182
-2
lines changed

11 files changed

+182
-2
lines changed

src/bsoncxx/private/helpers.hh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 MongoDB Inc.
1+
// Copyright 2015-present MongoDB Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
#include <bsoncxx/document/value.hpp>
1818
#include <bsoncxx/document/view.hpp>
19+
#include <bsoncxx/oid.hpp>
1920
#include <bsoncxx/private/libbson.hh>
2021

2122
#include <bsoncxx/config/private/prelude.hh>
@@ -33,6 +34,20 @@ inline document::value value_from_bson_t(const bson_t* bson) {
3334
return document::value{view_from_bson_t(bson)};
3435
}
3536

37+
/*
38+
Construct an oid from a bson_oid_t (which is a C API type that we don't want to
39+
expose to the world).
40+
41+
Note: passing a nullptr is unguarded
42+
Note: Deduction guides aren't yet available to us, so a factory it is! This is
43+
something that can be improved as part of CXX-2350 (migration to more recent C++
44+
standards).
45+
*/
46+
inline bsoncxx::oid make_oid(const bson_oid_t* bson_oid) {
47+
return bsoncxx::oid(reinterpret_cast<const char*>(bson_oid),
48+
bsoncxx::oid::size());
49+
}
50+
3651
} // namespace helpers
3752
BSONCXX_INLINE_NAMESPACE_END
3853
} // namespace bsoncxx

src/bsoncxx/stdx/optional.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ BSONCXX_INLINE_NAMESPACE_END
4040

4141
#include <boost/none.hpp>
4242
#include <boost/optional/optional.hpp>
43+
#include <boost/optional/optional_io.hpp>
4344

4445
namespace bsoncxx {
4546
BSONCXX_INLINE_NAMESPACE_BEGIN

src/bsoncxx/test_util/catch.hh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "catch.hpp"
1818
#include <bsoncxx/document/value.hpp>
1919
#include <bsoncxx/json.hpp>
20+
#include <bsoncxx/oid.hpp>
2021
#include <bsoncxx/stdx/optional.hpp>
2122

2223
#include <bsoncxx/config/private/prelude.hh>
@@ -25,6 +26,14 @@ namespace Catch {
2526
using namespace bsoncxx;
2627

2728
// Catch2 must be able to stringify documents, optionals, etc. if they're used in Catch2 macros.
29+
30+
template <>
31+
struct StringMaker<bsoncxx::oid> {
32+
static std::string convert(const bsoncxx::oid& value) {
33+
return value.to_string();
34+
}
35+
};
36+
2837
template <>
2938
struct StringMaker<bsoncxx::document::view> {
3039
static std::string convert(const bsoncxx::document::view& value) {

src/mongocxx/events/command_failed_event.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <bsoncxx/oid.hpp>
16+
#include <bsoncxx/private/helpers.hh>
17+
#include <bsoncxx/private/libbson.hh>
1518
#include <bsoncxx/stdx/make_unique.hpp>
19+
#include <bsoncxx/stdx/optional.hpp>
1620
#include <mongocxx/events/command_failed_event.hpp>
1721
#include <mongocxx/private/libmongoc.hh>
1822

@@ -52,6 +56,16 @@ std::int64_t command_failed_event::operation_id() const {
5256
static_cast<const mongoc_apm_command_failed_t*>(_failed_event));
5357
}
5458

59+
bsoncxx::stdx::optional<bsoncxx::oid> command_failed_event::service_id() const {
60+
const bson_oid_t* bson_oid = libmongoc::apm_command_failed_get_service_id(
61+
static_cast<const mongoc_apm_command_failed_t*>(_failed_event));
62+
63+
if (nullptr == bson_oid)
64+
return {bsoncxx::stdx::nullopt};
65+
66+
return {bsoncxx::helpers::make_oid(bson_oid)};
67+
}
68+
5569
bsoncxx::stdx::string_view command_failed_event::host() const {
5670
return libmongoc::apm_command_failed_get_host(
5771
static_cast<const mongoc_apm_command_failed_t*>(_failed_event))

src/mongocxx/events/command_failed_event.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <memory>
1818

1919
#include <bsoncxx/document/view.hpp>
20+
#include <bsoncxx/oid.hpp>
21+
#include <bsoncxx/stdx/optional.hpp>
2022

2123
#include <mongocxx/config/prelude.hpp>
2224

@@ -75,6 +77,13 @@ class MONGOCXX_API command_failed_event {
7577
///
7678
std::int64_t operation_id() const;
7779

80+
///
81+
/// Optionally returns the service id.
82+
///
83+
/// @return No contained value, or contains the service id if load balancing is enabled.
84+
///
85+
bsoncxx::stdx::optional<bsoncxx::oid> service_id() const;
86+
7887
///
7988
/// Returns the host name.
8089
///

src/mongocxx/events/command_started_event.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <bsoncxx/private/helpers.hh>
1516
#include <bsoncxx/stdx/make_unique.hpp>
1617
#include <mongocxx/events/command_started_event.hpp>
1718
#include <mongocxx/private/libmongoc.hh>
@@ -52,6 +53,16 @@ std::int64_t command_started_event::operation_id() const {
5253
static_cast<const mongoc_apm_command_started_t*>(_started_event));
5354
}
5455

56+
bsoncxx::stdx::optional<bsoncxx::oid> command_started_event::service_id() const {
57+
const bson_oid_t* bson_oid = libmongoc::apm_command_started_get_service_id(
58+
static_cast<const mongoc_apm_command_started_t*>(_started_event));
59+
60+
if (nullptr == bson_oid)
61+
return {bsoncxx::stdx::nullopt};
62+
63+
return {bsoncxx::helpers::make_oid(bson_oid)};
64+
}
65+
5566
bsoncxx::stdx::string_view command_started_event::host() const {
5667
return libmongoc::apm_command_started_get_host(
5768
static_cast<const mongoc_apm_command_started_t*>(_started_event))

src/mongocxx/events/command_started_event.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <memory>
1818

1919
#include <bsoncxx/document/view.hpp>
20+
#include <bsoncxx/oid.hpp>
21+
#include <bsoncxx/stdx/optional.hpp>
2022

2123
#include <mongocxx/config/prelude.hpp>
2224

@@ -75,6 +77,13 @@ class MONGOCXX_API command_started_event {
7577
///
7678
std::int64_t operation_id() const;
7779

80+
///
81+
/// Optionally returns the service id.
82+
///
83+
/// @return No contained value, or contains the service id if load balancing is enabled.
84+
///
85+
bsoncxx::stdx::optional<bsoncxx::oid> service_id() const;
86+
7887
///
7988
/// Returns the host name.
8089
///

src/mongocxx/events/command_succeeded_event.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <bsoncxx/private/helpers.hh>
1516
#include <bsoncxx/stdx/make_unique.hpp>
1617
#include <mongocxx/events/command_succeeded_event.hpp>
1718
#include <mongocxx/private/libmongoc.hh>
@@ -52,6 +53,16 @@ std::int64_t command_succeeded_event::operation_id() const {
5253
static_cast<const mongoc_apm_command_succeeded_t*>(_succeeded_event));
5354
}
5455

56+
bsoncxx::stdx::optional<bsoncxx::oid> command_succeeded_event::service_id() const {
57+
const bson_oid_t* bson_oid = libmongoc::apm_command_succeeded_get_service_id(
58+
static_cast<const mongoc_apm_command_succeeded_t*>(_succeeded_event));
59+
60+
if (nullptr == bson_oid)
61+
return {bsoncxx::stdx::nullopt};
62+
63+
return {bsoncxx::helpers::make_oid(bson_oid)};
64+
}
65+
5566
bsoncxx::stdx::string_view command_succeeded_event::host() const {
5667
return libmongoc::apm_command_succeeded_get_host(
5768
static_cast<const mongoc_apm_command_succeeded_t*>(_succeeded_event))

src/mongocxx/events/command_succeeded_event.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <memory>
1818

1919
#include <bsoncxx/document/view.hpp>
20+
#include <bsoncxx/oid.hpp>
21+
#include <bsoncxx/stdx/optional.hpp>
2022

2123
#include <mongocxx/config/prelude.hpp>
2224

@@ -75,6 +77,13 @@ class MONGOCXX_API command_succeeded_event {
7577
///
7678
std::int64_t operation_id() const;
7779

80+
///
81+
/// Optionally returns the service id.
82+
///
83+
/// @return No contained value, or contains the service id if load balancing is enabled.
84+
///
85+
bsoncxx::stdx::optional<bsoncxx::oid> service_id() const;
86+
7887
///
7988
/// Returns the host name.
8089
///

src/mongocxx/private/libmongoc_symbols.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ MONGOCXX_LIBMONGOC_SYMBOL(apm_command_failed_get_operation_id)
2525
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_failed_get_reply)
2626
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_failed_get_request_id)
2727
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_failed_get_server_id)
28+
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_failed_get_service_id)
2829
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_started_get_command)
2930
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_started_get_command_name)
3031
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_started_get_context)
@@ -33,6 +34,7 @@ MONGOCXX_LIBMONGOC_SYMBOL(apm_command_started_get_host)
3334
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_started_get_operation_id)
3435
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_started_get_request_id)
3536
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_started_get_server_id)
37+
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_started_get_service_id)
3638
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_succeeded_get_command_name)
3739
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_succeeded_get_context)
3840
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_succeeded_get_duration)
@@ -41,6 +43,7 @@ MONGOCXX_LIBMONGOC_SYMBOL(apm_command_succeeded_get_operation_id)
4143
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_succeeded_get_reply)
4244
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_succeeded_get_request_id)
4345
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_succeeded_get_server_id)
46+
MONGOCXX_LIBMONGOC_SYMBOL(apm_command_succeeded_get_service_id)
4447
MONGOCXX_LIBMONGOC_SYMBOL(apm_server_changed_get_context)
4548
MONGOCXX_LIBMONGOC_SYMBOL(apm_server_changed_get_host)
4649
MONGOCXX_LIBMONGOC_SYMBOL(apm_server_changed_get_new_description)

0 commit comments

Comments
 (0)