# Usage of ContextPropagator in Java SDK

**URL:** <https://community.temporal.io/t/usage-of-contextpropagator-in-java-sdk/1152>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [December 10, 2020, 5:09pm UTC](https://community.temporal.io/t/usage-of-contextpropagator-in-java-sdk/1152 "2020-12-10T17:09:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/juan/32/374_2.png) [@Juan](https://community.temporal.io/u/Juan)\
**Post date:** [December 10, 2020, 5:09pm UTC](https://community.temporal.io/t/usage-of-contextpropagator-in-java-sdk/1152/1 "2020-12-10T17:09:53Z")

</div>

Hi there,

I’m trying to use ContextPropagator to pass the requestId and to be able to log the runId from all activities.  
In the JavaDoc there is an example of how to implement and register an ContextPropagator, but I’m a bit lost on how to use there from inside the activities and/workflow code.

My goal is to be able to trace the requestId and runId on each log statement, having the requestId assigned on each signal/start

Any pointer any one can give?

Kind regards

---

<div class="post-metadata">

**Author:** ![ryland](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/ryland/32/11_2.png) [@ryland](https://community.temporal.io/u/ryland)\
**Post date:** [December 10, 2020, 5:55pm UTC](https://community.temporal.io/t/usage-of-contextpropagator-in-java-sdk/1152/2 "2020-12-10T17:55:22Z")

</div>

Hey I don’t know all that much about Java SDK so I’ll add @Vitaly. That being said, here is a test which uses ContextPropagator.

> <https://github.com/temporalio/sdk-java/blob/f0bbf84c496454d9909caefe6ca2784f76b8e592/temporal-sdk/src/test/java/io/temporal/internal/testing/WorkflowTestingTest.java#L928>

---

<div class="post-metadata">

**Author:** ![sdwr98](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/sdwr98/32/423_2.png) [@sdwr98](https://community.temporal.io/u/sdwr98)\
**Post date:** [December 10, 2020, 6:21pm UTC](https://community.temporal.io/t/usage-of-contextpropagator-in-java-sdk/1152/3 "2020-12-10T18:21:53Z")

</div>

Hi Juan,

The idea of the ContextPropagator is that it’s called before and after your workflow client, workflow, and activity code is executed so that you can grab some contextual information and pass it along. The way that we use it is to grab some values from the `org.slf4j.MDC` in your workflow client thread and make sure that those values are populated in the MDC before your workflow code or activity code start.

This is a sample context propagator in the unit test: [https://github.com/temporalio/sdk-java/blob/f0bbf84c496454d9909caefe6ca2784f76b8e592/temporal-sdk/src/test/java/io/temporal/internal/testing/WorkflowTestingTest.java#L878](https://github.com/temporalio/sdk-java/blob/f0bbf84c496454d9909caefe6ca2784f76b8e592/temporal-sdk/src/test/java/io/temporal/internal/testing/WorkflowTestingTest.java#L878)

The `getCurrentContext()` method is called before a workflow/child workflow/activity is started to grab the context from your code, then before your workflow/child workflow/activity starts, `setCurrentContext()` is called to set up the context in the child thread.

Does that help? You can define what “context” means, but those methods give you hooks to get/set it on your code’s threads.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/juan/32/374_2.png) [@Juan](https://community.temporal.io/u/Juan)\
**Post date:** [December 11, 2020, 8:29am UTC](https://community.temporal.io/t/usage-of-contextpropagator-in-java-sdk/1152/4 "2020-12-11T08:29:19Z")

</div>

Thanks so much. That gives me what I need to continue.
