open-telemetry / opentelemetry-java-instrumentation

OpenTelemetry auto-instrumentation and instrumentation libraries for Java
https://opentelemetry.io
Apache License 2.0
1.96k stars 860 forks source link

Do we have Layout Pattern tag support for OpenTelemetryAppender in logback file? #11702

Open ff-amarkande opened 4 months ago

ff-amarkande commented 4 months ago

Discussed in https://github.com/open-telemetry/opentelemetry-java-instrumentation/discussions/11695

Originally posted by **ff-amarkande** June 28, 2024 Hi Team, Trying to format the log message with custom layout for example, refer following logback file ``` %date %level [%thread] %logger{36} [%file:%line] [requestId=%X{requestId}] %msg%n ``` Custom layout code - Created **CustomPatternLayout** class for formatting by **extending PatternLayout class** ``` defaultConverterMap.put( "msg", CustomMessageConverter.class.getName() ); ``` Code `public class CustomMessageConverter extends MessageConverter { /** * @see ch.qos.logback.core.pattern.Converter#convert(java.lang.Object) */ @Override public String convert( ILoggingEvent event ) { return super.convert( new LoggingEventWrapper( event ) ).replace( System.getProperty( "line.separator" ), "" ); } private static class LoggingEventWrapper implements ILoggingEvent { private ILoggingEvent event; private String formattedMessage = null; public LoggingEventWrapper( ILoggingEvent event ) { super(); this.event = event; } @Override public String getThreadName() { return event.getThreadName(); } @Override public Level getLevel() { return this.event.getLevel(); } @Override public String getMessage() { return this.event.getMessage(); } @Override public Object[] getArgumentArray() { return LoggingUtil.encloseInQuotes( event.getArgumentArray() ); } @Override public String getFormattedMessage() { if ( null != formattedMessage ) { return formattedMessage; } formattedMessage = event.getMessage(); if ( event.getArgumentArray() != null ) { if ( event.getArgumentArray()[0] instanceof LoggingEvents ) { LoggingEvents e = (LoggingEvents)event.getArgumentArray()[0]; formattedMessage = // method to format and create a custom log with key=value pair } else { formattedMessage = MessageFormatter.arrayFormat( event.getMessage(), LoggingUtil.encloseInQuotes( event.getArgumentArray() ) ).getMessage(); } } return formattedMessage; } //Removed other override methods }` However when tried with **OpenTelemetryAppender** , log message is not formatted as per the layout. ``` true true true true true * %date %level [%thread] %logger{36} [%file:%line] [requestId=%X{requestId}] %msg%n ``` Excepted `2024-06-28 12:59:51,360 INFO [localhost-startStop-1] EventLogger[SystemInfoProvider.java:54] [ requestId=Test-123] [eventChannel=CONFIGURATION, eventId=SYSTEM_INFO, message="Event to indicate system information", machineId=machine-id-1]` Actual `Event to indicate system information ` Following is custom message formed inside **getFormattedMessage method** `[eventChannel=CONFIGURATION, eventId=SYSTEM_INFO, message="Event to indicate system information", machineId=machine-id-1]`
laurit commented 4 months ago

This is not supported. Instead of having requestId etc. as part of the message isn't it more useful to capture it as an attribute.

ff-amarkande commented 4 months ago

Is support for adding layout pattern tag planned in near future? Also is there any other way how we can customise the log message using OpenTelemetryAppender?

laurit commented 4 months ago

Is support for adding layout pattern tag planned in near future?

I'm not aware of any plans to support this. For such features we rely on interested parties submitting pull requests.

ff-amarkande commented 4 months ago

Is there any document which captures the details on how to contribute(create branch, raise PR, all the prerequisite etc) so that I can create PR for the same?

laurit commented 4 months ago

Is there any document which captures the details on how to contribute(create branch, raise PR, all the prerequisite etc) so that I can create PR for the same?

There isn't, nothing special is needed just fork the project and submit a PR from your fork. When you submit the PR you'll have to sign the CLA, the github bot that verifies the CLA will add comment to your PR with the details.