Prometheus Monitoring Using Spring Boot

Dependencies :

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Setting Up Configuration via Properties or YAML :

management.application.name= Spring Boot-Prometheus-Starter Example
management.server.port=9099
management.context-path=/insights
management.endpoints.web.base-path=/insights
management.server.servlet.context-path=/
management.endpoints.web.exposure.include=*
management.endpoints.prometheus.enabled=true
management.security.enabled=false
management.endpoint.metrics.enabled=true
management.endpoint.health.show-details=always
management.endpoints.enabled-by-default=true
management.endpoints.hypermedia.enabled=true
management.endpoints.shutdown.enabled=true
management.info.git.mode=full
management.metrics.export.prometheus.enabled=true

Code Configuration :

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.info.BuildProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MicrometerConfig {
public static final String SERVICE_NAME = "service";
public static final String ENVIRONMENT = "environment";
private final BuildProperties buildProperties;
private final String environment;
private final String artifactId;
public MicrometerConfig(
final BuildProperties pBuildProperties,
@Value("${env}") final String pEnvironment) {
buildProperties = pBuildProperties;
environment = pEnvironment;
artifactId = buildProperties.getArtifact().replace('-','_');
}
@Bean
MeterRegistryCustomizer<MeterRegistry> registerCommonTags(
@Value("${management.application.name}")
String applicationName
) {
return registry -> registry.config()
.commonTags(
"" + ENVIRONMENT, environment,
"" + SERVICE_NAME, artifactId,
"application", applicationName,
"build.group", buildProperties.getGroup(),
"build.artifact",buildProperties.getArtifact(),
"build.version", buildProperties.getVersion());
}
}
{application="Spring Boot-Prometheus-Starter Example", build_artifact="spring_boot_prometheus_starter_example" ,build_group="com.spring.boot.prometheus.config.metrics", build_version="21.12.1.5",environment="qa", service="spring_boot_prometheus_starter_example"}

Sample Implementation :

public interface MetricCollecting {
void increment(MetricType type);
}
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;
@Component("metrics-collector")
public class MetricCollector implements MetricCollecting {
private final MeterRegistry registry;public MetricCollector(final MeterRegistry pRegistry) {
registry = pRegistry;
}
@Override
public void increment(final MetricType type) {
registry.counter(type.getMetricName()).increment();
}
}
import lombok.Getter;@Getter
public enum MetricType {
SENT(MetricElements.Status.Types.Sent.KEY)
CANCELLED(MetricElements.Status.Types.Cancelled.KEY),
FAILED(MetricElements.Status.Types.Failed.KEY),
;
private final String metricName;MetricType(final String pMetricName) {
metricName = pMetricName;
}
}
public abstract class MetricElements {
public static abstract class Status {
public static abstract class Types {
public static abstract class Cancelled {
public static final String KEY = "status.type.cancelled";
}
public static abstract class Failed {
public static final String KEY = "status.type.failed";
}
public static abstract class Sent {
public static final String KEY = "status.type.sent";
}
}
}
}
public class StausSentExample {// some of your variables 
private final MetricCollecting metrics;
// constructor
public StausSentExample
(@Qualifier("metrics-collector") final MetricCollecting pMetrics) {
metrics = pMetrics;
}
public void processSentStatusExample { // your code that does job of your choice
metrics.increment(MetricType.SENT);
}
}
http://localhost:9099/insights/prometheus
# HELP status_type_sent_total  
# TYPE status_type_sent_total counter
status_type_sent 0.0

Running from docker :

# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'prometheus'
# # metrics_path defaults to '/metrics'
# # scheme defaults to 'http'.
static_configs:
- targets: ['127.0.0.1:9090']
- job_name: 'spring-prometheus-example'
metrics_path: '/insights/prometheus'
#scheme: https
scrape_interval: 5s
static_configs:
# - please update ip according to your local docker address.
# - port number is on which your application is currently running.
- targets: ['192.168.99.100:9099']
  • docker pull prom/prometheus will fetch the latest Prometheus image that which we need
  • docker pull grafana/grafana will fetch the latest Grafana image that which we need (if needed)
  • docker run -p 9090:9090 -v /C:/←-your local path — ->/src/main/resources/prometheus.yml prom/prometheus
  • docker run -d — name=grafana -p 3000:3000 grafana/grafana (if needed)
  • Later head to your favourite browser and hit docker_ip_address:port_number_the_prometheus_image
  • example : 192.168.99.100:9090 you will be able to see prometheus UI
  • Same for grafana, 192.168.99.100:30
Prometheus UI

--

--

--

Avid Reader,In Love With AI, Learner!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Auction03. Crypto Artist: Brinkman. Live Collection Jan 15th.

Detect Greek language in text using Docker, & store in S3 without Boto3.

How On-Demand Pricing Slashed our DynamoDB Bill by 90%

LikeCoin Semi-monthly Progress Update 2020.11.23

PYTHON IN AI — A POWERFUL INTEGRATION

Continuous integration for iPhone apps

Digital platform principles?

Free Distortion Vst 64

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
Kalyan

Kalyan

Avid Reader,In Love With AI, Learner!

More from Medium

Distributed micro-services using Spring Cloud — Service Discovery — Part 2

Kafka Messaging with Java Spring

Spring Cloud Kubernetes Persistence

Testing Schema Registry with Spring Boot and Spring Kafka using MockSchemaRegistryClient