An open API service indexing awesome lists of open source software.

https://github.com/simplaex/metrics


https://github.com/simplaex/metrics

Last synced: 9 months ago
JSON representation

Awesome Lists containing this project

README

          

# dogstatsd metrics

This library implements sending
[statsd metrics](https://github.com/statsd/statsd/blob/master/docs/metric_types.md)
with dogstatsd like tags. Currently it supports sending
`count` and `gauge` values as well as `timings`.

## Maven dependency

`com.simplaex:metrics` is deployed in Maven central as:

```

com.simplaex
metrics
1.4.0

```

## How to use

`com.simplaex.Metrics` requires a bit of work defining
the existing metrics that your application emits. This
is on purpose to make it easier to maintain the emitted
metrics as you will have a central definition of all
your metrics that an application emits. That allows you
to, for example, define sample rates for certain metrics
in one place.

```
package com.simplaex.metrics;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

public class UsageExample {

@RequiredArgsConstructor
public enum AppMetrics implements Metric {
SOME_METRIC("com.simplaex.request", Kind.COUNTER, 0.1),
SOME_OTHER_METRIC("com.simplaex.sessions", Kind.GAUGE);

AppMetrics(final String name, final Kind kind) {
this(name, kind, 1.0);
}

@Getter
private final String name;

@Getter
private final Metric.Kind kind;

@Getter
private final double sampleRate;

}

public static void main(final String... args) {
final MetricsSender metricsSender = new StatsdMetricSender<>();

metricsSender.emit(AppMetrics.SOME_METRIC.withTag("path", "/api/v1").withTag("method", "GET"));
}

}
```

## Kubernetes support

By default statsd metrics will be emitted to `localhost:8125`.
On kubernetes you may want to emit a statsd metrics collector
as a daemon set. In that case expose the UDP port 8125 or your
metrics collector contains as a host port. `com.simplaex:metrics`
will automatically figure out the ip address of the node your
pod is running on and send metrics there.

That way you do not need to send UDP metrics over the network.

## Usage with `vertx-sugar`

`com.simplaex:vertx-sugar` uses this library. To bind the metrics
sender in your module add a provider:

```
@Provides
public MetricsSender provideMetrics() {
return new StatsdMetricSender<>();
}
```

You can now inject:

```
private final MetricsSender metricsSender;
```

## Example usage in Scala

```
package your.sample.project

import com.simplaex.metrics.Metric
import com.simplaex.metrics.Metric.Kind

import scala.beans.BeanProperty

sealed abstract class ApplicationMetrics(
@BeanProperty val name: String,
@BeanProperty val kind: Metric.Kind,
sampleRate: Double = 1.0
) extends Metric[ApplicationMetrics]

object ApplicationMetrics {
object JobStarted extends ApplicationMetrics("fountain.started", Kind.COUNTER)
object JobFinished extends ApplicationMetrics("fountain.finished", Kind.COUNTER)
}
```