Skip to content

Latest commit

History

72 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

stackify-metrics

Maven CentralBuild StatusCoverage Status

API for sending custom metrics to Stackify.

Custom Metrics Overview:

http://support.stackify.com/custom-metrics-overview/

Sign Up for a Trial:

http://www.stackify.com/sign-up/

Installation

Add it as a maven dependency:

<dependency>
<groupId>com.stackify</groupId>
<artifactId>stackify-metrics</artifactId>
<version>2.1.1</version>
</dependency>

Usage

There are four different types of metrics:

  • Gauge: Keeps track of the last value that was set in the current minute
  • Counter: Calculates the rate per minute
  • Average: Calculates the average of all values in the current minute
  • Timer: Calculates the average elapsed time for an operation in the current minute
  • CounterAndTimer: Composite of the Counter and Timer metrics for convenience

All metrics are identified with a category and name. We will group metrics with the same category when they are displayed in Stackify. Use the MetricFactory to get different types of metrics. See below for more details on the operations available for each type of metric.

Gaugegauge = MetricFactory.getGauge("MyCategory", "MyGauge");
...

Be sure to properly shutdown to flush any queued metrics and shutdown the background thread:

MetricManager.shutdown();

Configuration

You need a stackify-api.properties file on your classpath that defines the configuration required for the Metrics API:

stackify.apiKey=YOUR_API_KEY
stackify.application=YOUR_APPLICATION_NAME
stackify.environment=YOUR_ENVIRONMENT

Note: If you are logging from a device that has the stackify-agent installed, the environment setting is optional. We will use the environment associated to your device in Stackify.

Programmatic Configuration (Optional)

Instead of providing a properties file in your classpath, you can configure the Metrics API programmatically:

ApiConfiguration.Builder builder = ApiConfiguration.newBuilder();
builder.apiKey("YOUR_API_KEY");
builder.application("YOUR_APPLICATION_NAME");
builder.environment("YOUR_ENVIRONMENT");
ApiConfiguration config = builder.build();
MetricManager.configure(config);

This needs to be done at application startup before any other interactions with the Metrics API.

Note: If you are logging from a device that has the stackify-agent installed, the environment setting is optional. We will use the environment associated to your device in Stackify.

Gauge Metric

// Get a Gauge metricGaugegauge = MetricFactory.getGauge("MyCategory", "MyGauge");
...
// Set the value of the metric to vgauge.set(v);
...
// Adds v to the current value of the metricgauge.add(v);
...
// Subtracts v from the current value of the metricgauge.subtract(v);
...
// Automatically report 0 for the value of the metric if not setgauge.autoReportZeroValue();
...
// Automatically report the last value for this metric if not setgauge.autoReportLastValue();
...

Counter Metric

// Get a Counter metricCountercounter = MetricFactory.getCounter("MyCategory", "MyCounter");
...
// Adds 1 to the current value of the metriccounter.increment();
...
// Adds v to the current value of the metriccounter.increment(v);
...
// Subtracts 1 from the current value of the metriccounter.decrement();
...
// Subtracts v from the current value of the metriccounter.decrement(v);
...
// Adds v to the current value of the metriccounter.add(v);
...
// Subtracts v from the current value of the metriccounter.subtract(v);
...
// Automatically report 0 for the value of the metric if not setcounter.autoReportZeroValue();
...

Average Metric

// Get a Average metricAverageaverage = MetricFactory.getAverage("MyCategory", "MyAverage");
...
// Adds a value to the average metricaverage.addValue(v);
...
// Automatically report 0 for the value of the metric if not setaverage.autoReportZeroValue();
...
// Automatically report the last value for this metric if not setaverage.autoReportLastValue();
...

Timer Metric

// Get a Timer metricTimertimer = MetricFactory.getTimer("MyCategory", "MyTimer");
...
// Calculates the time taken for this operation using the start time d (java.util.Date)timer.start(d);
...
// Calculates the time taken for this operation using the start time d (milliseconds)timer.startMs(d);
...
// Sets the time taken for this operation to d (milliseconds)timer.durationMs(d);
...
// Automatically report 0 for the value of the metric if not settimer.autoReportZeroValue();
...

CounterAndTimer Metric

// Get a CounterAndTimer metric (composite of a Counter metric and Timer metric)CounterAndTimercounterAndTimer = MetricFactory.getCounterAndTimer("MyCategory", "MyCounterAndTimer");
...
// Counter: Adds 1 to the current value of the metric// Timer: Calculates the time taken for this operation using the start time d (java.util.Date)counterAndTimer.start(d);
...
// Counter: Adds 1 to the current value of the metric// Timer: Calculates the time taken for this operation using the start time d (milliseconds)counterAndTimer.startMs(d);
...
// Counter: Adds 1 to the current value of the metric// Timer: Sets the time taken for this operation to d (milliseconds)counterAndTimer.durationMs(d);
...
// Automatically report 0 for the value of the metric if not setcounterAndTimer.autoReportZeroValue();
...

License

Copyright 2020 Stackify, LLC.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Stackify Metric API for Java

Topics

Resources

Stars

3 stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages