The above example would send a metric named application.timer.execution with the configured tags and the default timer aggregations.
Quick start
After declaring Statful Client as a dependency, you are ready to use it. The quickest way is to do the following:
// Instantiates a new HTTP client and build the configuration fluentlyStatfulClientclient= StatfulFactory.buildHTTPClient().with()
.token("STATFUL_API_TOKEN")
.build();
// Send a metric
client.counter("transactions", 1).send();
IMPORTANT: This configuration uses the default host and port. You can learn more about configuration in Reference.
Custom Transport
You can add support for your own custom transport, besides the currently supported HTTP and UDP. To adapt the client to your implementation, you must perform two steps:
Implement the TransportSender interface, which defines a method to send metric using your own transport.
Implement a client factory by extending the abstract class CustomStatfulFactory`.
By extending implementing TransportSender buildTransportSender(final ClientConfiguration configuration), your custom TransportSender` implementation has access to all global client configuration, as well as the asynchronous sending of metrics, so you only need to worry about sending metrics.
Custom Transport Example
First implement the TransportSender interface, then you can implement your client factory similar to the following way:
publicfinalclassStatfulFactory {
privatestaticfinalThriftClientFactoryTHRIFT_CLIENT_FACTORY=newThriftClientFactory();
privateStatfulFactory() { }
publicstatic StatfulClientBuilder buildThriftClient() {
// The client is built with default underlying mechanism used in HTTP and UDP transports, including asynchronous sendingreturn THRIFT_CLIENT_FACTORY.buildClient();
}
privatestaticclassThriftClientFactoryextendsCustomStatfulFactory {
ThriftClientFactory() {
super(Transport.OTHER);
}
@Overrideprotected TransportSender buildTransportSender(final ClientConfiguration configuration) {
// Has access to global configurationreturnnewThriftSender(configuration.getHost(), configuration.getPort());
}
}
}
To use your code, you need to include the following Statful dependency in your POM.xml file:
If you implement HTTP-based transport, you need to send the M-Api-Token header with your token. You can access the token name inside your client factory using ClientConfiguration.TOKEN_HEADER.
Avoiding metrics back-pressure
When the rate of metrics sent to the Statful client is too high, metrics start to be lost and warn log messages are printed. To help reduce this problem there are multiple configuration parameters that can be useful:
timeout: Reducing communication timeout, for when the network latency is too high, helps free busy workers sooner to handle other metrics in the buffer.
workerPoolSize: Increase the workers pool (default is 1). Note that each worker spawns a thread and, if too many workers are spawn, thread contention could start to be a problem.
flushSize and flushInterval: Increasing metrics flush size and interval helps reducing back-pressure but it'll have memory impact and and the payload size sent to Statful will be bigger.
Examples
You can find here some useful usage examples of the Statful Client. In the following examples is assumed you have already included Statful Client in your project.
UDP Configuration
Creates a simple UDP configuration for the client.
Creates a simple client configuration and use it to send some metrics.
StatfulClientclient= StatfulFactory.buildHTTPClient().with()
.app("AccountService")
.token("TOKEN")
.tag("cluster", "production")
.build();
// Send three different metrics (gauge, timer and a counter)
client.gauge("testGauge", 10).send();
client.timer("testTimer", 100).send();
client.counter("testTimer", 1).send();
client.put("testCustomMetric", 1).send();
// Metric to be sent with tags
client.counter("testCounter", 1).with()
.tag("host", "localhost").tag("status", "SUCCESS")
.send();
Add aggregated metrics
Create a simple client configuration and use it to store previously aggregated metrics.
This is useful for existing collectors that expose metrics aggregated over periods of time.
StatfulClientclient= StatfulFactory.buildHTTPClient().with()
.app("AccountService")
.token("TOKEN")
.tag("cluster", "production")
.build();
// Send three different metrics (gauge, timer and a counter)
client.aggregatedGauge("testGauge", 10, Aggregation.AVG, AggregationFreq.FREQ_120).send();
client.aggregatedTimer("testTimer", 100, Aggregation.AVG, AggregationFreq.FREQ_10).send();
client.aggregatedCounter("testTimer", 1, Aggregation.SUM, AggregationFreq.FREQ_10).send();
client.aggregatedPut("testCustomMetric", Aggregation.COUNT, AggregationFreq.FREQ_180).send();
// Metric to be sent with tags
client.aggregatedCounter("testCounter", 1, Aggregation.SUM, AggregationFreq.FREQ_30).with()
.tag("host", "localhost").tag("status", "SUCCESS")
.send();
Reference
Detailed reference if you want to take full advantage from Statful.
Global configuration
The custom options that can be set on config param are detailed below.
Option
Description
Type
Default
Required
app
Defines the application global name. If specified sets a global tag app=setValue.
String
none
NO
dryRun
Defines if metrics should be output to the logger instead of being sent. Log level is DEBUG.
boolean
false
NO
flushInterval
Defines the periodicity of buffer flushes in miliseconds.
int
3000
NO
flushSize
Defines the maximum buffer size before performing a flush.
int
1000
NO
namespace
Defines the global namespace.
String
application
NO
sampleRate
Defines the rate sampling. Should be a number between [1, 100].
int
100
NO
tags
Defines a list global tags.
String, String pairs
Empty list of tags
NO
host
Defines the host name to where the metrics should be sent.
String
api.statful.com
NO
port
Defines the port.
int
443
NO
token
Defines the authentication token to be used.
String
none
NO
timeout
Defines the timeout for the transport layers in miliseconds. Must be set inside api.
long
2000
NO
secure
Enable or disables HTTPS.
boolean
true
NO
connectTimeout
Connection timeout for http/tcp transports in milliseconds.
The methods for sending metrics receive a metric name and a metric value as arguments and send a counter/gauge/timer/custom metric.
Read the methods options reference bellow to get more information about the default values.
Option
Description
Default for Counter
Default for Gauge
Default for Timer
Default for Custom Metric
aggregations
Defines the aggregations to be executed. These aggregations are merged with the ones configured globally, including method defaults.
Valid Aggregations:AVG, COUNT, SUM, FIRST, LAST, P90, P95, P99 MIN, MAX
SUM, COUNT
LAST
AVG, P90, COUNT
none
aggFreq
Defines the aggregation frequency in seconds. It overrides the global aggregation frequency configuration.
Defines the namespace of the metric. It overrides the global namespace configuration.
application
application
application
application
tags
Defines the tags of the metric. These tags are merged with the ones configured globally, including method defaults.
none
none
unit: 'ms'
none
timestamp
Defines the timestamp of the metric. This timestamp is a POSIX/Epoch time in seconds.
current timestamp
current timestamp
current timestamp
current timestamp
Note that calling the aggregations method on the aggregatedX methods will throw UnsupportedOperationException.
Enabling/disabling Statful
// The following metric will be sent to Statful
statful.enable();
statful.counter("transactions").send();
// The following metric will NOT be sent to Statful
statful.disable();
statful.counter("transactions").send();
Statful Java Client is available under the MIT license. See the LICENSE file for more information.
FAQs
Client to send metrics over HTTP, HTTPS or UDP to Statful.
We found that com.statful.client:client-java demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 0 open source maintainers collaborating on the project.
Package last updated on 02 Sep 2019
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
The planned feature introduces a review step before releases go live, following the Shai-Hulud attacks and a rocky migration off classic tokens that disrupted maintainer workflows.