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

com.wavefront.ingester.EventIngesterFormatter Maven / Gradle / Ivy

There is a newer version: 2023-22.3
Show newest version
package com.wavefront.ingester;

import wavefront.report.ReportEvent;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
 * Ingestion formatter for events.
 *
 * @author [email protected].
 */
public class EventIngesterFormatter extends AbstractIngesterFormatter {

  private EventIngesterFormatter(List> elements) {
    super(elements);
  }

  public static class EventIngesterFormatBuilder extends IngesterFormatBuilder {
    @Override
    public EventIngesterFormatter build() {
      return new EventIngesterFormatter(elements);
    }
  }

  public static IngesterFormatBuilder newBuilder() {
    return new EventIngesterFormatBuilder();
  }

  @Override
  public ReportEvent drive(String input, Supplier defaultHostNameSupplier,
                           String customerId, @Nullable List customSourceTags,
                           @Nullable List customLogTimestampTags,
                           @Nullable List customLogMessageTags,
                           List customLogApplicationTags, List customLogServiceTags, @Nullable IngesterContext ingesterContext) {
    final ReportEvent event = new ReportEvent();
    StringParser parser = new StringParser(input);
    event.setHosts(new ArrayList<>());
    event.setAnnotations(new HashMap<>());

    for (FormatterElement element : elements) {
      element.consume(parser, event);
    }

    Iterator>> iter = event.getDimensions().entrySet().iterator();
    while (iter.hasNext()) {
      final Map.Entry> entry = iter.next();
      switch (entry.getKey()) {
        case "host":
          event.setHosts(entry.getValue());
          iter.remove();
          break;
        case "tag":
        case "eventTag":
          event.setTags(entry.getValue());
          iter.remove();
          break;
        default:
          // single-value dimensions should be moved to annotations
          if (entry.getValue().size() == 1) {
            event.getAnnotations().put(entry.getKey(), entry.getValue().get(0));
            iter.remove();
          }
      }
    }
    if (event.getDimensions().isEmpty()) {
      event.setDimensions(null);
    }
    // if no end time specified, we assume it's an instant event
    if (event.getEndTime() == 0 || event.getEndTime() <= event.getStartTime()) {
      event.setEndTime(event.getStartTime() + 1);
    }
    return event;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy