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

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

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

import com.google.common.base.Preconditions;

import java.util.List;
import java.util.function.Supplier;

import org.apache.commons.lang.StringUtils;

import wavefront.report.Span;

/**
 * Span decoder that takes in data in the following format:
 *
 * [span name] [annotations] [timestamp] [duration|timestamp]
 *
 * @author [email protected]
 */
public class SpanDecoder implements ReportableEntityDecoder {

  private static final AbstractIngesterFormatter FORMAT = SpanIngesterFormatter.newBuilder().
      text(Span::setName).
      annotationList(Span::setAnnotations, x -> !StringUtils.isNumeric(x)).
      rawTimestamp(Span::setStartMillis).
      rawTimestamp(SpanDecoder::setDuration).
      build();

  private final Supplier hostNameSupplier;

  public SpanDecoder(String hostName) {
    this(() -> hostName);
  }

  public SpanDecoder(Supplier hostNameSupplier) {
    Preconditions.checkNotNull(hostNameSupplier);
    this.hostNameSupplier = hostNameSupplier;
  }

  @Override
  public void decode(String msg, List out, String customerId, IngesterContext ctx) {
    Span span = FORMAT.drive(msg, hostNameSupplier, customerId, null, null, null, null, null, ctx);
    if (out != null) {
      out.add(span);
    }
  }

  private static void setDuration(Span span, Long durationTs) {
    Long startTs = span.getStartMillis();
    if (durationTs != null && startTs != null) {
      long duration = (durationTs - startTs >= 0) ? durationTs - startTs : durationTs;
      // convert both timestamps to millis
      if (startTs > 999999999999999999L) {
        // 19 digits == nanoseconds,
        span.setStartMillis(startTs / 1000_000);
        span.setDuration(duration / 1000_000);
      } else if (startTs > 999999999999999L) {
        // 16 digits == microseconds
        span.setStartMillis(startTs / 1000);
        span.setDuration(duration / 1000);
      } else if (startTs > 999999999999L) {
        // 13 digits == milliseconds
        span.setDuration(duration);
      } else {
        // seconds
        span.setStartMillis(startTs * 1000);
        span.setDuration(duration * 1000);
      }
    } else {
      throw new IllegalArgumentException("Both timestamp and duration expected");
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy