HubSpot/signalfx-java

Name: signalfx-java

Owner: HubSpot

Description: Java libraries for SignalFx code

Forked from: signalfx/signalfx-java

Created: 2017-12-12 18:49:41.0

Updated: 2017-12-12 18:49:43.0

Pushed: 2017-12-12 19:43:32.0

Homepage: https://signalfx.com/

Size: 479

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

SignalFx client libraries Build Status

This repository contains libraries for instrumenting Java applications and reporting metrics to SignalFx. You will need a SignalFx account and organization API token to use them. For more information on SignalFx and to create an account, go to http://www.signalfx.com.

We recommend sending metrics with Java using Codahale Metrics version 3.0+. You can also use Yammer Metrics 2.0.x (an earlier version of Codahale Metrics). More information on the Codahale Metrics library can be found on the Codahale Metrics website.

You can also use the module signalfx-java to send metrics directly to SignalFx using protocol buffers, without using Codahale or Yammer metrics.

Supported languages
Using this library in your project
With Maven

If you're using Maven, add the following to your project's pom.xml file.

endency>
roupId>com.signalfx.public</groupId>
rtifactId>signalfx-codahale</artifactId>
ersion>0.0.39</version>
pendency>
endency>
upId>com.signalfx.public</groupId>
rtifactId>signalfx-yammer</artifactId>
ersion>0.0.39</version>
pendency>
With SBT

If you're using SBT, add the following to your project's build.sbt file.

aryDependencies += "com.signalfx.public" % "signalfx-codahale" % "0.0.39"
aryDependencies += "com.signalfx.public" % "signalfx-yammer" % "0.0.39"
From source

You can also install this library from source by cloning the repo and using mvn install as follows. However, we strongly recommend using the automated mechanisms described above.

t clone https://github.com/signalfx/signalfx-java.git
ing into 'signalfx-java'...
te: Counting objects: 930, done.
te: Compressing objects: 100% (67/67), done.
te: Total 930 (delta 20), reused 0 (delta 0)
iving objects: 100% (930/930), 146.79 KiB | 0 bytes/s, done.
lving deltas: 100% (289/289), done.
king connectivity... done.
 signalfx-java
n install
O] Scanning for projects...



