com.wavefront.ingester.PickleProtocolMetricDecoder Maven / Gradle / Ivy
package com.wavefront.ingester;
import com.google.common.base.Preconditions;
import com.wavefront.common.MetricMangler;
import net.razorvine.pickle.Unpickler;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import wavefront.report.ReportMetric;
/**
* Pickle protocol format decoder.
* https://docs.python.org/2/library/pickle.html
* @author Mike McLaughlin ([email protected])
*/
public class PickleProtocolMetricDecoder implements ReportableEntityDecoder {
protected static final Logger logger = Logger.getLogger(
PickleProtocolDecoder.class.getCanonicalName());
private final int port;
private final String defaultHostName;
private final List customSourceTags;
private final MetricMangler metricMangler;
private final ThreadLocal unpicklerThreadLocal = ThreadLocal.withInitial(
Unpickler::new);
/**
* Constructor.
* @param hostName the default host name.
* @param customSourceTags list of source tags for this host.
* @param mangler the metric mangler object.
* @param port the listening port (for debug logging)
*/
public PickleProtocolMetricDecoder(String hostName, List customSourceTags,
MetricMangler mangler, int port) {
Preconditions.checkNotNull(hostName);
this.defaultHostName = hostName;
Preconditions.checkNotNull(customSourceTags);
this.customSourceTags = customSourceTags;
this.metricMangler = mangler;
this.port = port;
}
@Override
public void decode(byte[] msg, List out, String customerId, IngesterContext ctx) {
InputStream is = new ByteArrayInputStream(msg);
Object dataRaw;
try {
dataRaw = unpicklerThreadLocal.get().load(is);
if (!(dataRaw instanceof List)) {
throw new IllegalArgumentException(
String.format("[%d] unable to unpickle data (unpickle did not return list)", port));
}
} catch (final IOException ioe) {
throw new IllegalArgumentException(String.format("[%d] unable to unpickle data", port), ioe);
}
// [(path, (timestamp, value)), ...]
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy