diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index 5c0a4f687..d0a5b7dac 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -22,7 +22,6 @@ #include #include -using viam::robot::v1::Status; using namespace viam::sdk; namespace po = boost::program_options; diff --git a/src/viam/sdk/common/client_helper.hpp b/src/viam/sdk/common/client_helper.hpp index 126b65e8f..d8db1f338 100644 --- a/src/viam/sdk/common/client_helper.hpp +++ b/src/viam/sdk/common/client_helper.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -53,6 +54,11 @@ class ClientHelper { template ClientHelper& with(const ProtoStruct& extra, RequestSetupCallable&& rsc) { + auto key = extra.find(impl::debug_map_key); + if (key != extra.end()) { + ProtoValue value = key->second; + debug_key_ = *value.get(); + } *request_.mutable_extra() = map_to_struct(extra); return with(std::forward(rsc)); } @@ -67,6 +73,9 @@ class ClientHelper { *request_.mutable_name() = client_->name(); ClientContext ctx; + if (debug_key_ != "") { + ctx.set_debug_key(debug_key_); + } const auto result = (stub_->*pfn_)(ctx, request_, &response_); if (result.ok()) { return std::forward(rhc)( @@ -112,6 +121,7 @@ class ClientHelper { private: ClientType* client_; StubType* stub_; + std::string debug_key_; MethodType pfn_; RequestType request_; ResponseType response_; diff --git a/src/viam/sdk/common/private/utils.hpp b/src/viam/sdk/common/private/utils.hpp new file mode 100644 index 000000000..10f6f9c1c --- /dev/null +++ b/src/viam/sdk/common/private/utils.hpp @@ -0,0 +1,11 @@ +#pragma once + +namespace viam { +namespace sdk { +namespace impl { + +const char debug_map_key[] = "com.viam.debug_key_internal"; + +} // namespace impl +} // namespace sdk +} // namespace viam diff --git a/src/viam/sdk/common/utils.cpp b/src/viam/sdk/common/utils.cpp index cdd9689d9..b3fec14c9 100644 --- a/src/viam/sdk/common/utils.cpp +++ b/src/viam/sdk/common/utils.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -12,6 +13,7 @@ #include +#include #include #include #include @@ -106,6 +108,55 @@ void ClientContext::set_client_ctx_authority_() { wrapped_context_.set_authority("viam-placeholder"); } +std::string random_debug_key() { + static const char alphanum[] = "abcdefghijklmnopqrstuvwxyz"; + static std::default_random_engine generator( + std::chrono::system_clock::now().time_since_epoch().count()); + std::uniform_int_distribution distribution(0, sizeof(alphanum) - 1); + + std::string key; + key.reserve(6); + + for (int i = 0; i < 6; ++i) { + key += alphanum[distribution(generator)]; + }; + + return key; +} + +ProtoStruct debug_map() { + return debug_map(random_debug_key()); +} + +ProtoStruct debug_map(std::string debug_key) { + ProtoStruct map; + map.emplace(impl::debug_map_key, ProtoValue(std::move(debug_key))); + + return map; +} + +void add_debug_entry(ProtoStruct& map, std::string debug_key) { + map.emplace(impl::debug_map_key, ProtoValue(std::move(debug_key))); +} + +void add_debug_entry(ProtoStruct& map) { + add_debug_entry(map, random_debug_key()); +} + +ProtoStruct with_debug_entry(ProtoStruct&& map, std::string debug_key) { + add_debug_entry(map, std::move(debug_key)); + return map; +} + +ProtoStruct with_debug_entry(ProtoStruct&& map) { + add_debug_entry(map); + return map; +} + +void ClientContext::set_debug_key(const std::string& debug_key) { + wrapped_context_.AddMetadata("dtname", debug_key); +} + void ClientContext::add_viam_client_version_() { wrapped_context_.AddMetadata("viam_client", impl::k_version); } diff --git a/src/viam/sdk/common/utils.hpp b/src/viam/sdk/common/utils.hpp index 61ac5d595..20beca5ee 100644 --- a/src/viam/sdk/common/utils.hpp +++ b/src/viam/sdk/common/utils.hpp @@ -1,8 +1,5 @@ #pragma once -#include -#include - #include #include @@ -56,6 +53,7 @@ class ClientContext { ClientContext(); operator grpc::ClientContext*(); operator const grpc::ClientContext*() const; + void set_debug_key(const std::string& debug_key); private: void set_client_ctx_authority_(); @@ -63,6 +61,29 @@ class ClientContext { grpc::ClientContext wrapped_context_; }; +/// @brief Returns a new `ProtoStruct` with a random key for server-side debug logging +ProtoStruct debug_map(); + +/// @brief Returns a new `ProtoStruct` with @param debug_key for server-side debug logging +/// @throws Exception if the debug_key contains invalid (e.g., uppercase) gRPC characters +ProtoStruct debug_map(std::string debug_key); + +/// @brief Adds @param debug_key for server-side debug logging to @param map +/// @throws Exception if the debug_key contains invalid (e.g., uppercase) gRPC characters +void add_debug_entry(ProtoStruct& map, std::string debug_key); + +/// @brief Adds a random key to @param map for server-side debug logging +void add_debug_entry(ProtoStruct& map); + +/// @brief Adds @param debug_key for server-side debug logging to @param map +/// @throws Exception if the debug_key contains invalid (e.g., uppercase) gRPC characters +/// @returns the new ProtoStruct +ProtoStruct with_debug_entry(ProtoStruct&& map, std::string debug_key); + +/// @brief Adds a random key to @param map for server-side debug logging +/// @returns the new ProtoStruct +ProtoStruct with_debug_entry(ProtoStruct&& map); + /// @brief Set the boost trivial logger's severity depending on args. /// @param argc The number of args. /// @param argv The commandline arguments to parse. diff --git a/src/viam/sdk/components/arm.hpp b/src/viam/sdk/components/arm.hpp index 79cca9f41..8a75df707 100644 --- a/src/viam/sdk/components/arm.hpp +++ b/src/viam/sdk/components/arm.hpp @@ -93,6 +93,11 @@ class Arm : public Component, public Stoppable { /// @param extra Any additional arguments to the method. virtual std::vector get_joint_positions(const ProtoStruct& extra) = 0; + /// @brief Move each joint on the arm to the corresponding angle specified in @param positions + inline void move_to_joint_positions(const std::vector& positions) { + return move_to_joint_positions(positions, {}); + } + /// @brief Move each joint on the arm to the corresponding angle specified in @param positions /// @param extra Any additional arguments to the method. virtual void move_to_joint_positions(const std::vector& positions, diff --git a/src/viam/sdk/components/private/motor_client.cpp b/src/viam/sdk/components/private/motor_client.cpp index 429c51445..b12f46d96 100644 --- a/src/viam/sdk/components/private/motor_client.cpp +++ b/src/viam/sdk/components/private/motor_client.cpp @@ -1,8 +1,6 @@ #include -#include #include -#include #include #include diff --git a/src/viam/sdk/tests/mocks/mock_motion.cpp b/src/viam/sdk/tests/mocks/mock_motion.cpp index 8a47bf655..9d2eb9754 100644 --- a/src/viam/sdk/tests/mocks/mock_motion.cpp +++ b/src/viam/sdk/tests/mocks/mock_motion.cpp @@ -1,9 +1,10 @@ -#include "viam/sdk/services/motion.hpp" #include #include +#include #include +#include #include #include #include @@ -65,7 +66,12 @@ std::string MockMotion::move_on_globe( pose_in_frame MockMotion::get_pose(const Name&, const std::string&, const std::vector&, - const ProtoStruct&) { + const ProtoStruct& extra) { + auto key = extra.find(impl::debug_map_key); + if (key != extra.end()) { + ProtoValue value = key->second; + peek_debug_key = *value.get(); + } return current_location; } diff --git a/src/viam/sdk/tests/mocks/mock_motion.hpp b/src/viam/sdk/tests/mocks/mock_motion.hpp index 813929f82..aef661c96 100644 --- a/src/viam/sdk/tests/mocks/mock_motion.hpp +++ b/src/viam/sdk/tests/mocks/mock_motion.hpp @@ -91,6 +91,7 @@ class MockMotion : public sdk::Motion { std::string peek_destination_frame; double peek_heading; bool peek_stop_plan_called = false; + std::string peek_debug_key; std::vector peek_obstacles; std::vector peek_map_obstacles; std::shared_ptr peek_constraints; diff --git a/src/viam/sdk/tests/test_motion.cpp b/src/viam/sdk/tests/test_motion.cpp index a3ece12da..9e06c6aa4 100644 --- a/src/viam/sdk/tests/test_motion.cpp +++ b/src/viam/sdk/tests/test_motion.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -164,18 +165,20 @@ BOOST_AUTO_TEST_SUITE(test_motion_client_server) BOOST_AUTO_TEST_CASE(test_move_and_get_pose) { auto mock = std::make_shared("mock_motion"); - client_to_mock_pipeline(mock, [](Motion& client) { + client_to_mock_pipeline(mock, [&](Motion& client) { std::string destination_frame("destination"); std::vector transforms; - ProtoStruct extra = fake_map(); - pose_in_frame pose = client.get_pose(fake_component_name(), destination_frame, {}, extra); + std::string debug_key = "debug-key"; + pose_in_frame pose = client.get_pose( + fake_component_name(), destination_frame, {}, with_debug_entry(fake_map(), debug_key)); + BOOST_CHECK_EQUAL(mock->peek_debug_key, debug_key); BOOST_CHECK_EQUAL(pose, init_fake_pose()); auto ws = std::make_shared(mock_world_state()); bool success = client.move(fake_pose(), fake_component_name(), ws, nullptr, fake_map()); BOOST_TEST(success); - pose = client.get_pose(fake_component_name(), destination_frame, transforms, extra); + pose = client.get_pose(fake_component_name(), destination_frame, transforms, fake_map()); BOOST_CHECK_EQUAL(pose, fake_pose()); }); }