Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Ray c++ backend structured logging #44468

Merged
merged 20 commits into from
May 31, 2024
Merged

Conversation

jjyao
Copy link
Collaborator

@jjyao jjyao commented Apr 4, 2024

Why are these changes needed?

Introduced RAY_BACKEND_LOG_JSON=1 to control whether to emit backend c++ log in plain text or json.

To add contextual information to the log, use RAY_LOG(INFO).WithField("key1", "value1").WithField("key2", "value2") << "normal message" (This is the same as the existing RAY_EVENT)

Examples:

JSON format:

{"asctime":"2024-05-24 12:53:23,080","levelname":"I","message":"(raylet) Initializing NodeManager","filename":"node_manager.cc","lineno":287,"node_id":"99adb80d4e91c4765ef21f4964519d0743da3717bcfadbf80b0ede0a"}

TEXT format:

[2024-05-24 13:05:42,685 I 80577 16776052] (raylet) node_manager.cc:287: Initializing NodeManager node_id=b93cd25a3af0e1a178e0ceab636d2da14a0f094e0904ab0f09f5d373

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

@jjyao jjyao changed the title [Core][Prototype] ray structured logging [Core] C++ structured logging May 24, 2024
@jjyao jjyao changed the title [Core] C++ structured logging [Core] Ray c++ backend structured logging May 24, 2024
@jjyao jjyao marked this pull request as ready for review May 24, 2024 20:06
@jjyao jjyao requested a review from a team as a code owner May 24, 2024 20:06
jjyao added 2 commits May 24, 2024 21:52
Signed-off-by: Jiajun Yao <[email protected]>
Signed-off-by: Jiajun Yao <[email protected]>
@jjyao jjyao added the go add ONLY when ready to merge, run all tests label May 29, 2024
src/ray/util/logging.cc Outdated Show resolved Hide resolved
logger->log(GetMappedSeverity(severity_),
/*fmt*/ ",\"{}\":\"{}\"{}",
kLogKeyMessage,
json_escape_string(msg_osstream_.str()),
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use nlohmann/json to dump a json string instead of doing string concats and formats on our own?

if (severity_ == RayLogLevel::FATAL) {
std::_Exit(EXIT_FAILURE);
}
}

RayLog &RayLog::WithField(const std::string &key, const std::string &value) {
if (log_format_json_) {
context_osstream_ << ",\"" << key << "\":\"" << json_escape_string(value) << "\"";
Copy link
Contributor

Choose a reason for hiding this comment

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

It's possible for user to provide a same key multiple times, resulting in a json with duplicated keys. It's technically legal but I don't think we want it. To avoid this, maybe we can do this:

  1. in RayLog members, remove context_osstream_ and instead keep a map<str, str> fields_
  2. in WithField, add to that fields_ (overwriting existing keys)
  3. in ~RayLog, format the map in json or text mode, a la
#include <nlohmann/json.hpp>
#include "absl/strings/str_join.h"

std::string formatMap(const std::map<std::string, std::string>& map, bool jsonMode) {
    if (jsonMode) {
        nlohmann::json j(map);
        return j.dump();
    } else {
        return absl::StrJoin(map, " ", absl::PairFormatter("="));
    }
}

if (severity_ == RayLogLevel::FATAL) {
std::_Exit(EXIT_FAILURE);
}
}

RayLog &RayLog::WithField(const std::string &key, const std::string &value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This API requires all fields happen before any text strings, for which I am fine, though it may introduce some inconveniences when we have some optional outputs.

However it also only accepts strings, which seems to be a big deal, since it forces a base_id.Hex() call. What about:

template <typename T>
RayLog &RayLog::WithField(std::string_view key, const T &value) {
    std::stringstream ss;
    ss << value;
    // put ss.str() to field_ or context_osstream_...
}

constexpr char kLogKeyWorkerID[] = "worker_id";
constexpr char kLogKeyNodeID[] = "node_id";
constexpr char kLogKeyActorID[] = "actor_id";
constexpr char kLogKeyTaskID[] = "task_id";
Copy link
Contributor

Choose a reason for hiding this comment

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

what about:

constexpr std::string_view

And, are these keys pre-defined? Can/Should users use custom keys?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Users can provide custom keys. These are just common pre-defined keys that we will likely index.

jjyao added 2 commits May 30, 2024 15:58
Signed-off-by: Jiajun Yao <[email protected]>
Signed-off-by: Jiajun Yao <[email protected]>
Copy link
Contributor

@rynewang rynewang left a comment

Choose a reason for hiding this comment

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

LGTM with nits

src/ray/gcs/gcs_server/gcs_actor_manager.cc Outdated Show resolved Hide resolved
src/ray/util/logging.cc Outdated Show resolved Hide resolved
Signed-off-by: Jiajun Yao <[email protected]>
@jjyao jjyao enabled auto-merge (squash) May 31, 2024 04:45
Signed-off-by: Jiajun Yao <[email protected]>
@github-actions github-actions bot disabled auto-merge May 31, 2024 06:11
@jjyao jjyao enabled auto-merge (squash) May 31, 2024 06:50
Signed-off-by: Jiajun Yao <[email protected]>
@github-actions github-actions bot disabled auto-merge May 31, 2024 16:28
Signed-off-by: Jiajun Yao <[email protected]>
@jjyao jjyao merged commit a30630a into ray-project:master May 31, 2024
6 checks passed
@jjyao jjyao deleted the jjyao/log branch May 31, 2024 20:20
richardsliu pushed a commit to richardsliu/ray that referenced this pull request Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants