无法从标题获取新的文物跟踪ID

时间:2019-02-06 21:35:30

标签: java newrelic distributed-tracing

我一直在尝试从跟踪服务的请求中获取新的文物跟踪/交易ID。

不幸的是,我在获取值时遇到了麻烦。

当我尝试从标头中提取值时,不会填充它们。

我可以获得分布式有效载荷,但是不确定我要看什么。

结果:

@RequestHeader(name="X-NewRelic-ID", defaultValue="null") =“空”
@RequestHeader(name="X-NewRelic-Transaction", defaultValue="null") =“空”
NewRelic.getAgent().getTransaction().getRequestMetadata() =空
NewRelic.getAgent().getTransaction().getResponseMetadata() =空

DistributedTracePayload =

{
  "d": {
    "ac": "1234567",
    "pr": 1.300000,
    "tx": "15c1855ad4955424",
    "ti": 1234567890123,
    "ty": "App",
    "tr": "13c1423ad1941424",
    "sa": true,
    "ap": "12345678"
  },
  "v": [
    0,
    1
  ]
}

Web服务:

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);
    }
}

0 个答案:

没有答案