Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
aed304b7e3 | |||
b796c43257 | |||
b0691edf82 | |||
d43b59f2f3 | |||
f976516d6b | |||
8f5c03ad60 | |||
|
9e9c1434ff | ||
fe81a14413 | |||
eb56a48dda | |||
71e8a13c70 | |||
b538ef82b5 | |||
44e2d5f9a4 | |||
|
9426208e1c | ||
d6e73a3419 | |||
969e459e3d | |||
|
b2cd7d8b8e | ||
6d85d3ee41 | |||
d0978fb314 | |||
|
0bd81d1253 | ||
e34f57a515 |
7
.github/workflows/job_coverage.yml
vendored
7
.github/workflows/job_coverage.yml
vendored
@@ -8,12 +8,11 @@ on:
|
|||||||
- '.gitea/**'
|
- '.gitea/**'
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ main, v3, v4 ]
|
branches: [ main, v3, v4 ]
|
||||||
# Allows you to run this workflow manually from the Actions tab
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
|
||||||
build:
|
build:
|
||||||
|
if: github.server_url != 'https://github.com'
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: checkout code
|
- name: checkout code
|
||||||
@@ -42,8 +41,8 @@ jobs:
|
|||||||
name: autocommit
|
name: autocommit
|
||||||
with:
|
with:
|
||||||
commit_message: Apply Code Coverage Badge
|
commit_message: Apply Code Coverage Badge
|
||||||
skip_fetch: true
|
skip_fetch: false
|
||||||
skip_checkout: true
|
skip_checkout: false
|
||||||
file_pattern: ./README.md
|
file_pattern: ./README.md
|
||||||
|
|
||||||
- name: push
|
- name: push
|
||||||
|
2
.github/workflows/job_lint.yml
vendored
2
.github/workflows/job_lint.yml
vendored
@@ -24,6 +24,6 @@ jobs:
|
|||||||
- name: setup deps
|
- name: setup deps
|
||||||
run: go get -v ./...
|
run: go get -v ./...
|
||||||
- name: run lint
|
- name: run lint
|
||||||
uses: https://github.com/golangci/golangci-lint-action@v6
|
uses: golangci/golangci-lint-action@v6
|
||||||
with:
|
with:
|
||||||
version: 'latest'
|
version: 'latest'
|
||||||
|
50
.github/workflows/job_sync.yml
vendored
50
.github/workflows/job_sync.yml
vendored
@@ -8,7 +8,7 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
sync:
|
sync:
|
||||||
if: github.server_url == 'zhttps://github.com'
|
if: github.server_url != 'https://github.com'
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: init
|
- name: init
|
||||||
@@ -18,34 +18,76 @@ jobs:
|
|||||||
echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc
|
echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc
|
||||||
echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc
|
echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc
|
||||||
|
|
||||||
|
- name: check master
|
||||||
|
id: check_master
|
||||||
|
run: |
|
||||||
|
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
|
||||||
|
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
|
||||||
|
echo "src_hash=$src_hash"
|
||||||
|
echo "dst_hash=$dst_hash"
|
||||||
|
if [ "$src_hash" != "$dst_hash" ]; then
|
||||||
|
echo "sync_needed=true" >> $GITHUB_OUTPUT
|
||||||
|
else
|
||||||
|
echo "sync_needed=false" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
|
|
||||||
- name: sync master
|
- name: sync master
|
||||||
|
if: steps.check_master.outputs.sync_needed == 'true'
|
||||||
run: |
|
run: |
|
||||||
git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
||||||
cd repo
|
cd repo
|
||||||
git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY}
|
git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY}
|
||||||
git merge upstream/master
|
git pull --rebase upstream master
|
||||||
git push upstream master --progress
|
git push upstream master --progress
|
||||||
git push origin master --progress
|
git push origin master --progress
|
||||||
cd ../
|
cd ../
|
||||||
rm -rf repo
|
rm -rf repo
|
||||||
|
|
||||||
|
- name: check v3
|
||||||
|
id: check_v3
|
||||||
|
run: |
|
||||||
|
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
|
||||||
|
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
|
||||||
|
echo "src_hash=$src_hash"
|
||||||
|
echo "dst_hash=$dst_hash"
|
||||||
|
if [ "$src_hash" != "$dst_hash" ]; then
|
||||||
|
echo "sync_needed=true" >> $GITHUB_OUTPUT
|
||||||
|
else
|
||||||
|
echo "sync_needed=false" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
|
|
||||||
- name: sync v3
|
- name: sync v3
|
||||||
|
if: steps.check_v3.outputs.sync_needed == 'true'
|
||||||
run: |
|
run: |
|
||||||
git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
||||||
cd repo
|
cd repo
|
||||||
git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY}
|
git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY}
|
||||||
git merge upstream/v3
|
git pull --rebase upstream v3
|
||||||
git push upstream v3 --progress
|
git push upstream v3 --progress
|
||||||
git push origin v3 --progress
|
git push origin v3 --progress
|
||||||
cd ../
|
cd ../
|
||||||
rm -rf repo
|
rm -rf repo
|
||||||
|
|
||||||
|
- name: check v4
|
||||||
|
id: check_v4
|
||||||
|
run: |
|
||||||
|
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
|
||||||
|
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
|
||||||
|
echo "src_hash=$src_hash"
|
||||||
|
echo "dst_hash=$dst_hash"
|
||||||
|
if [ "$src_hash" != "$dst_hash" ]; then
|
||||||
|
echo "sync_needed=true" >> $GITHUB_OUTPUT
|
||||||
|
else
|
||||||
|
echo "sync_needed=false" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
|
|
||||||
- name: sync v4
|
- name: sync v4
|
||||||
|
if: steps.check_v4.outputs.sync_needed == 'true'
|
||||||
run: |
|
run: |
|
||||||
git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
||||||
cd repo
|
cd repo
|
||||||
git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY}
|
git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY}
|
||||||
git merge upstream/v4
|
git pull --rebase upstream v4
|
||||||
git push upstream v4 --progress
|
git push upstream v4 --progress
|
||||||
git push origin v4 --progress
|
git push origin v4 --progress
|
||||||
cd ../
|
cd ../
|
||||||
|
6
.github/workflows/job_tests.yml
vendored
6
.github/workflows/job_tests.yml
vendored
@@ -32,19 +32,19 @@ jobs:
|
|||||||
go-version: 'stable'
|
go-version: 'stable'
|
||||||
- name: setup go work
|
- name: setup go work
|
||||||
env:
|
env:
|
||||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
GOWORK: ${{ github.workspace }}/go.work
|
||||||
run: |
|
run: |
|
||||||
go work init
|
go work init
|
||||||
go work use .
|
go work use .
|
||||||
go work use micro-tests
|
go work use micro-tests
|
||||||
- name: setup deps
|
- name: setup deps
|
||||||
env:
|
env:
|
||||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
GOWORK: ${{ github.workspace }}/go.work
|
||||||
run: go get -v ./...
|
run: go get -v ./...
|
||||||
- name: run tests
|
- name: run tests
|
||||||
env:
|
env:
|
||||||
INTEGRATION_TESTS: yes
|
INTEGRATION_TESTS: yes
|
||||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
GOWORK: ${{ github.workspace }}/go.work
|
||||||
run: |
|
run: |
|
||||||
cd micro-tests
|
cd micro-tests
|
||||||
go test -mod readonly -v ./... || true
|
go test -mod readonly -v ./... || true
|
||||||
|
11
README.md
11
README.md
@@ -1,9 +1,2 @@
|
|||||||
# broker-kgo
|
# micro-broker-kgo
|
||||||
|