O] SignalFx parent .................................. SUCCESS [  2.483 s]
O] SignalFx Protocol Buffer definitions ............. SUCCESS [  5.503 s]
O] SignalFx Protobuf Utilities ...................... SUCCESS [  2.269 s]
O] SignalFx java libraries .......................... SUCCESS [  3.728 s]
O] Codahale to SignalFx ............................. SUCCESS [  2.910 s]
O] ------------------------------------------------------------------------
O] BUILD SUCCESS
O] ------------------------------------------------------------------------
O] Total time: 17.120 s
O] ------------------------------------------------------------------------
Sending metrics
Codahale Metrics 3.0.x
1. Set up the Codahale reporter
l MetricRegistry metricRegistry = new MetricRegistry();
l SignalFxReporter signalfxReporter = new SignalFxReporter.Builder(
metricRegistry,
"SIGNALFX_AUTH_TOKEN"
ild();
alfxReporter.start(1, TimeUnit.SECONDS);
l MetricMetadata metricMetadata = signalfxReporter.getMetricMetadata();
2. Send a metric
his will send the current time in ms to SignalFx as a gauge
icRegistry.register("gauge", new Gauge<Long>() {
public Long getValue() {
    return System.currentTimeMillis();
}

3. Add existing dimensions and metadata to metrics

You can add SignalFx specific metadata to Codahale metrics by first gathering available metadata using getMetricMetadata(), then attaching the MetricMetadata to the metric.

When you use MetricMetadata, call the .register() method you get from the call forMetric() rather than registering your metric directly with the metricRegistry. This will construct a unique Codahale string for your metric.


his will send the size of a queue as a gauge, and attach dimension
queue_name' to the gauge.

l Queue customerQueue = new ArrayBlockingQueue(100);
icMetadata.forMetric(new Gauge<Long>() {
@Override
public Long getValue() {
    return customerQueue.size();
}
ithDimension("queue_name", "customer_backlog")
egister(metricRegistry);
4. (optional) Add dimensions without knowing if they already exist

We recommend creating your Codahale object as a field of your class, as a counter or gauge, then using that field to increment values. If you don't want to maintain this for reasons of code cleanliness, you can create it on the fly with our builders.

For example, if you wanted a timer that included a dimension indicating which store it is from, you could use code like this.

r t = metricMetadata
.forBuilder(MetricBuilder.TIMERS)
.withMetricName("request_time")
.withDimension("storename", "electronics")
.createOrGet(metricRegistery);

r.Context c = t.time();
{
System.out.println("Doing store things");
nally {
c.close();



ava 7 alternative:

ry (Timer.Context ignored = t.time()) {
   System.out.println("Doing store things");


After setting up Codahale

After setting up a SignalFxReporter, you can use Codahale metrics as you normally would, reported at the frequency configured by the SignalFxReporter.

Default Dimensions

Sometimes there is a desire to set one or more dimension key/value pairs on every datapoint that is reported by this library. In order to do this call addDimension(String key, String value) or addDimensions(Map<String,String> dimensions) on the SignalFxReport.Builder object. Note that if IncrementalCounter is used to create a distributed counter you will want to make sure that none of the dimensions passed to addDimension/addDimensions are unique to the reporting source (e.g. hostname, AWSUniqueId) as this will make make the counter non-distributed. For such dimensions use addUniqueDimensions/addUniqueDimension on the SignalFxReport.Builder object.

AWS Integration

To enable AWS integration in SignalFx (i.e aws tag/property syncing) to a metric you can use com.signalfx.metrics.aws.AWSInstanceInfo. And either add it as a dimension in MetricMetadata or add it as a default dimension.

ng instanceInfo = AWSInstanceInfo.get()
r t = metricMetadata
.forBuilder(MetricBuilder.TIMERS)
.withMetricName("request_time")
.withDimension(AWSInstanceInfo.DIMENSION_NAME, instanceInfo)
.createOrGet(metricRegistery);


s default dimension

l SignalFxReporter signalfxReporter = new SignalFxReporter.Builder(
metricRegistry,
"SIGNALFX_AUTH_TOKEN"
dUniqueDimension(AWSInstanceInfo.DIMENSION_NAME, instanceInfo).build();
Yammer Metrics

You can also use this library with Yammer metrics 2.0.x as shown in the following examples.

1. Set up Yammer metrics
l MetricRegistry metricRegistry = new MetricRegistry();
l SignalFxReporter signalfxReporter = new SignalFxReporter.Builder(
metricRegistery,
"SIGNALFX_AUTH_TOKEN"
ild();
alfxReporter.start(1, TimeUnit.SECONDS);
l MetricMetadata metricMetadata = signalfxReporter.getMetricMetadata();
2. Send a metric with Yammer metrics
his will send the current time in ms to SignalFx as a gauge
icName gaugeName = new MetricName("group", "type", "gauge");
ic gauge = metricRegistry.newGauge(gaugeName, new Gauge<Long>() {
@Override
public Long value() {
    return System.currentTimeMillis();
}

3. Add Dimensions and SignalFx metadata to Yammer metrics

Use the MetricMetadata of the reporter as shown.

l Queue customerQueue = new ArrayBlockingQueue(100);

icName gaugeName = new MetricName("group", "type", "gauge");
ic gauge = metricRegistry.newGauge(gaugeName, new Gauge<Integer>() {
@Override
public Integer value() {
    return customerQueue.size();
}


icMetadata.forMetric(gauge)
.withDimension("queue_name", "customer_backlog");
4. Adding Dimensions without knowing if they already exist

This is not supported in Yammer Metrics 2.0.x.

Changing the default source

The default source name for metrics is discovered by SourceNameHelper. If you want to override the default behavior, you can pass a third parameter to your Builder and that String is then used as the source.

For example:

l SignalFxReporter signalfxReporter = new SignalFxReporter.Builder(
metricRegistry,
"SIGNALFX_AUTH_TOKEN",
"MYHOST1"
ild();
Default Dimensions

Sometimes there is a desire to set one or more dimension key/value pairs on every datapoint that is reported by this library. In order to do this call addDimension(String key, String value) or addDimensions(Map<String,String> dimensions) on the SignalFxReport.Builder object.

AWS Integration

To enable AWS integration in SignalFx (i.e aws tag/property syncing) to a metric you can use com.signalfx.metrics.aws.AWSInstanceInfo. And either add it as a dimension in MetricMetadata or add it as a default dimension.

ng instanceInfo = AWSInstanceInfo.get()
r t = metricMetadata
.forBuilder(MetricBuilder.TIMERS)
.withMetricName("request_time")
.withDimension(AWSInstanceInfo.DIMENSION_NAME, instanceInfo)
.createOrGet(metricRegistery);


s default dimension

l SignalFxReporter signalfxReporter = new SignalFxReporter.Builder(
metricRegistry,
"SIGNALFX_AUTH_TOKEN"
dDimension(AWSInstanceInfo.DIMENSION_NAME, instanceInfo).build();
Example Project

You can find a full-stack example project called “signalfx-java-examples” in the repo.

Run it as follows:

  1. Download the code and create an “auth” file in the “signalfx-java-examples” directory. The auth file should contain the following:

    =<signalfx API Token>
    =https://ingest.signalfx.com
    
  2. Run the following commands in your terminal to install and run the example project, replacing path/to/signalfx-java-examples with the location of the example project code in your environment. You must have Maven installed.

    ath/to/signalfx-java-examples
    install
     example for Yammer 2.x metrics
    exec:java -Dexec.mainClass="com.signalfx.example.YammerExample"
     example for sending datapoints and events using protocol buffers
    exec:java -Dexec.mainClass="com.signalfx.example.ProtobufExample"
    

    New metrics and events from the example project should appear in SignalFx.

Sending metrics without using Codahale

We recommend sending metrics using Codahale as shown above. You can also interact with our Java library directly if you do not want to use Codahale. To do this, you will need to build the metric manually using protocol buffers as shown in the following example. Sending both datapoints and events are now supported using protocol buffers.

alFxReceiverEndpoint signalFxEndpoint = new SignalFxEndpoint();
egateMetricSender mf = new AggregateMetricSender("test.SendMetrics",
new HttpDataPointProtobufReceiverFactory(signalFxEndpoint).setVersion(2),
new HttpEventProtobufReceiverFactory(signalFxEndpoint),
new StaticAuthToken(auth_token),
Collections.<OnSendErrorHandler> singleton(new OnSendErrorHandler() {
    @Override
    public void handleError(MetricError metricError) {
        System.out.println("Unable to POST metrics: " + metricError.getMessage());
    }
}));

(AggregateMetricSender.Session i = mf.createSession()) {
i.setDatapoint(
    SignalFxProtocolBuffers.DataPoint.newBuilder()
        .setMetric("curtime")
        .setMetricType(SignalFxProtocolBuffers.MetricType.GAUGE)
        .setValue(
            SignalFxProtocolBuffers.Datum.newBuilder()
                .setIntValue(System.currentTimeMillis()))
        .addDimensions(
            SignalFxProtocolBuffers.Dimension.newBuilder()
                .setKey("source")
                .setValue("java"))
        .build());

i.setEvent(
        SignalFxProtocolBuffers.Event.newBuilder()
              .setEventType("Deployments")
              .setCategory(SignalFxProtocolBuffers.EventCategory.USER_DEFINED)
              .setTimestamp(System.currentTimeMillis())
              .addDimensions(
                SignalFxProtocolBuffers.Dimension.newBuilder()
                                  .setKey("source")
                                  .setValue("java"))
              .addProperties(
                SignalFxProtocolBuffers.Property.newBuilder()
                                .setKey("version")
                                .setValue(
                                        SignalFxProtocolBuffers.PropertyValue.newBuilder()
                                                .setIntValue(2)
                                                .build())
                                .build())
              .build());

Sending metrics through a http proxy

To send metrics through a http proxy one can set the standard java system properties used to control http protocol handling. There are 3 properties you can set to specify the proxy that will be used by the http protocol handler:

Basic example:

va -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080?

Example with directive to bypass proxy for localhost and host.mydomain.com:

va -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080 -Dhttp.noProxyHosts=?localhost|host.mydomain.com?
Executing SignalFlow computations

SignalFlow is SignalFx's real-time analytics computation language. The SignalFlow API allows SignalFx users to execute real-time streaming analytics computations on the SignalFx platform. For more information, head over to our Developers documentation:

Executing a SignalFlow program is very simple with this client library:

ng program = "data('cpu.utilization').mean().publish()";
alFlowClient flow = new SignalFlowClient("MY_TOKEN");
{
System.out.println("Executing " + program);
Computation computation = flow.execute(program);
for (ChannelMessage message : computation) {
    switch (message.getType()) {
    case DATA_MESSAGE:
        DataMessage dataMessage = (DataMessage) message;
        System.out.printf("%d: %s%n",
                dataMessage.getLogicalTimestampMs(), dataMessage.getData());
        break;

    case EVENT_MESSAGE:
        EventMessage eventMessage = (EventMessage) message;
        System.out.printf("%d: %s%n",
                eventMessage.getTimestampMs(),
                eventMessage.getProperties());
        break;
    }
}

Metadata about the timeseries is received from the iterable stream, and it is also automatically intercepted by the client library and made available through the Computation object returned by execute():

 DATA_MESSAGE:
DataMessage dataMessage = (DataMessage) message;
for (Map<String, Number> datum : dataMessage.getData()) {
    Map<String,Object> metadata = computation.getMetadata(datum.getKey());
    // ...
}
License

Apache Software License v2. Copyright © 2014-2017 SignalFx


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.