<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="320075" data-post-id="320075">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="spacebat" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/spacebat/120/25330_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  spacebat
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I found a minimal change to Ash.ProcessHelpers that makes carrying the ecto dynamic repo info around possible:</p>
<aside class="onebox githubpullrequest" data-onebox-src="https://github.com/spacebat/ash/pull/1/files">
  <header class="source">

      <a href="https://github.com/spacebat/ash/pull/1/files" target="_blank" rel="noopener nofollow ugc">github.com/spacebat/ash</a>
  </header>

  <article class="onebox-body">
    <div class="github-row" data-github-private-repo="false">



    <div class="github-icon-container" title="Pull Request">
      <svg width="60" height="60" class="github-icon" viewBox="0 0 12 16" aria-hidden="true"><path fill-rule="evenodd" d="M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z"></path></svg>
    </div>

  <div class="github-info-container">



      <h4>
        <a href="https://github.com/spacebat/ash/pull/1/files" target="_blank" rel="noopener nofollow ugc">Transfer {dynamic_repo_module, dynamic_repo} across processes (#1)</a>
      </h4>

    <div class="branches">
      <code>main</code> ← <code>dynamic-repos-across-processes</code>
    </div>

      <div class="github-info">
        <div class="date">
          opened <span class="discourse-local-date" data-format="ll" data-date="2024-03-05" data-time="02:58:47" data-timezone="UTC">02:58AM - 05 Mar 24 UTC</span>
        </div>

        <div class="user">
          <a href="https://github.com/spacebat" target="_blank" rel="noopener nofollow ugc">
            <img alt="" src="https://avatars.githubusercontent.com/u/216899?v=4" class="onebox-avatar-inline" width="20" height="20">
            spacebat
          </a>
        </div>

        <div class="lines" title="1 commits changed 1 files with 20 additions and 0 deletions">
          <a href="https://github.com/spacebat/ash/pull/1/files" target="_blank" rel="noopener nofollow ugc">
            <span class="added">+20</span>
            <span class="removed">-0</span>
          </a>
        </div>
      </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">This allows for the info used in Ecto.Repo.put_dynamic_repo\1 to carry over to t<span class="show-more-container"><a href="https://github.com/spacebat/ash/pull/1" target="_blank" rel="noopener nofollow ugc" class="show-more">…</a></span><span class="excerpt hidden">he process actually performing the query.

# Contributor checklist

- [ ] Bug fixes include regression tests
- [ ] Chores
- [ ] Documentation changes
- [ ] Features include unit/acceptance tests
- [ ] Refactoring
- [ ] Update dependencies</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>That enables this preparation to work:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">      prepare fn query, _context -&gt;
        if %{arguments: %{tenant: tenant}} = query do
          pid = Dyndb.Repo.get_connection!(tenant)
          Dyndb.Repo.put_dynamic_repo(pid)
          Ash.set_context(%{dynamic_repo_module: Dyndb.Repo})
        end

        query
      end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="320075" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/using-an-ash-resource-with-a-dynamic-repository/61138/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-320075" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="320075"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="320076" data-post-id="320076">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zachdaniel" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/120/31980_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zachdaniel
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Ash</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>So, I added a comment to your PR that you linked to, but I believe that the preparation you added plus a custom tracer ought to do what you need. The custom tracer example is here: <a href="https://forum.elixirforum.com/t/using-an-ash-resource-with-a-dynamic-repository/61138/10" class="inline-onebox" rel="nofollow">Using an Ash Resource with a Dynamic Repository - #10 by zachdaniel</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="320076" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/using-an-ash-resource-with-a-dynamic-repository/61138/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-320076" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="320076"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="320079" data-post-id="320079">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="spacebat" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/spacebat/120/25330_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  spacebat
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’ve tried that again and I’m running into the same problem as before - in the preparation I set the span context (which does a <code>Process.put</code>) and set the tracer in the query context, but then <code>get_span_context</code> (which would do a <code>Process.get</code> and then call <code>put_dynamic_repo</code>) is never called.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">      prepare fn query, context -&gt;
        if %{arguments: %{tenant: tenant}} = query do
          pid = Dyndb.Repo.get_connection!(tenant)
          Ash.Tracer.set_span_context(Dyndb.DynamicRepoTracer, {Dyndb.Repo, pid})
          Ash.Query.set_context(query, %{tracer: Dyndb.DynamicRepoTracer})
        else
          query
        end
      end
</code></pre>
<p>…and the tracer…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Dyndb.DynamicRepoTracer do
  use Ash.Tracer

  @impl Ash.Tracer
  def get_span_context() do
    {repo_module, repo_pid} = Process.get(:dynamic_repo_tracer)
    repo_module.put_dynamic_repo(repo_pid)
  end

  @impl Ash.Tracer
  def set_span_context({_repo_module, _repo_pid} = dynamic_repo) do
    Process.put(:dynamic_repo_tracer, dynamic_repo)
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="320079" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/using-an-ash-resource-with-a-dynamic-repository/61138/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-320079" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="320079"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="320082" data-post-id="320082">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zachdaniel" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/120/31980_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zachdaniel
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Ash</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Did you configure your app to use the tracer?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">config :ash, tracer: [Dyndb.DynamicRepoTracer]
</code></pre>
<p>Apologies if I forgot to mention that.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="320082" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/using-an-ash-resource-with-a-dynamic-repository/61138/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-320082" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="320082"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="320086" data-post-id="320086">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="spacebat" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/spacebat/120/25330_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  spacebat
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hey I got it working with the tracer - having not used tracers before and couldn’t find examples of use was the problem. Now I’m worried that the tracer is being called everywhere - is there a way to only turn on the tracer for the duration/scope of a dynamic repo query?</p>
<p>Thanks for all your help!</p>
<p>The preparation:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">      prepare fn query, context -&gt;
        if %{arguments: %{tenant: tenant}} = query do
          pid = Dyndb.Repo.get_connection!(tenant)
          Dyndb.DynamicRepoTracer.set_span_context({Dyndb.Repo, pid})
        end

        query
      end
</code></pre>
<p>…and the custom part of the tracer…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @impl Ash.Tracer
  def get_span_context() do
    Process.get(:dynamic_repo_tracer)
  end

  @impl Ash.Tracer
  def set_span_context({repo_module, repo_pid} = dynamic_repo) do
    Process.put(:dynamic_repo_tracer, dynamic_repo)
    repo_module.put_dynamic_repo(repo_pid)
    :ok
  end
</code></pre>
<p>The github repo has been updated.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="320086" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/using-an-ash-resource-with-a-dynamic-repository/61138/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-320086" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="320086"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="320121" data-post-id="320121">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zachdaniel" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/120/31980_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zachdaniel
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Ash</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I don’t think that there is a way currently to conditionally enable the tracer, or at least not a way that I think would be seamless for you. However, <code>Process.get/1</code> is very cheap, so I think that if your <code>set_span_context</code> just handles the case where that value is <code>nil</code> then it shouldn’t be a concern.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="320121" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/using-an-ash-resource-with-a-dynamic-repository/61138/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-320121" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="320121"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>