# APM manual distributed tracing

**URL:** <https://discuss.elastic.co/t/apm-manual-distributed-tracing/192532>\
**Category:** APM\
**Tags:** java\
**Created:** [July 27, 2019, 7:01pm UTC](https://discuss.elastic.co/t/apm-manual-distributed-tracing/192532 "2019-07-27T19:01:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nashwin2804](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@nashwin2804](https://discuss.elastic.co/u/nashwin2804)\
**Post date:** [July 27, 2019, 7:01pm UTC](https://discuss.elastic.co/t/apm-manual-distributed-tracing/192532/1 "2019-07-27T19:01:57Z")

</div>

**Problem Description**  
I have a client application that makes http get request to server. With auto instrumentation, I am getting this as a single trace. But with manual instrumentation, I am getting 2 different traces. How to capture everything in one trace?

**#my client side code**

```
Transaction transaction = ElasticApm.startTransaction();
    
    Span span = ElasticApm.currentSpan()
                 .startSpan("external", "http", null)
                 .setName("Myspan");
    
     try (final Scope scope = transaction.activate()) {
         transaction.setName("MyTransaction");
         transaction.setType(Transaction.TYPE_REQUEST);

        try {
                URL urlForGetRequest;
                urlForGetRequest = new URL("url to server function");
                
                String readLine = null;
                HttpURLConnection conn = 
                          (HttpURLConnection)urlForGetRequest.openConnection();

                
                span.injectTraceHeaders((name, value) -> 
                    conn.setRequestProperty("Myspan", "100"));
                conn.setRequestMethod("GET");
                
                int responseCode = conn.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader in = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));

                    while ((readLine = in .readLine()) != null) {
                            response.append(readLine);
                    } in .close();

                    conn.disconnect();

                    
                } 
                else
                	throw new RuntimeException("Failed : HTTP error code : "
                                    + conn.getResponseCode());

        }
         catch (IOException e) {
                e.printStackTrace();      
        }
		Thread.sleep(1000);
     } catch (Exception e) {
    	    transaction.captureException(e);
    	    
	} finally {
	    transaction.end();
	}

```

**My server code**  
Transaction transaction =ElasticApm.startTransactionWithRemoteParent(key\>  
request.getHeader("Myspan"));

```
		try (final Scope scope = transaction.activate()) {
			transaction.setName("MyTransaction2");
		        transaction.setType(Transaction.TYPE_REQUEST);
		    
		        //my code
			
			
		} catch (Exception e) {
		    transaction.captureException(e);
		    throw e;
		} finally {
		    transaction.end();
		}

```

**Actual output**  
Getting 2 seperate traces

**Expected Ouput**  
Want 'MyTransaction2' to be created as a child of 'MyTransaction'

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [July 28, 2019, 9:52am UTC](https://discuss.elastic.co/t/apm-manual-distributed-tracing/192532/3 "2019-07-28T09:52:34Z")

</div>

Welcome to the forum and thanks for the question 🙂

You are using the distributed-tracing APIs wrong. You found the right documentation, but misinterpreted the code - it uses lambdas that only implement the way of setting and getting the header, without knowing the header name and value. Those are the responsibility of the agent.

Try changing

```auto
span.injectTraceHeaders((name, value) -> 
                    conn.setRequestProperty("Myspan", "100"));

```

to

```auto
span.injectTraceHeaders((name, value) -> 
                    conn.addRequestProperty(name, value));

```

and

```auto
Transaction transaction =ElasticApm.startTransactionWithRemoteParent(key>
                    request.getHeader("Myspan"));

```

to

```auto
Transaction transaction = ElasticApm.startTransactionWithRemoteParent(key ->
                    request.getHeader(key));

```

---

<div class="post-metadata">

**Author:** ![nashwin2804](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@nashwin2804](https://discuss.elastic.co/u/nashwin2804)\
**Post date:** [July 31, 2019, 2:06pm UTC](https://discuss.elastic.co/t/apm-manual-distributed-tracing/192532/4 "2019-07-31T14:06:28Z")

</div>

Thank you very much

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 21, 2019, 10:06am UTC](https://discuss.elastic.co/t/apm-manual-distributed-tracing/192532/5 "2019-08-21T10:06:31Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
