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

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

The newest version!
package com.wavefront.ingester;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import wavefront.report.SpanLog;
import wavefront.report.SpanLogs;

/**
 * Span logs decoder that converts a JSON object in the following format:
 *
 * {
 *   "traceId": "...",
 *   "spanId": "...",
 *   "logs": [
 *     {
 *       "timestamp": 1234567890000000,
 *       "fields": {
 *         "key": "value",
 *         "key2": "value2"
 *       }
 *     }
 *   ]
 * }
 *
 * @author [email protected]
 */
public class SpanLogsDecoder implements ReportableEntityDecoder {

  private static final ObjectMapper JSON_PARSER = new ObjectMapper();

  public SpanLogsDecoder() {
  }

  @Override
  public void decode(JsonNode msg, List out, String customerId, IngesterContext ctx) {
    Iterable iterable = () -> msg.get("logs").elements();
    //noinspection unchecked
    SpanLogs spanLogs = SpanLogs.newBuilder().
        setCustomer(customerId).
        setTraceId(msg.get("traceId") == null ? null : msg.get("traceId").textValue()).
        setSpanId(msg.get("spanId") == null ? null : msg.get("spanId").textValue()).
        setSpanSecondaryId(msg.get("spanSecondaryId") == null ? (msg.get("_spanSecondaryId") == null ? null :
            msg.get("_spanSecondaryId").textValue()) :
            msg.get("spanSecondaryId").textValue()).
        setLogs(StreamSupport.stream(iterable.spliterator(), false).
            map(x -> SpanLog.newBuilder().
                setTimestamp(x.get("timestamp").asLong()).
                setFields(JSON_PARSER.convertValue(x.get("fields"), Map.class)).
                build()
            ).collect(Collectors.toList())).
        setSpan(msg.get("span") == null ? null : msg.get("span").textValue()).
        build();
    if (out != null) {
      out.add(spanLogs);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy