All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wavefront.agent.preprocessor.InteractivePreprocessorTester Maven / Gradle / Ivy

There is a newer version: 9999.0
Show newest version
package com.wavefront.agent.preprocessor;

import com.wavefront.agent.InteractiveTester;
import com.wavefront.agent.formatter.DataFormat;
import com.wavefront.agent.handlers.HandlerKey;
import com.wavefront.agent.handlers.ReportableEntityHandler;
import com.wavefront.agent.handlers.ReportableEntityHandlerFactory;
import com.wavefront.agent.listeners.WavefrontPortUnificationHandler;
import com.wavefront.agent.listeners.tracing.SpanUtils;
import com.wavefront.agent.listeners.tracing.TracePortUnificationHandler;
import com.wavefront.data.ReportableEntityType;
import com.wavefront.ingester.HistogramDecoder;
import com.wavefront.ingester.ReportPointDecoder;
import com.wavefront.ingester.ReportPointDecoderWrapper;
import com.wavefront.ingester.ReportPointSerializer;
import com.wavefront.ingester.ReportableEntityDecoder;
import com.wavefront.ingester.SpanDecoder;
import com.wavefront.ingester.SpanSerializer;
import wavefront.report.ReportPoint;
import wavefront.report.Span;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Scanner;
import java.util.function.Supplier;

/**
 * Interactive tester for preprocessor rules.
 *
 * @author [email protected]
 */
public class InteractivePreprocessorTester implements InteractiveTester {
  private static final SpanSerializer SPAN_SERIALIZER = new SpanSerializer();
  private static final ReportableEntityDecoder SPAN_DECODER =
      new SpanDecoder("unknown");

  private final Scanner stdin = new Scanner(System.in);
  private final Supplier preprocessorSupplier;
  private final ReportableEntityType entityType;
  private final String port;
  private final List customSourceTags;

  private final ReportableEntityHandlerFactory factory = new ReportableEntityHandlerFactory() {
    @SuppressWarnings("unchecked")
    @Override
    public  ReportableEntityHandler getHandler(HandlerKey handlerKey) {
      if (handlerKey.getEntityType() == ReportableEntityType.TRACE) {
        return (ReportableEntityHandler) new ReportableEntityHandler() {
          @Override
          public void report(Span reportSpan) {
            System.out.println(SPAN_SERIALIZER.apply(reportSpan));
          }

          @Override
          public void block(Span reportSpan) {
            System.out.println("Blocked: " + reportSpan);
          }

          @Override
          public void block(@Nullable Span reportSpan, @Nullable String message) {
            System.out.println("Blocked: " + SPAN_SERIALIZER.apply(reportSpan));
          }

          @Override
          public void reject(@Nullable Span reportSpan, @Nullable String message) {
            System.out.println("Rejected: " + SPAN_SERIALIZER.apply(reportSpan));
          }

          @Override
          public void reject(@Nonnull String t, @Nullable String message) {
            System.out.println("Rejected: " + t);
          }

          @Override
          public void shutdown() {
          }
        };
      }
      return (ReportableEntityHandler) new ReportableEntityHandler() {
        @Override
        public void report(ReportPoint reportPoint) {
          System.out.println(ReportPointSerializer.pointToString(reportPoint));
        }

        @Override
        public void block(ReportPoint reportPoint) {
          System.out.println("Blocked: " + ReportPointSerializer.pointToString(reportPoint));
        }

        @Override
        public void block(@Nullable ReportPoint reportPoint, @Nullable String message) {
          System.out.println("Blocked: " + ReportPointSerializer.pointToString(reportPoint));
        }

        @Override
        public void reject(@Nullable ReportPoint reportPoint, @Nullable String message) {
          System.out.println("Rejected: " + ReportPointSerializer.pointToString(reportPoint));
        }

        @Override
        public void reject(@Nonnull String t, @Nullable String message) {
          System.out.println("Rejected: " + t);
        }

        @Override
        public void shutdown() {
        }
      };
    }

    @Override
    public void shutdown(@Nonnull String handle) {
    }
  };

  /**
   * @param preprocessorSupplier supplier for {@link ReportableEntityPreprocessor}
   * @param entityType           entity type (to determine whether it's a span or a point)
   * @param port                 handler key
   * @param customSourceTags     list of custom source tags (for parsing)
   */
  public InteractivePreprocessorTester(Supplier preprocessorSupplier,
                                       ReportableEntityType entityType,
                                       String port,
                                       List customSourceTags) {
    this.preprocessorSupplier = preprocessorSupplier;
    this.entityType = entityType;
    this.port = port;
    this.customSourceTags = customSourceTags;
  }

  @Override
  public boolean interactiveTest() {
    String line = stdin.nextLine();
    if (entityType == ReportableEntityType.TRACE) {
      ReportableEntityHandler handler = factory.getHandler(entityType, port);
      SpanUtils.preprocessAndHandleSpan(line, SPAN_DECODER, handler,
          handler::report, preprocessorSupplier, null, x -> true);
    } else {
      ReportableEntityHandler handler = factory.getHandler(entityType, port);
      ReportableEntityDecoder decoder;
      if (DataFormat.autodetect(line) == DataFormat.HISTOGRAM) {
        decoder = new ReportPointDecoderWrapper(new HistogramDecoder());
      } else {
        decoder = new ReportPointDecoder(() -> "unknown", customSourceTags);
      }
      WavefrontPortUnificationHandler.preprocessAndHandlePoint(line, decoder, handler,
          preprocessorSupplier, null, "");
    }
    return stdin.hasNext();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy