From 09ef8fd6ac99e1829571af29d6cfc97b6c73d5ef Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Fri, 28 Jul 2023 12:37:16 +0200 Subject: [PATCH 1/7] Allow multimatch for telemetry log processor --- .../internal/processors/ProcessorUtil.java | 2 +- .../ExporterWithLogProcessorTest.java | 31 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java index f912cceb466..be3a621e11e 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java @@ -24,7 +24,7 @@ public static String applyRule( StringBuilder sb = new StringBuilder(); int lastEnd = 0; // As of now we are considering only first match. - if (matcher.find()) { + while (matcher.find()) { sb.append(name, lastEnd, matcher.start()); int innerLastEnd = matcher.start(); for (int i = 1; i <= groupNamesList.size(); i++) { diff --git a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java index e0cca444d03..b9218a3c4cb 100644 --- a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java +++ b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java @@ -184,7 +184,7 @@ void multiRuleToAttributesTest() { LogRecordExporter logExporter = new ExporterWithLogProcessor(config, mockExporter); TestLogRecordData mockLogA = TestLogRecordData.builder() - .setBody("yyyPassword=123 aba Pass=555 xyx Pass=777 zzz") + .setBody("yyyPassword=123 aba Pass=555 xyx") .setAttributes(attributes) .build(); @@ -229,7 +229,7 @@ void multiRuleToAttributesTest() { resultA.getAttributes().get(AttributeKey.stringKey("password2")))) .isEqualTo("555"); assertThat(resultA.getBody().asString()) - .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass=777 zzz"); + .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx"); assertThat( Objects.requireNonNull( resultB.getAttributes().get(AttributeKey.stringKey("password1")))) @@ -241,6 +241,33 @@ void multiRuleToAttributesTest() { assertThat(resultB.getBody().asString()).isEqualTo("yyyPassword={password1} aba"); } + @Test + void multiMatch() { + config.id = "MultiRuleToAttributes"; + config.body = new NameConfig(); + ToAttributeConfig toAttributeConfig = new ToAttributeConfig(); + toAttributeConfig.rules = new ArrayList<>(); + toAttributeConfig.rules.add("Password=(?[^ ]+)"); + config.body.toAttributes = toAttributeConfig; + LogRecordExporter logExporter = new ExporterWithLogProcessor(config, mockExporter); + TestLogRecordData mockLogA = + TestLogRecordData.builder() + .setBody("yyyPassword=123 aba Password=555 xyx") + .setAttributes(attributes) + .build(); + + List logs = new ArrayList<>(); + logs.add(mockLogA); + logExporter.export(logs); + + // verify that resulting logs are filtered in the way we want + List result = mockExporter.getLogs(); + LogRecordData resultA = result.get(0); + + assertThat(resultA.getBody().asString()) + .isEqualTo("yyyPassword={x} aba Password={x} xyx"); + } + @Test void simpleRenameLogTestWithLogProcessor() { config.id = "SimpleRenameLog"; From 1810268aad1f3d0f6bc094a8b6fa4319f005eee0 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Fri, 28 Jul 2023 12:45:40 +0200 Subject: [PATCH 2/7] spotless --- .../internal/processors/ExporterWithLogProcessorTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java index b9218a3c4cb..1d64143cc7e 100644 --- a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java +++ b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java @@ -264,8 +264,7 @@ void multiMatch() { List result = mockExporter.getLogs(); LogRecordData resultA = result.get(0); - assertThat(resultA.getBody().asString()) - .isEqualTo("yyyPassword={x} aba Password={x} xyx"); + assertThat(resultA.getBody().asString()).isEqualTo("yyyPassword={x} aba Password={x} xyx"); } @Test From 0786c695f3a57be5bfb7641cd76b2ce8b5f74133 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Fri, 28 Jul 2023 13:19:52 +0200 Subject: [PATCH 3/7] Adapt test --- .../processors/ExporterWithSpanProcessorTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java index 98c87097b00..c3cdca3146a 100644 --- a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java +++ b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java @@ -355,9 +355,9 @@ void multiRuleToAttributesTest() { assertThat( Objects.requireNonNull( resultSpanA.getAttributes().get(AttributeKey.stringKey("password2")))) - .isEqualTo("555"); + .isEqualTo("777"); assertThat(resultSpanA.getName()) - .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass=777 zzz"); + .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass={password2} zzz"); assertThat( Objects.requireNonNull( resultSpanB.getAttributes().get(AttributeKey.stringKey("password1")))) @@ -383,7 +383,7 @@ void extractAttributesWithIncludeExcludeTest() { config.exclude.matchType = MatchType.STRICT; config.exclude.spanNames = Arrays.asList("donot/change"); config.name.toAttributes = new ToAttributeConfig(); - config.name.toAttributes.rules = Arrays.asList("(?.*?)$"); + config.name.toAttributes.rules = Arrays.asList("(?.*?)/.*$"); SpanExporter exampleExporter = new ExporterWithSpanProcessor(config, mockSpanExporter); Span spanA = @@ -439,20 +439,20 @@ void extractAttributesWithIncludeExcludeTest() { SpanData resultSpanB = result.get(1); SpanData resultSpanC = result.get(2); SpanData resultSpanD = result.get(3); - assertThat(resultSpanA.getName()).isEqualTo("{operationwebsite}"); + assertThat(resultSpanA.getName()).isEqualTo("{operationwebsite}/test"); assertThat(resultSpanA.getAttributes().get(AttributeKey.stringKey("operationwebsite"))) .isNotNull(); assertThat( Objects.requireNonNull( resultSpanA.getAttributes().get(AttributeKey.stringKey("operationwebsite")))) - .isEqualTo("svcA/test"); - assertThat(resultSpanB.getName()).isEqualTo("{operationwebsite}"); + .isEqualTo("svcA"); + assertThat(resultSpanB.getName()).isEqualTo("{operationwebsite}/test"); assertThat(resultSpanB.getAttributes().get(AttributeKey.stringKey("operationwebsite"))) .isNotNull(); assertThat( Objects.requireNonNull( resultSpanB.getAttributes().get(AttributeKey.stringKey("operationwebsite")))) - .isEqualTo("svcB/test"); + .isEqualTo("svcB"); assertThat(resultSpanC.getName()).isEqualTo("svcC"); assertThat(resultSpanD.getName()).isEqualTo("donot/change"); } From 9176c091e62bab02256469e42891c9d96e9c4f55 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Tue, 1 Aug 2023 11:18:21 -0700 Subject: [PATCH 4/7] Update agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java --- .../agent/internal/processors/ProcessorUtil.java | 1 - 1 file changed, 1 deletion(-) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java index be3a621e11e..b801407c3d1 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java @@ -23,7 +23,6 @@ public static String applyRule( Matcher matcher = pattern.matcher(name); StringBuilder sb = new StringBuilder(); int lastEnd = 0; - // As of now we are considering only first match. while (matcher.find()) { sb.append(name, lastEnd, matcher.start()); int innerLastEnd = matcher.start(); From eff0d782f94b6c728013b97b21f132e5a26faca3 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 23 Aug 2023 19:25:59 +0200 Subject: [PATCH 5/7] Don't modify the current behavior of attribute extraction --- .../internal/processors/LogProcessor.java | 5 +- .../internal/processors/ProcessorUtil.java | 62 ++++++++++++++----- .../internal/processors/SpanProcessor.java | 6 +- .../ExporterWithSpanProcessorTest.java | 14 ++--- 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java index b089da9b3e5..beee14274d9 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java @@ -3,7 +3,7 @@ package com.microsoft.applicationinsights.agent.internal.processors; -import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.applyRule; +import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.applyRuleOnAllMatches; import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.getGroupNamesList; import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig; @@ -104,7 +104,8 @@ public LogRecordData processToAttributes(LogRecordData log) { AttributesBuilder builder = log.getAttributes().toBuilder(); for (int i = 0; i < groupNames.size(); i++) { bodyAsString = - applyRule(groupNames.get(i), toAttributeRulePatterns.get(i), bodyAsString, builder); + applyRuleOnAllMatches( + groupNames.get(i), toAttributeRulePatterns.get(i), bodyAsString, builder); } return new MyLogData(log, builder.build(), Body.string(bodyAsString)); diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java index b801407c3d1..47ce6026fc1 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java @@ -15,7 +15,7 @@ public class ProcessorUtil { - public static String applyRule( + public static String applyRuleOnFirstMatch( List groupNamesList, Pattern pattern, String name, AttributesBuilder builder) { if (groupNamesList.isEmpty()) { return name; @@ -23,26 +23,60 @@ public static String applyRule( Matcher matcher = pattern.matcher(name); StringBuilder sb = new StringBuilder(); int lastEnd = 0; + if (matcher.find()) { + boolean firstMatch = true; + lastEnd = applyRule(groupNamesList, name, builder, sb, lastEnd, matcher, firstMatch); + } + sb.append(name, lastEnd, name.length()); + + return sb.toString(); + } + + // Rule applied on all matches for the returned String (not the attributes) + public static String applyRuleOnAllMatches( + List groupNamesList, Pattern pattern, String name, AttributesBuilder builder) { + if (groupNamesList.isEmpty()) { + return name; + } + Matcher matcher = pattern.matcher(name); + StringBuilder sb = new StringBuilder(); + int lastEnd = 0; + boolean firstMatch = true; while (matcher.find()) { - sb.append(name, lastEnd, matcher.start()); - int innerLastEnd = matcher.start(); - for (int i = 1; i <= groupNamesList.size(); i++) { - sb.append(name, innerLastEnd, matcher.start(i)); - sb.append("{"); - sb.append(groupNamesList.get(i - 1)); - // add attribute key=groupNames.get(i-1), value=matcher.group(i) - builder.put(groupNamesList.get(i - 1), matcher.group(i)); - sb.append("}"); - innerLastEnd = matcher.end(i); - } - sb.append(name, innerLastEnd, matcher.end()); - lastEnd = matcher.end(); + lastEnd = applyRule(groupNamesList, name, builder, sb, lastEnd, matcher, firstMatch); + firstMatch = false; } sb.append(name, lastEnd, name.length()); return sb.toString(); } + private static int applyRule( + List groupNamesList, + String name, + AttributesBuilder builder, + StringBuilder sb, + int lastEnd, + Matcher matcher, + boolean firstMatch) { + sb.append(name, lastEnd, matcher.start()); + int innerLastEnd = matcher.start(); + for (int i = 1; i <= groupNamesList.size(); i++) { + sb.append(name, innerLastEnd, matcher.start(i)); + sb.append("{"); + sb.append(groupNamesList.get(i - 1)); + // add attribute key=groupNames.get(i-1), value=matcher.group(i) + if (firstMatch) { + builder.put(groupNamesList.get(i - 1), matcher.group(i)); + } + sb.append("}"); + innerLastEnd = matcher.end(i); + } + sb.append(name, innerLastEnd, matcher.end()); + lastEnd = matcher.end(); + return lastEnd; + } + public static List> getGroupNamesList(List toAttributeRules) { List> groupNamesList = new ArrayList<>(); for (String rule : toAttributeRules) { diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/SpanProcessor.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/SpanProcessor.java index 068326db518..e6eefffb1fe 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/SpanProcessor.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/SpanProcessor.java @@ -3,7 +3,7 @@ package com.microsoft.applicationinsights.agent.internal.processors; -import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.applyRule; +import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.applyRuleOnFirstMatch; import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.getGroupNamesList; import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.spanHasAllFromAttributeKeys; @@ -101,7 +101,9 @@ public SpanData processToAttributes(SpanData span) { // they will be overwritten. Need a way to optimize this. AttributesBuilder builder = span.getAttributes().toBuilder(); for (int i = 0; i < groupNames.size(); i++) { - spanName = applyRule(groupNames.get(i), toAttributeRulePatterns.get(i), spanName, builder); + spanName = + applyRuleOnFirstMatch( + groupNames.get(i), toAttributeRulePatterns.get(i), spanName, builder); } return new MySpanData(span, builder.build(), spanName); } diff --git a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java index c3cdca3146a..98c87097b00 100644 --- a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java +++ b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java @@ -355,9 +355,9 @@ void multiRuleToAttributesTest() { assertThat( Objects.requireNonNull( resultSpanA.getAttributes().get(AttributeKey.stringKey("password2")))) - .isEqualTo("777"); + .isEqualTo("555"); assertThat(resultSpanA.getName()) - .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass={password2} zzz"); + .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass=777 zzz"); assertThat( Objects.requireNonNull( resultSpanB.getAttributes().get(AttributeKey.stringKey("password1")))) @@ -383,7 +383,7 @@ void extractAttributesWithIncludeExcludeTest() { config.exclude.matchType = MatchType.STRICT; config.exclude.spanNames = Arrays.asList("donot/change"); config.name.toAttributes = new ToAttributeConfig(); - config.name.toAttributes.rules = Arrays.asList("(?.*?)/.*$"); + config.name.toAttributes.rules = Arrays.asList("(?.*?)$"); SpanExporter exampleExporter = new ExporterWithSpanProcessor(config, mockSpanExporter); Span spanA = @@ -439,20 +439,20 @@ void extractAttributesWithIncludeExcludeTest() { SpanData resultSpanB = result.get(1); SpanData resultSpanC = result.get(2); SpanData resultSpanD = result.get(3); - assertThat(resultSpanA.getName()).isEqualTo("{operationwebsite}/test"); + assertThat(resultSpanA.getName()).isEqualTo("{operationwebsite}"); assertThat(resultSpanA.getAttributes().get(AttributeKey.stringKey("operationwebsite"))) .isNotNull(); assertThat( Objects.requireNonNull( resultSpanA.getAttributes().get(AttributeKey.stringKey("operationwebsite")))) - .isEqualTo("svcA"); - assertThat(resultSpanB.getName()).isEqualTo("{operationwebsite}/test"); + .isEqualTo("svcA/test"); + assertThat(resultSpanB.getName()).isEqualTo("{operationwebsite}"); assertThat(resultSpanB.getAttributes().get(AttributeKey.stringKey("operationwebsite"))) .isNotNull(); assertThat( Objects.requireNonNull( resultSpanB.getAttributes().get(AttributeKey.stringKey("operationwebsite")))) - .isEqualTo("svcB"); + .isEqualTo("svcB/test"); assertThat(resultSpanC.getName()).isEqualTo("svcC"); assertThat(resultSpanD.getName()).isEqualTo("donot/change"); } From 7c5320aab6ae954d963eec9d6a02f63b9c4530a9 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Wed, 23 Aug 2023 20:21:51 +0200 Subject: [PATCH 6/7] Keep previous input of simpleToAttributesTest --- .../internal/processors/ExporterWithLogProcessorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java index 1d64143cc7e..e19ffb9301b 100644 --- a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java +++ b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java @@ -184,7 +184,7 @@ void multiRuleToAttributesTest() { LogRecordExporter logExporter = new ExporterWithLogProcessor(config, mockExporter); TestLogRecordData mockLogA = TestLogRecordData.builder() - .setBody("yyyPassword=123 aba Pass=555 xyx") + .setBody("yyyPassword=123 aba Pass=555 xyx Pass=777 zzz") .setAttributes(attributes) .build(); @@ -229,7 +229,7 @@ void multiRuleToAttributesTest() { resultA.getAttributes().get(AttributeKey.stringKey("password2")))) .isEqualTo("555"); assertThat(resultA.getBody().asString()) - .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx"); + .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass={password2} zzz"); assertThat( Objects.requireNonNull( resultB.getAttributes().get(AttributeKey.stringKey("password1")))) From ddd8e2a618a7b5923c20c505e1e2730668a849e6 Mon Sep 17 00:00:00 2001 From: Jean Bisutti Date: Thu, 24 Aug 2023 11:28:06 +0200 Subject: [PATCH 7/7] Apply multimatching on span name --- .../internal/processors/LogProcessor.java | 5 +- .../internal/processors/ProcessorUtil.java | 54 ++++++++----------- .../internal/processors/SpanProcessor.java | 6 +-- .../ExporterWithLogProcessorTest.java | 2 +- .../ExporterWithSpanProcessorTest.java | 14 ++--- 5 files changed, 33 insertions(+), 48 deletions(-) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java index beee14274d9..b089da9b3e5 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java @@ -3,7 +3,7 @@ package com.microsoft.applicationinsights.agent.internal.processors; -import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.applyRuleOnAllMatches; +import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.applyRule; import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.getGroupNamesList; import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig; @@ -104,8 +104,7 @@ public LogRecordData processToAttributes(LogRecordData log) { AttributesBuilder builder = log.getAttributes().toBuilder(); for (int i = 0; i < groupNames.size(); i++) { bodyAsString = - applyRuleOnAllMatches( - groupNames.get(i), toAttributeRulePatterns.get(i), bodyAsString, builder); + applyRule(groupNames.get(i), toAttributeRulePatterns.get(i), bodyAsString, builder); } return new MyLogData(log, builder.build(), Body.string(bodyAsString)); diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java index 47ce6026fc1..352db2b3578 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ProcessorUtil.java @@ -15,64 +15,52 @@ public class ProcessorUtil { - public static String applyRuleOnFirstMatch( - List groupNamesList, Pattern pattern, String name, AttributesBuilder builder) { - if (groupNamesList.isEmpty()) { - return name; - } - Matcher matcher = pattern.matcher(name); - StringBuilder sb = new StringBuilder(); - int lastEnd = 0; - if (matcher.find()) { - boolean firstMatch = true; - lastEnd = applyRule(groupNamesList, name, builder, sb, lastEnd, matcher, firstMatch); - } - sb.append(name, lastEnd, name.length()); - - return sb.toString(); - } - - // Rule applied on all matches for the returned String (not the attributes) - public static String applyRuleOnAllMatches( - List groupNamesList, Pattern pattern, String name, AttributesBuilder builder) { + // Rule applied on all matches for the returned string. The first match is taken to populate + // extracted attributes. + public static String applyRule( + List groupNamesList, + Pattern pattern, + String name, + AttributesBuilder attributesBuilder) { if (groupNamesList.isEmpty()) { return name; } Matcher matcher = pattern.matcher(name); - StringBuilder sb = new StringBuilder(); + StringBuilder output = new StringBuilder(); int lastEnd = 0; boolean firstMatch = true; while (matcher.find()) { - lastEnd = applyRule(groupNamesList, name, builder, sb, lastEnd, matcher, firstMatch); + lastEnd = + applyRule(groupNamesList, name, attributesBuilder, output, lastEnd, matcher, firstMatch); firstMatch = false; } - sb.append(name, lastEnd, name.length()); + output.append(name, lastEnd, name.length()); - return sb.toString(); + return output.toString(); } private static int applyRule( List groupNamesList, String name, - AttributesBuilder builder, - StringBuilder sb, + AttributesBuilder attributesBuilder, + StringBuilder output, int lastEnd, Matcher matcher, boolean firstMatch) { - sb.append(name, lastEnd, matcher.start()); + output.append(name, lastEnd, matcher.start()); int innerLastEnd = matcher.start(); for (int i = 1; i <= groupNamesList.size(); i++) { - sb.append(name, innerLastEnd, matcher.start(i)); - sb.append("{"); - sb.append(groupNamesList.get(i - 1)); + output.append(name, innerLastEnd, matcher.start(i)); + output.append("{"); + output.append(groupNamesList.get(i - 1)); // add attribute key=groupNames.get(i-1), value=matcher.group(i) if (firstMatch) { - builder.put(groupNamesList.get(i - 1), matcher.group(i)); + attributesBuilder.put(groupNamesList.get(i - 1), matcher.group(i)); } - sb.append("}"); + output.append("}"); innerLastEnd = matcher.end(i); } - sb.append(name, innerLastEnd, matcher.end()); + output.append(name, innerLastEnd, matcher.end()); lastEnd = matcher.end(); return lastEnd; } diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/SpanProcessor.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/SpanProcessor.java index e6eefffb1fe..068326db518 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/SpanProcessor.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/SpanProcessor.java @@ -3,7 +3,7 @@ package com.microsoft.applicationinsights.agent.internal.processors; -import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.applyRuleOnFirstMatch; +import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.applyRule; import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.getGroupNamesList; import static com.microsoft.applicationinsights.agent.internal.processors.ProcessorUtil.spanHasAllFromAttributeKeys; @@ -101,9 +101,7 @@ public SpanData processToAttributes(SpanData span) { // they will be overwritten. Need a way to optimize this. AttributesBuilder builder = span.getAttributes().toBuilder(); for (int i = 0; i < groupNames.size(); i++) { - spanName = - applyRuleOnFirstMatch( - groupNames.get(i), toAttributeRulePatterns.get(i), spanName, builder); + spanName = applyRule(groupNames.get(i), toAttributeRulePatterns.get(i), spanName, builder); } return new MySpanData(span, builder.build(), spanName); } diff --git a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java index e19ffb9301b..4fddc756373 100644 --- a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java +++ b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java @@ -227,7 +227,7 @@ void multiRuleToAttributesTest() { assertThat( Objects.requireNonNull( resultA.getAttributes().get(AttributeKey.stringKey("password2")))) - .isEqualTo("555"); + .isEqualTo("555"); // The first match is taken to populate the attribute assertThat(resultA.getBody().asString()) .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass={password2} zzz"); assertThat( diff --git a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java index 98c87097b00..5003f3138f5 100644 --- a/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java +++ b/agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithSpanProcessorTest.java @@ -355,9 +355,9 @@ void multiRuleToAttributesTest() { assertThat( Objects.requireNonNull( resultSpanA.getAttributes().get(AttributeKey.stringKey("password2")))) - .isEqualTo("555"); + .isEqualTo("555"); // The first match is taken to populate the attribute assertThat(resultSpanA.getName()) - .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass=777 zzz"); + .isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass={password2} zzz"); assertThat( Objects.requireNonNull( resultSpanB.getAttributes().get(AttributeKey.stringKey("password1")))) @@ -383,7 +383,7 @@ void extractAttributesWithIncludeExcludeTest() { config.exclude.matchType = MatchType.STRICT; config.exclude.spanNames = Arrays.asList("donot/change"); config.name.toAttributes = new ToAttributeConfig(); - config.name.toAttributes.rules = Arrays.asList("(?.*?)$"); + config.name.toAttributes.rules = Arrays.asList("(?.*?)/.*$"); SpanExporter exampleExporter = new ExporterWithSpanProcessor(config, mockSpanExporter); Span spanA = @@ -439,20 +439,20 @@ void extractAttributesWithIncludeExcludeTest() { SpanData resultSpanB = result.get(1); SpanData resultSpanC = result.get(2); SpanData resultSpanD = result.get(3); - assertThat(resultSpanA.getName()).isEqualTo("{operationwebsite}"); + assertThat(resultSpanA.getName()).isEqualTo("{operationwebsite}/test"); assertThat(resultSpanA.getAttributes().get(AttributeKey.stringKey("operationwebsite"))) .isNotNull(); assertThat( Objects.requireNonNull( resultSpanA.getAttributes().get(AttributeKey.stringKey("operationwebsite")))) - .isEqualTo("svcA/test"); - assertThat(resultSpanB.getName()).isEqualTo("{operationwebsite}"); + .isEqualTo("svcA"); + assertThat(resultSpanB.getName()).isEqualTo("{operationwebsite}/test"); assertThat(resultSpanB.getAttributes().get(AttributeKey.stringKey("operationwebsite"))) .isNotNull(); assertThat( Objects.requireNonNull( resultSpanB.getAttributes().get(AttributeKey.stringKey("operationwebsite")))) - .isEqualTo("svcB/test"); + .isEqualTo("svcB"); assertThat(resultSpanC.getName()).isEqualTo("svcC"); assertThat(resultSpanD.getName()).isEqualTo("donot/change"); }