|
||||||
TODO:
|
|
||||||
|
|
||||||
1) экспортируем текущий оффсет для каждой партиции в группе
|
|
||||||
2) экспортируем лаг для группы
|
|
||||||
3) мониторим
|
|
||||||
1) если есть лаг больше нуля
|
|
||||||
2) если дельта оффсета за нужное нам время не
|
|
||||||
|
26
broker.go
26
broker.go
@@ -26,8 +26,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (m *hookEvent) OnGroupManageError(err error) {
|
func (m *hookEvent) OnGroupManageError(err error) {
|
||||||
if err != nil {
|
if err != nil && !kgo.IsRetryableBrokerErr(err) {
|
||||||
// m.connected.Store(0)
|
m.connected.Store(0)
|
||||||
if m.fatalOnError {
|
if m.fatalOnError {
|
||||||
m.log.Fatal(context.TODO(), "kgo.OnGroupManageError", err)
|
m.log.Fatal(context.TODO(), "kgo.OnGroupManageError", err)
|
||||||
}
|
}
|
||||||
@@ -35,8 +35,8 @@ func (m *hookEvent) OnGroupManageError(err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *hookEvent) OnBrokerConnect(_ kgo.BrokerMetadata, _ time.Duration, _ net.Conn, err error) {
|
func (m *hookEvent) OnBrokerConnect(_ kgo.BrokerMetadata, _ time.Duration, _ net.Conn, err error) {
|
||||||
if err != nil {
|
if err != nil && !kgo.IsRetryableBrokerErr(err) {
|
||||||
// m.connected.Store(0)
|
m.connected.Store(0)
|
||||||
if m.fatalOnError {
|
if m.fatalOnError {
|
||||||
m.log.Fatal(context.TODO(), "kgo.OnBrokerConnect", err)
|
m.log.Fatal(context.TODO(), "kgo.OnBrokerConnect", err)
|
||||||
}
|
}
|
||||||
@@ -48,8 +48,8 @@ func (m *hookEvent) OnBrokerDisconnect(_ kgo.BrokerMetadata, _ net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *hookEvent) OnBrokerWrite(_ kgo.BrokerMetadata, _ int16, _ int, _ time.Duration, _ time.Duration, err error) {
|
func (m *hookEvent) OnBrokerWrite(_ kgo.BrokerMetadata, _ int16, _ int, _ time.Duration, _ time.Duration, err error) {
|
||||||
if err != nil {
|
if err != nil && !kgo.IsRetryableBrokerErr(err) {
|
||||||
// m.connected.Store(0)
|
m.connected.Store(0)
|
||||||
if m.fatalOnError {
|
if m.fatalOnError {
|
||||||
m.log.Fatal(context.TODO(), "kgo.OnBrokerWrite", err)
|
m.log.Fatal(context.TODO(), "kgo.OnBrokerWrite", err)
|
||||||
}
|
}
|
||||||
@@ -57,13 +57,19 @@ func (m *hookEvent) OnBrokerWrite(_ kgo.BrokerMetadata, _ int16, _ int, _ time.D
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *hookEvent) OnBrokerRead(_ kgo.BrokerMetadata, _ int16, _ int, _ time.Duration, _ time.Duration, err error) {
|
func (m *hookEvent) OnBrokerRead(_ kgo.BrokerMetadata, _ int16, _ int, _ time.Duration, _ time.Duration, err error) {
|
||||||
if err != nil {
|
if err != nil && !kgo.IsRetryableBrokerErr(err) {
|
||||||
// m.connected.Store(0)
|
m.connected.Store(0)
|
||||||
|
if m.fatalOnError {
|
||||||
|
m.log.Fatal(context.TODO(), "kgo.OnBrokerRead", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *hookEvent) OnProduceRecordUnbuffered(_ *kgo.Record, err error) {
|
func (m *hookEvent) OnProduceRecordUnbuffered(_ *kgo.Record, err error) {
|
||||||
if err != nil {
|
if err != nil && !kgo.IsRetryableBrokerErr(err) {
|
||||||
// m.connected.Store(0)
|
m.connected.Store(0)
|
||||||
|
if m.fatalOnError {
|
||||||
|
m.log.Fatal(context.TODO(), "kgo.OnProduceRecordUnbuffered", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
go.mod
15
go.mod
@@ -1,24 +1,23 @@
|
|||||||
module go.unistack.org/micro-broker-kgo/v4
|
module go.unistack.org/micro-broker-kgo/v4
|
||||||
|
|
||||||
go 1.23.0
|
go 1.23.8
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/twmb/franz-go v1.18.1
|
github.com/twmb/franz-go v1.19.5
|
||||||
github.com/twmb/franz-go/pkg/kadm v1.16.0
|
github.com/twmb/franz-go/pkg/kadm v1.16.0
|
||||||
|
github.com/twmb/franz-go/pkg/kfake v0.0.0-20250508175730-72e1646135e3
|
||||||
github.com/twmb/franz-go/pkg/kmsg v1.11.2
|
github.com/twmb/franz-go/pkg/kmsg v1.11.2
|
||||||
go.opentelemetry.io/otel v1.35.0
|
go.opentelemetry.io/otel v1.36.0
|
||||||
go.unistack.org/micro/v4 v4.1.7
|
go.unistack.org/micro/v4 v4.1.17
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ash3in/uuidv8 v1.2.0 // indirect
|
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/klauspost/compress v1.18.0 // indirect
|
github.com/klauspost/compress v1.18.0 // indirect
|
||||||
github.com/matoous/go-nanoid v1.5.1 // indirect
|
github.com/matoous/go-nanoid v1.5.1 // indirect
|
||||||
github.com/pierrec/lz4/v4 v4.1.22 // indirect
|
github.com/pierrec/lz4/v4 v4.1.22 // indirect
|
||||||
github.com/spf13/cast v1.7.1 // indirect
|
github.com/spf13/cast v1.9.2 // indirect
|
||||||
go.unistack.org/micro-proto/v4 v4.1.0 // indirect
|
go.unistack.org/micro-proto/v4 v4.1.0 // indirect
|
||||||
golang.org/x/crypto v0.37.0 // indirect
|
golang.org/x/crypto v0.39.0 // indirect
|
||||||
google.golang.org/protobuf v1.36.6 // indirect
|
google.golang.org/protobuf v1.36.6 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
||||||
)
|
)
|
||||||
|
29
go.sum
29
go.sum
@@ -1,7 +1,3 @@
|
|||||||
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
|
||||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
|
||||||
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
|
||||||
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||||
@@ -24,28 +20,27 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
|
|||||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||||
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
|
github.com/spf13/cast v1.9.2 h1:SsGfm7M8QOFtEzumm7UZrZdLLquNdzFYfIbEXntcFbE=
|
||||||
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
github.com/spf13/cast v1.9.2/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/twmb/franz-go v1.18.1 h1:D75xxCDyvTqBSiImFx2lkPduE39jz1vaD7+FNc+vMkc=
|
github.com/twmb/franz-go v1.19.5 h1:W7+o8D0RsQsedqib71OVlLeZ0zI6CbFra7yTYhZTs5Y=
|
||||||
github.com/twmb/franz-go v1.18.1/go.mod h1:Uzo77TarcLTUZeLuGq+9lNpSkfZI+JErv7YJhlDjs9M=
|
github.com/twmb/franz-go v1.19.5/go.mod h1:4kFJ5tmbbl7asgwAGVuyG1ZMx0NNpYk7EqflvWfPCpM=
|
||||||
github.com/twmb/franz-go/pkg/kadm v1.16.0 h1:STMs1t5lYR5mR974PSiwNzE5TvsosByTp+rKXLOhAjE=
|
github.com/twmb/franz-go/pkg/kadm v1.16.0 h1:STMs1t5lYR5mR974PSiwNzE5TvsosByTp+rKXLOhAjE=
|
||||||
github.com/twmb/franz-go/pkg/kadm v1.16.0/go.mod h1:MUdcUtnf9ph4SFBLLA/XxE29rvLhWYLM9Ygb8dfSCvw=
|
github.com/twmb/franz-go/pkg/kadm v1.16.0/go.mod h1:MUdcUtnf9ph4SFBLLA/XxE29rvLhWYLM9Ygb8dfSCvw=
|
||||||
|
github.com/twmb/franz-go/pkg/kfake v0.0.0-20250508175730-72e1646135e3 h1:p24opKWPySAy8xSl8NqRgOv7Q+bX7kdrQirBVRJzQfo=
|
||||||
|
github.com/twmb/franz-go/pkg/kfake v0.0.0-20250508175730-72e1646135e3/go.mod h1:7uQs3Ae6HkWT1Y9elMbqtAcNFCI0y6+iS+Phw49L49U=
|
||||||
github.com/twmb/franz-go/pkg/kmsg v1.11.2 h1:hIw75FpwcAjgeyfIGFqivAvwC5uNIOWRGvQgZhH4mhg=
|
github.com/twmb/franz-go/pkg/kmsg v1.11.2 h1:hIw75FpwcAjgeyfIGFqivAvwC5uNIOWRGvQgZhH4mhg=
|
||||||
github.com/twmb/franz-go/pkg/kmsg v1.11.2/go.mod h1:CFfkkLysDNmukPYhGzuUcDtf46gQSqCZHMW1T4Z+wDE=
|
github.com/twmb/franz-go/pkg/kmsg v1.11.2/go.mod h1:CFfkkLysDNmukPYhGzuUcDtf46gQSqCZHMW1T4Z+wDE=
|
||||||
go.opentelemetry.io/otel v1.35.0 h1:xKWKPxrxB6OtMCbmMY021CqC45J+3Onta9MqjhnusiQ=
|
go.opentelemetry.io/otel v1.36.0 h1:UumtzIklRBY6cI/lllNZlALOF5nNIzJVb16APdvgTXg=
|
||||||
go.opentelemetry.io/otel v1.35.0/go.mod h1:UEqy8Zp11hpkUrL73gSlELM0DupHoiq72dR+Zqel/+Y=
|
go.opentelemetry.io/otel v1.36.0/go.mod h1:/TcFMXYjyRNh8khOAO9ybYkqaDBb/70aVwkNML4pP8E=
|
||||||
go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
|
go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
|
||||||
go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||||
go.unistack.org/micro/v4 v4.1.7 h1:4/dSEMTqxYoHimn/8wKDohfTXi2zDy6eWXYBCnBaZdc=
|
go.unistack.org/micro/v4 v4.1.17 h1:26QDtRSYVpozYuassyvLP4sEQRo3dxgD3sVILRXmIPo=
|
||||||
go.unistack.org/micro/v4 v4.1.7/go.mod h1:lr3oYED8Ay1vjK68QqRw30QOtdk/ffpZqMFDasOUhKw=
|
go.unistack.org/micro/v4 v4.1.17/go.mod h1:xleO2M5Yxh4s6I+RUcLrEpUjobefh+71ctrdIfn7TUs=
|
||||||
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
|
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
|
||||||
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
|
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
|
||||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
|
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
|
||||||
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
|
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
125
kgo.go
125
kgo.go
@@ -74,7 +74,7 @@ type Broker struct {
|
|||||||
|
|
||||||
opts broker.Options
|
opts broker.Options
|
||||||
|
|
||||||
sync.RWMutex
|
mu sync.RWMutex
|
||||||
init bool
|
init bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +108,7 @@ type kgoMessage struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
body []byte
|
body []byte
|
||||||
hdr metadata.Metadata
|
hdr metadata.Metadata
|
||||||
opts broker.PublishOptions
|
opts broker.MessageOptions
|
||||||
ack bool
|
ack bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,17 +141,21 @@ func (b *Broker) newCodec(ct string) (codec.Codec, error) {
|
|||||||
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
ct = ct[:idx]
|
ct = ct[:idx]
|
||||||
}
|
}
|
||||||
b.RLock()
|
b.mu.RLock()
|
||||||
c, ok := b.opts.Codecs[ct]
|
c, ok := b.opts.Codecs[ct]
|
||||||
b.RUnlock()
|
b.mu.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
return nil, codec.ErrUnknownContentType
|
return nil, codec.ErrUnknownContentType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Broker) NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...broker.PublishOption) (broker.Message, error) {
|
func (b *Broker) NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...broker.MessageOption) (broker.Message, error) {
|
||||||
options := broker.NewPublishOptions(opts...)
|
options := broker.NewMessageOptions(opts...)
|
||||||
|
if options.ContentType == "" {
|
||||||
|
options.ContentType = b.opts.ContentType
|
||||||
|
}
|
||||||
|
|
||||||
m := &kgoMessage{ctx: ctx, hdr: hdr, opts: options}
|
m := &kgoMessage{ctx: ctx, hdr: hdr, opts: options}
|
||||||
c, err := b.newCodec(m.opts.ContentType)
|
c, err := b.newCodec(m.opts.ContentType)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -234,10 +238,10 @@ func (k *Broker) Connect(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
k.Lock()
|
k.mu.Lock()
|
||||||
k.c = c
|
k.c = c
|
||||||
k.connected.Store(1)
|
k.connected.Store(1)
|
||||||
k.Unlock()
|
k.mu.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -255,8 +259,8 @@ func (k *Broker) Disconnect(ctx context.Context) error {
|
|||||||
ctx, span = k.opts.Tracer.Start(ctx, "Disconnect")
|
ctx, span = k.opts.Tracer.Start(ctx, "Disconnect")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
k.Lock()
|
k.mu.Lock()
|
||||||
defer k.Unlock()
|
defer k.mu.Unlock()
|
||||||
select {
|
select {
|
||||||
case <-nctx.Done():
|
case <-nctx.Done():
|
||||||
return nctx.Err()
|
return nctx.Err()
|
||||||
@@ -280,8 +284,8 @@ func (k *Broker) Disconnect(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *Broker) Init(opts ...broker.Option) error {
|
func (k *Broker) Init(opts ...broker.Option) error {
|
||||||
k.Lock()
|
k.mu.Lock()
|
||||||
defer k.Unlock()
|
defer k.mu.Unlock()
|
||||||
|
|
||||||
if len(opts) == 0 && k.init {
|
if len(opts) == 0 && k.init {
|
||||||
return nil
|
return nil
|
||||||
@@ -340,51 +344,31 @@ func (b *Broker) fnPublish(ctx context.Context, topic string, messages ...broker
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.Message) error {
|
func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.Message) error {
|
||||||
if b.connected.Load() == 0 {
|
var records []*kgo.Record
|
||||||
c, _, err := b.connect(ctx, b.kopts...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
b.Lock()
|
|
||||||
b.c = c
|
|
||||||
b.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
records := make([]*kgo.Record, 0, len(messages))
|
|
||||||
var errs []string
|
|
||||||
var key []byte
|
|
||||||
var promise func(*kgo.Record, error)
|
|
||||||
|
|
||||||
for _, msg := range messages {
|
for _, msg := range messages {
|
||||||
|
|
||||||
if mctx := msg.Context(); mctx != nil {
|
|
||||||
if k, ok := mctx.Value(publishKey{}).([]byte); ok && k != nil {
|
|
||||||
key = k
|
|
||||||
}
|
|
||||||
if p, ok := mctx.Value(publishPromiseKey{}).(func(*kgo.Record, error)); ok && p != nil {
|
|
||||||
promise = p
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rec := &kgo.Record{
|
rec := &kgo.Record{
|
||||||
Context: ctx,
|
Context: msg.Context(),
|
||||||
Key: key,
|
|
||||||
Topic: topic,
|
Topic: topic,
|
||||||
Value: msg.Body(),
|
Value: msg.Body(),
|
||||||
}
|
}
|
||||||
|
|
||||||
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Inc()
|
var promise func(*kgo.Record, error)
|
||||||
|
if rec.Context != nil {
|
||||||
|
if k, ok := rec.Context.Value(messageKey{}).([]byte); ok && k != nil {
|
||||||
|
rec.Key = k
|
||||||
|
}
|
||||||
|
if p, ok := rec.Context.Value(messagePromiseKey{}).(func(*kgo.Record, error)); ok && p != nil {
|
||||||
|
promise = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setHeaders(rec, msg.Header())
|
setHeaders(rec, msg.Header())
|
||||||
|
|
||||||
records = append(records, rec)
|
if promise != nil {
|
||||||
}
|
ts := time.Now()
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Inc()
|
||||||
ts := time.Now()
|
|
||||||
|
|
||||||
if promise != nil {
|
|
||||||
|
|
||||||
for _, rec := range records {
|
|
||||||
b.c.Produce(ctx, rec, func(r *kgo.Record, err error) {
|
b.c.Produce(ctx, rec, func(r *kgo.Record, err error) {
|
||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Dec()
|
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Dec()
|
||||||
@@ -397,27 +381,33 @@ func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.M
|
|||||||
}
|
}
|
||||||
promise(r, err)
|
promise(r, err)
|
||||||
})
|
})
|
||||||
}
|
continue
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
results := b.c.ProduceSync(ctx, records...)
|
|
||||||
|
|
||||||
te := time.Since(ts)
|
|
||||||
for _, result := range results {
|
|
||||||
b.opts.Meter.Summary(semconv.PublishMessageLatencyMicroseconds, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Update(te.Seconds())
|
|
||||||
b.opts.Meter.Histogram(semconv.PublishMessageDurationSeconds, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Update(te.Seconds())
|
|
||||||
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Dec()
|
|
||||||
if result.Err != nil {
|
|
||||||
b.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", result.Record.Topic, "topic", result.Record.Topic, "status", "failure").Inc()
|
|
||||||
errs = append(errs, result.Err.Error())
|
|
||||||
} else {
|
} else {
|
||||||
b.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", result.Record.Topic, "topic", result.Record.Topic, "status", "success").Inc()
|
records = append(records, rec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(errs) > 0 {
|
if len(records) > 0 {
|
||||||
return fmt.Errorf("publish error: %s", strings.Join(errs, "\n"))
|
var errs []string
|
||||||
|
ts := time.Now()
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", topic, "topic", topic).Set(uint64(len(records)))
|
||||||
|
results := b.c.ProduceSync(ctx, records...)
|
||||||
|
te := time.Since(ts)
|
||||||
|
for _, result := range results {
|
||||||
|
b.opts.Meter.Summary(semconv.PublishMessageLatencyMicroseconds, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Update(te.Seconds())
|
||||||
|
b.opts.Meter.Histogram(semconv.PublishMessageDurationSeconds, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Update(te.Seconds())
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Dec()
|
||||||
|
if result.Err != nil {
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", result.Record.Topic, "topic", result.Record.Topic, "status", "failure").Inc()
|
||||||
|
errs = append(errs, result.Err.Error())
|
||||||
|
} else {
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", result.Record.Topic, "topic", result.Record.Topic, "status", "success").Inc()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(errs) > 0 {
|
||||||
|
return fmt.Errorf("publish error: %s", strings.Join(errs, "\n"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -474,11 +464,15 @@ func (b *Broker) fnSubscribe(ctx context.Context, topic string, handler interfac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var messagePool bool
|
||||||
var fatalOnError bool
|
var fatalOnError bool
|
||||||
if b.opts.Context != nil {
|
if b.opts.Context != nil {
|
||||||
if v, ok := b.opts.Context.Value(fatalOnErrorKey{}).(bool); ok && v {
|
if v, ok := b.opts.Context.Value(fatalOnErrorKey{}).(bool); ok && v {
|
||||||
fatalOnError = v
|
fatalOnError = v
|
||||||
}
|
}
|
||||||
|
if v, ok := b.opts.Context.Value(subscribeMessagePoolKey{}).(bool); ok && v {
|
||||||
|
messagePool = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.Context != nil {
|
if options.Context != nil {
|
||||||
@@ -496,6 +490,7 @@ func (b *Broker) fnSubscribe(ctx context.Context, topic string, handler interfac
|
|||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
fatalOnError: fatalOnError,
|
fatalOnError: fatalOnError,
|
||||||
connected: b.connected,
|
connected: b.connected,
|
||||||
|
messagePool: messagePool,
|
||||||
}
|
}
|
||||||
|
|
||||||
kopts := append(b.kopts,
|
kopts := append(b.kopts,
|
||||||
@@ -540,9 +535,9 @@ func (b *Broker) fnSubscribe(ctx context.Context, topic string, handler interfac
|
|||||||
|
|
||||||
go sub.poll(ctx)
|
go sub.poll(ctx)
|
||||||
|
|
||||||
b.Lock()
|
b.mu.Lock()
|
||||||
b.subs = append(b.subs, sub)
|
b.subs = append(b.subs, sub)
|
||||||
b.Unlock()
|
b.mu.Unlock()
|
||||||
|
|
||||||
return sub, nil
|
return sub, nil
|
||||||
}
|
}
|
||||||
|
102
kgo_test.go
102
kgo_test.go
@@ -2,15 +2,15 @@ package kgo_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/twmb/franz-go/pkg/kfake"
|
||||||
kg "github.com/twmb/franz-go/pkg/kgo"
|
kg "github.com/twmb/franz-go/pkg/kgo"
|
||||||
kgo "go.unistack.org/micro-broker-kgo/v4"
|
kgo "go.unistack.org/micro-broker-kgo/v4"
|
||||||
"go.unistack.org/micro/v4/broker"
|
"go.unistack.org/micro/v4/broker"
|
||||||
|
"go.unistack.org/micro/v4/codec"
|
||||||
"go.unistack.org/micro/v4/logger"
|
"go.unistack.org/micro/v4/logger"
|
||||||
"go.unistack.org/micro/v4/logger/slog"
|
"go.unistack.org/micro/v4/logger/slog"
|
||||||
"go.unistack.org/micro/v4/metadata"
|
"go.unistack.org/micro/v4/metadata"
|
||||||
@@ -20,31 +20,33 @@ var (
|
|||||||
msgcnt = int64(1200)
|
msgcnt = int64(1200)
|
||||||
group = "38"
|
group = "38"
|
||||||
prefill = true
|
prefill = true
|
||||||
loglevel = logger.DebugLevel
|
loglevel = logger.ErrorLevel
|
||||||
|
cluster *kfake.Cluster
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFail(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
cluster = kfake.MustCluster(
|
||||||
t.Skip()
|
kfake.AllowAutoTopicCreation(),
|
||||||
}
|
)
|
||||||
|
defer cluster.Close()
|
||||||
|
m.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFail(t *testing.T) {
|
||||||
logger.DefaultLogger = slog.NewLogger()
|
logger.DefaultLogger = slog.NewLogger()
|
||||||
if err := logger.DefaultLogger.Init(logger.WithLevel(loglevel)); err != nil {
|
if err := logger.DefaultLogger.Init(logger.WithLevel(loglevel)); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
var addrs []string
|
|
||||||
if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 {
|
|
||||||
addrs = []string{"127.0.0.1:9092"}
|
|
||||||
} else {
|
|
||||||
addrs = strings.Split(addr, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
b := kgo.NewBroker(
|
b := kgo.NewBroker(
|
||||||
broker.Addrs(addrs...),
|
broker.ContentType("application/octet-stream"),
|
||||||
|
broker.Codec("application/octet-stream", codec.NewCodec()),
|
||||||
|
broker.Addrs(cluster.ListenAddrs()...),
|
||||||
kgo.CommitInterval(5*time.Second),
|
kgo.CommitInterval(5*time.Second),
|
||||||
kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*1024),
|
kgo.Options(
|
||||||
|
kg.ClientID("test"),
|
||||||
|
kg.FetchMaxBytes(10*1024*1024),
|
||||||
kg.AllowAutoTopicCreation(),
|
kg.AllowAutoTopicCreation(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -69,33 +71,39 @@ func TestFail(t *testing.T) {
|
|||||||
t.Logf("broker health %v", b.Health())
|
t.Logf("broker health %v", b.Health())
|
||||||
msgs := make([]broker.Message, 0, msgcnt)
|
msgs := make([]broker.Message, 0, msgcnt)
|
||||||
for i := int64(0); i < msgcnt; i++ {
|
for i := int64(0); i < msgcnt; i++ {
|
||||||
m, _ := b.NewMessage(ctx, metadata.Pairs("hkey", "hval"), []byte(`test`))
|
m, err := b.NewMessage(ctx, metadata.Pairs("hkey", "hval"), []byte(`test`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
msgs = append(msgs, m)
|
msgs = append(msgs, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, msg := range msgs {
|
go func() {
|
||||||
t.Logf("broker publish")
|
for _, msg := range msgs {
|
||||||
if err := b.Publish(ctx, "test", msg); err != nil {
|
// t.Logf("broker publish")
|
||||||
t.Fatal(err)
|
if err := b.Publish(ctx, "test.fail", msg); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
// t.Skip()
|
// t.Skip()
|
||||||
|
|
||||||
idx := int64(0)
|
idx := int64(0)
|
||||||
fn := func(msg broker.Message) error {
|
fn := func(msg broker.Message) error {
|
||||||
atomic.AddInt64(&idx, 1)
|
atomic.AddInt64(&idx, 1)
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
t.Logf("ack")
|
// t.Logf("ack")
|
||||||
return msg.Ack()
|
return msg.Ack()
|
||||||
}
|
}
|
||||||
|
|
||||||
sub, err := b.Subscribe(ctx, "test", fn,
|
sub, err := b.Subscribe(ctx, "test.fail", fn,
|
||||||
broker.SubscribeAutoAck(true),
|
broker.SubscribeAutoAck(true),
|
||||||
broker.SubscribeGroup(group),
|
broker.SubscribeGroup(group),
|
||||||
broker.SubscribeBodyOnly(true))
|
broker.SubscribeBodyOnly(true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := sub.Unsubscribe(ctx); err != nil {
|
if err := sub.Unsubscribe(ctx); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -108,17 +116,26 @@ func TestFail(t *testing.T) {
|
|||||||
t.Logf("health works")
|
t.Logf("health works")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
time.Sleep(1 * time.Second)
|
t.Logf("health sleep")
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
if err := b.Disconnect(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConnect(t *testing.T) {
|
func TestConnect(t *testing.T) {
|
||||||
var addrs []string
|
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
b := kgo.NewBroker(
|
b := kgo.NewBroker(
|
||||||
broker.Addrs(addrs...),
|
broker.ContentType("application/octet-stream"),
|
||||||
|
broker.Codec("application/octet-stream", codec.NewCodec()),
|
||||||
|
broker.Addrs(cluster.ListenAddrs()...),
|
||||||
kgo.CommitInterval(5*time.Second),
|
kgo.CommitInterval(5*time.Second),
|
||||||
kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*1024)),
|
kgo.Options(
|
||||||
|
kg.ClientID("test"),
|
||||||
|
kg.FetchMaxBytes(10*1024*1024),
|
||||||
|
kg.AllowAutoTopicCreation(),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
if err := b.Init(); err != nil {
|
if err := b.Init(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -130,26 +147,21 @@ func TestConnect(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPubSub(t *testing.T) {
|
func TestPubSub(t *testing.T) {
|
||||||
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
|
||||||
t.Skip()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := logger.DefaultLogger.Init(logger.WithLevel(loglevel)); err != nil {
|
if err := logger.DefaultLogger.Init(logger.WithLevel(loglevel)); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
var addrs []string
|
|
||||||
if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 {
|
|
||||||
addrs = []string{"127.0.0.1:29091", "127.0.0.2:29092", "127.0.0.3:29093"}
|
|
||||||
} else {
|
|
||||||
addrs = strings.Split(addr, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
b := kgo.NewBroker(
|
b := kgo.NewBroker(
|
||||||
broker.Addrs(addrs...),
|
broker.ContentType("application/octet-stream"),
|
||||||
|
broker.Codec("application/octet-stream", codec.NewCodec()),
|
||||||
|
broker.Addrs(cluster.ListenAddrs()...),
|
||||||
kgo.CommitInterval(5*time.Second),
|
kgo.CommitInterval(5*time.Second),
|
||||||
kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*1024)),
|
kgo.Options(
|
||||||
|
kg.ClientID("test"),
|
||||||
|
kg.FetchMaxBytes(10*1024*1024),
|
||||||
|
kg.AllowAutoTopicCreation(),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
if err := b.Init(); err != nil {
|
||||||
@@ -172,7 +184,7 @@ func TestPubSub(t *testing.T) {
|
|||||||
msgs = append(msgs, m)
|
msgs = append(msgs, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := b.Publish(ctx, "test", msgs...); err != nil {
|
if err := b.Publish(ctx, "test.pubsub", msgs...); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
// t.Skip()
|
// t.Skip()
|
||||||
@@ -185,7 +197,7 @@ func TestPubSub(t *testing.T) {
|
|||||||
return msg.Ack()
|
return msg.Ack()
|
||||||
}
|
}
|
||||||
|
|
||||||
sub, err := b.Subscribe(ctx, "test", fn,
|
sub, err := b.Subscribe(ctx, "test.pubsub", fn,
|
||||||
broker.SubscribeAutoAck(true),
|
broker.SubscribeAutoAck(true),
|
||||||
broker.SubscribeGroup(group),
|
broker.SubscribeGroup(group),
|
||||||
broker.SubscribeBodyOnly(true))
|
broker.SubscribeBodyOnly(true))
|
||||||
@@ -210,7 +222,7 @@ func TestPubSub(t *testing.T) {
|
|||||||
if prc := atomic.LoadInt64(&idx); prc == msgcnt {
|
if prc := atomic.LoadInt64(&idx); prc == msgcnt {
|
||||||
close(done)
|
close(done)
|
||||||
} else {
|
} else {
|
||||||
t.Logf("processed %v\n", prc)
|
t.Logf("processed %v of %v\n", prc, msgcnt)
|
||||||
}
|
}
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
close(done)
|
close(done)
|
||||||
|
23
options.go
23
options.go
@@ -27,11 +27,11 @@ func SubscribeContext(ctx context.Context) broker.SubscribeOption {
|
|||||||
return broker.SetSubscribeOption(subscribeContextKey{}, ctx)
|
return broker.SetSubscribeOption(subscribeContextKey{}, ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
type publishKey struct{}
|
type messageKey struct{}
|
||||||
|
|
||||||
// PublishKey set the kafka message key (broker option)
|
// MessageKey set the kafka message key (broker option)
|
||||||
func PublishKey(key []byte) broker.PublishOption {
|
func MessageKey(key []byte) broker.MessageOption {
|
||||||
return broker.SetPublishOption(publishKey{}, key)
|
return broker.SetMessageOption(messageKey{}, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
type optionsKey struct{}
|
type optionsKey struct{}
|
||||||
@@ -103,9 +103,16 @@ func SubscribeFatalOnError(b bool) broker.SubscribeOption {
|
|||||||
return broker.SetSubscribeOption(fatalOnErrorKey{}, b)
|
return broker.SetSubscribeOption(fatalOnErrorKey{}, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
type publishPromiseKey struct{}
|
type messagePromiseKey struct{}
|
||||||
|
|
||||||
// PublishPromise set the kafka promise func for Produce
|
// MessagePromise set the kafka promise func for Produce
|
||||||
func PublishPromise(fn func(*kgo.Record, error)) broker.PublishOption {
|
func MessagePromise(fn func(*kgo.Record, error)) broker.MessageOption {
|
||||||
return broker.SetPublishOption(publishPromiseKey{}, fn)
|
return broker.SetMessageOption(messagePromiseKey{}, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
type subscribeMessagePoolKey struct{}
|
||||||
|
|
||||||
|
// SubscribeMessagePool optionaly enabled/disable message pool
|
||||||
|
func SubscribeMessagePool(b bool) broker.SubscribeOption {
|
||||||
|
return broker.SetSubscribeOption(subscribeMessagePoolKey{}, b)
|
||||||
}
|
}
|
||||||
|
134
subscriber.go
134
subscriber.go
@@ -24,32 +24,32 @@ type tp struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type consumer struct {
|
type consumer struct {
|
||||||
topic string
|
topic string
|
||||||
c *kgo.Client
|
c *kgo.Client
|
||||||
htracer *hookTracer
|
htracer *hookTracer
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
recs chan kgo.FetchTopicPartition
|
recs chan kgo.FetchTopicPartition
|
||||||
kopts broker.Options
|
kopts broker.Options
|
||||||
partition int32
|
partition int32
|
||||||
opts broker.SubscribeOptions
|
opts broker.SubscribeOptions
|
||||||
handler interface{}
|
handler interface{}
|
||||||
connected *atomic.Uint32
|
connected *atomic.Uint32
|
||||||
|
messagePool bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Subscriber struct {
|
type Subscriber struct {
|
||||||
consumers map[tp]*consumer
|
consumers map[tp]*consumer
|
||||||
c *kgo.Client
|
c *kgo.Client
|
||||||
htracer *hookTracer
|
htracer *hookTracer
|
||||||
topic string
|
topic string
|
||||||
|
messagePool bool
|
||||||
handler interface{}
|
handler interface{}
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
kopts broker.Options
|
kopts broker.Options
|
||||||
opts broker.SubscribeOptions
|
opts broker.SubscribeOptions
|
||||||
|
connected *atomic.Uint32
|
||||||
connected *atomic.Uint32
|
mu sync.RWMutex
|
||||||
sync.RWMutex
|
|
||||||
closed bool
|
closed bool
|
||||||
fatalOnError bool
|
fatalOnError bool
|
||||||
}
|
}
|
||||||
@@ -117,11 +117,11 @@ func (s *Subscriber) poll(ctx context.Context) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Lock()
|
s.mu.Lock()
|
||||||
for p, l := range lmap {
|
for p, l := range lmap {
|
||||||
s.kopts.Meter.Counter(semconv.BrokerGroupLag, "topic", s.topic, "group", s.opts.Group, "partition", strconv.Itoa(int(p))).Set(uint64(l.Lag))
|
s.kopts.Meter.Counter(semconv.BrokerGroupLag, "topic", s.topic, "group", s.opts.Group, "partition", strconv.Itoa(int(p))).Set(uint64(l.Lag))
|
||||||
}
|
}
|
||||||
s.Unlock()
|
s.mu.Unlock()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,7 +146,12 @@ func (s *Subscriber) poll(ctx context.Context) {
|
|||||||
|
|
||||||
fetches.EachPartition(func(p kgo.FetchTopicPartition) {
|
fetches.EachPartition(func(p kgo.FetchTopicPartition) {
|
||||||
tps := tp{p.Topic, p.Partition}
|
tps := tp{p.Topic, p.Partition}
|
||||||
s.consumers[tps].recs <- p
|
s.mu.Lock()
|
||||||
|
c := s.consumers[tps]
|
||||||
|
s.mu.Unlock()
|
||||||
|
if c != nil {
|
||||||
|
c.recs <- p
|
||||||
|
}
|
||||||
})
|
})
|
||||||
s.c.AllowRebalance()
|
s.c.AllowRebalance()
|
||||||
}
|
}
|
||||||
@@ -160,11 +165,15 @@ func (s *Subscriber) killConsumers(ctx context.Context, lost map[string][]int32)
|
|||||||
for topic, partitions := range lost {
|
for topic, partitions := range lost {
|
||||||
for _, partition := range partitions {
|
for _, partition := range partitions {
|
||||||
tps := tp{topic, partition}
|
tps := tp{topic, partition}
|
||||||
|
s.mu.Lock()
|
||||||
pc, ok := s.consumers[tps]
|
pc, ok := s.consumers[tps]
|
||||||
|
s.mu.Unlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
s.mu.Lock()
|
||||||
delete(s.consumers, tps)
|
delete(s.consumers, tps)
|
||||||
|
s.mu.Unlock()
|
||||||
close(pc.quit)
|
close(pc.quit)
|
||||||
if s.kopts.Logger.V(logger.DebugLevel) {
|
if s.kopts.Logger.V(logger.DebugLevel) {
|
||||||
s.kopts.Logger.Debug(ctx, fmt.Sprintf("[kgo] waiting for work to finish topic %s partition %d", topic, partition))
|
s.kopts.Logger.Debug(ctx, fmt.Sprintf("[kgo] waiting for work to finish topic %s partition %d", topic, partition))
|
||||||
@@ -207,21 +216,22 @@ func (s *Subscriber) assigned(_ context.Context, c *kgo.Client, assigned map[str
|
|||||||
for topic, partitions := range assigned {
|
for topic, partitions := range assigned {
|
||||||
for _, partition := range partitions {
|
for _, partition := range partitions {
|
||||||
pc := &consumer{
|
pc := &consumer{
|
||||||
c: c,
|
c: c,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
partition: partition,
|
partition: partition,
|
||||||
htracer: s.htracer,
|
htracer: s.htracer,
|
||||||
quit: make(chan struct{}),
|
quit: make(chan struct{}),
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
recs: make(chan kgo.FetchTopicPartition, 100),
|
recs: make(chan kgo.FetchTopicPartition, 100),
|
||||||
handler: s.handler,
|
handler: s.handler,
|
||||||
kopts: s.kopts,
|
messagePool: s.messagePool,
|
||||||
opts: s.opts,
|
kopts: s.kopts,
|
||||||
connected: s.connected,
|
opts: s.opts,
|
||||||
|
connected: s.connected,
|
||||||
}
|
}
|
||||||
s.Lock()
|
s.mu.Lock()
|
||||||
s.consumers[tp{topic, partition}] = pc
|
s.consumers[tp{topic, partition}] = pc
|
||||||
s.Unlock()
|
s.mu.Unlock()
|
||||||
go pc.consume()
|
go pc.consume()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,6 +246,8 @@ func (pc *consumer) consume() {
|
|||||||
defer pc.kopts.Logger.Debug(pc.kopts.Context, fmt.Sprintf("killing, topic %s partition %d", pc.topic, pc.partition))
|
defer pc.kopts.Logger.Debug(pc.kopts.Context, fmt.Sprintf("killing, topic %s partition %d", pc.topic, pc.partition))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var pm *kgoMessage
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-pc.quit:
|
case <-pc.quit:
|
||||||
@@ -245,49 +257,59 @@ func (pc *consumer) consume() {
|
|||||||
ctx, sp := pc.htracer.WithProcessSpan(record)
|
ctx, sp := pc.htracer.WithProcessSpan(record)
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Inc()
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Inc()
|
||||||
p := messagePool.Get().(*kgoMessage)
|
|
||||||
|
|
||||||
p.body = record.Value
|
if pc.messagePool {
|
||||||
p.topic = record.Topic
|
pm = messagePool.Get().(*kgoMessage)
|
||||||
p.ack = false
|
} else {
|
||||||
p.hdr = metadata.New(len(record.Headers))
|
pm = &kgoMessage{}
|
||||||
p.ctx = ctx
|
}
|
||||||
|
pm.body = record.Value
|
||||||
|
pm.topic = record.Topic
|
||||||
|
pm.ack = false
|
||||||
|
pm.hdr = metadata.New(len(record.Headers))
|
||||||
|
pm.ctx = ctx
|
||||||
for _, hdr := range record.Headers {
|
for _, hdr := range record.Headers {
|
||||||
p.hdr.Set(hdr.Key, string(hdr.Value))
|
pm.hdr.Set(hdr.Key, string(hdr.Value))
|
||||||
}
|
}
|
||||||
|
|
||||||
switch h := pc.handler.(type) {
|
switch h := pc.handler.(type) {
|
||||||
case func(broker.Message) error:
|
case func(broker.Message) error:
|
||||||
err = h(p)
|
err = h(pm)
|
||||||
case func([]broker.Message) error:
|
case func([]broker.Message) error:
|
||||||
err = h([]broker.Message{p})
|
err = h([]broker.Message{pm})
|
||||||
}
|
}
|
||||||
|
|
||||||
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Dec()
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Dec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
if sp != nil {
|
||||||
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
||||||
|
}
|
||||||
pc.kopts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", record.Topic, "topic", record.Topic, "status", "failure").Inc()
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", record.Topic, "topic", record.Topic, "status", "failure").Inc()
|
||||||
} else if pc.opts.AutoAck {
|
} else if pc.opts.AutoAck {
|
||||||
p.ack = true
|
pm.ack = true
|
||||||
}
|
}
|
||||||
|
|
||||||
te := time.Since(ts)
|
te := time.Since(ts)
|
||||||
pc.kopts.Meter.Summary(semconv.SubscribeMessageLatencyMicroseconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
pc.kopts.Meter.Summary(semconv.SubscribeMessageLatencyMicroseconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
||||||
pc.kopts.Meter.Histogram(semconv.SubscribeMessageDurationSeconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
pc.kopts.Meter.Histogram(semconv.SubscribeMessageDurationSeconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
||||||
|
|
||||||
ack := p.ack
|
ack := pm.ack
|
||||||
messagePool.Put(p)
|
if pc.messagePool {
|
||||||
|
messagePool.Put(p)
|
||||||
|
}
|
||||||
if ack {
|
if ack {
|
||||||
pc.c.MarkCommitRecords(record)
|
pc.c.MarkCommitRecords(record)
|
||||||
} else {
|
} else {
|
||||||
sp.Finish()
|
if sp != nil {
|
||||||
|
sp.Finish()
|
||||||
|
}
|
||||||
// pc.connected.Store(0)
|
// pc.connected.Store(0)
|
||||||
pc.kopts.Logger.Fatal(pc.kopts.Context, "[kgo] message not commited")
|
pc.kopts.Logger.Fatal(pc.kopts.Context, "[kgo] message not commited")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if sp != nil {
|
||||||
sp.Finish()
|
sp.Finish()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
tracer.go
19
tracer.go
@@ -32,6 +32,9 @@ var (
|
|||||||
// the record's context, so it can be ended in the OnProduceRecordUnbuffered
|
// the record's context, so it can be ended in the OnProduceRecordUnbuffered
|
||||||
// hook.
|
// hook.
|
||||||
func (m *hookTracer) OnProduceRecordBuffered(r *kgo.Record) {
|
func (m *hookTracer) OnProduceRecordBuffered(r *kgo.Record) {
|
||||||
|
if !m.tracer.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
// Set up span options.
|
// Set up span options.
|
||||||
attrs := []interface{}{
|
attrs := []interface{}{
|
||||||
messagingSystem,
|
messagingSystem,
|
||||||
@@ -77,6 +80,9 @@ func (m *hookTracer) OnProduceRecordBuffered(r *kgo.Record) {
|
|||||||
// It sets attributes with values unset when producing and records any error
|
// It sets attributes with values unset when producing and records any error
|
||||||
// that occurred during the publish operation.
|
// that occurred during the publish operation.
|
||||||
func (m *hookTracer) OnProduceRecordUnbuffered(r *kgo.Record, err error) {
|
func (m *hookTracer) OnProduceRecordUnbuffered(r *kgo.Record, err error) {
|
||||||
|
if !m.tracer.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
if span, ok := tracer.SpanFromContext(r.Context); ok {
|
if span, ok := tracer.SpanFromContext(r.Context); ok {
|
||||||
span.AddLabels(
|
span.AddLabels(
|
||||||
semconv.MessagingKafkaDestinationPartition(int(r.Partition)),
|
semconv.MessagingKafkaDestinationPartition(int(r.Partition)),
|
||||||
@@ -96,6 +102,9 @@ func (m *hookTracer) OnProduceRecordUnbuffered(r *kgo.Record, err error) {
|
|||||||
// OnFetchRecordUnbuffered hook and can be used in downstream consumer
|
// OnFetchRecordUnbuffered hook and can be used in downstream consumer
|
||||||
// processing.
|
// processing.
|
||||||
func (m *hookTracer) OnFetchRecordBuffered(r *kgo.Record) {
|
func (m *hookTracer) OnFetchRecordBuffered(r *kgo.Record) {
|
||||||
|
if !m.tracer.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
// Set up the span options.
|
// Set up the span options.
|
||||||
attrs := []interface{}{
|
attrs := []interface{}{
|
||||||
messagingSystem,
|
messagingSystem,
|
||||||
@@ -141,6 +150,9 @@ func (m *hookTracer) OnFetchRecordBuffered(r *kgo.Record) {
|
|||||||
// OnFetchRecordUnbuffered continues and ends the "receive" span for an
|
// OnFetchRecordUnbuffered continues and ends the "receive" span for an
|
||||||
// unbuffered record.
|
// unbuffered record.
|
||||||
func (m *hookTracer) OnFetchRecordUnbuffered(r *kgo.Record, _ bool) {
|
func (m *hookTracer) OnFetchRecordUnbuffered(r *kgo.Record, _ bool) {
|
||||||
|
if !m.tracer.Enabled() {
|
||||||
|
return
|
||||||
|
}
|
||||||
span, _ := tracer.SpanFromContext(r.Context)
|
span, _ := tracer.SpanFromContext(r.Context)
|
||||||
span.Finish()
|
span.Finish()
|
||||||
}
|
}
|
||||||
@@ -155,6 +167,13 @@ func (m *hookTracer) OnFetchRecordUnbuffered(r *kgo.Record, _ bool) {
|
|||||||
// not a record which has been created for producing, so call this at the start of each
|
// not a record which has been created for producing, so call this at the start of each
|
||||||
// iteration of your processing for the record.
|
// iteration of your processing for the record.
|
||||||
func (m *hookTracer) WithProcessSpan(r *kgo.Record) (context.Context, tracer.Span) {
|
func (m *hookTracer) WithProcessSpan(r *kgo.Record) (context.Context, tracer.Span) {
|
||||||
|
if r.Context == nil {
|
||||||
|
r.Context = context.Background()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !m.tracer.Enabled() {
|
||||||
|
return r.Context, nil
|
||||||
|
}
|
||||||
// Set up the span options.
|
// Set up the span options.
|
||||||
attrs := []interface{}{
|
attrs := []interface{}{
|
||||||
messagingSystem,
|
messagingSystem,
|
||||||
|
Reference in New Issue
Block a user