Skip to content
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

feat: support segmentio/kafka.go.v0 #293

Merged
merged 25 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8c5f1cb
wip: feat: support segmentio/kafka.go.v0
rarguelloF Sep 24, 2024
24b275b
wip: segmentio/kafka-go implementation
rarguelloF Sep 25, 2024
56c948e
chore: update generated files
RomainMuller Sep 25, 2024
2150d79
add metrics and change type of meta to map[string]string
rarguelloF Sep 25, 2024
cd797bc
do not trace ReadMessage
rarguelloF Sep 25, 2024
4d9cd63
rename trace.Span to trace.Trace
rarguelloF Sep 25, 2024
c7658a9
fix RequireAnyMatch
rarguelloF Sep 26, 2024
5b0af0d
ctx gets modified
rarguelloF Sep 26, 2024
57571fa
fix sibling spans issue
rarguelloF Sep 26, 2024
8e29247
chore: update generated files
RomainMuller Sep 26, 2024
4f6a593
Merge branch 'main' into rarguellof/APPSEC-53289/segmentio-kafka
rarguelloF Sep 30, 2024
2ddefb6
update go.mod
rarguelloF Sep 30, 2024
901c6fb
refactor
rarguelloF Oct 8, 2024
cbaddd0
Merge branch 'main' into rarguellof/APPSEC-53289/segmentio-kafka
rarguelloF Oct 23, 2024
eeea7fc
used tagged version
rarguelloF Oct 23, 2024
726454d
Merge branch 'main' into rarguellof/APPSEC-53289/segmentio-kafka
rarguelloF Oct 23, 2024
151cc76
Merge branch 'main' into rarguellof/APPSEC-53289/segmentio-kafka
eliottness Oct 30, 2024
4e7d0d4
Merge branch 'main' into rarguellof/APPSEC-53289/segmentio-kafka
rarguelloF Oct 30, 2024
2353116
chore: update generated files
RomainMuller Oct 30, 2024
4873e8f
Merge branch 'main' into rarguellof/APPSEC-53289/segmentio-kafka
rarguelloF Oct 31, 2024
85dc6ca
prevent panic on macos
rarguelloF Oct 31, 2024
2801a98
Merge branch 'main' into rarguellof/APPSEC-53289/segmentio-kafka
rarguelloF Nov 5, 2024
2edb1d9
run integration tests only 10 at a time
rarguelloF Nov 5, 2024
6714b15
Revert "run integration tests only 10 at a time"
rarguelloF Nov 5, 2024
71134fa
remove unrelated change
rarguelloF Nov 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ require (
github.com/labstack/echo/v4 v4.12.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/redis/go-redis/v9 v9.7.0
github.com/segmentio/kafka-go v0.4.42
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.34.0
github.com/testcontainers/testcontainers-go/modules/cassandra v0.34.0
Expand Down
2 changes: 2 additions & 0 deletions _integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,8 @@ github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQ
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
github.com/secure-systems-lab/go-securesystemslib v0.8.0 h1:mr5An6X45Kb2nddcFlbmfHkLguCE9laoZCUzEEpIZXA=
github.com/secure-systems-lab/go-securesystemslib v0.8.0/go.mod h1:UH2VZVuJfCYR8WgMlCU1uFsOUU+KeyrTWcSS73NBOzU=
github.com/segmentio/kafka-go v0.4.42 h1:qffhBZCz4WcWyNuHEclHjIMLs2slp6mZO8px+5W5tfU=
github.com/segmentio/kafka-go v0.4.42/go.mod h1:d0g15xPMqoUookug0OU75DhGZxXwCFxSLeJ4uphwJzg=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
Expand Down
19 changes: 19 additions & 0 deletions _integration-tests/tests/segmentio_kafka.v0/gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

245 changes: 245 additions & 0 deletions _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

package segmentio_kafka_v0

import (
"context"
"errors"
"net"
"strconv"
"testing"
"time"

"github.com/segmentio/kafka-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
kafkatest "github.com/testcontainers/testcontainers-go/modules/kafka"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

"datadoghq.dev/orchestrion/_integration-tests/utils"
"datadoghq.dev/orchestrion/_integration-tests/validator/trace"
)

const (
topicA = "topic-A"
topicB = "topic-B"
consumerGroup = "group-A"
)

type TestCase struct {
kafka *kafkatest.KafkaContainer
addr string
writer *kafka.Writer
}

func (tc *TestCase) Setup(t *testing.T) {
utils.SkipIfProviderIsNotHealthy(t)

tc.kafka, tc.addr = utils.StartKafkaTestContainer(t)

tc.writer = &kafka.Writer{
Addr: kafka.TCP(tc.addr),
Balancer: &kafka.LeastBytes{},
}
tc.createTopic(t)
}

func (tc *TestCase) newReader(topic string) *kafka.Reader {
return kafka.NewReader(kafka.ReaderConfig{
Brokers: []string{tc.addr},
GroupID: consumerGroup,
Topic: topic,
MaxWait: 10 * time.Millisecond,
MaxBytes: 10e6, // 10MB
})
}

func (tc *TestCase) createTopic(t *testing.T) {
conn, err := kafka.Dial("tcp", tc.addr)
require.NoError(t, err)
defer conn.Close()

Check warning on line 65 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L65

Added line #L65 was not covered by tests
controller, err := conn.Controller()
require.NoError(t, err)

Check warning on line 68 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L68

Added line #L68 was not covered by tests
controllerConn, err := kafka.Dial("tcp", net.JoinHostPort(controller.Host, strconv.Itoa(controller.Port)))
require.NoError(t, err)
defer controllerConn.Close()

Check warning on line 72 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L72

Added line #L72 was not covered by tests
topicConfigs := []kafka.TopicConfig{
{

Check warning on line 74 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L74

Added line #L74 was not covered by tests
Topic: topicA,
NumPartitions: 1,
ReplicationFactor: 1,
},
{

Check warning on line 79 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L79

Added line #L79 was not covered by tests
Topic: topicB,
NumPartitions: 1,
ReplicationFactor: 1,
},
}

Check warning on line 84 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L84

Added line #L84 was not covered by tests
err = controllerConn.CreateTopics(topicConfigs...)
require.NoError(t, err)
}

func (tc *TestCase) Run(t *testing.T) {
tc.produce(t)
tc.consume(t)
}

func (tc *TestCase) produce(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Check warning on line 97 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L97

Added line #L97 was not covered by tests
span, ctx := tracer.StartSpanFromContext(ctx, "test.root")
defer span.Finish()

Check warning on line 100 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L100

Added line #L100 was not covered by tests
messages := []kafka.Message{
{

Check warning on line 102 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L102

Added line #L102 was not covered by tests
Topic: topicA,
Key: []byte("Key-A"),
Value: []byte("Hello World!"),
},
{

Check warning on line 107 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L107

Added line #L107 was not covered by tests
Topic: topicB,
Key: []byte("Key-A"),
Value: []byte("Second message"),
},
{

Check warning on line 112 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L112

Added line #L112 was not covered by tests
Topic: topicB,
Key: []byte("Key-A"),
Value: []byte("Third message"),
},
}

Check warning on line 117 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L117

Added line #L117 was not covered by tests
const (
maxRetries = 10
retryDelay = 100 * time.Millisecond
)
var (
retryCount int
err error
)
for retryCount < maxRetries {
err = tc.writer.WriteMessages(ctx, messages...)
if err == nil {
break
}
// This error happens sometimes with brand-new topics, as there is a delay between when the topic is created
// on the broker, and when the topic can actually be written to.
if errors.Is(err, kafka.UnknownTopicOrPartition) {
retryCount++
t.Logf("failed to produce kafka messages, will retry in %s (retryCount: %d)", retryDelay, retryCount)
time.Sleep(retryDelay)
}

Check warning on line 137 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L133-L137

Added lines #L133 - L137 were not covered by tests
}
require.NoError(t, err)
require.NoError(t, tc.writer.Close())
}

func (tc *TestCase) consume(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Check warning on line 146 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L146

Added line #L146 was not covered by tests
readerA := tc.newReader(topicA)
m, err := readerA.ReadMessage(ctx)
require.NoError(t, err)
assert.Equal(t, "Hello World!", string(m.Value))
assert.Equal(t, "Key-A", string(m.Key))
require.NoError(t, readerA.Close())

Check warning on line 153 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L153

Added line #L153 was not covered by tests
readerB := tc.newReader(topicB)
m, err = readerB.FetchMessage(ctx)
require.NoError(t, err)
assert.Equal(t, "Second message", string(m.Value))
assert.Equal(t, "Key-A", string(m.Key))
err = readerB.CommitMessages(ctx, m)
require.NoError(t, err)
require.NoError(t, readerB.Close())
}

func (tc *TestCase) Teardown(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
require.NoError(t, tc.kafka.Terminate(ctx))
}

func (*TestCase) ExpectedTraces() trace.Traces {
return trace.Traces{
{

Check warning on line 172 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L172

Added line #L172 was not covered by tests
Tags: map[string]any{
"name": "test.root",
},
Children: trace.Traces{
{

Check warning on line 177 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L177

Added line #L177 was not covered by tests
Tags: map[string]any{
"name": "kafka.produce",
"type": "queue",
"service": "kafka",
"resource": "Produce Topic topic-A",
},
Meta: map[string]string{
"span.kind": "producer",
"component": "segmentio/kafka.go.v0",
},
Children: trace.Traces{
{

Check warning on line 189 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L189

Added line #L189 was not covered by tests
Tags: map[string]any{
"name": "kafka.consume",
"type": "queue",
"service": "kafka",
"resource": "Consume Topic topic-A",
},
Meta: map[string]string{
"span.kind": "consumer",
"component": "segmentio/kafka.go.v0",
},
},
},
},
{

Check warning on line 203 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L203

Added line #L203 was not covered by tests
Tags: map[string]any{
"name": "kafka.produce",
"type": "queue",
"service": "kafka",
"resource": "Produce Topic topic-B",
},
Meta: map[string]string{
"span.kind": "producer",
"component": "segmentio/kafka.go.v0",
},
Children: trace.Traces{
{

Check warning on line 215 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L215

Added line #L215 was not covered by tests
Tags: map[string]any{
"name": "kafka.consume",
"type": "queue",
"service": "kafka",
"resource": "Consume Topic topic-B",
},
Meta: map[string]string{
"span.kind": "consumer",
"component": "segmentio/kafka.go.v0",
},
},
},
},
{

Check warning on line 229 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L229

Added line #L229 was not covered by tests
Tags: map[string]any{
"name": "kafka.produce",
"type": "queue",
"service": "kafka",
"resource": "Produce Topic topic-B",
},
Meta: map[string]string{
"span.kind": "producer",
"component": "segmentio/kafka.go.v0",
},
Children: nil,
},
},
},
}

Check warning on line 244 in _integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go

View check run for this annotation

Codecov / codecov/patch

_integration-tests/tests/segmentio_kafka.v0/segmentio_kafka.go#L244

Added line #L244 was not covered by tests
}
79 changes: 78 additions & 1 deletion internal/injector/builtin/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/injector/builtin/generated_deps.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading