Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-53151

withContext not working as expected in combination with consoleLogFilter

      I'm trying to make a custom ConsoleLogFilter in my pipeline script, however for some reason it breaks the echo functionality.

       

      Here's my code:

      final class TestStream extends OutputStream {  
        private final OutputStream delegate;  
        TestStream(OutputStream delegate) {
          this.delegate = delegate
        }  
        public void write(int b) throws IOException {
          delegate.write(b);
        }  
        public void write(byte[] b) throws IOException {
          delegate.write(b);
        }  
        public void write(byte[] b, int off, int len) throws IOException {
          delegate.write(b, off, len);
        }  
        public void flush() throws IOException {
          delegate.flush();
        }  
         public void close() throws IOException {
          delegate.close();
        }
      }
      class TestFilter extends ConsoleLogFilter  {     
          @Override
          public OutputStream decorateLogger(AbstractBuild build, OutputStream logger)
                  throws IOException, InterruptedException {
              if (logger == null) {
                  return null;
              }
           
              return new TestStream(logger)
          }
      }
      ConsoleLogFilter subsequent = new TestFilter();
      ConsoleLogFilter original = getContext(ConsoleLogFilter.class);
      println original //prints null
      timestamps {
          original = getContext(ConsoleLogFilter.class);
          println original //prints  TimestampNotesConsoleLogFilter object
          echo "Out of Context" //Visible
          withContext(BodyInvoker.mergeConsoleLogFilters(original, subsequent)) {
              echo "In context " //Not visible
          }
          withContext(new TestFilter()) {
              echo "In context " //Not visible
          }
      }
      

      Here is the output:

      Started by user jenkins
      Running in Durability level: PERFORMANCE_OPTIMIZED
      [Pipeline] getContext
      [Pipeline] echo
      null
      [Pipeline] timestamps
      [Pipeline] {
      [Pipeline] getContext
      [Pipeline] echo
      07:22:58 hudson.plugins.timestamper.pipeline.TimestamperStep$TimestampNotesConsoleLogFilter@1d678d7a
      [Pipeline] echo
      07:22:58 Out of Context
      [Pipeline] withContext
      [Pipeline] {
      [Pipeline] echo
      [Pipeline] }
      [Pipeline] // withContext
      [Pipeline] withContext
      [Pipeline] {
      [Pipeline] echo
      [Pipeline] }
      [Pipeline] // withContext
      [Pipeline] }
      [Pipeline] // timestamps
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

       

      The 'in context' print is never written to the outputstream

          [JENKINS-53151] withContext not working as expected in combination with consoleLogFilter

          roel postelmans created issue -
          roel postelmans made changes -
          Description Original: I'm trying to make a custom ConsoleLogFilter in my pipeline script, however for some reason it breaks the echo functionality.

           

          Here's my code:
          {code:java}

          final class TestStream extends OutputStream { private final OutputStream delegate; TestStream(OutputStream delegate) {
              this.delegate = delegate
            } public void write(int b) throws IOException {
              delegate.write(b);
            } public void write(byte[] b) throws IOException {
              delegate.write(b);
            }
            
            public void write(byte[] b, int off, int len) throws IOException {
              delegate.write(b, off, len);
            } public void flush() throws IOException {
              delegate.flush();
            } public void close() throws IOException {
              delegate.close();
            }
          }
          class TestFilter extends ConsoleLogFilter { @Override
              public OutputStream decorateLogger(AbstractBuild build, OutputStream logger)
                      throws IOException, InterruptedException {
                  if (logger == null) {
                      return null;
                  }
               
                  return new TestStream(logger)
              }
          }ConsoleLogFilter subsequent = new TestFilter();
          ConsoleLogFilter original = getContext(ConsoleLogFilter.class);
          println original //prints null
          timestamps {
              original = getContext(ConsoleLogFilter.class);
              println original //prints TimestampNotesConsoleLogFilter object
              echo "Out of Context" //Visible
              withContext(BodyInvoker.mergeConsoleLogFilters(original, subsequent)) {
                  echo "In context " //Not visible
              }
              withContext(new TestFilter()) {
                  echo "In context " //Not visible
              }
          }
          {code}
          Here is the output:
          {code:java}
          Started by user jenkins
          Running in Durability level: PERFORMANCE_OPTIMIZED
          [Pipeline] getContext
          [Pipeline] echo
          null
          [Pipeline] timestamps
          [Pipeline] {
          [Pipeline] getContext
          [Pipeline] echo
          07:22:58 hudson.plugins.timestamper.pipeline.TimestamperStep$TimestampNotesConsoleLogFilter@1d678d7a
          [Pipeline] echo
          07:22:58 Out of Context
          [Pipeline] withContext
          [Pipeline] {
          [Pipeline] echo
          [Pipeline] }
          [Pipeline] // withContext
          [Pipeline] withContext
          [Pipeline] {
          [Pipeline] echo
          [Pipeline] }
          [Pipeline] // withContext
          [Pipeline] }
          [Pipeline] // timestamps
          [Pipeline] End of Pipeline
          Finished: SUCCESS
          {code}
           

          The 'in context' print is never written to the outputstream
          New: I'm trying to make a custom ConsoleLogFilter in my pipeline script, however for some reason it breaks the echo functionality.

           

          Here's my code:
          {code:java}
          final class TestStream extends OutputStream {
            private final OutputStream delegate;
            TestStream(OutputStream delegate) {
              this.delegate = delegate
            }
            public void write(int b) throws IOException {
              delegate.write(b);
            }
            public void write(byte[] b) throws IOException {
              delegate.write(b);
            }
            public void write(byte[] b, int off, int len) throws IOException {
              delegate.write(b, off, len);
            }
            public void flush() throws IOException {
              delegate.flush();
            }
             public void close() throws IOException {
              delegate.close();
            }
          }
          class TestFilter extends ConsoleLogFilter {
              @Override
              public OutputStream decorateLogger(AbstractBuild build, OutputStream logger)
                      throws IOException, InterruptedException {
                  if (logger == null) {
                      return null;
                  }
               
                  return new TestStream(logger)
              }
          }
          ConsoleLogFilter subsequent = new TestFilter();
          ConsoleLogFilter original = getContext(ConsoleLogFilter.class);
          println original //prints null
          timestamps {
              original = getContext(ConsoleLogFilter.class);
              println original //prints TimestampNotesConsoleLogFilter object
              echo "Out of Context" //Visible
              withContext(BodyInvoker.mergeConsoleLogFilters(original, subsequent)) {
                  echo "In context " //Not visible
              }
              withContext(new TestFilter()) {
                  echo "In context " //Not visible
              }
          }
          {code}
          Here is the output:
          {code:java}
          Started by user jenkins
          Running in Durability level: PERFORMANCE_OPTIMIZED
          [Pipeline] getContext
          [Pipeline] echo
          null
          [Pipeline] timestamps
          [Pipeline] {
          [Pipeline] getContext
          [Pipeline] echo
          07:22:58 hudson.plugins.timestamper.pipeline.TimestamperStep$TimestampNotesConsoleLogFilter@1d678d7a
          [Pipeline] echo
          07:22:58 Out of Context
          [Pipeline] withContext
          [Pipeline] {
          [Pipeline] echo
          [Pipeline] }
          [Pipeline] // withContext
          [Pipeline] withContext
          [Pipeline] {
          [Pipeline] echo
          [Pipeline] }
          [Pipeline] // withContext
          [Pipeline] }
          [Pipeline] // timestamps
          [Pipeline] End of Pipeline
          Finished: SUCCESS
          {code}
           

          The 'in context' print is never written to the outputstream
          Andrew Bayer made changes -
          Component/s New: workflow-basic-steps-plugin [ 21712 ]
          Component/s Original: pipeline [ 21692 ]
          Component/s Original: pipeline-model-definition-plugin [ 21706 ]
          Assignee Original: Andrew Bayer [ abayer ]
          Vivek Pandey made changes -
          Labels Original: console consoleoutput jenkins pipeline New: complex-cps-code console consoleoutput jenkins pipeline triaged-2018-11

            Unassigned Unassigned
            roel0 roel postelmans
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: