Skip to content

Commit

Permalink
Add observation support
Browse files Browse the repository at this point in the history
Add banner
Fix docker volume location
Improve swagger documentation
Remove unused ID in Product request
  • Loading branch information
Darren Buller committed Nov 14, 2024
1 parent a8d5cf6 commit db06948
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ build/

### VS Code ###
.vscode/

### Docker volumes ###
/docker/volumes/
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ services:
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: product-service
volumes:
- ./docker/mongodb/data:/data/db
- ./docker/volumes/mongodb/data:/data/db
39 changes: 39 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,45 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Observation -->
<!-- Enable AOP for the endpoints -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- Enable JVM Metrics -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Configure Micrometer to expose an endpoint that can be scraped by Prometheus. -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Add distributed tracing capabilities to our application -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
<!-- Add datasource monitoring -->
<dependency>
<groupId>net.ttddyy.observation</groupId>
<artifactId>datasource-micrometer-spring-boot</artifactId>
<version>1.0.1</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>com.github.loki4j</groupId>
<artifactId>loki-logback-appender</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.microservices.product.config;

import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.aop.ObservedAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ObservationConfig {
@Bean
ObservedAspect observedAspect(ObservationRegistry registry) {
return new ObservedAspect(registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.example.microservices.product.service.ProductService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.microservices.product.dto;

import java.math.BigDecimal;
import io.swagger.v3.oas.annotations.media.Schema;

public record ProductRequest(String id, String name, String description,
String skuCode, BigDecimal price) {
@Schema(description = "Details the product to create")
public record ProductRequest(String name, String description,
String skuCode, BigDecimal price) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.math.BigDecimal;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "Product details")
public record ProductResponse(String id, String name, String description,
String skuCode, BigDecimal price) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

import com.example.microservices.product.model.Product;

import io.micrometer.observation.annotation.Observed;

@Observed
public interface ProductRepository extends MongoRepository<Product, String> {
}
11 changes: 10 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.application.name=product
spring.application.name=product-service
## MongoDB
spring.data.mongodb.uri=mongodb://root:password@localhost:27017/product-service?authSource=admin
## Swagger
Expand All @@ -8,3 +8,12 @@ springdoc.api-docs.enabled=true
springdoc.api-docs.path=/api-docs
springdoc.packagesToScan=com.example.microservices.product.controller
springdoc.pathsToMatch=/**

## Observation management
management.endpoints.web.exposure.include=health, info, metrics, prometheus
management.metrics.distribution.percentiles-histogram.http.server.requests=true
management.observations.key-values.application=product-service
management.tracing.sampling.probability=1.0

# Loki URL
loki.url=http://localhost:3100/loki/api/v1/push
9 changes: 9 additions & 0 deletions src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
______ _ _ _____ _
| ___ \ | | | | / ___| (_)
| |_/ / __ ___ __| |_ _ ___| |_ \ `--. ___ _ ____ ___ ___ ___
| __/ '__/ _ \ / _` | | | |/ __| __| `--. \/ _ \ '__\ \ / / |/ __/ _ \
| | | | | (_) | (_| | |_| | (__| |_ /\__/ / __/ | \ V /| | (_| __/
\_| |_| \___/ \__,_|\__,_|\___|\__| \____/ \___|_| \_/ |_|\___\___|

${application.title} ${application.version}
Powered by Spring Boot ${spring-boot.version}
25 changes: 25 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<springProperty scope="context" name="appName" source="spring.application.name"/>
<springProperty scope="context" name="lokiUrl" source="loki.url"/>

<appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
<http>
<url>${lokiUrl}</url>
</http>
<format>
<label>
<pattern>application=${appName},host=${HOSTNAME},level=%level</pattern>
</label>
<message>
<pattern>${FILE_LOG_PATTERN}</pattern>
</message>
<sortByTime>true</sortByTime>
</format>
</appender>

<root level="INFO">
<appender-ref ref="LOKI"/>
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void shouldCreateProduct() throws Exception {
}

private ProductRequest getProductRequest() {
return new ProductRequest("iPhone 13", "iPhone 13", "iPhone 13", "iPhone 13", BigDecimal.valueOf(1200));
return new ProductRequest("iPhone 13", "iPhone 13", "iPhone 13", BigDecimal.valueOf(1200));
}

@Test
Expand Down

0 comments on commit db06948

Please sign in to comment.