<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tracing &#8211; Solr.pl</title>
	<atom:link href="https://solr.pl/en/tag/tracing-2/feed/" rel="self" type="application/rss+xml" />
	<link>https://solr.pl/en/</link>
	<description>All things to be found - Blog related to Apache Solr &#38; Lucene projects - https://solr.apache.org</description>
	<lastBuildDate>Sat, 14 Nov 2020 15:24:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>Enabling Tracing in Solr</title>
		<link>https://solr.pl/en/2020/11/02/enabling-tracing-in-solr/</link>
					<comments>https://solr.pl/en/2020/11/02/enabling-tracing-in-solr/#respond</comments>
		
		<dc:creator><![CDATA[Rafał Kuć]]></dc:creator>
		<pubDate>Mon, 02 Nov 2020 15:23:48 +0000</pubDate>
				<category><![CDATA[Solr]]></category>
		<category><![CDATA[distributed tracing]]></category>
		<category><![CDATA[open tracing]]></category>
		<category><![CDATA[tracing]]></category>
		<guid isPermaLink="false">http://sematext.solr.pl/?p=1042</guid>

					<description><![CDATA[With the release of Solr 8.2, we&#8217;ve got the support for Open Tracing. Vendor-neutral APIs that support distributed tracing and allow us to choose whatever backend or vendor that we want to store our traces. No matter if we want]]></description>
										<content:encoded><![CDATA[
<p>With the release of Solr 8.2, we&#8217;ve got the support for Open Tracing. Vendor-neutral APIs that support distributed tracing and allow us to choose whatever backend or vendor that we want to store our traces. No matter if we want to stay in the open-source world or we want to go with one of the commercial vendors supporting Open Tracing. Let&#8217;s have a look at how into how we can enable distributed tracing in Solr.</p>



<span id="more-1042"></span>



<h2 class="wp-block-heading">The Setup</h2>



<p>For the blog post, I set up a simple SolrCloud cluster built of two nodes, all running on the same machine. Nothing sophisticated, but it will be enough to show us what are the benefits of using distributed tracing.</p>



<h2 class="wp-block-heading">Open Tracing Backend</h2>



<p>To be able to use distributed tracing we need to choose some backend. At the moment of writing the only backend supported by Solr out-of-the-box is Jaeger. Adding other backends, even the commercial ones are not very problematic, but that is not the main focus in the blog post, so I&#8217;ll omit to describe that.</p>



<p>For my tests, I will just run the&nbsp;<strong>jaegertracing/all-in-one</strong>&nbsp;Docker container in its latest version. It is as simple as running the following command:</p>



<pre class="wp-block-code"><code class="">$ docker run -d --name jaeger -p 16686:16686 -p 6831:6831/udp -p 5775:5775/udp jaegertracing/all-in-one:latest</code></pre>



<p>We run the container under the name&nbsp;<strong>jaeger</strong>&nbsp;and we map three ports &#8211;&nbsp;<strong>16686</strong>,&nbsp;<strong>6831</strong>, and&nbsp;<strong>5775</strong>. The&nbsp;<strong>16686</strong>&nbsp;is needed to connect to the Jaeger UI and the&nbsp;<strong>5775</strong>&nbsp;is the port we will use for shipping tracing data.&nbsp;</p>



<p>You can check if everything is running correctly by running:</p>



<pre class="wp-block-code"><code class="">$ docker ps</code></pre>



<h2 class="wp-block-heading">Configuring Solr for Open Tracing</h2>



<p>The next thing we need to do is setting up Solr. To do that the libraries from&nbsp;<strong>contrib/jaegertracer-configurator/lib/</strong>&nbsp;directory and the&nbsp;<strong>solr-jaegertracer-configurator-8.6.0.jar</strong>&nbsp;from the&nbsp;<strong>dist</strong>&nbsp;directory needs to be placed in the Solr classpath. In my case, I just created the&nbsp;<strong>lib</strong>&nbsp;directory in the&nbsp;<strong>server/solr</strong>&nbsp;directory and copied the mentioned jar files there.</p>



<p>You also need to modify the&nbsp;<strong>solr.xml</strong>&nbsp;file that is present in the&nbsp;<strong>server/solr</strong>&nbsp;directory and include the tracer configuration there. If you are working with an example Solr instance you will already have some configuration in the&nbsp;<strong>solr.xml</strong>&nbsp;file, so you just need to add the following section there:</p>



<pre class="wp-block-code"><code class="">&lt;tracerConfig name="tracerConfig" class="org.apache.solr.jaeger.JaegerTracerConfigurator">
  &lt;str name="agentHost">localhost&lt;/str>
  &lt;int name="agentPort">5775&lt;/int>
  &lt;bool name="logSpans">true&lt;/bool>
  &lt;int name="flushInterval">1000&lt;/int>
  &lt;int name="maxQueueSize">10000&lt;/int>
&lt;/tracerConfig></code></pre>



<p>The above&nbsp;<strong>tracerConfig</strong>&nbsp;tag configures the Jaeger distributed tracer. We define the agent host as&nbsp;<strong>localhost</strong>&nbsp;and we set the port to&nbsp;<strong>5775</strong>. We also tell it to log spans and we define the maximum flush interval and the maximum queue size.&nbsp;</p>



<p>Remember that all the configuration and libraries must be present on all of our Solr nodes.</p>



<h2 class="wp-block-heading">The Test Cluster &amp; Data</h2>



<p>After doing everything above we can just start our Solr instances. In this case, I&#8217;ve used the following commands:</p>



<pre class="wp-block-code"><code class="">$ bin/solr start -c -f
$ bin/solr start -f -p 6883 -z localhost:9983</code></pre>



<p>So I started two Solr instances. The first one with embedded Zookeeper running along with Solr instance and the second one connecting to that Zookeeper. A test SolrCloud cluster.</p>



<p>Before creating the collection that will be used for testing, I did one more thing &#8211; I set the tracing sampling to 100%, which means that every span will be shipped to our Jaeger backend. That is done by setting the cluster property called&nbsp;<strong>samplePercentage</strong>&nbsp;and giving it a value of&nbsp;<strong>100</strong>. The command that I used was as follows:</p>



<pre class="wp-block-code"><code class="">$ curl -XGET 'localhost:8983/solr/admin/collections?action=CLUSTERPROP&amp;name=samplePercentage&amp;val=100'</code></pre>



<p>Now keep in mind that this is only done for tests and in a real, production system you may want to use sampling to lower down the amount of data that you store for your distributed traces.</p>



<p>I also created a collection called&nbsp;<strong>test</strong>&nbsp;using the&nbsp;<strong>_default</strong>&nbsp;configuration. I don&#8217;t need any fancy features, I just need a few documents indexed and a simple query. Because of that, the following command was everything that was needed:</p>



<pre class="wp-block-code"><code class="">$ curl -XPOST -H 'Content-type:application/json' 'http://localhost:8983/api/c/'  -d '{ 
  "create": { 
    "name": "test",
    "numShards": "2"
  } 
}'</code></pre>



<p>I indexed data by using the following command:</p>



<pre class="wp-block-code"><code class="">$ curl -XPOST -H 'Content-type:application/json' 'localhost:8983/solr/test/update?commit=true' -d '[
 {
  "id": 1,
  "name": "Test document 1",
  "tags": [ "doc", "test" ]
 },
 {
  "id": 2,
  "name": "Test document 2",
  "tags": [ "doc", "test" ]
 },
 {
  "id": 3,
  "name": "Test document 3",
  "tags": [ "doc", "test" ]
 }
]'</code></pre>



<p>After that I run the following query:</p>



<pre class="wp-block-code"><code class="">$ curl -XGET -H 'Content-type:application/json' 'localhost:8983/solr/test/select' -d '{
  "query" : "name:document",
  "facet": {
    "tags" : {
      "terms" : {
        "field" : "tags"
      }
    }
  }	
}'</code></pre>



<h2 class="wp-block-heading">Looking into Jaeger UI</h2>



<p>After running the query we should already have something in Jaeger. We indexed data, we run the query. After going to&nbsp;<strong>localhost:16686</strong>&nbsp;and choosing&nbsp;<strong>solr</strong>&nbsp;as the service type we can see traces. For example, one for query:</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://solr.pl/wp-content/uploads/2020/11/jaeger-query-1024x248.png" alt="" class="wp-image-4967"/></figure>



<p>If you need more data the&nbsp;<strong>tags</strong>&nbsp;section is there for the rescue:</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://solr.pl/wp-content/uploads/2020/11/jaeger-query-details-1024x539.png" alt="" class="wp-image-4968"/></figure>



<p>Everything available just by including additional libraries and a few lines of configuration in Solr.</p>



<h2 class="wp-block-heading">Going Further &#8211; Tracing In Your Code</h2>



<p>Of course, the power of distributed tracing is that you are not limited to traces from a single source &#8211; Solr in this case. We can also include tracing in our code.</p>



<p>For example, if we have a simple application that runs queries to Solr we could include Open Tracing&nbsp;<strong>span</strong>&nbsp;creation and configure the Jaeger tracer, just like we did in Solr by modifying the&nbsp;<strong>solr.xml</strong>&nbsp;file. An example code in Java could look as follows (the full code is available on <a rel="noreferrer noopener" href="https://github.com/solrpl/blog/tree/master/posts/tracing" target="_blank">Github</a>):</p>



<pre class="wp-block-code"><code class="">public class App {
    private JaegerTracer tracer;
    private HttpSolrClient solrClient;

    public static void main(String[] args) throws Exception {
        App app = new App();
        app.initTracer();
        app.initSolrClient();
        app.start();
    }

    public void start() throws Exception {
        Span span = tracer.buildSpan("example query").start();

        final Map&lt;String, String> query = new HashMap&lt;>();
        query.put("q", "*:*");
        MapSolrParams queryParams = new MapSolrParams(query);

        final QueryResponse queryResponse = solrClient.query("test", queryParams);
        final SolrDocumentList documents = queryResponse.getResults();

        sleep(10);
        processDocumentsSlow(documents, span, 100);

        span.finish();
    }

    private void processDocumentsSlow(SolrDocumentList documents, Span rootSpan, long sleepTime) {
        Span span = tracer
            .buildSpan("process documents")
            .asChildOf(rootSpan)
            .start();

        processDocumentsSlowNext(documents, span, 300);
        sleep(sleepTime);

        span.finish();
    }

    private void processDocumentsSlowNext(SolrDocumentList documents, Span rootSpan, long sleepTime) {
        Span span = tracer
            .buildSpan("process documents next")
            .asChildOf(rootSpan)
            .start();

        sleep(sleepTime);

        span.finish();
    }

    private void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (Exception ex) {}
    }

    public void initTracer() {
        if (this.tracer == null) {
            Configuration.SamplerConfiguration samplerConfiguration = new Configuration
                .SamplerConfiguration()
                .withType(ConstSampler.TYPE)
                .withParam(1);

            Configuration.ReporterConfiguration reporterConfiguration = Configuration
                .ReporterConfiguration
                .fromEnv();

            Configuration.SenderConfiguration senderConfig = reporterConfiguration
                .getSenderConfiguration()
                .withAgentHost("localhost")
                .withAgentPort(5775);

            reporterConfiguration
                .withLogSpans(true)
                .withSender(senderConfig);

            Configuration configuration = new Configuration("Jaeger with Solr")
                .withSampler(samplerConfiguration)
                .withReporter(reporterConfiguration);

            this.tracer = configuration.getTracer();
        }
    }

    public void initSolrClient() {
        if (this.solrClient == null) {
            this.solrClient = new HttpSolrClient
                .Builder("http://localhost:8983/solr")
                .build();
        }
    }
}</code></pre>



<p>Apart from the&nbsp;<strong>initTracer</strong>&nbsp;method which shows how to configure<strong>&nbsp;Jaeger</strong>&nbsp;tracer programmatically the interesting piece is in the&nbsp;<strong>start</strong>&nbsp;method. We create a top-level span called&nbsp;<strong>example query</strong>, build a query to Solr and execute it. Next, we simulate some slowness by first calling the&nbsp;<strong>processDocumentsSlow</strong>&nbsp;method and inside it the&nbsp;<strong>processDocumentsSlowNext</strong>&nbsp;method. Each of those methods creates its span and includes it as the child of another span by calling the&nbsp;<strong>asChildOf</strong>&nbsp;method and providing the root span. This ends up looking in Jaeger UI as follows:</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://solr.pl/wp-content/uploads/2020/11/jaeger-with-code-1024x215.png" alt="" class="wp-image-4969"/></figure>



<p>So now we get visibility not only into the Solr itself, but also into our code.</p>



<h2 class="wp-block-heading">The Next Steps</h2>



<p>The power of distributed is fully visibile when the whole code is producing spans and when it all gives you full visibility into the execution and the timings related to that. Open Tracing supports not only Java, but also JavaScript, Go, Python, PHP, Objective-C, C++, C#, and Ruby. So if your application stack is developed using those you can instrument your code or without any problem.</p>



<p>It is also worth noting that Open Tracing is just a set of APIs and Jaeger is just one of the tracers that support that API. You can use both open source solutions as well as commercial ones, depending on your needs and depending on what you are already using for monitoring logs and metrics.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://solr.pl/en/2020/11/02/enabling-tracing-in-solr/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
