Skip to content

Commit

Permalink
Add Null Checks for Synthetics-Info Header Attributes (newrelic#1690)
Browse files Browse the repository at this point in the history
* added null checks for synthetics-info attrs before adding to intrinsics
  • Loading branch information
deleonenriqueta authored and kanderson250 committed Jan 18, 2024
1 parent de83f4c commit c3a7f8c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public InboundHeaderState(Transaction tx, InboundHeaders inboundHeaders) {
this.catState = CatState.NONE;
} else {
this.synState = parseSyntheticsHeader();
this.synInfoState = parseSyntheticsInfoHeader();
if (inboundHeaders.getHeader("X-NewRelic-Synthetics-Info") == null) {
this.synInfoState = SyntheticsInfoState.NONE;
} else {
this.synInfoState = parseSyntheticsInfoHeader();
}
if (tx.getAgentConfig().getDistributedTracingConfig().isEnabled() && tx.getSpanProxy().getInboundDistributedTracePayload() == null) {
parseDistributedTraceHeaders();
this.catState = CatState.NONE;
Expand Down
27 changes: 16 additions & 11 deletions newrelic-agent/src/main/java/com/newrelic/agent/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -1093,19 +1093,24 @@ private void finishTransaction() {
this.getInboundHeaderState().getSyntheticsMonitorId());
getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_JOB_ID,
this.getInboundHeaderState().getSyntheticsJobId());
getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_TYPE,
this.getInboundHeaderState().getSyntheticsType());
getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_INITIATOR,
this.getInboundHeaderState().getSyntheticsInitiator());
getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_VERSION,
this.getInboundHeaderState().getSyntheticsVersion());

Map<String, String> attrsMap = this.getInboundHeaderState().getSyntheticsAttrs();
String attrName;
String.valueOf(this.getInboundHeaderState().getSyntheticsVersion()));
if (this.getInboundHeaderState().getSyntheticsType() != null) {
getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_TYPE,
this.getInboundHeaderState().getSyntheticsType());
}
if (this.getInboundHeaderState().getSyntheticsInitiator() != null) {
getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_INITIATOR,
this.getInboundHeaderState().getSyntheticsInitiator());
}
if (this.getInboundHeaderState().getSyntheticsAttrs() != null) {
Map<String, String> attrsMap = this.getInboundHeaderState().getSyntheticsAttrs();
String attrName;

for (String key : attrsMap.keySet()) {
attrName = String.format("synthetics_%s", key);
getIntrinsicAttributes().put(attrName, attrsMap.get(key));
for (String key : attrsMap.keySet()) {
attrName = String.format("synthetics_%s", key);
getIntrinsicAttributes().put(attrName, attrsMap.get(key));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,23 @@ public Map<String, String> createHeaderMap() {
return headerMap;
}

public Map<String, String> createHeaderMapWithoutSynthInfo() {
Map<String, String> headerMap = new HashMap<>();

String synthVal = "[\n" +
" 1,\n" +
" 417446,\n" +
" \"fd09bfa1-bd85-4f8a-9bee-8d51582f5a54\",\n" +
" \"77cbc5dc-327b-4542-90f0-335644134bed\",\n" +
" \"3e5c28ac-7cf3-4faf-ae52-ff36bc93504a\"\n" +
"]";
String obfuscatedSynthVal = Obfuscator.obfuscateNameUsingKey(synthVal, "anotherExampleKey");

headerMap.put("X-NewRelic-Synthetics", obfuscatedSynthVal);

return headerMap;
}

public InboundHeaders createInboundHeaders(Map<String, String> map) {
Map<String, String> headerMap = createHeaderMap();

Expand All @@ -1515,6 +1532,27 @@ public String getHeader(String name) {
};
}



public InboundHeaders createInboundHeadersWithoutSyntheticsInfoHeader(Map<String, String> map) {
Map<String, String> headerMap = createHeaderMapWithoutSynthInfo();

return new InboundHeaders() {
@Override
public HeaderType getHeaderType() {
return HeaderType.HTTP;
}

@Override
public String getHeader(String name) {
if (headerMap.containsKey(name)) {
return headerMap.get(name);
}
return null;
}
};
}

private Transaction createTxWithSyntheticsHeaders() throws Exception {
Map<String, Object> configMap = createConfigMap();
createServiceManager(configMap);
Expand All @@ -1531,6 +1569,21 @@ private Transaction createTxWithSyntheticsHeaders() throws Exception {
return tx;
}

private Transaction createTxWithSyntheticsHeaderWithoutSyntheticsInfoHeader() throws Exception {
Map<String, Object> configMap = createConfigMap();
createServiceManager(configMap);
InboundHeaders inboundHeaders = createInboundHeadersWithoutSyntheticsInfoHeader(createHeaderMapWithoutSynthInfo());
Transaction tx = Transaction.getTransaction(true);
Tracer dispatcherTracer = createDispatcherTracer(false);
tx.getTransactionActivity().tracerStarted(dispatcherTracer);
MockHttpRequest httpRequest = new MockHttpRequest();
MockHttpResponse httpResponse = new MockHttpResponse();
httpRequest.setHeader("X-NewRelic-Synthetics", inboundHeaders.getHeader("X-NewRelic-Synthetics"));
tx.setRequestAndResponse(httpRequest, httpResponse);
tx.getTransactionActivity().tracerFinished(dispatcherTracer, 0);
return tx;
}

@Test
public void testInboundHeaderStateForSyntheticsInformation() throws Exception {

Expand Down Expand Up @@ -1561,7 +1614,23 @@ public void testTransactionIntrinsicAttrsForSyntheticsInformation() throws Excep
assertEquals("scheduled", tx.getIntrinsicAttributes().get("synthetics_type"));
assertEquals("Value1", tx.getIntrinsicAttributes().get("synthetics_example1"));
assertEquals("Value2", tx.getIntrinsicAttributes().get("synthetics_example2"));
assertEquals(1, tx.getIntrinsicAttributes().get("synthetics_version"));
assertEquals("1", tx.getIntrinsicAttributes().get("synthetics_version"));
}

@Test
public void testTransactionIntrinsicAttrsWhenNoSyntheticsInfoHeaderIsReceived() throws Exception {

Transaction tx = createTxWithSyntheticsHeaderWithoutSyntheticsInfoHeader();

assertNotNull(tx.getIntrinsicAttributes());
assertEquals("77cbc5dc-327b-4542-90f0-335644134bed", tx.getIntrinsicAttributes().get("synthetics_job_id"));
assertEquals("fd09bfa1-bd85-4f8a-9bee-8d51582f5a54", tx.getIntrinsicAttributes().get("synthetics_resource_id"));
assertEquals("3e5c28ac-7cf3-4faf-ae52-ff36bc93504a", tx.getIntrinsicAttributes().get("synthetics_monitor_id"));
assertEquals("1", tx.getIntrinsicAttributes().get("synthetics_version"));
assertNull(tx.getIntrinsicAttributes().get("synthetics_initiator"));
assertNull(tx.getIntrinsicAttributes().get("synthetics_type"));
assertNull(tx.getIntrinsicAttributes().get("synthetics_example1"));
assertNull(tx.getIntrinsicAttributes().get("synthetics_example2"));
}

private Map<String, Object> createNRDTConfigMap(boolean excludeNewRelicHeader) {
Expand Down

0 comments on commit c3a7f8c

Please sign in to comment.