Skip to content
devlibx edited this page Dec 13, 2022 · 2 revisions

This module allows you to log metrics which provides business/operation observability. You can implement IMetrics interface and use it across any library in "easy" e.g. EasyHttp, Messaging, MySQL module reports all metrics via IMetrics.

We already have Prometheus and StatsD implementations. Give below are the examples.

<dependency>
   <groupId>io.github.devlibx.easy</groupId>
   <artifactId>metrics-prometheus</artifactId>
</dependency>

OR

<dependency>
   <groupId>io.github.devlibx.easy</groupId>
   <artifactId>metrics-statsd</artifactId>
</dependency>

Reporting counter, gauge, time

# Example of counter 
IMetric metric = <StatsdMetrics or any other IMetric implementation>
metric.inc("product_order_metric", "city", "bangalore", "type", "mobile");

# Example of time taken by some call
long start = System.currentTimeMillis()
... do something e.g. make a HTTP call ...
long end = System.currentTimeMillis()
metric.observe("product_order_api_time_metric", (end - start), "city", "bangalore", "type", "mobile");


# Example of gauge taken by some call
metric.gauge("product_order_item_sold_guage_metric", 20, "city", "bangalore", "type", "mobile");

Reporting via StatsD

Given below is a sample to send metric via StatsD. This library helps you to simplify the usage of StatsD.

NOTE - this class implements IMetrics, so you can bind this object to IMetrics and it will be used in all the projects in "easy" e.g. EasyHttp , Messaging, MySQL module reports all metrics via IMetrics.

 StatsdMetrics statsdMetrics = new StatsdMetrics(MetricsConfig.builder()
                .env("prod")
                .host("YOUR HOST NAME")
                .port(80)
                .prefix("business_metric")
                .serviceName("order_service")
                .pushInterval(100)
                .enabled(true)
                .build()
        );

You can also set it via Yaml:

statsdMetrics:
  service_name: order_service
  prefix: business_metric 
  env: prod
  host: YOUR HOST NAME
  port: 80
  push_interval: 100 
  enabled: true

Dependency Injection

If you use Guice for dependency injection then you can use the following. All easy models created via this injector will automatically get this ```IMetric````.

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Scopes;
import io.gitbub.devlibx.easy.helper.ApplicationContext;
import io.gitbub.devlibx.easy.helper.metrics.IMetrics;
import io.gitbub.devlibx.easy.helper.metrics.MetricsConfig;

 MetricsConfig metricsConfig = MetricsConfig.builder()
                .env("prod")
                .host("YOUR HOST NAME")
                .port(80)
                .prefix("business_metric")
                .serviceName("order_service")
                .pushInterval(100)
                .enabled(true)
                .build()

Injector injector = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                bind(IMetrics.class).to(StatsdMetrics.class).in(Scopes.SINGLETON);
                bind(MetricsConfig.class).toInstance(metricsConfig);
            }
        });
ApplicationContext.setInjector(injector);
Clone this wiki locally