Skip to content

Commit

Permalink
basic prometheus-setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Arslanka committed Oct 4, 2024
1 parent 3b2a731 commit 256e5fa
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 47 deletions.
4 changes: 3 additions & 1 deletion core-booking/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
implementation("org.springframework.kafka:spring-kafka")

implementation("io.micrometer:micrometer-core:1.13.5")
implementation("io.micrometer:micrometer-registry-prometheus:1.13.5")
implementation("org.springframework.boot:spring-boot-starter-actuator:3.3.4")
}

tasks.test {
Expand Down
25 changes: 25 additions & 0 deletions core-booking/src/main/kotlin/service/CoreCatalogAdapterService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.example.service

import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
import ru.openbook.model.BookInstanceResponse
import ru.openbook.model.BookReservationInitializationRequest
import java.util.UUID

@Service
class CoreCatalogAdapterService(
private val restTemplate: RestTemplate,
@Value("\${spring.clients.core-catalog.url}")
private val clientUrl: String,
) {

internal fun reserveBook(bookId: UUID, reservationId: UUID, userId: UUID): BookInstanceResponse? {
val url = "$clientUrl/v1/books/$bookId/reservation"
val requestBody = BookReservationInitializationRequest(
externalId = reservationId,
userId = userId
)
return restTemplate.postForEntity(url, requestBody, BookInstanceResponse::class.java).body
}
}
7 changes: 3 additions & 4 deletions core-booking/src/main/kotlin/service/ReservationService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.time.Instant
import java.util.UUID
import kotlin.math.log

@Service
class ReservationService(
private val reservationRepository: ReservationRepository,
private val taskScheduler: RetryableTaskScheduler,
@Value("\${spring.timeout.core-catalog-callback-timeout-millis}")
private val tpsTimeoutMillis: Long,
private val restRequestSendService: RestRequestSendService,
private val restRequestSendService: CoreCatalogAdapterService,
) {

private val logger = LoggerFactory.getLogger(ReservationService::class.java)
Expand Down Expand Up @@ -73,8 +72,8 @@ class ReservationService(

var reservation = originalReservation
logger.debug("Sending to core-catalog reservation: $reservation")
restRequestSendService.sendBookInstance(
bookInstanceId = reservation.bookId,
restRequestSendService.reserveBook(
bookId = reservation.bookId,
reservationId = reservation.id,
userId = reservation.userId
)
Expand Down
32 changes: 0 additions & 32 deletions core-booking/src/main/kotlin/service/RestRequestSendService.kt

This file was deleted.

8 changes: 4 additions & 4 deletions core-booking/src/main/resources/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ components:
BookReservationInitializationRequest:
type: object
required:
- external_id
- user_id
- externalId
- userId
properties:
external_id:
externalId:
type: string
format: uuid
user_id:
userId:
type: string
format: uuid

Expand Down
17 changes: 17 additions & 0 deletions core-booking/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@ spring:
retry:
max-attempts: 3
delay-between-retries-millis: 5000

clients:
core-catalog:
url: http://core-catalog-app-1:8081

management:
endpoints:
web:
exposure:
include: health,prometheus
metrics:
export:
prometheus:
enabled: true
distribution:
percentiles-histogram:
"[http.server.requests]": true
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class BookInstanceRepository(
""".trimIndent()

val SELECT_BOOK_INSTANCE = """
SELECT id, book_content_id, status from book_instance where id = :id
SELECT id, book_content_id, status from book_instance where id = :id and status = 'FREE';
""".trimIndent()
}
}
8 changes: 4 additions & 4 deletions core-catalog/src/main/resources/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ components:
BookReservationInitializationRequest:
type: object
required:
- external_id
- user_id
- externalId
- userId
properties:
external_id:
externalId:
type: string
format: uuid
user_id:
userId:
type: string
format: uuid

Expand Down
6 changes: 5 additions & 1 deletion deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

docker compose -f ./core-catalog/docker-compose.yaml down -v
docker compose -f ./core-booking/docker-compose.yaml down -v
docker compose -f ./gateway/docker-compose.yaml down -v
docker compose -f ./metrics/docker-compose.yaml down -v

docker compose -f ./core-catalog/docker-compose.yaml up --build
docker compose -f ./core-booking/docker-compose.yaml up --build
docker compose -f ./core-booking/docker-compose.yaml up --build
docker compose -f ./gateway/docker-compose.yaml up --build
docker compose -f ./metrics/docker-compose.yaml up --build
14 changes: 14 additions & 0 deletions gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
FROM gradle:7.4.2-jdk17 as builder

WORKDIR /gateway

COPY gradlew /gateway/
COPY gradle /gateway/gradle
COPY build.gradle.kts /gateway/
COPY settings.gradle.kts /gateway/
COPY src /gateway/src

RUN chmod +x gradlew

RUN ./gradlew build

FROM openjdk:17-jdk-slim

WORKDIR /gateway
Expand Down
2 changes: 2 additions & 0 deletions metrics/config/grafana/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM grafana/grafana
ADD ./provisioning /etc/grafana/provisioning
15 changes: 15 additions & 0 deletions metrics/config/prometheus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
global:
scrape_interval: 15s

scrape_configs:
- job_name: 'prometheus'
scrape_interval: 1s
static_configs:
- targets: ['localhost:9090']

- job_name: 'core-booking-1'
scrape_interval: 1s
metrics_path: '/actuator/metrics'
static_configs:
- targets: ['core-booking-core-booking-1-1:8080']

36 changes: 36 additions & 0 deletions metrics/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
# grafana:
# build: './config/grafana'
# ports:
# - 3000:3000
# volumes:
# - ./grafana:/var/lib/grafana
# environment:
# - GF_SECURITY_ADMIN_USER=admin
# - GF_SECURITY_ADMIN_PASSWORD=admin
# networks:
# monitoring:
# aliases:
# - grafana
prometheus:
image: prom/prometheus
container_name: 'prometheus'
hostname: prometheus
user: root
ports:
- 9090:9090
volumes:
- ./config/prometheus.yaml:/etc/prometheus/prometheus.yaml
- prometheus:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yaml'
networks:
- shared-network

networks:
monitoring:
shared-network:
external: true

volumes:
prometheus: {}

0 comments on commit 256e5fa

Please sign in to comment.