From f99d65798147c69e8add12607d22ffdd940e4119 Mon Sep 17 00:00:00 2001 From: edeleon Date: Fri, 12 Jan 2024 15:48:55 -0800 Subject: [PATCH 1/5] added null checks for synthetics-info attrs before adding to intrinsics --- .../java/com/newrelic/agent/Transaction.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/Transaction.java b/newrelic-agent/src/main/java/com/newrelic/agent/Transaction.java index 99b40e47fa..ebf1c95e5f 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/Transaction.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/Transaction.java @@ -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()); + 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 attrsMap = this.getInboundHeaderState().getSyntheticsAttrs(); + String attrName; - Map 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)); + } } } From 9669111899baab7166f99d4d879e6bcf66f2f4f9 Mon Sep 17 00:00:00 2001 From: edeleon Date: Fri, 12 Jan 2024 15:49:58 -0800 Subject: [PATCH 2/5] added test for checking synthetics attrs placed correctly in intrinsics --- .../com/newrelic/agent/TransactionTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/newrelic-agent/src/test/java/com/newrelic/agent/TransactionTest.java b/newrelic-agent/src/test/java/com/newrelic/agent/TransactionTest.java index 655adfd5a7..0923073c05 100644 --- a/newrelic-agent/src/test/java/com/newrelic/agent/TransactionTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/agent/TransactionTest.java @@ -1496,6 +1496,23 @@ public Map createHeaderMap() { return headerMap; } + public Map createHeaderMapWithoutSynthInfo() { + Map 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 map) { Map headerMap = createHeaderMap(); @@ -1515,6 +1532,27 @@ public String getHeader(String name) { }; } + + + public InboundHeaders createInboundHeadersWithoutSyntheticsInfoHeader(Map map) { + Map 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 configMap = createConfigMap(); createServiceManager(configMap); @@ -1531,6 +1569,21 @@ private Transaction createTxWithSyntheticsHeaders() throws Exception { return tx; } + private Transaction createTxWithSyntheticsHeaderWithoutSyntheticsInfoHeader() throws Exception { + Map 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 { @@ -1564,6 +1617,22 @@ public void testTransactionIntrinsicAttrsForSyntheticsInformation() throws Excep 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 createNRDTConfigMap(boolean excludeNewRelicHeader) { Map configMap = createConfigMap(); configMap.put(AgentConfigImpl.DISTRIBUTED_TRACING, ImmutableMap.of( From e19afa7d83d4457f321357f8036e07251c5f2f20 Mon Sep 17 00:00:00 2001 From: edeleon Date: Tue, 16 Jan 2024 13:06:29 -0800 Subject: [PATCH 3/5] add null check for synth-info header before parsing --- .../main/java/com/newrelic/agent/InboundHeaderState.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/InboundHeaderState.java b/newrelic-agent/src/main/java/com/newrelic/agent/InboundHeaderState.java index c3f582eea6..2e058dc669 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/InboundHeaderState.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/InboundHeaderState.java @@ -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; From dc5c6ae91fc064898de8e585d4d8aeaaac131fa4 Mon Sep 17 00:00:00 2001 From: edeleon Date: Tue, 16 Jan 2024 16:36:49 -0800 Subject: [PATCH 4/5] updated value type for synthetics version --- .../src/main/java/com/newrelic/agent/Transaction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/Transaction.java b/newrelic-agent/src/main/java/com/newrelic/agent/Transaction.java index ebf1c95e5f..3829699352 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/Transaction.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/Transaction.java @@ -1094,7 +1094,7 @@ private void finishTransaction() { getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_JOB_ID, this.getInboundHeaderState().getSyntheticsJobId()); getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_VERSION, - this.getInboundHeaderState().getSyntheticsVersion()); + String.valueOf(this.getInboundHeaderState().getSyntheticsVersion())); if (this.getInboundHeaderState().getSyntheticsType() != null) { getIntrinsicAttributes().put(AttributeNames.SYNTHETICS_TYPE, this.getInboundHeaderState().getSyntheticsType()); From 1f209cc32dcb63f2d898e2cd72533bf366424360 Mon Sep 17 00:00:00 2001 From: edeleon Date: Tue, 16 Jan 2024 16:37:58 -0800 Subject: [PATCH 5/5] updated tests for checking intrinsic attrs for synthetics info --- .../src/test/java/com/newrelic/agent/TransactionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/newrelic-agent/src/test/java/com/newrelic/agent/TransactionTest.java b/newrelic-agent/src/test/java/com/newrelic/agent/TransactionTest.java index 0923073c05..a6bad5990a 100644 --- a/newrelic-agent/src/test/java/com/newrelic/agent/TransactionTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/agent/TransactionTest.java @@ -1614,7 +1614,7 @@ 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 @@ -1626,7 +1626,7 @@ public void testTransactionIntrinsicAttrsWhenNoSyntheticsInfoHeaderIsReceived() 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")); + 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"));