33
44package com .azure .monitor .opentelemetry .exporter .implementation ;
55
6+ import static io .opentelemetry .api .common .AttributeKey .stringKey ;
7+
68import com .azure .core .util .logging .ClientLogger ;
79import com .azure .monitor .opentelemetry .exporter .implementation .builders .AbstractTelemetryBuilder ;
810import com .azure .monitor .opentelemetry .exporter .implementation .builders .ExceptionTelemetryBuilder ;
@@ -25,16 +27,59 @@ public class LogDataMapper {
2527
2628 private static final ClientLogger logger = new ClientLogger (LogDataMapper .class );
2729
30+ private static final String LOG4J_MDC_PREFIX = "log4j.mdc." ; // log4j 1.2
31+ private static final String LOG4J_CONTEXT_DATA_PREFIX = "log4j.context_data." ; // log4j 2.x
32+ private static final String LOGBACK_MDC_PREFIX = "logback.mdc." ;
33+ private static final String JBOSS_LOGGING_MDC_PREFIX = "jboss-logmanager.mdc." ;
34+
35+ private static final AttributeKey <String > LOG4J_MARKER = stringKey ("log4j.marker" );
36+ private static final AttributeKey <String > LOGBACK_MARKER = stringKey ("logback.marker" );
37+
38+ private static final Mappings MAPPINGS ;
39+
40+ static {
41+ MappingsBuilder mappingsBuilder =
42+ new MappingsBuilder ()
43+ .prefix (
44+ LOG4J_MDC_PREFIX ,
45+ (telemetryBuilder , key , value ) -> {
46+ telemetryBuilder .addProperty (
47+ key .substring (LOG4J_MDC_PREFIX .length ()), String .valueOf (value ));
48+ })
49+ .prefix (
50+ LOG4J_CONTEXT_DATA_PREFIX ,
51+ (telemetryBuilder , key , value ) -> {
52+ telemetryBuilder .addProperty (
53+ key .substring (LOG4J_CONTEXT_DATA_PREFIX .length ()), String .valueOf (value ));
54+ })
55+ .prefix (
56+ LOGBACK_MDC_PREFIX ,
57+ (telemetryBuilder , key , value ) -> {
58+ telemetryBuilder .addProperty (
59+ key .substring (LOGBACK_MDC_PREFIX .length ()), String .valueOf (value ));
60+ })
61+ .prefix (
62+ JBOSS_LOGGING_MDC_PREFIX ,
63+ (telemetryBuilder , key , value ) -> {
64+ telemetryBuilder .addProperty (
65+ key .substring (JBOSS_LOGGING_MDC_PREFIX .length ()), String .valueOf (value ));
66+ })
67+ .exactString (SemanticAttributes .CODE_FILEPATH , "FileName" )
68+ .exactString (SemanticAttributes .CODE_NAMESPACE , "ClassName" )
69+ .exactString (SemanticAttributes .CODE_FUNCTION , "MethodName" )
70+ .exactLong (SemanticAttributes .CODE_LINENO , "LineNumber" )
71+ .exactString (LOG4J_MARKER , "Marker" )
72+ .exactString (LOGBACK_MARKER , "Marker" );
73+
74+ SpanDataMapper .applyCommonTags (mappingsBuilder );
75+
76+ MAPPINGS = mappingsBuilder .build ();
77+ }
78+
2879 private final boolean captureLoggingLevelAsCustomDimension ;
2980 private final boolean captureAzureFunctionsAttributes ;
3081 private final BiConsumer <AbstractTelemetryBuilder , Resource > telemetryInitializer ;
3182
32- private static final AttributeKey <String > OTEL_LOG4J_MARKER =
33- AttributeKey .stringKey ("log4j.marker" );
34-
35- private static final AttributeKey <String > OTEL_LOGBACK_MARKER =
36- AttributeKey .stringKey ("logback.marker" );
37-
3883 public LogDataMapper (
3984 boolean captureLoggingLevelAsCustomDimension ,
4085 boolean captureAzureFunctionsAttributes ,
@@ -64,7 +109,10 @@ private TelemetryItem createMessageTelemetryItem(LogRecordData log, @Nullable Lo
64109
65110 // update tags
66111 Attributes attributes = log .getAttributes ();
67- setExtraAttributes (telemetryBuilder , attributes );
112+ if (captureAzureFunctionsAttributes ) {
113+ setFunctionExtraTraceAttributes (telemetryBuilder , attributes );
114+ }
115+ MAPPINGS .map (attributes , telemetryBuilder );
68116
69117 telemetryBuilder .setSeverityLevel (toSeverityLevel (log .getSeverity ()));
70118 telemetryBuilder .setMessage (log .getBody ().asString ());
@@ -91,7 +139,7 @@ private TelemetryItem createExceptionTelemetryItem(
91139
92140 // update tags
93141 Attributes attributes = log .getAttributes ();
94- setExtraAttributes ( telemetryBuilder , attributes );
142+ MAPPINGS . map ( attributes , telemetryBuilder );
95143
96144 telemetryBuilder .setExceptions (Exceptions .minimalParse (stack ));
97145 telemetryBuilder .setSeverityLevel (toSeverityLevel (log .getSeverity ()));
@@ -143,79 +191,7 @@ private static void setItemCount(
143191 }
144192 }
145193
146- private static final String LOG4J1_2_MDC_PREFIX = "log4j.mdc." ;
147- private static final String LOG4J2_CONTEXT_DATA_PREFIX = "log4j.context_data." ;
148- private static final String LOGBACK_MDC_PREFIX = "logback.mdc." ;
149- private static final String JBOSS_LOGGING_MDC_PREFIX = "jboss-logmanager.mdc." ;
150-
151- private void setExtraAttributes (
152- AbstractTelemetryBuilder telemetryBuilder , Attributes attributes ) {
153- if (captureAzureFunctionsAttributes ) {
154- setFunctionExtraAttributes (telemetryBuilder , attributes );
155- }
156- attributes .forEach (
157- (attributeKey , value ) -> {
158- String key = attributeKey .getKey ();
159- if (key .startsWith (LOG4J2_CONTEXT_DATA_PREFIX )) {
160- telemetryBuilder .addProperty (
161- key .substring (LOG4J2_CONTEXT_DATA_PREFIX .length ()), String .valueOf (value ));
162- return ;
163- }
164- if (key .startsWith (LOGBACK_MDC_PREFIX )) {
165- telemetryBuilder .addProperty (
166- key .substring (LOGBACK_MDC_PREFIX .length ()), String .valueOf (value ));
167- return ;
168- }
169- if (SemanticAttributes .CODE_FILEPATH .getKey ().equals (key )) {
170- telemetryBuilder .addProperty ("FileName" , String .valueOf (value ));
171- return ;
172- }
173- if (SemanticAttributes .CODE_NAMESPACE .getKey ().equals (key )) {
174- telemetryBuilder .addProperty ("ClassName" , String .valueOf (value ));
175- return ;
176- }
177- if (SemanticAttributes .CODE_FUNCTION .getKey ().equals (key )) {
178- telemetryBuilder .addProperty ("MethodName" , String .valueOf (value ));
179- return ;
180- }
181- if (SemanticAttributes .CODE_LINENO .getKey ().equals (key )) {
182- telemetryBuilder .addProperty ("LineNumber" , String .valueOf (value ));
183- return ;
184- }
185- if (OTEL_LOG4J_MARKER .getKey ().equals (key ) || OTEL_LOGBACK_MARKER .getKey ().equals (key )) {
186- telemetryBuilder .addProperty ("Marker" , String .valueOf (value ));
187- return ;
188- }
189- if (key .startsWith (JBOSS_LOGGING_MDC_PREFIX )) {
190- telemetryBuilder .addProperty (
191- key .substring (JBOSS_LOGGING_MDC_PREFIX .length ()), String .valueOf (value ));
192- return ;
193- }
194- if (key .startsWith (LOG4J1_2_MDC_PREFIX )) {
195- telemetryBuilder .addProperty (
196- key .substring (LOG4J1_2_MDC_PREFIX .length ()), String .valueOf (value ));
197- return ;
198- }
199- if (SpanDataMapper .applyCommonTags (telemetryBuilder , key , value )) {
200- return ;
201- }
202- if (key .startsWith ("applicationinsights.internal." )) {
203- return ;
204- }
205- if (key .startsWith ("thread." )) {
206- return ;
207- }
208- if (key .startsWith ("exception." )) {
209- return ;
210- }
211- String val = SpanDataMapper .convertToString (value , attributeKey .getType ());
212- if (val != null ) {
213- telemetryBuilder .addProperty (attributeKey .getKey (), val );
214- }
215- });
216- }
217-
218- private static void setFunctionExtraAttributes (
194+ private static void setFunctionExtraTraceAttributes (
219195 AbstractTelemetryBuilder telemetryBuilder , Attributes attributes ) {
220196 String invocationId = attributes .get (AiSemanticAttributes .AZ_FN_INVOCATION_ID );
221197 if (invocationId != null ) {
0 commit comments