com.signalfx.metrics.protobuf.SignalFxProtocolBuffers Java Examples

The following examples show how to use com.signalfx.metrics.protobuf.SignalFxProtocolBuffers. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SignalFxReporterTest.java    From signalfx-java with Apache License 2.0 6 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testUpdatingMetricTypeToIncompatibleValue() {
    reporter.getMetricMetadata()
            .forBuilder(IncrementalCounter.Builder.INSTANCE)
            .withSourceName(SOURCE_NAME)
            .withMetricName("name")
            .createOrGet(metricRegistry)
            .inc(3);
    reporter.getMetricMetadata()
            .forBuilder(IncrementalCounter.Builder.INSTANCE)
            .withSourceName(SOURCE_NAME)
            .withMetricName("name")
            .withMetricType(SignalFxProtocolBuffers.MetricType.GAUGE)
            .createOrGet(metricRegistry);
    fail("I expect an error if it's a gauge");
}
 
Example #2
Source File: SignalFxMeterRegistry.java    From micrometer with Apache License 2.0 6 votes vote down vote up
private Stream<SignalFxProtocolBuffers.DataPoint.Builder> addMeter(Meter meter) {
    return stream(meter.measure().spliterator(), false).flatMap(measurement -> {
        String statSuffix = NamingConvention.camelCase.tagKey(measurement.getStatistic().toString());
        switch (measurement.getStatistic()) {
            case TOTAL:
            case TOTAL_TIME:
            case COUNT:
            case DURATION:
                return Stream.of(addDatapoint(meter, COUNTER, statSuffix, measurement.getValue()));
            case MAX:
            case VALUE:
            case UNKNOWN:
            case ACTIVE_TASKS:
                return Stream.of(addDatapoint(meter, GAUGE, statSuffix, measurement.getValue()));
        }
        return Stream.empty();
    });
}
 
Example #3
Source File: SignalFxMeterRegistry.java    From micrometer with Apache License 2.0 6 votes vote down vote up
private SignalFxProtocolBuffers.DataPoint.Builder addDatapoint(Meter meter, SignalFxProtocolBuffers.MetricType metricType, @Nullable String statSuffix, Number value) {
    SignalFxProtocolBuffers.Datum.Builder datumBuilder = SignalFxProtocolBuffers.Datum.newBuilder();
    SignalFxProtocolBuffers.Datum datum = (value instanceof Double ?
            datumBuilder.setDoubleValue((Double) value) :
            datumBuilder.setIntValue(value.longValue())
    ).build();

    String metricName = config().namingConvention().name(statSuffix == null ? meter.getId().getName() : meter.getId().getName() + "." + statSuffix,
            meter.getId().getType(), meter.getId().getBaseUnit());

    SignalFxProtocolBuffers.DataPoint.Builder dataPointBuilder = SignalFxProtocolBuffers.DataPoint.newBuilder()
            .setMetric(metricName)
            .setMetricType(metricType)
            .setValue(datum);

    for (Tag tag : getConventionTags(meter.getId())) {
        dataPointBuilder.addDimensions(SignalFxProtocolBuffers.Dimension.newBuilder()
                .setKey(tag.getKey())
                .setValue(tag.getValue())
                .build());
    }

    return dataPointBuilder;
}
 
Example #4
Source File: AbstractHttpEventProtobufReceiverConnection.java    From signalfx-java with Apache License 2.0 6 votes vote down vote up
@Override
public void addEvents(String auth, List<SignalFxProtocolBuffers.Event> events)
        throws SignalFxMetricsException {
    if (events.isEmpty()) {
        return;
    }
    try {
        CloseableHttpResponse resp = null;
        try {
            resp = postToEndpoint(auth,
                    getEntityForVersion(events),
                    getEndpointForAddEvents(),
                    false);
            checkHttpResponse(resp);
        } finally {
            if (resp != null) {
                resp.close();
            }
        }
    } catch (IOException e) {
        throw new SignalFxMetricsException("Exception posting to addEvents", e);
    }
}
 
Example #5
Source File: StoredDataPointReceiver.java    From signalfx-java with Apache License 2.0 6 votes vote down vote up
@Override
public void addDataPoints(String auth, List<SignalFxProtocolBuffers.DataPoint> 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<String, String> key = Pair.of(source, dp.getMetric());
        if (pointsFor.containsKey(key)) {
            pointsFor.get(key).add(dp.getValue());
        } else {
            pointsFor.put(key, Lists.newArrayList(dp.getValue()));
        }
    }
}
 
Example #6
Source File: HttpDataPointProtobufReceiverConnectionTest.java    From signalfx-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testBackfill() throws Exception {
    Server server = new Server(0);
    server.setHandler(new BackfillHandler());
    server.start();
    URI uri = server.getURI();
    DataPointReceiver dpr = new HttpDataPointProtobufReceiverFactory(
            new SignalFxEndpoint(uri.getScheme(), uri.getHost(), uri.getPort()))
            .createDataPointReceiver();

    ArrayList<SignalFxProtocolBuffers.PointValue> values = new ArrayList<SignalFxProtocolBuffers.PointValue>(Arrays.asList(
            SignalFxProtocolBuffers.PointValue.newBuilder().setTimestamp(System.currentTimeMillis())
                    .setValue(SignalFxProtocolBuffers.Datum.newBuilder().setDoubleValue(123.0)).build()
    ));
    HashMap<String,String> dims = new HashMap<String,String>();
    dims.put("baz", "gorch");
    dims.put("moo", "cow");

    dpr.backfillDataPoints(AUTH_TOKEN, "foo.bar.baz", "counter", "ABC123", dims, values);
    server.stop();
}
 
Example #7
Source File: HttpDataPointProtobufReceiverConnectionTest.java    From signalfx-java with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request,
                   HttpServletResponse response) throws IOException, ServletException {
    if (!request.getHeader("X-SF-TOKEN").equals(AUTH_TOKEN)) {
        error("Invalid auth token", response, baseRequest);
        return;
    }
    if (!request.getHeader("User-Agent")
            .equals(AbstractHttpReceiverConnection.USER_AGENT)) {
        error("Invalid User agent: " + request.getHeader("User-Agent") + " vs "
                + AbstractHttpReceiverConnection.USER_AGENT, response, baseRequest);
        return;
    }
    SignalFxProtocolBuffers.DataPointUploadMessage all_datapoints =
            SignalFxProtocolBuffers.DataPointUploadMessage.parseFrom(
                    new GZIPInputStream(baseRequest.getInputStream()));
    if (!all_datapoints.getDatapoints(0).getSource().equals("source")) {
        error("Invalid datapoint source", response, baseRequest);
        return;
    }
    response.setStatus(HttpStatus.SC_OK);
    response.getWriter().write("\"OK\"");
    baseRequest.setHandled(true);
}
 
Example #8
Source File: AggregateMetricSenderSessionWrapper.java    From signalfx-java with Apache License 2.0 6 votes vote down vote up
/**
 * Add Metered metric
 * @param baseName
 * @param metered
 */

void addMetered(MetricName baseName, Metered metered) {
    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.COUNT,
            SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, metered.count());
    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.RATE_15_MIN,
            SignalFxProtocolBuffers.MetricType.GAUGE, metered.fifteenMinuteRate());
    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.RATE_1_MIN,
            SignalFxProtocolBuffers.MetricType.GAUGE, metered.oneMinuteRate());
    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.RATE_5_MIN,
            SignalFxProtocolBuffers.MetricType.GAUGE, metered.fiveMinuteRate());

    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.RATE_MEAN,
            SignalFxProtocolBuffers.MetricType.GAUGE, metered.meanRate());
}
 
Example #9
Source File: AggregateMetricSenderSessionWrapper.java    From signalfx-java with Apache License 2.0 6 votes vote down vote up
void addMetered(String baseName, Metered metered) {
    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.COUNT,
            SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, metered.getCount());
    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.RATE_15_MIN,
            SignalFxProtocolBuffers.MetricType.GAUGE, metered.getFifteenMinuteRate());
    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.RATE_1_MIN,
            SignalFxProtocolBuffers.MetricType.GAUGE, metered.getOneMinuteRate());
    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.RATE_5_MIN,
            SignalFxProtocolBuffers.MetricType.GAUGE, metered.getFiveMinuteRate());

    addMetric(metered, baseName,
            SignalFxReporter.MetricDetails.RATE_MEAN,
            SignalFxProtocolBuffers.MetricType.GAUGE, metered.getMeanRate());
}
 
Example #10
Source File: SignalFxReporterTest.java    From signalfx-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testReportingGaugeAsCummulativeCounter() {
    String cumulativeCounterCallback = "cumulativeCounterCallback";
    sfxMetrics.registerGaugeAsCumulativeCounter(cumulativeCounterCallback,
            new Gauge<Long>() {
                private long i = 0;

                @Override
                public Long getValue() {
                    return i++;
                }
            });
    reporter.report();
    assertEquals(0, dbank.lastValueFor(SOURCE_NAME, cumulativeCounterCallback).getIntValue());
    reporter.report();
    reporter.report();
    assertEquals(2, dbank.lastValueFor(SOURCE_NAME, cumulativeCounterCallback).getIntValue());
    assertEquals(SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER,
            dbank.registeredMetrics.get(cumulativeCounterCallback));
}
 
Example #11
Source File: AggregateMetricSender.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
@Override
public Session setDatapoint(String source, String metric,
                                      SignalFxProtocolBuffers.MetricType metricType,
                                      long value) {
    check(metric, metricType);
    pointsToFlush.add(SignalFxProtocolBuffers.DataPoint.newBuilder()
                              .setSource(source)
                              .setMetricType(metricType)
                              .setMetric(metric).setValue(
                    SignalFxProtocolBuffers.Datum.newBuilder().setIntValue(value).build())
                              .build());
    return this;
}
 
Example #12
Source File: SignalFxMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private Stream<SignalFxProtocolBuffers.DataPoint.Builder> addFunctionTimer(FunctionTimer timer) {
    return Stream.of(
            addDatapoint(timer, COUNTER, "count", timer.count()),
            addDatapoint(timer, COUNTER, "totalTime", timer.totalTime(getBaseTimeUnit())),
            addDatapoint(timer, GAUGE, "avg", timer.mean(getBaseTimeUnit()))
    );
}
 
Example #13
Source File: HttpDataPointProtobufReceiverConnectionV2.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Boolean> registerMetrics(String auth,
                                            Map<String, SignalFxProtocolBuffers.MetricType> metricTypes)
        throws SignalFxMetricsException {
    Map<String, Boolean> res = new HashMap<String, Boolean>();
    for (Map.Entry<String, SignalFxProtocolBuffers.MetricType> i : metricTypes.entrySet()) {
        res.put(i.getKey(), true);
    }
    return res;
}
 
Example #14
Source File: StoredDataPointReceiver.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
public StoredDataPointReceiver() {
    addDataPoints = Collections
            .synchronizedList(new ArrayList<SignalFxProtocolBuffers.DataPointOrBuilder>());
    registeredMetrics = Collections.synchronizedMap(new HashMap<String, SignalFxProtocolBuffers.MetricType>());

    pointsFor = Maps.newHashMap();
}
 
Example #15
Source File: SignalFxMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private Stream<SignalFxProtocolBuffers.DataPoint.Builder> addTimer(Timer timer) {
    return Stream.of(
            addDatapoint(timer, COUNTER, "count", timer.count()),
            addDatapoint(timer, COUNTER, "totalTime", timer.totalTime(getBaseTimeUnit())),
            addDatapoint(timer, GAUGE, "avg", timer.mean(getBaseTimeUnit())),
            addDatapoint(timer, GAUGE, "max", timer.max(getBaseTimeUnit()))
    );
}
 
Example #16
Source File: StoredDataPointReceiver.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Boolean> registerMetrics(String auth,
                            Map<String, SignalFxProtocolBuffers.MetricType> metricTypes)
        throws SignalFxMetricsException {
    registeredMetrics.putAll(metricTypes);
    Map<String, Boolean> ret = new HashMap<String, Boolean>();
    for (Map.Entry<String, SignalFxProtocolBuffers.MetricType> i: metricTypes.entrySet()) {
        ret.put(i.getKey(), true);
    }
    return ret;
}
 
Example #17
Source File: SignalFxMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private Stream<SignalFxProtocolBuffers.DataPoint.Builder> addDistributionSummary(DistributionSummary summary) {
    return Stream.of(
            addDatapoint(summary, COUNTER, "count", summary.count()),
            addDatapoint(summary, COUNTER, "totalTime", summary.totalAmount()),
            addDatapoint(summary, GAUGE, "avg", summary.mean()),
            addDatapoint(summary, GAUGE, "max", summary.max())
    );
}
 
Example #18
Source File: StoredDataPointReceiver.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
public SignalFxProtocolBuffers.Datum lastValueFor(String source, String metric) {
    List<SignalFxProtocolBuffers.Datum> vals = valuesFor(source, metric);
    if (vals.isEmpty()) {
        throw new RuntimeException("No value for source/metric");
    } else {
        return vals.get(vals.size() - 1);
    }
}
 
Example #19
Source File: AbstractHttpDataPointProtobufReceiverConnection.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
@Override
public void addDataPoints(String auth, List<SignalFxProtocolBuffers.DataPoint> dataPoints)
        throws SignalFxMetricsException {
    if (dataPoints.isEmpty()) {
        return;
    }
    try {
        CloseableHttpResponse resp = null;
        try {
            resp = postToEndpoint(auth,
                    getEntityForVersion(dataPoints),
                    getEndpointForAddDatapoints(),
                    compress);

            int code = resp.getStatusLine().getStatusCode();
            if (code != HttpStatus.SC_OK) {
                throw new SignalFxMetricsException("Invalid status code " + code);
            }
        } finally {
            if (resp != null) {
                resp.close();
            }
        }
    } catch (IOException e) {
        throw new SignalFxMetricsException("Exception posting to addDataPoints", e);
    }
}
 
Example #20
Source File: HttpDataPointProtobufReceiverConnectionTest.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpConnection() throws Exception {
    Server server = new Server(0);
    server.setHandler(new AddPointsHandler());
    server.start();
    URI uri = server.getURI();
    DataPointReceiver dpr = new HttpDataPointProtobufReceiverFactory(
            new SignalFxEndpoint(uri.getScheme(), uri.getHost(), uri.getPort()))
            .createDataPointReceiver();
    dpr.addDataPoints(AUTH_TOKEN, Collections.singletonList(
            SignalFxProtocolBuffers.DataPoint.newBuilder().setSource("source").build()));
    server.stop();
}
 
Example #21
Source File: MetricMetadataImpl.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<SignalFxProtocolBuffers.MetricType> getMetricType(Metric metric) {
    Metadata existingMetaData = metaDataCollection.get(metric);
    if (existingMetaData == null || existingMetaData.metricType == null) {
        return Optional.absent();
    } else {
        return Optional.of(existingMetaData.metricType);
    }
}
 
Example #22
Source File: AggregateMetricSenderSessionWrapper.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
/**
 * Add Histogram metric
 * @param baseName
 * @param histogram
 */

void addHistogram(MetricName baseName,
                  Histogram histogram) {
    addMetric(histogram, baseName,
            Optional.of(SignalFxReporter.MetricDetails.COUNT),
            SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, histogram.count());
    addSampling(baseName, histogram);
}
 
Example #23
Source File: AggregateMetricSenderSessionWrapper.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
/**
 * Add metric
 * @param metric
 * @param codahaleName
 * @param defaultMetricType
 * @param originalValue
 */

void addMetric(Metric metric, MetricName codahaleName,
                         SignalFxProtocolBuffers.MetricType defaultMetricType,
                         Object originalValue) {
    addMetric(metric, codahaleName, Optional.<SignalFxReporter.MetricDetails>absent(),
            defaultMetricType, originalValue);
}
 
