Skip to content

Commit

Permalink
sleuth -> micrometer tracing + zipkin (#9)
Browse files Browse the repository at this point in the history
migrating to(from sleuth) micrometer tracing with zipkin
  • Loading branch information
gtiwari333 authored Dec 25, 2023
1 parent 0865627 commit b829eab
Show file tree
Hide file tree
Showing 23 changed files with 231 additions and 78 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

MicroService:

[//]: # (- Spring Micrometer based tracing)
[//]: # (- Spring micrometer based tracing with zipkin)
- Exposing and implementing Open Feign clients
- Spring Cloud Contract (WIP)

Expand Down Expand Up @@ -62,6 +62,7 @@ Misc:
- Approval/flagging api - message based
- Nested comment
- Cache implemented
- Zipkin tracing
- Websocket implemented to show article/comment review status/notifications..

Future: do more stuff
Expand All @@ -70,7 +71,6 @@ Future: do more stuff
- Docker-compose deploy/kubernetes
- Visitors log - IP, browser, etc
- Centralized error reporting
- Visual Tracing
- Geo-Spatial query for visitors
- Grafana Dashboard, @Timed and more ...
- logback LevelChangePropagator integration
Expand Down
7 changes: 7 additions & 0 deletions config/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ services:
- 8025:8025 # use this to access from browser
networks:
- seedappnet
zipkin:
image: 'openzipkin/zipkin'
container_name: zipkin
ports:
- 9411:9411
networks:
- seedappnet
volumes:
esdata1:
driver: local
Expand Down
13 changes: 13 additions & 0 deletions content-checker/content-checker-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- tracing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ server:

jms:
content-checkerrequest-queue: jms-content-checker-requestqueue
logging:
level:
web: INFO
ROOT: WARN
gt: DEBUG

management:
tracing:
sampling:
probability: 1.0
zipkin:
tracing:
endpoint: http://localhost:9411/api/v2/spans
health:
jms:
enabled: true
13 changes: 13 additions & 0 deletions email/email-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<!-- tracing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
<!-- processors/code generators -->
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
19 changes: 15 additions & 4 deletions email/email-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ spring:
server:
port: 8085

logging.level:
org.springframework.web: WARN
ROOT: WARN
gt: WARN
logging:
level:
org.springframework.web: INFO
ROOT: WARN
gt: DEBUG

management:
tracing:
sampling:
probability: 1.0
zipkin:
tracing:
endpoint: http://localhost:9411/api/v2/spans


26 changes: 22 additions & 4 deletions main-app/main-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-sleuth</artifactId>-->
<!-- </dependency>-->
<!-- tracing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-micrometer</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
Expand Down Expand Up @@ -239,6 +253,10 @@
<groupId>com.github.gavlyukovskiy</groupId>
<artifactId>datasource-proxy-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>net.ttddyy.observation</groupId>
<artifactId>datasource-micrometer-spring-boot</artifactId>
</dependency>

<!-- spock deps -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package gt.app;

import brave.sampler.Sampler;
import gt.app.config.AppProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;

Expand Down Expand Up @@ -38,5 +40,9 @@ public static void main(String[] args) throws UnknownHostException {
Arrays.toString(env.getActiveProfiles())
);
}

//Force sample
@Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package gt.app.config.logging;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.Interceptor;
import org.hibernate.resource.jdbc.spi.StatementInspector;

import java.io.Serial;
import java.io.Serializable;

@Slf4j
public class HibernateStatInterceptor implements Interceptor, Serializable {
public class HibernateStatementStatInterceptor implements StatementInspector, Serializable {

@Serial
private static final long serialVersionUID = -7875557911815131906L;
Expand All @@ -25,13 +25,13 @@ public void clearCounter() {
queryCount.remove();
}

// @Override
// public String onPrepareStatement(String sql) {
// Long count = queryCount.get();
// if (count != null) {
// queryCount.set(count + 1);
// }
//
// return super.onPrepareStatement(sql);
// }
@Override
public String inspect(String sql) {
Long count = queryCount.get();
if (count != null) {
queryCount.set(count + 1);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class WebRequestInterceptor implements AsyncHandlerInterceptor {

private final ThreadLocal<Long> exeTimeThreadLocal = new ThreadLocal<>();

private final HibernateStatInterceptor statisticsInterceptor;
private final HibernateStatementStatInterceptor statisticsInterceptor;

public WebRequestInterceptor(HibernateStatInterceptor statisticsInterceptor) {
public WebRequestInterceptor(HibernateStatementStatInterceptor statisticsInterceptor) {
this.statisticsInterceptor = statisticsInterceptor;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gt.app.config.metrics;

import gt.app.config.logging.HibernateStatInterceptor;
import gt.app.config.logging.HibernateStatementStatInterceptor;
import gt.app.config.logging.WebRequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.cfg.AvailableSettings;
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
Expand All @@ -10,18 +13,24 @@

@Configuration
@Profile("!test")
@Slf4j
class RequestStatisticsConfiguration implements WebMvcConfigurer {

@Bean
public HibernateStatInterceptor hibernateInterceptor() {
return new HibernateStatInterceptor();
public HibernateStatementStatInterceptor hibernateInterceptor() {
return new HibernateStatementStatInterceptor();
}

// @Bean this is not working
@Bean
public WebRequestInterceptor requestStatisticsInterceptor() {
return new WebRequestInterceptor(hibernateInterceptor());
}

@Bean
public HibernatePropertiesCustomizer hibernateCustomizer(HibernateStatementStatInterceptor statInterceptor) {
return (properties) -> properties.put(AvailableSettings.STATEMENT_INSPECTOR, statInterceptor);
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestStatisticsInterceptor()).addPathPatterns("/", "/article/**");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ void handle(Response resp) {
default -> throw new UnsupportedOperationException();
}

websocketHandler.sendToUser(c.getLastModifiedByUser().getUsername(), "Your comment " + c.getContent().substring(0, 20) + " has been " + (resp.getContentCheckOutcome() == PASSED ? "approved." : "queued for manual review."));
int maxLengthToTrim = Math.min(c.getContent().length(), 20);

websocketHandler.sendToUser(c.getLastModifiedByUser().getUsername(), "Your comment " + c.getContent().substring(0, maxLengthToTrim) + " has been " + (resp.getContentCheckOutcome() == PASSED ? "approved." : "queued for manual review."));
if (resp.getContentCheckOutcome() != PASSED) {
websocketHandler.sendToUser("system", "A new comment " + c.getContent().substring(0, 20) + " has is queued for system admin review.");
websocketHandler.sendToUser("system", "A new comment " + c.getContent().substring(0, maxLengthToTrim) + " has is queued for system admin review.");
}

commentRepository.save(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ContentCheckResponseListenerService {

@JmsListener(destination = "${app-properties.jms.content-checkercallback-response-queue}")
void handleContentCheckResponse(Response resp) {

log.info("Received review response for entity id {}, entity type {}, outcome {}", resp.getEntityId(), resp.getRequestType(), resp.getContentCheckOutcome());
switch (resp.getRequestType()) {
case COMMENT -> commentReviewResponseService.handle(resp);
case ARTICLE -> articleReviewResponseService.handle(resp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gt.app.domain.Article;
import gt.app.domain.Comment;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Async;
Expand All @@ -16,21 +17,24 @@
@RequiredArgsConstructor
@Service
@Profile("!test")
@Slf4j
public class JmsContentCheckService implements ContentCheckService {

private final JmsTemplate jmsTemplate;
private final AppProperties appProperties;

@Override
@Async
@Async //TODO: tracing is not working with async yet !
public void sendForAutoContentReview(Article a) {
log.info("Sending article {} for review", a.getId());
var req = withArticle(a.getContent(), appProperties.getJms().getContentCheckerCallBackResponseQueue(), Long.toString(a.getId()), ARTICLE);
jmsTemplate.convertAndSend(appProperties.getJms().getContentCheckerRequestQueue(), req);
}

@Override
@Async
public void sendForAutoContentReview(Comment c) {
log.info("Sending comment {} for review", c.getId());
var req = withArticle(c.getContent(), appProperties.getJms().getContentCheckerCallBackResponseQueue(), Long.toString(c.getId()), COMMENT);
jmsTemplate.convertAndSend(appProperties.getJms().getContentCheckerRequestQueue(), req);
}
Expand Down
10 changes: 5 additions & 5 deletions main-app/main-webapp/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring:
thymeleaf:
cache: false # set to false, this if you are working on UI
cache: true # set to false, this if you are working on UI
prefix: file:main-app/main-webapp/src/main/resources/templates/ #directly serve from src folder instead of target
web:
resources:
Expand All @@ -10,10 +10,10 @@ spring:
- classpath:/resources/
- classpath:/static/
- classpath:/public/
# cache:
# period: 500000
# chain:
# cache: true
cache:
period: 500000
chain:
cache: true
mvc:
static-path-pattern: /static/**

Expand Down
Loading

0 comments on commit b829eab

Please sign in to comment.