66package io .opentelemetry .instrumentation .log4j .appender .v2_17 .internal ;
77
88import io .opentelemetry .api .common .AttributeKey ;
9- import io .opentelemetry .api .common .Attributes ;
10- import io .opentelemetry .api .common .AttributesBuilder ;
119import io .opentelemetry .api .incubator .logs .ExtendedLogRecordBuilder ;
1210import io .opentelemetry .api .logs .LogRecordBuilder ;
1311import 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