Context local worker_ctx#438
Conversation
|
Nameko makes a conscious choice to use dependency injection rather than global or thread-local variables. Dependency injection is chosen because it makes testing easier, and many of the bundled test helpers rely on this pattern. The "nameko way" of achieving your use-case would be to define a It's also viable to expose different parts of the |
|
Implementing this in a DependencyProvider instead of global variable seems extremely reasonable for me. class CorrelationIdFilter(logging.Filter):
def filter(self, record):
if global_worker_ctx:
record.correlation_id = global_worker_ctx.call_id_ctack[0]
return TrueWith logging cfg smth like: LOGGING:
...
formatters:
....
format: '(asctime)(msecs)(levelname)(process)(module)(lineno)(args)(msg)(correlation_id)'
class: pythonjsonlogger.jsonlogger.JsonFormatter
filters:
request_id_filter:
(): my_module.CorrelationIdFilter
...Such approach gives ability to tag and retrieve all log records made for particular request even made by other libraries. |
|
I understand the desire now. In this case, I would implement a Also, note we use a "correlation_id" as an internal part of the RPC protocol. You might want to change your naming convention to reflect that what you're capturing is really the Nameko call_id. |
|
Yeah, I've used correlation_id just for example. |
global.worker_ctx context local variable added (request in flask style).
It is pushed in entrypoints and in standalone proxy creation.
It can be used in the scope of request handling e.g. in log formatters for logging call_ids, etc.
We write logs in json, tag them with call_id_stack[0] and store them in elasticsearch. With such approach we are able to retrieve logs from all microservices for specified request.