Setting Up A Comprehensive Monitoring System With Prometheus

What is Prometheus?

./data
├── 01BKGV7JBM69T2GIITC864HV
│ └── meta.json
├── 01BKGTZQ1SYQJTRKJJLLKS85HF6
│ ├── chunks
│ │ └── 000001
│ ├── tombstones
│ ├── index
│ └── meta.json
├── 01BKGTZQ1HHWHV8FB75GC75JHC
│ └── meta.json
├── 01BKGV7JC0RY8A864HCE24HD09
│ ├── chunks
│ │ └── 000001
│ ├── tombstones
│ ├── index
│ └── meta.json
└── wal
├── 00000002
└── checkpoint.000001

Different metrics in Prometheus

  • Counter - A cumulative metric that represents a single monotonically increasing counter whose value can only increase or be reset to zero on restart
  • Gauge - A metric that represents a single numerical value that can arbitrarily go up and down.
  • Histogram - A histogram samples observations (usually things like request duration or response sizes) and counts them in configurable buckets. It also provides a sum of all observed values.
  • Summary - This metric samples observations (usually things like request duration and response sizes). While it also provides a total count of observations and a sum of all observed values, it calculates configurable quantiles over a sliding time window.

Steps to Integrate your application with Prometheus.

<! — Actuator dependency →
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<! — Prometheus dependency →
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
management:
endpoints:
web:
exposure:
include : health,prometheus
base-path: /monitor
enabled-by-default: true
Prometheus default metrics created for your application
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: ‘your_service’
scrape_interval: ‘10s’
metrics_path: ‘/monitor/prometheus’
static_configs:
— targets: [‘localhost:8090’]
Your service connectivity with Prometheus
up{job=”your_service”}
Your service availability metric in Prometheus

Custom Metrics for your application

  • First you need to create a metrics manager to your application. All your custom metrics will be implemented inside this class.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;

@Component
public class MetricsManager {

private static final String METRIC_TAG = "filter";

@Autowired
private MeterRegistry meterRegistry;

public MetricsManager(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}

public void initMetricsCounter(String filterTag) {
Counter counter;
counter= this.meterRegistry.counter("api.request.", METRIC_TAG, filterTag );
counter.increment(1.0);
}
}
  • Now you can use your metrics manager to instantiate and generate values for your metrics.
@Component
public class RequestProcessor {

@Autowired
MetricsManager metricsManager;

@Override
public void process(Exchange exchange) throws Exception {
metricsManager.initMetricsCounter("TCP");
}
}
Your custom metric in Prometheus server
Your custom metric in Prometheus dashboard

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Danuka Praneeth

Danuka Praneeth

Senior Software Engineer | BSc (Hons) Engineering | CIMA | Autodidact | Knowledge-Seeker