Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/viam/examples/dial_api_key/example_dial_api_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <viam/sdk/robot/service.hpp>
#include <viam/sdk/rpc/dial.hpp>

using viam::robot::v1::Status;
using namespace viam::sdk;

namespace po = boost::program_options;
Expand Down
12 changes: 12 additions & 0 deletions src/viam/sdk/common/client_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <grpcpp/support/sync_stream.h>

#include <viam/sdk/common/exception.hpp>
#include <viam/sdk/common/private/utils.hpp>
#include <viam/sdk/common/proto_type.hpp>
#include <viam/sdk/common/utils.hpp>

Expand Down Expand Up @@ -53,6 +54,13 @@ class ClientHelper {

template <typename RequestSetupCallable>
ClientHelper& with(const AttributeMap& extra, RequestSetupCallable&& rsc) {
if (extra) {
auto key = extra->find(impl::debug_map_key);
if (key != extra->end()) {
ProtoType value = *(key->second);
debug_key_ = *value.get<std::string>();
}
}
*request_.mutable_extra() = map_to_struct(extra);
return with(std::forward<RequestSetupCallable>(rsc));
}
Expand All @@ -67,6 +75,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<ResponseHandlerCallable>(rhc)(
Expand Down Expand Up @@ -112,6 +123,7 @@ class ClientHelper {
private:
ClientType* client_;
StubType* stub_;
std::string debug_key_;
MethodType pfn_;
RequestType request_;
ResponseType response_;
Expand Down
11 changes: 11 additions & 0 deletions src/viam/sdk/common/private/utils.hpp
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions src/viam/sdk/common/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <viam/sdk/common/utils.hpp>

#include <random>
#include <unordered_map>
#include <vector>

Expand All @@ -14,6 +15,7 @@

#include <viam/api/common/v1/common.pb.h>

#include <viam/sdk/common/private/utils.hpp>
#include <viam/sdk/common/private/version_metadata.hpp>
#include <viam/sdk/components/component.hpp>
#include <viam/sdk/registry/registry.hpp>
Expand Down Expand Up @@ -108,6 +110,53 @@ 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<int> distribution(0, sizeof(alphanum) - 1);

std::string key;
key.reserve(6);

for (int i = 0; i < 6; ++i) {
key += alphanum[distribution(generator)];
};

return key;
}

AttributeMap debug_map() {
auto debug_key = random_debug_key();
return debug_map(debug_key);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return debug_map(debug_key);
return debug_map(random_debug_key());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this suggestion doesn't remove the debug_key variable creation so I'm modifying locally and pushing, but will make this change!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops yes haha limitation of github inline code suggestions

}

AttributeMap debug_map(std::string debug_key) {
AttributeMap map =
std::make_shared<std::unordered_map<std::string, std::shared_ptr<ProtoType>>>();
map->emplace(impl::debug_map_key, std::make_shared<ProtoType>(std::move(debug_key)));

return map;
}

AttributeMap add_debug_entry(AttributeMap map, std::string debug_key) {
map->emplace(impl::debug_map_key, std::make_shared<ProtoType>(std::move(debug_key)));
return map;
}

AttributeMap add_debug_entry(AttributeMap&& map, std::string debug_key) {
return add_debug_entry(map, std::move(debug_key));
}

AttributeMap add_debug_entry(AttributeMap&& map) {
auto debug_key = random_debug_key();
return add_debug_entry(map, std::move(debug_key));
}

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);
}
Expand Down
19 changes: 19 additions & 0 deletions src/viam/sdk/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,32 @@ 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_();
void add_viam_client_version_();
grpc::ClientContext wrapped_context_;
};

/// @brief Returns a new `AttributeMap` with a random key for server-side debug logging
AttributeMap debug_map();

/// @brief Returns a new `AttributeMap` with @param debug_key for server-side debug logging
/// @throws Exception if the debug_key contains invalid (e.g., uppercase) gRPC characters
AttributeMap 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slight typo with the close paren

AttributeMap add_debug_entry(AttributeMap&& 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
AttributeMap add_debug_entry(AttributeMap map, std::string debug_key);

/// @brief Adds a random key to @param map for server-side debug logging
AttributeMap add_debug_entry(AttributeMap&& map);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the one above should probably pass the parameter in the same way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm maybe you can help me out here! I have an add_debug_entry that passes the param the same way above, but with just those two I get compilation errors (candidate function not viable: expects an rvalue for 1st argument). Having the "pass by value" version fixed that, though perhaps it shouldn't be exposed to users?

Copy link
Member Author

@stuqdog stuqdog Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually nevermind! I think after I rebase with your ProtoStruct changes this issue goes away :)

edit: actually maybe not haha, we'll see!

edit edit: okay std::move okay I think we're good.


/// @brief Set the boost trivial logger's severity depending on args.
/// @param argc The number of args.
/// @param argv The commandline arguments to parse.
Expand Down
5 changes: 5 additions & 0 deletions src/viam/sdk/components/arm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class Arm : public Component, public Stoppable {
/// @param extra Any additional arguments to the method.
virtual std::vector<double> get_joint_positions(const AttributeMap& 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<double>& 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<double>& positions,
Expand Down
2 changes: 0 additions & 2 deletions src/viam/sdk/components/private/motor_client.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <viam/sdk/components/private/motor_client.hpp>

#include <algorithm>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>

Expand Down