Example #24
Source File: AggregateMetricSenderSessionWrapper.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
/**
 * Add metric
 * @param metric
 * @param codahaleName
 * @param metricDetails
 * @param defaultMetricType
 * @param originalValue
 */

private void addMetric(Metric metric, MetricName codahaleName, SignalFxReporter.MetricDetails metricDetails,
                      SignalFxProtocolBuffers.MetricType defaultMetricType,
                      Object originalValue) {
    addMetric(metric, codahaleName, Optional.of(metricDetails),
            defaultMetricType, originalValue);

}
 
Example #25
Source File: DimensionInclusion.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
public boolean shouldInclude(SignalFxProtocolBuffers.MetricType metricType) {
    switch (metricType) {
    case GAUGE:
        return checkBit(GAUGE);
    case COUNTER:
        return checkBit(COUNTER);
    case CUMULATIVE_COUNTER:
        return checkBit(CUMULATIVE_COUNTER);
    case ENUM:
    default:
        return false;
    }
}
 
Example #26
Source File: AggregateMetricSenderSessionWrapper.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
void addHistogram(String baseName,
                  Histogram histogram) {
    addMetric(histogram, baseName,
            Optional.of(SignalFxReporter.MetricDetails.COUNT),
            SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER, histogram.getCount());
    addSampling(baseName, histogram);
}
 
Example #27
Source File: AggregateMetricSenderSessionWrapper.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
private void addMetric(Metric metric, String codahaleName, SignalFxReporter.MetricDetails metricDetails,
                      SignalFxProtocolBuffers.MetricType defaultMetricType,
                      Object originalValue) {
    addMetric(metric, codahaleName, Optional.of(metricDetails),
            defaultMetricType, originalValue);

}
 
Example #28
Source File: SignalFxReporterTest.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testRawCounter() {
    Counter rawCounter = reporter.getMetricMetadata()
            .forMetric(metricRegistry.counter("rawCounter"))
            .withMetricType(SignalFxProtocolBuffers.MetricType.COUNTER).metric();
    rawCounter.inc(10);
    reporter.report();
    rawCounter.inc(14);
    reporter.report();
    // Users have to use an IncrementalCounter if they want to see the count value of 14 each time
    assertEquals(24, dbank.lastValueFor(SOURCE_NAME, "rawCounter").getIntValue());
    assertEquals(SignalFxProtocolBuffers.MetricType.COUNTER,
            dbank.registeredMetrics.get("rawCounter"));
}
 
Example #29
Source File: SignalFxReporterTest.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimer() throws Exception {
    Timer timer = sfxMetrics.timer("timer", "dimName", "dimValue");
    // track time taken.
    for (int i = 0; i < 4; i++) {
        Timer.Context context = timer.time();
        try {
            Thread.sleep(10 + i * 10);
        } finally {
            context.close();
        }
    }
    /*
    Java 7 alternative:
    try (Timer.Context ignored = t.time()) {
        System.out.println("Doing store things");
    }
    */
    reporter.report();
    assertEquals(3, dbank.addDataPoints.size());
    assertEquals(SignalFxProtocolBuffers.MetricType.CUMULATIVE_COUNTER,
            dbank.registeredMetrics.get("timer.count"));
    assertEquals(SignalFxProtocolBuffers.MetricType.GAUGE,
            dbank.registeredMetrics.get("timer.max"));
    assertEquals(SignalFxProtocolBuffers.MetricType.GAUGE,
            dbank.registeredMetrics.get("timer.min"));
    assertTrue(sfxMetrics.unregister(timer));
}
 
Example #30
Source File: StoredDataPointReceiver.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
public List<SignalFxProtocolBuffers.Datum> valuesFor(String source, String metric) {
    Pair<String, String> key = Pair.of(source, metric);
    List<SignalFxProtocolBuffers.Datum> ret = pointsFor.get(key);
    if (ret == null) {
        return Collections.emptyList();
    } else {
        return Collections.unmodifiableList(ret);
    }
}