我一直在尝试从跟踪服务的请求中获取 New Relic Trace/Transaction ID。
不幸的是,我无法检索该值。
当我尝试从标题中提取值时,它们不会被填充。
我可以获得分布式有效负载,但不完全确定我在看什么。
结果:
@RequestHeader(name="X-NewRelic-ID", defaultValue="null")
=“空”
@RequestHeader(name="X-NewRelic-Transaction", defaultValue="null")
=“空”
NewRelic.getAgent().getTransaction().getRequestMetadata()
=空
NewRelic.getAgent().getTransaction().getResponseMetadata()
=空
分布式跟踪有效负载 =
{
"d": {
"ac": "1234567",
"pr": 1.300000,
"tx": "15c1855ad4955424",
"ti": 1234567890123,
"ty": "App",
"tr": "13c1423ad1941424",
"sa": true,
"ap": "12345678"
},
"v": [
0,
1
]
}
网络服务:
import java.text.MessageFormat;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.bah.egis.recompete.service.JsonService;
import com.newrelic.api.agent.DistributedTracePayload;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@Api(value = "JSON API", tags = { "JSON REST Api" })
public class TestController {
@Qualifier("jsonService")
@Autowired
private JsonService jsonService;
@Trace(dispatcher=true)
@CrossOrigin
@ApiOperation(value = "Ingest JSON Data and do something with it")
@RequestMapping(value = "/registerTraceID", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> registerTraceID(@RequestBody String jsonData, @RequestHeader(name="X-NewRelic-ID", defaultValue="") String newRelicID, @RequestHeader(name="X-NewRelic-Transaction", defaultValue="") String newRelicTransactionID) {
String result;
if (StringUtils.isBlank(newRelicID) && StringUtils.isBlank(newRelicTransactionID)) {
DistributedTracePayload distributedTracePayload = NewRelic.getAgent().getTransaction().createDistributedTracePayload();
if (distributedTracePayload == null) {
result = "";
} else {
result = distributedTracePayload.text();
}
} else {
result = MessageFormat.format("'{'X-NewRelic-ID: {0}, X-NewRelic-Transaction: {1}'}'", newRelicID, newRelicTransactionID);
}
return ResponseEntity.ok().body(result);
}
}