文章目录
Install Prometheus Server Install Grafana Server Spring Prometheus Project Spring Prometheus Properties Spring Prometheus Application Spring Prometheus Configuration Spring Prometheus Controller Configure Grafana Dash Board
Install Prometheus Server
# download package
https://github.com/prometheus/prometheus/releases/download/v3.1.0/prometheus-3.1.0.linux-amd64.tar.gz
# edit prometheus config
prometheus-linux-amd64/prometheus.yml
scrape_configs :
- job_name : "prometheus"
metrics_path : '/actuator/prometheus'
scheme : 'http'
static_configs :
- targets : [ "localhost:10003" ]
# run server
sudo ./prometheus
# visit admin page
http://localhost:9090
Install Grafana Server
# download package
https://dl.grafana.com/enterprise/release/grafana-enterprise-11.5.0.linux-amd64.tar.gz
# run server
sudo bin/grafana server
# visit admin page
http://localhost:3000 admin/admin
Spring Prometheus Project
pluginManagement {
repositories {
gradlePluginPortal ( )
google ( )
mavenCentral ( )
}
}
dependencyResolutionManagement {
repositoriesMode = RepositoriesMode. PREFER_SETTINGS
repositories {
gradlePluginPortal ( )
google ( )
mavenCentral ( )
}
}
buildscript {
repositories {
gradlePluginPortal ( )
google ( )
mavenCentral ( )
}
}
plugins {
id ( "org.jetbrains.kotlin.jvm" ) version "2.0.21" apply false
id ( "org.jetbrains.kotlin.kapt" ) version "2.0.21" apply false
id ( "org.jetbrains.kotlin.plugin.spring" ) version "2.0.21" apply false
id ( "org.springframework.boot" ) version "3.4.1" apply false
}
include ( "prometheus-app" )
Spring Prometheus Properties
# service
server.port=10003
spring.application.name=prometheus-app
spring.profiles.active=dev
spring.devtools.add-properties=false
management.endpoints.web.exposure.include=*
# prometheus
management.prometheus.metrics.export.enabled=true
Spring Prometheus Application
package x. spring. hello
import org. springframework. boot. autoconfigure. SpringBootApplication
import org. springframework. boot. runApplication
@SpringBootApplication
class PrometheusApplication
fun main ( args: Array< String> ) {
runApplication< PrometheusApplication> ( * args)
}
Spring Prometheus Configuration
package x. spring. hello. component
import io. micrometer. core. instrument. Counter
import io. micrometer. core. instrument. MeterRegistry
import org. springframework. beans. factory. annotation. Value
import org. springframework. context. annotation. Bean
import org. springframework. stereotype. Component
@Component
class MetricsProvider {
@Bean ( name = [ "count" ] )
fun getCounterMetrics (
registry: MeterRegistry,
@Value ( "\${spring.application.name}" ) name: String
) : Counter {
return registry. counter ( "count_api_calling_times" , "application" , name)
}
}
Spring Prometheus Controller
package x. spring. hello. controller
import io. micrometer. core. instrument. Counter
import jakarta. annotation. Resource
import org. springframework. web. bind. annotation. GetMapping
import org. springframework. web. bind. annotation. RestController
@RestController
class MetricsController {
@Resource ( name = "count" )
private lateinit var counterMetrics: Counter
@GetMapping ( "/count" )
fun count ( ) : String {
counterMetrics. increment ( )
return "count_api_calling_times_total +1"
}
}
Configure Grafana Dash Board
# add data source
Connections -> Data Sources -> Add Data Source -> Prometheus
settings.name=prometheus
connection.url=http://localhost:9090
# create dash board
Dashboards -> Create Dashboard -> Add Visualization -> Add Queries -> Run Queries
datasource=prometheus
metrics=count_api_calling_times_total