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

com.signalfx.metrics.connection.StoredDataPointReceiver Maven / Gradle / Ivy

package com.signalfx.metrics.connection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.signalfx.shaded.apache.commons.lang3.tuple.Pair;

import com.signalfx.metrics.SignalFxMetricsException;
import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers;
import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.Dimension;

/**
 * Factory that just stores results to later be tested.
 *
 * @author jack
 */
public class StoredDataPointReceiver implements DataPointReceiver {
    public final List addDataPoints;
    private final Map, List> pointsFor;
    public final Map registeredMetrics;
    public boolean throwOnAdd = false;

    public StoredDataPointReceiver() {
        addDataPoints = Collections.synchronizedList(new ArrayList<>());
        registeredMetrics = Collections.synchronizedMap(new HashMap<>());

        pointsFor = new HashMap<>();
    }

    @Override
    public void addDataPoints(String auth, List dataPoints)
            throws SignalFxMetricsException {
        if (throwOnAdd) {
            throw new SignalFxMetricsException("Flag set to true");
        }
        addDataPoints.addAll(dataPoints);
        for (SignalFxProtocolBuffers.DataPoint dp: dataPoints) {
            String source = dp.getSource();
            if ("".equals(source)) {
                source = findSfSourceDim(dp.getDimensionsList());
            }
            Pair key = Pair.of(source, dp.getMetric());
            if (pointsFor.containsKey(key)) {
                pointsFor.get(key).add(dp.getValue());
            } else {
                pointsFor.put(key, new ArrayList<>(Collections.singletonList(dp.getValue())));
            }
        }
    }

    private String findSfSourceDim(List dimensionsList) {
        for (Dimension dim: dimensionsList) {
            if ("sf_source".equals(dim.getKey())) {
                return dim.getValue();
            }
        }
        return "";
    }

    @Override
    public void backfillDataPoints(String auth, String metric, String metricType, String orgId, Map dimensions,
                                   List datumPoints)
            throws SignalFxMetricsException {}

    @Override
    public Map registerMetrics(String auth,
                                Map metricTypes)
            throws SignalFxMetricsException {
        registeredMetrics.putAll(metricTypes);
        Map ret = new HashMap();
        for (Map.Entry i: metricTypes.entrySet()) {
            ret.put(i.getKey(), true);
        }
        return ret;
    }

    public List valuesFor(String source, String metric) {
        Pair key = Pair.of(source, metric);
        List ret = pointsFor.get(key);
        if (ret == null) {
            return Collections.emptyList();
        } else {
            return Collections.unmodifiableList(ret);
        }
    }

    public SignalFxProtocolBuffers.Datum lastValueFor(String source, String metric) {
        List vals = valuesFor(source, metric);
        if (vals.isEmpty()) {
            throw new RuntimeException("No value for source/metric");
        } else {
            return vals.get(vals.size() - 1);
        }
    }

    public boolean clearValues(String source, String metric) {
        Pair key = Pair.of(source, metric);
        return pointsFor.remove(key) != null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy