Enabling Tracing in Solr

With the release of Solr 8.2, we’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’s have a look at how into how we can enable distributed tracing in Solr.

The Setup

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.

Open Tracing Backend

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’ll omit to describe that.

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

$ docker run -d --name jaeger -p 16686:16686 -p 6831:6831/udp -p 5775:5775/udp jaegertracing/all-in-one:latest

We run the container under the name jaeger and we map three ports – 16686, 6831, and 5775. The 16686 is needed to connect to the Jaeger UI and the 5775 is the port we will use for shipping tracing data. 

You can check if everything is running correctly by running:

$ docker ps

Configuring Solr for Open Tracing

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

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

<tracerConfig name="tracerConfig" class="org.apache.solr.jaeger.JaegerTracerConfigurator">
  <str name="agentHost">localhost</str>
  <int name="agentPort">5775</int>
  <bool name="logSpans">true</bool>
  <int name="flushInterval">1000</int>
  <int name="maxQueueSize">10000</int>
</tracerConfig>

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

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

The Test Cluster & Data

After doing everything above we can just start our Solr instances. In this case, I’ve used the following commands:

$ bin/solr start -c -f
$ bin/solr start -f -p 6883 -z localhost:9983

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.

Before creating the collection that will be used for testing, I did one more thing – 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 samplePercentage and giving it a value of 100. The command that I used was as follows:

$ curl -XGET 'localhost:8983/solr/admin/collections?action=CLUSTERPROP&name=samplePercentage&val=100'

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.

I also created a collection called test using the _default configuration. I don’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:

$ curl -XPOST -H 'Content-type:application/json' 'http://localhost:8983/api/c/'  -d '{ 
  "create": { 
    "name": "test",
    "numShards": "2"
  } 
}'

I indexed data by using the following command:

$ 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" ]
 }
]'

After that I run the following query:

$ curl -XGET -H 'Content-type:application/json' 'localhost:8983/solr/test/select' -d '{
  "query" : "name:document",
  "facet": {
    "tags" : {
      "terms" : {
        "field" : "tags"
      }
    }
  }	
}'

Looking into Jaeger UI

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

If you need more data the tags section is there for the rescue:

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

Going Further – Tracing In Your Code

Of course, the power of distributed tracing is that you are not limited to traces from a single source – Solr in this case. We can also include tracing in our code.

For example, if we have a simple application that runs queries to Solr we could include Open Tracing span creation and configure the Jaeger tracer, just like we did in Solr by modifying the solr.xml file. An example code in Java could look as follows (the full code is available on Github):

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<String, String> query = new HashMap<>();
        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();
        }
    }
}

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

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

The Next Steps

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners.
Cookies settings
Accept
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active

Privacy Policy

What information do we collect?

We collect information from you when you register on our site or place an order. When ordering or registering on our site, as appropriate, you may be asked to enter your: name, e-mail address or mailing address.

What do we use your information for?

Any of the information we collect from you may be used in one of the following ways: To personalize your experience (your information helps us to better respond to your individual needs) To improve our website (we continually strive to improve our website offerings based on the information and feedback we receive from you) To improve customer service (your information helps us to more effectively respond to your customer service requests and support needs) To process transactions Your information, whether public or private, will not be sold, exchanged, transferred, or given to any other company for any reason whatsoever, without your consent, other than for the express purpose of delivering the purchased product or service requested. To administer a contest, promotion, survey or other site feature To send periodic emails The email address you provide for order processing, will only be used to send you information and updates pertaining to your order.

How do we protect your information?

We implement a variety of security measures to maintain the safety of your personal information when you place an order or enter, submit, or access your personal information. We offer the use of a secure server. All supplied sensitive/credit information is transmitted via Secure Socket Layer (SSL) technology and then encrypted into our Payment gateway providers database only to be accessible by those authorized with special access rights to such systems, and are required to?keep the information confidential. After a transaction, your private information (credit cards, social security numbers, financials, etc.) will not be kept on file for more than 60 days.

Do we use cookies?

Yes (Cookies are small files that a site or its service provider transfers to your computers hard drive through your Web browser (if you allow) that enables the sites or service providers systems to recognize your browser and capture and remember certain information We use cookies to help us remember and process the items in your shopping cart, understand and save your preferences for future visits, keep track of advertisements and compile aggregate data about site traffic and site interaction so that we can offer better site experiences and tools in the future. We may contract with third-party service providers to assist us in better understanding our site visitors. These service providers are not permitted to use the information collected on our behalf except to help us conduct and improve our business. If you prefer, you can choose to have your computer warn you each time a cookie is being sent, or you can choose to turn off all cookies via your browser settings. Like most websites, if you turn your cookies off, some of our services may not function properly. However, you can still place orders by contacting customer service. Google Analytics We use Google Analytics on our sites for anonymous reporting of site usage and for advertising on the site. If you would like to opt-out of Google Analytics monitoring your behaviour on our sites please use this link (https://tools.google.com/dlpage/gaoptout/)

Do we disclose any information to outside parties?

We do not sell, trade, or otherwise transfer to outside parties your personally identifiable information. This does not include trusted third parties who assist us in operating our website, conducting our business, or servicing you, so long as those parties agree to keep this information confidential. We may also release your information when we believe release is appropriate to comply with the law, enforce our site policies, or protect ours or others rights, property, or safety. However, non-personally identifiable visitor information may be provided to other parties for marketing, advertising, or other uses.

Registration

The minimum information we need to register you is your name, email address and a password. We will ask you more questions for different services, including sales promotions. Unless we say otherwise, you have to answer all the registration questions. We may also ask some other, voluntary questions during registration for certain services (for example, professional networks) so we can gain a clearer understanding of who you are. This also allows us to personalise services for you. To assist us in our marketing, in addition to the data that you provide to us if you register, we may also obtain data from trusted third parties to help us understand what you might be interested in. This ‘profiling’ information is produced from a variety of sources, including publicly available data (such as the electoral roll) or from sources such as surveys and polls where you have given your permission for your data to be shared. You can choose not to have such data shared with the Guardian from these sources by logging into your account and changing the settings in the privacy section. After you have registered, and with your permission, we may send you emails we think may interest you. Newsletters may be personalised based on what you have been reading on theguardian.com. At any time you can decide not to receive these emails and will be able to ‘unsubscribe’. Logging in using social networking credentials If you log-in to our sites using a Facebook log-in, you are granting permission to Facebook to share your user details with us. This will include your name, email address, date of birth and location which will then be used to form a Guardian identity. You can also use your picture from Facebook as part of your profile. This will also allow us and Facebook to share your, networks, user ID and any other information you choose to share according to your Facebook account settings. If you remove the Guardian app from your Facebook settings, we will no longer have access to this information. If you log-in to our sites using a Google log-in, you grant permission to Google to share your user details with us. This will include your name, email address, date of birth, sex and location which we will then use to form a Guardian identity. You may use your picture from Google as part of your profile. This also allows us to share your networks, user ID and any other information you choose to share according to your Google account settings. If you remove the Guardian from your Google settings, we will no longer have access to this information. If you log-in to our sites using a twitter log-in, we receive your avatar (the small picture that appears next to your tweets) and twitter username.

Children’s Online Privacy Protection Act Compliance

We are in compliance with the requirements of COPPA (Childrens Online Privacy Protection Act), we do not collect any information from anyone under 13 years of age. Our website, products and services are all directed to people who are at least 13 years old or older.

Updating your personal information

We offer a ‘My details’ page (also known as Dashboard), where you can update your personal information at any time, and change your marketing preferences. You can get to this page from most pages on the site – simply click on the ‘My details’ link at the top of the screen when you are signed in.

Online Privacy Policy Only

This online privacy policy applies only to information collected through our website and not to information collected offline.

Your Consent

By using our site, you consent to our privacy policy.

Changes to our Privacy Policy

If we decide to change our privacy policy, we will post those changes on this page.
Save settings
Cookies settings