-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds distributed tracing but with bug in coroutines context: micromet…
- Loading branch information
1 parent
b535d14
commit 60d5182
Showing
12 changed files
with
128 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/com/omprakash/springbootplayground/SpringBootPlaygroundApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/com/omprakash/springbootplayground/config/TracingConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.omprakash.springbootplayground.config | ||
|
||
import brave.baggage.BaggageField | ||
import brave.baggage.CorrelationScopeConfig | ||
import brave.context.slf4j.MDCScopeDecorator | ||
import brave.propagation.CurrentTraceContext | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.context.annotation.Bean | ||
|
||
class TracingConfig { | ||
@Bean(name = ["bookId"]) | ||
fun bookId(): BaggageField = BaggageField.create("bookId") | ||
|
||
@Bean | ||
fun mdcScopeDecorator(@Qualifier("bookId") bookId: BaggageField): CurrentTraceContext.ScopeDecorator { | ||
return MDCScopeDecorator.newBuilder().clear() | ||
.add(CorrelationScopeConfig.SingleCorrelationField.newBuilder(bookId).flushOnUpdate().build()) | ||
.build() | ||
} | ||
} |
38 changes: 30 additions & 8 deletions
38
src/main/kotlin/com/omprakash/springbootplayground/controllers/BookController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,51 @@ | ||
package com.omprakash.springbootplayground.controllers | ||
|
||
import brave.baggage.BaggageField | ||
import com.omprakash.springbootplayground.kafka.BookCreatedKafkaProducer | ||
import com.omprakash.springbootplayground.models.Book | ||
import com.omprakash.springbootplayground.models.request.CreateBook | ||
import com.omprakash.springbootplayground.services.BookService | ||
import io.micrometer.core.instrument.kotlin.asContextElement | ||
import io.micrometer.observation.ObservationRegistry | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.reactor.mono | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import reactor.core.publisher.Mono | ||
|
||
@RestController | ||
@RequestMapping("/books") | ||
class BookController(private val bookService: BookService, private val bookKafkaProducer: BookCreatedKafkaProducer) { | ||
class BookController( | ||
private val bookService: BookService, | ||
private val bookKafkaProducer: BookCreatedKafkaProducer, | ||
@Qualifier("bookId") var bookIdBaggageField: BaggageField, | ||
private val observationRegistry: ObservationRegistry | ||
) { | ||
|
||
var logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
@GetMapping | ||
fun findAll(): Flow<Book> { | ||
return bookService.findAll() | ||
} | ||
|
||
@GetMapping("/publish") | ||
suspend fun publishBook(): String { | ||
return try { | ||
bookKafkaProducer.publishBook(1L, "Test", "1234").toString() | ||
} catch (e: Exception) { | ||
println("Exception while publishing books ${e.message}") | ||
e.message ?: "Exception" | ||
@PostMapping("/publish") | ||
fun publishBook(@RequestBody createBook: CreateBook): Mono<String> { | ||
return mono(observationRegistry.asContextElement()) { | ||
try { | ||
bookIdBaggageField.updateValue(createBook.id.toString()) | ||
bookKafkaProducer.publishBook(createBook.id, createBook.title, createBook.isbn).toString() | ||
logger.info("Book published") | ||
} catch (e: Exception) { | ||
println("Exception while publishing books ${e.message}") | ||
e.message ?: "Exception" | ||
} | ||
"Completed" | ||
} | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
src/main/kotlin/com/omprakash/springbootplayground/kafka/BookCreatedKafkaConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
package com.omprakash.springbootplayground.kafka | ||
|
||
import org.apache.avro.generic.GenericRecord | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.kafka.annotation.KafkaListener | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class BookCreatedKafkaConsumer { | ||
|
||
var logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
@KafkaListener(topics = [TOPICS.BOOKS_CREATED], groupId = GROUP_ID.BOOKS_CREATED_CONSUMER, containerFactory = "bookCreatedKafkaListenerContainerFactory") | ||
fun onBookCreated(bookCreated: GenericRecord) { | ||
println("Consumed message $bookCreated") | ||
logger.info("Consumed message $bookCreated") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/com/omprakash/springbootplayground/models/request/CreateBook.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.omprakash.springbootplayground.models.request | ||
|
||
data class CreateBook(val id: Long, val title: String, val isbn: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
spring: | ||
application: | ||
name: spring-boot-playground | ||
sql: | ||
init: | ||
schema-locations: classpath:schema.sql | ||
mode: always | ||
|
||
logging: | ||
config: classpath:logback-spring.xml | ||
pattern: | ||
level: "%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]" | ||
|
||
management: | ||
tracing: | ||
enabled: true | ||
propagation: | ||
type: b3 | ||
sampling: | ||
probability: 1.0 | ||
baggage: | ||
correlation: | ||
enabled: true | ||
fields: bookId | ||
remote-fields: bookId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<configuration> | ||
<springProperty scope="context" name="service" source="spring.application.name"/> | ||
<property scope="context" name="hostname" value="${HOSTNAME}"/> | ||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder class="net.logstash.logback.encoder.LogstashEncoder"> | ||
<timeZone>UTC</timeZone> | ||
<includeMdcKeyName>traceId</includeMdcKeyName> | ||
<includeMdcKeyName>bookId</includeMdcKeyName> | ||
<includeContext>false</includeContext> | ||
<customFields>{"hostname":"${HOSTNAME}","service":"${service}"}</customFields> | ||
</encoder> | ||
</appender> | ||
<root level="info"> | ||
<appender-ref ref="console"/> | ||
</root> | ||
</configuration> |