HeaderInjector doesn't work

I use HeaderInjector for a rpc call,but it doesn't form a trace.The client code is:

    Transaction  transaction = ElasticApm.currentTransaction();
    Span span = transaction.startSpan("external", "tcp", null).setName(methodName);
    		span.activate();
    		try{
    			span.injectTraceHeaders(new HeaderInjector() {
    				@Override
    				public void addHeader(String headerName, String headerValue) {
    					context.put(headerName, headerValue);
    				}
    			});
    		} catch (Exception e) {
    			span.captureException(e);
                span.end();
    		}

The server code:

    Transaction  transaction = ElasticApm.startTransactionWithRemoteParent(new HeaderExtractor() {
    			@Override
    			public String getFirstHeader(String headerName) {
    				return context.get(headerName);
    			}
    		});
    	    transaction.activate();
    	    try {
    	    	transaction.setName("tcp-server");
    		    transaction.setType(Transaction.TYPE_REQUEST);
    	    } catch (Exception e) {
    	        transaction.captureException(e);
                transaction.end();
    	    }

Could you add log statements to addHeader and getFirstHeader to see the values it adds and gets?

Also, you should use try-with-resources blocks when using activate(). See the example here: https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-transaction-inject-trace-headers

If I creat a new transtraction it’s work,but if achieve current transtraction it’s doesn’t work

That probably means that there is no current transaction.

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