1010
1111import io .opentelemetry .api .GlobalOpenTelemetry ;
1212import io .opentelemetry .api .common .AttributeKey ;
13- import io .opentelemetry .api .common .Attributes ;
14- import io .opentelemetry .api .common .AttributesBuilder ;
1513import io .opentelemetry .api .incubator .logs .ExtendedLogRecordBuilder ;
1614import io .opentelemetry .api .logs .LogRecordBuilder ;
1715import io .opentelemetry .api .logs .Severity ;
@@ -31,7 +29,7 @@ public final class LoggingEventMapper {
3129 public static final LoggingEventMapper INSTANCE = new LoggingEventMapper ();
3230
3331 // copied from EventIncubatingAttributes
34- private static final AttributeKey < String > EVENT_NAME = AttributeKey . stringKey ( "event.name" ) ;
32+ private static final String EVENT_NAME = "event.name" ;
3533 private static final Cache <String , AttributeKey <String >> mdcAttributeKeys = Cache .bounded (100 );
3634
3735 private final List <String > captureMdcAttributes ;
@@ -82,35 +80,17 @@ public void capture(Logger logger, ExtLogRecord record) {
8280 builder .setSeverityText (level .toString ());
8381 }
8482
85- AttributesBuilder attributes = Attributes .builder ();
86-
8783 Throwable throwable = record .getThrown ();
8884 if (throwable != null ) {
8985 // this cast is safe within java agent instrumentation
9086 ((ExtendedLogRecordBuilder ) builder ).setException (throwable );
9187 }
92- captureMdcAttributes (attributes );
88+ captureMdcAttributes (builder );
9389
9490 if (captureExperimentalAttributes ) {
9591 Thread currentThread = Thread .currentThread ();
96- attributes .put (ThreadIncubatingAttributes .THREAD_NAME , currentThread .getName ());
97- attributes .put (ThreadIncubatingAttributes .THREAD_ID , currentThread .getId ());
98- }
99-
100- Attributes realizedAttributes = attributes .build ();
101- if (captureEventName ) {
102- realizedAttributes .forEach (
103- (attributeKey , value ) -> {
104- if (attributeKey .equals (EVENT_NAME )) {
105- builder .setEventName (String .valueOf (value ));
106- } else {
107- @ SuppressWarnings ("unchecked" )
108- AttributeKey <Object > attributeKeyAsObject = (AttributeKey <Object >) attributeKey ;
109- builder .setAttribute (attributeKeyAsObject , value );
110- }
111- });
112- } else {
113- builder .setAllAttributes (realizedAttributes );
92+ builder .setAttribute (ThreadIncubatingAttributes .THREAD_NAME , currentThread .getName ());
93+ builder .setAttribute (ThreadIncubatingAttributes .THREAD_ID , currentThread .getId ());
11494 }
11595
11696 builder .setContext (Context .current ());
@@ -119,15 +99,19 @@ public void capture(Logger logger, ExtLogRecord record) {
11999 builder .emit ();
120100 }
121101
122- private void captureMdcAttributes (AttributesBuilder attributes ) {
102+ private void captureMdcAttributes (LogRecordBuilder builder ) {
123103
124104 Map <String , String > context = MDC .copy ();
125105
126106 if (captureAllMdcAttributes ) {
127107 if (context != null ) {
128108 for (Map .Entry <String , String > entry : context .entrySet ()) {
129- attributes .put (
130- getMdcAttributeKey (String .valueOf (entry .getKey ())), String .valueOf (entry .getValue ()));
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+ }
131115 }
132116 }
133117 return ;
@@ -136,7 +120,7 @@ private void captureMdcAttributes(AttributesBuilder attributes) {
136120 for (String key : captureMdcAttributes ) {
137121 Object value = context .get (key );
138122 if (value != null ) {
139- attributes . put (key , value .toString ());
123+ builder . setAttribute (key , value .toString ());
140124 }
141125 }
142126 }
0 commit comments