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

Add logging of per-context variables to logging system and network stack #1507

Merged
merged 3 commits into from
May 16, 2024

Conversation

uhertlein
Copy link
Collaborator

@uhertlein uhertlein commented May 8, 2024

This change adds a per-thread logging context to the logging system, which can be set by the user and will then be logged from all threads and tasks created with that context.

This is useful to correlate requests from the client to any network requests made based on that request.

The logging formatter must be set up by including a MessageFormatter::ElementType::ContextValue with the 'format' set to the context key to use:

using ElementT = olp::logging::MessageFormatter::ElementType;
std::vector<ElementT> elements = {
    ElementT(ElementType::Time, "%F %T"),
    ElementT(ElementType::TimeMs, ".%03u "),
    ElementT(ElementType::Level, "%s ["),
    ElementT(ElementType::ContextValue, X_CORRELATION_ID),
    ElementT(ElementType::Tag, "] %s: "),
    ElementT(ElementType::Message, "%s")
};

The log context must then be created and set up once manually from the top-most thread:

std::shared_ptr<olp::logging::LogContext>
createLogContext(const std::string& correlationID)
{
    auto context = std::make_shared<olp::logging::LogContext>();
    (*context)[X_CORRELATION_ID] = correlationID;

    return context;
}

The log context is stored for each thread, so whenever a new thread or task is created, this context must somehow be copied into the thread-local storage of that new thread.

For ThreadPoolTaskScheduler, the current log context is captured for the new task lambda and made active when the task is activated.

For NetworkCurl, the current log context is captured inside the RequestHandle on creation and subsequently made active whenever this request is processed.

Resolves: OLPEDGE-2893

@uhertlein uhertlein force-pushed the olpedge-2893 branch 3 times, most recently from 56a9691 to 59cea17 Compare May 14, 2024 10:07
Copy link

codecov bot commented May 14, 2024

Codecov Report

Attention: Patch coverage is 97.29730% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 81.59%. Comparing base (e2b4c85) to head (7eaf577).

Files Patch % Lines
olp-cpp-sdk-core/src/logging/LogContext.cpp 96.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1507      +/-   ##
==========================================
+ Coverage   81.50%   81.59%   +0.09%     
==========================================
  Files         341      342       +1     
  Lines       13417    13452      +35     
  Branches     1447     1449       +2     
==========================================
+ Hits        10935    10975      +40     
+ Misses       1904     1898       -6     
- Partials      578      579       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@uhertlein uhertlein force-pushed the olpedge-2893 branch 2 times, most recently from b7d3f70 to 6e4ebdd Compare May 14, 2024 13:24
Relates-to: OLPEDGE-2893
Signed-off-by: Hertlein, Ulrich <[email protected]>
Hertlein, Ulrich added 2 commits May 16, 2024 11:45
This change adds a per-thread logging context to the logging
system, which can be set by the user and will then be logged
from all threads and tasks created with that context.

This is useful to correlate requests from the client to any
network requests made based on that request.

The logging formatter must be set up by including a
`MessageFormatter::ElementType::ContextValue` with the 'format'
set to the context key to use.

Resolves: OLPEDGE-2893
Signed-off-by: Hertlein, Ulrich <[email protected]>
When a network requests is created, the current log context is
captured and subsequently made active whenever this request is
processed.

Resolves: OLPEDGE-2893
Signed-off-by: Hertlein, Ulrich <[email protected]>
@uhertlein uhertlein merged commit a99a285 into heremaps:master May 16, 2024
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants