Skip to content

Commit

Permalink
Adding OpenTelemetry tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
edeandrea committed Mar 27, 2024
1 parent 75b6bdf commit bcba5b7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 10 deletions.
27 changes: 25 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-bom-alpha</artifactId>
<version>2.2.0-alpha</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -81,6 +88,22 @@
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

<!--Distributed tracing -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>

<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down Expand Up @@ -292,7 +315,7 @@
<repository>
<id>redhat-ga-repository</id>
<name>Red Hat GA Repository</name>
<url>http://maven.repository.redhat.com/ga/</url>
<url>https://maven.repository.redhat.com/ga/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
Expand Down Expand Up @@ -321,7 +344,7 @@
<pluginRepository>
<id>redhat-ga-repository</id>
<name>Red Hat GA Repository</name>
<url>http://maven.repository.redhat.com/ga/</url>
<url>https://maven.repository.redhat.com/ga/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,14 +30,16 @@ public AlbumEventPublishingService(AlbumRepository albumRepository, ApplicationE
}

@Override
@NewSpan(name = "AlbumService.getAllAlbums")
public Iterable<Album> getAllAlbums() {
LOGGER.info("Getting all albums");
return this.albumRepository.findAll();
}

@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);
Expand All @@ -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 -> {
Expand All @@ -56,14 +61,16 @@ public void updateAlbum(Album album) {
}

@Override
public Optional<Album> getAlbum(String albumId) {
@NewSpan(name = "AlbumService.getAlbum")
public Optional<Album> 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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,28 +27,33 @@ public DefaultOutboxEventService(OutboxEventRepository outboxEventRepository) {
}

@Override
@NewSpan(name = "OutboxEventService.getAllEventsOrderedByTimestampDescending")
public Iterable<OutboxEvent> getAllEventsOrderedByTimestampDescending() {
LOGGER.info("Getting all album events ordered by timestamp descending");
return this.outboxEventRepository.findAll(Sort.by("eventTimestamp").descending());
}

@Override
public Optional<OutboxEvent> getById(long eventId) {
@NewSpan(name = "OutboxEventService.getById")
public Optional<OutboxEvent> getById(@SpanTag(key = "arg.eventId") long eventId) {
return this.outboxEventRepository.findById(eventId);
}

@Override
public Iterable<OutboxEvent> getAllEventsForAlbumIdOrderedByTimestampDescending(String albumId) {
@NewSpan(name = "OutboxEventService.getAllEventsForAlbumIdOrderedByTimestampDescending")
public Iterable<OutboxEvent> 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")
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit bcba5b7

Please sign in to comment.