-
Couldn't load subscription status.
- Fork 6
Add example of batch reading from Kafka with context propagation #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b84dec9
add task functions to kotlin module
eb3073e
add span column, change unique constraint
marijarin 0c15d7a
правки по батчевой отправке и тесты
ee1a2d3
rearrange imports
7d35754
Merge branch 'master' into issue-224
47da8a2
Fix problem with propagator
mfvanek 27e558b
Merge branch 'master' into issue-224
mfvanek 36dd99f
Merge branch 'master' into issue-224
mfvanek fce4531
review fixes
67440f6
Merge branch 'issue-224' of https://github.com/mfvanek/spring-boot-op…
dd6800f
fix changed java version
4368029
fix after review
767234f
rewrite asserts without warnings
581f6c1
Merge branch 'master' into issue-224
mfvanek 3a3e3e6
add test for KafkaReadingService
ba1b851
Merge branch 'issue-224' of https://github.com/mfvanek/spring-boot-op…
2130414
Merge branch 'master' into issue-224
marijarin dad70c0
add new tracing test for kafka consumer
marijarin 61427d9
Merge branch 'master' into issue-224
marijarin c587676
Merge branch 'master' into issue-224
mfvanek 05a28e2
Fix properties
mfvanek 6e3d9dc
Refactor tests
mfvanek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -12,8 +12,8 @@ allprojects { | |
| version = "0.5.0" | ||
|
|
||
| repositories { | ||
| mavenLocal() | ||
| mavenCentral() | ||
| mavenLocal() | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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,4 +1,15 @@ | ||
| plugins { | ||
| id("java") | ||
| id("java-library") | ||
| id("sb-ot-demo.java-conventions") | ||
| id("io.freefair.lombok") | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(platform(project(":common-internal-bom"))) | ||
| implementation(platform(libs.spring.boot.v3.dependencies)) | ||
|
|
||
| implementation("io.micrometer:micrometer-tracing") | ||
| implementation("org.apache.kafka:kafka-clients") | ||
| implementation("org.slf4j:slf4j-api") | ||
| implementation("org.springframework:spring-jdbc") | ||
| } |
47 changes: 47 additions & 0 deletions
47
db-migrations/src/main/java/io/github/mfvanek/db/migrations/common/saver/DbSaver.java
This file contains hidden or 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,47 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
|
|
||
| package io.github.mfvanek.db.migrations.common.saver; | ||
|
|
||
| import io.micrometer.tracing.Span; | ||
| import io.micrometer.tracing.Tracer; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.slf4j.MDC; | ||
| import org.springframework.jdbc.core.simple.JdbcClient; | ||
|
|
||
| import java.time.Clock; | ||
| import java.time.LocalDateTime; | ||
| import java.util.UUID; | ||
|
|
||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public class DbSaver { | ||
|
|
||
| private final String tenantName; | ||
| private final Tracer tracer; | ||
| private final Clock clock; | ||
| private final JdbcClient jdbcClient; | ||
|
|
||
| public void processSingleRecord(ConsumerRecord<UUID, String> record) { | ||
| try (MDC.MDCCloseable ignored = MDC.putCloseable("tenant.name", tenantName)) { | ||
| final Span currentSpan = tracer.currentSpan(); | ||
| final String traceId = currentSpan != null ? currentSpan.context().traceId() : ""; | ||
| final String spanId = currentSpan != null ? currentSpan.context().spanId() : ""; | ||
| log.info("Received record: {} with traceId {} spanId {}", record.value(), traceId, spanId); | ||
| jdbcClient.sql(""" | ||
| insert into otel_demo.storage(message, trace_id, span_id, created_at) | ||
| values(:msg, :traceId, :currentSpan, :createdAt);""") | ||
| .param("msg", record.value()) | ||
| .param("traceId", traceId) | ||
| .param("currentSpan", spanId) | ||
| .param("createdAt", LocalDateTime.now(clock)) | ||
| .update(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
db-migrations/src/main/resources/db/changelog/sql/add_span_column.sql
This file contains hidden or 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,7 @@ | ||
| --liquibase formatted sql | ||
|
|
||
| --changeset marina.zharinova:2025.08.31:add span column | ||
| alter table otel_demo.storage add column span_id text; | ||
|
|
||
| --changeset marina.zharinova:2025.08.31:comment on span_id | ||
| comment on column otel_demo.storage.span_id is 'SpanId of operation'; |
7 changes: 7 additions & 0 deletions
7
db-migrations/src/main/resources/db/changelog/sql/set_span_and_trace_unique.sql
This file contains hidden or 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,7 @@ | ||
| --liquibase formatted sql | ||
|
|
||
| --changeset ivan.vakhrushev:2025.08.31:remove unique from trace_id | ||
| alter table otel_demo.storage drop constraint storage_trace_id_key; | ||
|
|
||
| --changeset marina.zharinova:2025.08.31:add constraint on trace_id with span_id | ||
| alter table otel_demo.storage add constraint trace_span_unique unique(trace_id, span_id); |
This file contains hidden or 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
30 changes: 30 additions & 0 deletions
30
...-app-kotlin/src/main/kotlin/io/github/mfvanek/spring/boot3/kotlin/test/config/DbConfig.kt
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * Copyright (c) 2020-2025. Ivan Vakhrushev and others. | ||
| * https://github.com/mfvanek/spring-boot-open-telemetry-demo | ||
| * | ||
| * Licensed under the Apache License 2.0 | ||
| */ | ||
|
|
||
| package io.github.mfvanek.spring.boot3.kotlin.test.config | ||
|
|
||
| import io.github.mfvanek.db.migrations.common.saver.DbSaver | ||
| import io.micrometer.tracing.Tracer | ||
| import org.springframework.beans.factory.annotation.Value | ||
| import org.springframework.context.annotation.Bean | ||
| import org.springframework.context.annotation.Configuration | ||
| import org.springframework.jdbc.core.simple.JdbcClient | ||
| import java.time.Clock | ||
|
|
||
| @Configuration(proxyBeanMethods = false) | ||
| class DbConfig { | ||
|
|
||
| @Bean | ||
| fun dbSaver( | ||
| @Value("\${app.tenant.name}") tenantName: String, | ||
| tracer: Tracer, | ||
| clock: Clock, | ||
| jdbcClient: JdbcClient | ||
| ): DbSaver { | ||
| return DbSaver(tenantName, tracer, clock, jdbcClient) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.