Skip to content

Commit 7c4d4d8

Browse files
committed
Address body value deprecations
1 parent 3f5a008 commit 7c4d4d8

File tree

5 files changed

+76
-17
lines changed

5 files changed

+76
-17
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/DelegatingLogData.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static java.util.Objects.requireNonNull;
77

88
import io.opentelemetry.api.common.Attributes;
9+
import io.opentelemetry.api.common.Value;
910
import io.opentelemetry.api.logs.Severity;
1011
import io.opentelemetry.api.trace.SpanContext;
1112
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
@@ -59,10 +60,16 @@ public String getSeverityText() {
5960
}
6061

6162
@Override
63+
@SuppressWarnings("deprecation") // Implementation of deprecated method
6264
public Body getBody() {
6365
return delegate.getBody();
6466
}
6567

68+
@Override
69+
public Value<?> getBodyValue() {
70+
return delegate.getBodyValue();
71+
}
72+
6673
@Override
6774
public Attributes getAttributes() {
6875
return delegate.getAttributes();
@@ -100,8 +107,8 @@ public String toString() {
100107
+ "severityText="
101108
+ getSeverityText()
102109
+ ", "
103-
+ "body="
104-
+ getBody()
110+
+ "bodyValue="
111+
+ getBodyValue()
105112
+ "}";
106113
}
107114
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessor.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig;
77
import com.microsoft.applicationinsights.agent.internal.processors.AgentProcessor.IncludeExclude;
8+
import io.opentelemetry.api.common.Value;
9+
import io.opentelemetry.api.common.ValueType;
810
import io.opentelemetry.sdk.common.CompletableResultCode;
911
import io.opentelemetry.sdk.logs.data.LogRecordData;
1012
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
@@ -36,12 +38,22 @@ public CompletableResultCode export(Collection<LogRecordData> logs) {
3638

3739
private LogRecordData process(LogRecordData log) {
3840
IncludeExclude include = logProcessor.getInclude();
39-
if (include != null && !include.isMatch(log.getAttributes(), log.getBody().asString())) {
41+
Value<?> bodyValue = log.getBodyValue();
42+
String body;
43+
if (bodyValue == null) {
44+
body = "";
45+
} else if (bodyValue.getType() == ValueType.STRING) {
46+
body = bodyValue.asString();
47+
} else {
48+
// TODO (trask) support complex log bodies
49+
body = "";
50+
}
51+
if (include != null && !include.isMatch(log.getAttributes(), body)) {
4052
// If Not included we can skip further processing
4153
return log;
4254
}
4355
IncludeExclude exclude = logProcessor.getExclude();
44-
if (exclude != null && exclude.isMatch(log.getAttributes(), log.getBody().asString())) {
56+
if (exclude != null && exclude.isMatch(log.getAttributes(), body)) {
4557
return log;
4658
}
4759

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/LogProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import io.opentelemetry.api.common.AttributeKey;
1111
import io.opentelemetry.api.common.Attributes;
1212
import io.opentelemetry.api.common.AttributesBuilder;
13-
import io.opentelemetry.sdk.logs.data.Body;
1413
import io.opentelemetry.sdk.logs.data.LogRecordData;
1514
import java.util.ArrayList;
1615
import java.util.List;
@@ -84,7 +83,7 @@ public LogRecordData processFromAttributes(LogRecordData log) {
8483
updatedLogBuffer.setLength(updatedLogBuffer.length() - separator.length());
8584
}
8685

87-
return new MyLogData(log, existingLogAttributes, Body.string(updatedLogBuffer.toString()));
86+
return new MyLogData(log, existingLogAttributes, updatedLogBuffer.toString());
8887
}
8988

9089
return log;
@@ -107,7 +106,7 @@ public LogRecordData processToAttributes(LogRecordData log) {
107106
applyRule(groupNames.get(i), toAttributeRulePatterns.get(i), bodyAsString, builder);
108107
}
109108

110-
return new MyLogData(log, builder.build(), Body.string(bodyAsString));
109+
return new MyLogData(log, builder.build(), bodyAsString);
111110
}
112111

113112
public static boolean logHasAllFromAttributeKeys(

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/processors/MyLogData.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,40 @@
44
package com.microsoft.applicationinsights.agent.internal.processors;
55

66
import io.opentelemetry.api.common.Attributes;
7+
import io.opentelemetry.api.common.Value;
78
import io.opentelemetry.sdk.logs.data.Body;
89
import io.opentelemetry.sdk.logs.data.LogRecordData;
910

11+
// backwards compatibility is just in case any extensions out there are using custom log processors
12+
@SuppressWarnings("deprecation") // using deprecated Body for backwards compatibility
1013
public class MyLogData extends DelegatingLogData {
1114

1215
private final Attributes attributes;
1316
private final Body body;
17+
private final Value<?> bodyValue;
1418

1519
public MyLogData(LogRecordData delegate, Attributes attributes) {
16-
this(delegate, attributes, delegate.getBody());
20+
this(delegate, attributes, delegate.getBodyValue(), delegate.getBody());
1721
}
1822

19-
public MyLogData(LogRecordData delegate, Attributes attributes, Body body) {
23+
public MyLogData(LogRecordData delegate, Attributes attributes, String body) {
24+
this(delegate, attributes, Value.of(body), Body.string(body));
25+
}
26+
27+
private MyLogData(LogRecordData delegate, Attributes attributes, Value<?> bodyValue, Body body) {
2028
super(delegate);
2129
this.attributes = attributes;
2230
this.body = body;
31+
this.bodyValue = bodyValue;
32+
}
33+
34+
@Override
35+
public Value<?> getBodyValue() {
36+
return bodyValue;
2337
}
2438

2539
@Override
40+
@Deprecated
2641
public Body getBody() {
2742
return body;
2843
}

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/processors/ExporterWithLogProcessorTest.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ToAttributeConfig;
1414
import io.opentelemetry.api.common.AttributeKey;
1515
import io.opentelemetry.api.common.Attributes;
16+
import io.opentelemetry.api.common.Value;
17+
import io.opentelemetry.api.common.ValueType;
1618
import io.opentelemetry.sdk.logs.data.LogRecordData;
1719
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
1820
import io.opentelemetry.sdk.testing.logs.TestLogRecordData;
@@ -85,7 +87,10 @@ void simpleRenameLogMessageTest() {
8587
// verify that resulting logs are filtered in the way we want
8688
List<LogRecordData> result = mockExporter.getLogs();
8789
LogRecordData resultLog = result.get(0);
88-
assertThat(resultLog.getBody().asString()).isEqualTo("locationget1234");
90+
Value<?> bodyValue = resultLog.getBodyValue();
91+
assertThat(bodyValue).isNotNull();
92+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
93+
assertThat(bodyValue.asString()).isEqualTo("locationget1234");
8994
}
9095

9196
@Test
@@ -104,7 +109,10 @@ void simpleRenameLogWithSeparatorTest() {
104109
// verify that resulting logs are filtered in the way we want
105110
List<LogRecordData> result = mockExporter.getLogs();
106111
LogRecordData resultLog = result.get(0);
107-
assertThat(resultLog.getBody().asString()).isEqualTo("location::get::1234");
112+
Value<?> bodyValue = resultLog.getBodyValue();
113+
assertThat(bodyValue).isNotNull();
114+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
115+
assertThat(bodyValue.asString()).isEqualTo("location::get::1234");
108116
}
109117

110118
@Test
@@ -124,7 +132,10 @@ void simpleRenameLogWithMissingKeysTest() {
124132
// verify that resulting logs are filtered in the way we want
125133
List<LogRecordData> result = mockExporter.getLogs();
126134
LogRecordData resultLog = result.get(0);
127-
assertThat(resultLog.getBody().asString()).isEqualTo("location::get::1234");
135+
Value<?> bodyValue = resultLog.getBodyValue();
136+
assertThat(bodyValue).isNotNull();
137+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
138+
assertThat(bodyValue.asString()).isEqualTo("location::get::1234");
128139
}
129140

130141
@Test
@@ -169,7 +180,10 @@ void simpleToAttributesTest() {
169180
Objects.requireNonNull(
170181
resultLog.getAttributes().get(AttributeKey.stringKey("documentId"))))
171182
.isEqualTo("12345678");
172-
assertThat(resultLog.getBody().asString()).isEqualTo("/api/v1/document/{documentId}/update");
183+
Value<?> bodyValue = resultLog.getBodyValue();
184+
assertThat(bodyValue).isNotNull();
185+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
186+
assertThat(bodyValue.asString()).isEqualTo("/api/v1/document/{documentId}/update");
173187
}
174188

175189
@Test
@@ -228,7 +242,10 @@ void multiRuleToAttributesTest() {
228242
Objects.requireNonNull(
229243
resultA.getAttributes().get(AttributeKey.stringKey("password2"))))
230244
.isEqualTo("555"); // The first match is taken to populate the attribute
231-
assertThat(resultA.getBody().asString())
245+
Value<?> bodyValueA = resultA.getBodyValue();
246+
assertThat(bodyValueA).isNotNull();
247+
assertThat(bodyValueA.getType()).isEqualTo(ValueType.STRING);
248+
assertThat(bodyValueA.asString())
232249
.isEqualTo("yyyPassword={password1} aba Pass={password2} xyx Pass={password2} zzz");
233250
assertThat(
234251
Objects.requireNonNull(
@@ -238,7 +255,10 @@ void multiRuleToAttributesTest() {
238255
Objects.requireNonNull(
239256
resultB.getAttributes().get(AttributeKey.stringKey("password1"))))
240257
.isEqualTo("****");
241-
assertThat(resultB.getBody().asString()).isEqualTo("yyyPassword={password1} aba");
258+
Value<?> bodyValueB = resultB.getBodyValue();
259+
assertThat(bodyValueB).isNotNull();
260+
assertThat(bodyValueB.getType()).isEqualTo(ValueType.STRING);
261+
assertThat(bodyValueB.asString()).isEqualTo("yyyPassword={password1} aba");
242262
}
243263

244264
@Test
@@ -264,7 +284,10 @@ void multiMatch() {
264284
List<LogRecordData> result = mockExporter.getLogs();
265285
LogRecordData resultA = result.get(0);
266286

267-
assertThat(resultA.getBody().asString()).isEqualTo("yyyPassword={x} aba Password={x} xyx");
287+
Value<?> bodyValueA = resultA.getBodyValue();
288+
assertThat(bodyValueA).isNotNull();
289+
assertThat(bodyValueA.getType()).isEqualTo(ValueType.STRING);
290+
assertThat(bodyValueA.asString()).isEqualTo("yyyPassword={x} aba Password={x} xyx");
268291
}
269292

270293
@Test
@@ -292,6 +315,9 @@ void simpleRenameLogTestWithLogProcessor() {
292315
// verify that resulting logs are not modified
293316
List<LogRecordData> result = mockExporter.getLogs();
294317
LogRecordData resultLog = result.get(0);
295-
assertThat(resultLog.getBody().asString()).isEqualTo("locationget1234");
318+
Value<?> bodyValue = resultLog.getBodyValue();
319+
assertThat(bodyValue).isNotNull();
320+
assertThat(bodyValue.getType()).isEqualTo(ValueType.STRING);
321+
assertThat(bodyValue.asString()).isEqualTo("locationget1234");
296322
}
297323
}

0 commit comments

Comments
 (0)