-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to use GCP Pub/Sub instead of Kafka (#61)
- Loading branch information
Showing
20 changed files
with
494 additions
and
89 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package artie | ||
|
||
import ( | ||
"cloud.google.com/go/pubsub" | ||
"context" | ||
"fmt" | ||
"github.com/artie-labs/transfer/lib/telemetry/metrics" | ||
"github.com/segmentio/kafka-go" | ||
"time" | ||
) | ||
|
||
type Kind int | ||
|
||
const ( | ||
Invalid Kind = iota | ||
Kafka | ||
PubSub | ||
) | ||
|
||
type pubsubWrapper struct { | ||
topic string | ||
*pubsub.Message | ||
} | ||
|
||
type Message struct { | ||
KafkaMsg *kafka.Message | ||
PubSub *pubsubWrapper | ||
} | ||
|
||
func NewMessage(kafkaMsg *kafka.Message, pubsubMsg *pubsub.Message, topic string) Message { | ||
var msg Message | ||
if kafkaMsg != nil { | ||
msg.KafkaMsg = kafkaMsg | ||
} | ||
|
||
if pubsubMsg != nil { | ||
msg.PubSub = &pubsubWrapper{ | ||
topic: topic, | ||
Message: pubsubMsg, | ||
} | ||
} | ||
|
||
return msg | ||
} | ||
|
||
func (m *Message) Kind() Kind { | ||
if m.KafkaMsg != nil { | ||
return Kafka | ||
} | ||
|
||
if m.PubSub != nil { | ||
return PubSub | ||
} | ||
|
||
return Invalid | ||
} | ||
|
||
func (m *Message) EmitIngestionLag(ctx context.Context, groupID string) { | ||
metrics.FromContext(ctx).Timing("ingestion.lag", time.Since(m.PublishTime()), map[string]string{ | ||
"groupID": groupID, | ||
"topic": m.Topic(), | ||
"partition": m.Partition(), | ||
}) | ||
} | ||
|
||
func (m *Message) PublishTime() time.Time { | ||
if m.KafkaMsg != nil { | ||
return m.KafkaMsg.Time | ||
} | ||
|
||
if m.PubSub != nil { | ||
return m.PubSub.PublishTime | ||
} | ||
|
||
return time.Time{} | ||
} | ||
|
||
func (m *Message) Topic() string { | ||
if m.KafkaMsg != nil { | ||
return m.KafkaMsg.Topic | ||
} | ||
|
||
if m.PubSub != nil { | ||
return m.PubSub.topic | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func (m *Message) Partition() string { | ||
if m.KafkaMsg != nil { | ||
return fmt.Sprint(m.KafkaMsg.Partition) | ||
} | ||
|
||
if m.PubSub != nil { | ||
// Pub/Sub doesn't have partitions. | ||
return "no_partition" | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func (m *Message) Key() []byte { | ||
if m.KafkaMsg != nil { | ||
return m.KafkaMsg.Key | ||
} | ||
|
||
if m.PubSub != nil { | ||
return []byte(m.PubSub.OrderingKey) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *Message) Value() []byte { | ||
if m.KafkaMsg != nil { | ||
return m.KafkaMsg.Value | ||
} | ||
|
||
if m.PubSub != nil { | ||
return m.PubSub.Data | ||
} | ||
|
||
return nil | ||
} |
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,43 @@ | ||
package artie | ||
|
||
import ( | ||
"cloud.google.com/go/pubsub" | ||
"github.com/segmentio/kafka-go" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
const keyString = "Struct{id=12}" | ||
|
||
func TestNewMessage_Pubsub(t *testing.T) { | ||
msg := NewMessage(nil, nil, "") | ||
assert.Equal(t, msg.Topic(), "", "empty topic") | ||
|
||
pubsubMsg := &pubsub.Message{} | ||
msg = NewMessage(nil, pubsubMsg, "") | ||
assert.Equal(t, "no_partition", msg.Partition()) | ||
|
||
pubsubMsg.Data = []byte("hello_world") | ||
assert.Equal(t, "hello_world", string(msg.Value())) | ||
|
||
pubsubMsg.OrderingKey = keyString | ||
assert.Equal(t, []byte(keyString), msg.Key()) | ||
|
||
msg = NewMessage(nil, pubsubMsg, "database.schema.table") | ||
assert.Equal(t, "database.schema.table", msg.Topic()) | ||
} | ||
|
||
func TestNewMessage(t *testing.T) { | ||
kafkaMsg := &kafka.Message{ | ||
Topic: "test_topic", | ||
Partition: 5, | ||
Key: []byte(keyString), | ||
Value: []byte("kafka_value"), | ||
} | ||
|
||
msg := NewMessage(kafkaMsg, nil, "") | ||
assert.Equal(t, "test_topic", msg.Topic()) | ||
assert.Equal(t, "5", msg.Partition()) | ||
assert.Equal(t, keyString, string(msg.Key())) | ||
assert.Equal(t, "kafka_value", string(msg.Value())) | ||
} |
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
Oops, something went wrong.