Skip to content

Commit 0465fb5

Browse files
committed
add attributes to LogRecordBuilder instead of AttributesBuilder
1 parent 7baa040 commit 0465fb5

File tree

5 files changed

+230
-276
lines changed
  • instrumentation
    • jboss-logmanager/jboss-logmanager-appender-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jbosslogmanager/appender/v1_1
    • log4j/log4j-appender-2.17/library/src
    • logback/logback-appender-1.0/library/src

5 files changed

+230
-276
lines changed

instrumentation/jboss-logmanager/jboss-logmanager-appender-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jbosslogmanager/appender/v1_1/LoggingEventMapper.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1010

1111
import io.opentelemetry.api.GlobalOpenTelemetry;
12-
import io.opentelemetry.api.common.AttributeKey;
1312
import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder;
1413
import io.opentelemetry.api.logs.LogRecordBuilder;
1514
import io.opentelemetry.api.logs.Severity;
1615
import io.opentelemetry.context.Context;
17-
import io.opentelemetry.instrumentation.api.internal.cache.Cache;
1816
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
1917
import io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes;
2018
import java.util.List;
@@ -30,7 +28,6 @@ public final class LoggingEventMapper {
3028

3129
// copied from EventIncubatingAttributes
3230
private static final String EVENT_NAME = "event.name";
33-
private static final Cache<String, AttributeKey<String>> mdcAttributeKeys = Cache.bounded(100);
3431

3532
private final List<String> captureMdcAttributes;
3633

@@ -106,27 +103,26 @@ private void captureMdcAttributes(LogRecordBuilder builder) {
106103
if (captureAllMdcAttributes) {
107104
if (context != null) {
108105
for (Map.Entry<String, String> entry : context.entrySet()) {
109-
if (captureEventName && entry.getKey().equals(EVENT_NAME)) {
110-
builder.setEventName(entry.getValue());
111-
} else {
112-
builder.setAttribute(
113-
getMdcAttributeKey(String.valueOf(entry.getKey())), String.valueOf(entry.getValue()));
114-
}
106+
setAttributeMaybeEventName(builder, entry.getKey(), String.valueOf(entry.getValue()));
115107
}
116108
}
117109
return;
118110
}
119111

120112
for (String key : captureMdcAttributes) {
121113
Object value = context.get(key);
122-
if (value != null) {
123-
builder.setAttribute(key, value.toString());
124-
}
114+
setAttributeMaybeEventName(builder, key, value.toString());
125115
}
126116
}
127117

128-
public static AttributeKey<String> getMdcAttributeKey(String key) {
129-
return mdcAttributeKeys.computeIfAbsent(key, AttributeKey::stringKey);
118+
private void setAttributeMaybeEventName(LogRecordBuilder builder, String key, String value) {
119+
if (value != null) {
120+
if (captureEventName && key.equals(EVENT_NAME)) {
121+
builder.setEventName(value);
122+
} else {
123+
builder.setAttribute(key, value);
124+
}
125+
}
130126
}
131127

132128
private static Severity levelToSeverity(java.util.logging.Level level) {

instrumentation/log4j/log4j-appender-2.17/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_17/internal/LogEventMapper.java

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
package io.opentelemetry.instrumentation.log4j.appender.v2_17.internal;
77

88
import io.opentelemetry.api.common.AttributeKey;
9-
import io.opentelemetry.api.common.Attributes;
10-
import io.opentelemetry.api.common.AttributesBuilder;
119
import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder;
1210
import io.opentelemetry.api.logs.LogRecordBuilder;
1311
import io.opentelemetry.api.logs.Severity;
@@ -43,12 +41,10 @@ public final class LogEventMapper<T> {
4341
private static final AttributeKey<Long> THREAD_ID = AttributeKey.longKey("thread.id");
4442
private static final AttributeKey<String> THREAD_NAME = AttributeKey.stringKey("thread.name");
4543
// copied from EventIncubatingAttributes
46-
private static final AttributeKey<String> EVENT_NAME = AttributeKey.stringKey("event.name");
44+
private static final String EVENT_NAME = "event.name";
4745

4846
private static final String SPECIAL_MAP_MESSAGE_ATTRIBUTE = "message";
4947

50-
private static final Cache<String, AttributeKey<String>> contextDataAttributeKeyCache =
51-
Cache.bounded(100);
5248
private static final Cache<String, AttributeKey<String>> mapMessageAttributeKeyCache =
5349
Cache.bounded(100);
5450

@@ -106,14 +102,12 @@ public void mapLogEvent(
106102
Supplier<StackTraceElement> sourceSupplier,
107103
Context context) {
108104

109-
AttributesBuilder attributes = Attributes.builder();
110-
111-
captureMessage(builder, attributes, message);
105+
captureMessage(builder, message);
112106

113107
if (captureMarkerAttribute) {
114108
if (marker != null) {
115109
String markerName = marker.getName();
116-
attributes.put(LOG_MARKER, markerName);
110+
builder.setAttribute(LOG_MARKER, markerName);
117111
}
118112
}
119113

@@ -123,14 +117,14 @@ public void mapLogEvent(
123117
}
124118

125119
if (throwable != null) {
126-
setThrowable(builder, attributes, throwable);
120+
setThrowable(builder, throwable);
127121
}
128122

129-
captureContextDataAttributes(attributes, contextData);
123+
captureContextDataAttributes(builder, contextData);
130124

131125
if (captureExperimentalAttributes) {
132-
attributes.put(THREAD_NAME, threadName);
133-
attributes.put(THREAD_ID, threadId);
126+
builder.setAttribute(THREAD_NAME, threadName);
127+
builder.setAttribute(THREAD_ID, threadId);
134128
}
135129

136130
if (captureCodeAttributes) {
@@ -139,55 +133,39 @@ public void mapLogEvent(
139133
String fileName = source.getFileName();
140134
if (fileName != null) {
141135
if (SemconvStability.isEmitStableCodeSemconv()) {
142-
attributes.put(CodeAttributes.CODE_FILE_PATH, fileName);
136+
builder.setAttribute(CodeAttributes.CODE_FILE_PATH, fileName);
143137
}
144138
if (SemconvStability.isEmitOldCodeSemconv()) {
145-
attributes.put(CODE_FILEPATH, fileName);
139+
builder.setAttribute(CODE_FILEPATH, fileName);
146140
}
147141
}
148142
if (SemconvStability.isEmitStableCodeSemconv()) {
149-
attributes.put(
143+
builder.setAttribute(
150144
CodeAttributes.CODE_FUNCTION_NAME,
151145
source.getClassName() + "." + source.getMethodName());
152146
}
153147
if (SemconvStability.isEmitOldCodeSemconv()) {
154-
attributes.put(CODE_NAMESPACE, source.getClassName());
155-
attributes.put(CODE_FUNCTION, source.getMethodName());
148+
builder.setAttribute(CODE_NAMESPACE, source.getClassName());
149+
builder.setAttribute(CODE_FUNCTION, source.getMethodName());
156150
}
157151

158152
int lineNumber = source.getLineNumber();
159153
if (lineNumber > 0) {
160154
if (SemconvStability.isEmitStableCodeSemconv()) {
161-
attributes.put(CodeAttributes.CODE_LINE_NUMBER, lineNumber);
155+
builder.setAttribute(CodeAttributes.CODE_LINE_NUMBER, (long) lineNumber);
162156
}
163157
if (SemconvStability.isEmitOldCodeSemconv()) {
164-
attributes.put(CODE_LINENO, lineNumber);
158+
builder.setAttribute(CODE_LINENO, (long) lineNumber);
165159
}
166160
}
167161
}
168162
}
169163

170-
Attributes realizedAttributes = attributes.build();
171-
if (captureEventName) {
172-
realizedAttributes.forEach(
173-
(attributeKey, value) -> {
174-
if (attributeKey.equals(EVENT_NAME)) {
175-
builder.setEventName(String.valueOf(value));
176-
} else {
177-
@SuppressWarnings("unchecked")
178-
AttributeKey<Object> attributeKeyAsObject = (AttributeKey<Object>) attributeKey;
179-
builder.setAttribute(attributeKeyAsObject, value);
180-
}
181-
});
182-
} else {
183-
builder.setAllAttributes(realizedAttributes);
184-
}
185-
186164
builder.setContext(context);
187165
}
188166

189167
// visible for testing
190-
void captureMessage(LogRecordBuilder builder, AttributesBuilder attributes, Message message) {
168+
void captureMessage(LogRecordBuilder builder, Message message) {
191169
if (message == null) {
192170
return;
193171
}
@@ -216,37 +194,37 @@ void captureMessage(LogRecordBuilder builder, AttributesBuilder attributes, Mess
216194
(key, value) -> {
217195
if (value != null
218196
&& (!checkSpecialMapMessageAttribute
219-
|| !key.equals(SPECIAL_MAP_MESSAGE_ATTRIBUTE))) {
220-
attributes.put(getMapMessageAttributeKey(key), value.toString());
197+
|| !key.equals(SPECIAL_MAP_MESSAGE_ATTRIBUTE))) {
198+
builder.setAttribute(getMapMessageAttributeKey(key), value.toString());
221199
}
222200
});
223201
}
224202
}
225203

226204
// visible for testing
227-
void captureContextDataAttributes(AttributesBuilder attributes, T contextData) {
205+
void captureContextDataAttributes(LogRecordBuilder builder, T contextData) {
228206

229207
if (captureAllContextDataAttributes) {
230208
contextDataAccessor.forEach(
231209
contextData,
232-
(key, value) -> {
233-
if (value != null) {
234-
attributes.put(getContextDataAttributeKey(key), value);
235-
}
236-
});
210+
(key, value) -> setAttributeMaybeEventName(builder, key, value));
237211
return;
238212
}
239213

240214
for (String key : captureContextDataAttributes) {
241215
String value = contextDataAccessor.getValue(contextData, key);
242-
if (value != null) {
243-
attributes.put(getContextDataAttributeKey(key), value);
244-
}
216+
setAttributeMaybeEventName(builder, key, value);
245217
}
246218
}
247219

248-
public static AttributeKey<String> getContextDataAttributeKey(String key) {
249-
return contextDataAttributeKeyCache.computeIfAbsent(key, AttributeKey::stringKey);
220+
private void setAttributeMaybeEventName(LogRecordBuilder builder, String key, String value) {
221+
if (value != null) {
222+
if (captureEventName && key.equals(EVENT_NAME)) {
223+
builder.setEventName(value);
224+
} else {
225+
builder.setAttribute(key, value);
226+
}
227+
}
250228
}
251229

252230
public static AttributeKey<String> getMapMessageAttributeKey(String key) {
@@ -255,15 +233,15 @@ public static AttributeKey<String> getMapMessageAttributeKey(String key) {
255233
}
256234

257235
private static void setThrowable(
258-
LogRecordBuilder builder, AttributesBuilder attributes, Throwable throwable) {
236+
LogRecordBuilder builder, Throwable throwable) {
259237
if (builder instanceof ExtendedLogRecordBuilder) {
260238
((ExtendedLogRecordBuilder) builder).setException(throwable);
261239
} else {
262-
attributes.put(ExceptionAttributes.EXCEPTION_TYPE, throwable.getClass().getName());
263-
attributes.put(ExceptionAttributes.EXCEPTION_MESSAGE, throwable.getMessage());
240+
builder.setAttribute(ExceptionAttributes.EXCEPTION_TYPE, throwable.getClass().getName());
241+
builder.setAttribute(ExceptionAttributes.EXCEPTION_MESSAGE, throwable.getMessage());
264242
StringWriter writer = new StringWriter();
265243
throwable.printStackTrace(new PrintWriter(writer));
266-
attributes.put(ExceptionAttributes.EXCEPTION_STACKTRACE, writer.toString());
244+
builder.setAttribute(ExceptionAttributes.EXCEPTION_STACKTRACE, writer.toString());
267245
}
268246
}
269247

0 commit comments

Comments
 (0)