From bcba5b70f97effcdecb4ec9c039aa1218f482d2e Mon Sep 17 00:00:00 2001 From: Eric Deandrea Date: Wed, 27 Mar 2024 13:36:15 -0400 Subject: [PATCH] Adding OpenTelemetry tracing --- pom.xml | 27 +++++++++++++++++-- .../config/OtelDataSourceConfig.java | 26 ++++++++++++++++++ .../listener/AlbumEventListener.java | 5 +++- .../service/AlbumEventPublishingService.java | 15 ++++++++--- .../service/DefaultOutboxEventService.java | 13 ++++++--- src/main/resources/application.yml | 9 +++++++ 6 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/redhat/springmusic/config/OtelDataSourceConfig.java diff --git a/pom.xml b/pom.xml index 960404f..0311d9d 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,13 @@ pom import + + io.opentelemetry.instrumentation + opentelemetry-instrumentation-bom-alpha + 2.2.0-alpha + pom + import + @@ -81,6 +88,22 @@ micrometer-registry-prometheus + + + io.micrometer + micrometer-tracing-bridge-otel + + + + io.opentelemetry + opentelemetry-exporter-otlp + + + + io.opentelemetry.instrumentation + opentelemetry-jdbc + + org.apache.commons commons-lang3 @@ -292,7 +315,7 @@ redhat-ga-repository Red Hat GA Repository - http://maven.repository.redhat.com/ga/ + https://maven.repository.redhat.com/ga/ true never @@ -321,7 +344,7 @@ redhat-ga-repository Red Hat GA Repository - http://maven.repository.redhat.com/ga/ + https://maven.repository.redhat.com/ga/ true never diff --git a/src/main/java/com/redhat/springmusic/config/OtelDataSourceConfig.java b/src/main/java/com/redhat/springmusic/config/OtelDataSourceConfig.java new file mode 100644 index 0000000..3d87aeb --- /dev/null +++ b/src/main/java/com/redhat/springmusic/config/OtelDataSourceConfig.java @@ -0,0 +1,26 @@ +package com.redhat.springmusic.config; + +import javax.sql.DataSource; + +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.jdbc.datasource.JdbcTelemetry; + +@Configuration +public class OtelDataSourceConfig { + @Bean + public DataSource dataSource(DataSourceProperties dataSourceProperties, OpenTelemetry openTelemetry) { + var datasSource = DataSourceBuilder.create() + .driverClassName(dataSourceProperties.determineDriverClassName()) + .url(dataSourceProperties.determineUrl()) + .username(dataSourceProperties.getUsername()) + .password(dataSourceProperties.getPassword()) + .build(); + + return JdbcTelemetry.create(openTelemetry).wrap(datasSource); + } +} diff --git a/src/main/java/com/redhat/springmusic/listener/AlbumEventListener.java b/src/main/java/com/redhat/springmusic/listener/AlbumEventListener.java index a0bb30f..aebbe6e 100644 --- a/src/main/java/com/redhat/springmusic/listener/AlbumEventListener.java +++ b/src/main/java/com/redhat/springmusic/listener/AlbumEventListener.java @@ -9,6 +9,8 @@ import com.redhat.springmusic.domain.event.AlbumEvent; import com.redhat.springmusic.service.OutboxEventService; +import io.micrometer.tracing.annotation.NewSpan; +import io.micrometer.tracing.annotation.SpanTag; @Component public class AlbumEventListener { @@ -20,7 +22,8 @@ protected AlbumEventListener(OutboxEventService outboxEventService) { } @EventListener - public void handleAlbumEvent(AlbumEvent albumEvent) { + @NewSpan(name = "AlbumEventListener.handleAlbumEvent") + public void handleAlbumEvent(@SpanTag(key = "albumEvent") AlbumEvent albumEvent) { Assert.notNull(albumEvent, "albumEvent can not be null"); LOGGER.info("Handling AlbumEvent {}", albumEvent); diff --git a/src/main/java/com/redhat/springmusic/service/AlbumEventPublishingService.java b/src/main/java/com/redhat/springmusic/service/AlbumEventPublishingService.java index 87143f4..fb356ed 100644 --- a/src/main/java/com/redhat/springmusic/service/AlbumEventPublishingService.java +++ b/src/main/java/com/redhat/springmusic/service/AlbumEventPublishingService.java @@ -15,6 +15,8 @@ import com.redhat.springmusic.domain.event.AlbumUpdatedEvent; import com.redhat.springmusic.domain.jpa.Album; import com.redhat.springmusic.repositories.jpa.AlbumRepository; +import io.micrometer.tracing.annotation.NewSpan; +import io.micrometer.tracing.annotation.SpanTag; @Service public class AlbumEventPublishingService implements AlbumService { @@ -28,6 +30,7 @@ public AlbumEventPublishingService(AlbumRepository albumRepository, ApplicationE } @Override + @NewSpan(name = "AlbumService.getAllAlbums") public Iterable getAllAlbums() { LOGGER.info("Getting all albums"); return this.albumRepository.findAll(); @@ -35,7 +38,8 @@ public Iterable getAllAlbums() { @Override @Transactional - public Album createAlbum(Album album) { + @NewSpan(name = "AlbumService.createAlbum") + public Album createAlbum(@SpanTag(key = "arg.album") Album album) { LOGGER.info("Creating album {}", album); Album newAlbum = this.albumRepository.save(album); @@ -46,7 +50,8 @@ public Album createAlbum(Album album) { @Override @Transactional - public void updateAlbum(Album album) { + @NewSpan(name = "AlbumService.updateAlbum") + public void updateAlbum(@SpanTag(key = "arg.album") Album album) { this.albumRepository.findById(album.getId()) .map(this.albumRepository::detach) .ifPresent(existingAlbum -> { @@ -56,14 +61,16 @@ public void updateAlbum(Album album) { } @Override - public Optional getAlbum(String albumId) { + @NewSpan(name = "AlbumService.getAlbum") + public Optional getAlbum(@SpanTag(key = "arg.albumId") String albumId) { LOGGER.info("Getting album {}", albumId); return this.albumRepository.findById(albumId); } @Override @Transactional - public void deleteAlbum(String albumId) { + @NewSpan(name = "AlbumService.deleteAlbum") + public void deleteAlbum(@SpanTag(key = "arg.albumId") String albumId) { this.albumRepository.findById(albumId) .map(this.albumRepository::detach) .ifPresent(existingAlbum -> { diff --git a/src/main/java/com/redhat/springmusic/service/DefaultOutboxEventService.java b/src/main/java/com/redhat/springmusic/service/DefaultOutboxEventService.java index a6775d7..d23a716 100644 --- a/src/main/java/com/redhat/springmusic/service/DefaultOutboxEventService.java +++ b/src/main/java/com/redhat/springmusic/service/DefaultOutboxEventService.java @@ -13,6 +13,8 @@ import com.redhat.springmusic.domain.event.AlbumEvent; import com.redhat.springmusic.domain.jpa.OutboxEvent; import com.redhat.springmusic.repositories.jpa.OutboxEventRepository; +import io.micrometer.tracing.annotation.NewSpan; +import io.micrometer.tracing.annotation.SpanTag; @Service public class DefaultOutboxEventService implements OutboxEventService { @@ -25,28 +27,33 @@ public DefaultOutboxEventService(OutboxEventRepository outboxEventRepository) { } @Override + @NewSpan(name = "OutboxEventService.getAllEventsOrderedByTimestampDescending") public Iterable getAllEventsOrderedByTimestampDescending() { LOGGER.info("Getting all album events ordered by timestamp descending"); return this.outboxEventRepository.findAll(Sort.by("eventTimestamp").descending()); } @Override - public Optional getById(long eventId) { + @NewSpan(name = "OutboxEventService.getById") + public Optional getById(@SpanTag(key = "arg.eventId") long eventId) { return this.outboxEventRepository.findById(eventId); } @Override - public Iterable getAllEventsForAlbumIdOrderedByTimestampDescending(String albumId) { + @NewSpan(name = "OutboxEventService.getAllEventsForAlbumIdOrderedByTimestampDescending") + public Iterable getAllEventsForAlbumIdOrderedByTimestampDescending(@SpanTag(key = "arg.albumId") String albumId) { return this.outboxEventRepository.findAllByAggregateIdOrderByEventTimestampDesc(albumId); } @Override + @NewSpan(name = "OutboxEventService.deleteAllEvents") public void deleteAllEvents() { this.outboxEventRepository.deleteAll(); } @Override - public OutboxEvent persistEvent(AlbumEvent event) { + @NewSpan(name = "OutboxEventService.persistEvent") + public OutboxEvent persistEvent(@SpanTag(key = "arg.event") AlbumEvent event) { return this.outboxEventRepository.save( OutboxEvent.builder() .aggregateType("Album") diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b77670c..4448b80 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -28,6 +28,15 @@ management: info: git: mode: full + observations: + annotations: + enabled: true + otlp: + tracing: + endpoint: http://localhost:4318/v1/traces + tracing: + sampling: + probability: 1.0 logging: level: