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(collector): kafka exporter support key for raw producer message to make order and same partition #6384

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 cmd/monitor/collector/bootstrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ erda.oap.collector.exporter.stdout:

erda.oap.collector.exporter.kafka@collector:
metadata_key_of_topic: "KAFKA-TOPIC"
produce_raw_with_key: ${COLLECTOR_KAFKA_PRODUCE_RAW_WITH_KEY:false}

erda.oap.collector.exporter.kafka@spot-metrics:
topic: "spot-metrics"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,22 @@ type config struct {
Keyexclude []string `file:"keyexclude"`

MetadataKeyOfTopic string `file:"metadata_key_of_topic" desc:"note: only for raw data type"`
Topic string `file:"topic"`
// produce raw data with key for same partition in kafka detail: https://erda.cloud/erda/dop/projects/387/issues/all?id=611023&iterationID=12783&tab=BUG&type=BUG
ProduceRawWithKey bool `file:"produce_raw_with_key" default:"false"`
Topic string `file:"topic"`
}

var _ model.Exporter = (*provider)(nil)

type produceRawFunc func(item *odata.Raw) *sarama.ProducerMessage

// +provider
type provider struct {
Cfg *config
Log logs.Logger
Kafka kafka.Interface `autowired:"kafkago"`
writer writer.Writer
Cfg *config
Log logs.Logger
Kafka kafka.Interface `autowired:"kafkago"`
writer writer.Writer
rawFunc produceRawFunc
}

func (p *provider) ComponentClose() error {
Expand Down Expand Up @@ -83,25 +88,44 @@ func (p *provider) ExportSpan(items ...*trace.Span) error { return nil }
func (p *provider) ExportProfile(items ...*profile.Output) error { return nil }
func (p *provider) ExportRaw(items ...*odata.Raw) error {
for _, item := range items {
topic := p.Cfg.Topic
if p.Cfg.MetadataKeyOfTopic != "" {
tmp, ok := item.Meta[p.Cfg.MetadataKeyOfTopic]
if ok {
topic = tmp
}
}
msg := p.rawFunc(item)

if err := p.writer.Write(&sarama.ProducerMessage{
Topic: topic,
Value: sarama.ByteEncoder(item.Data),
}); err != nil {
p.Log.Errorf("write data to topic %s err: %s", topic, err)
if err := p.writer.Write(msg); err != nil {
p.Log.Errorf("write data to topic %s err: %s", msg.Topic, err)
}

}
return nil
}

func (p *provider) produceRawMessage(item *odata.Raw) *sarama.ProducerMessage {
topic := p.Cfg.Topic
if p.Cfg.MetadataKeyOfTopic != "" {
tmp, ok := item.Meta[p.Cfg.MetadataKeyOfTopic]
if ok {
topic = tmp
}
}
msg := &sarama.ProducerMessage{
Topic: topic,
Value: sarama.ByteEncoder(item.Data),
}
return msg
}

type key struct {
ID string `json:"id"`
}

func (p *provider) produceRawMessageWithKey(item *odata.Raw) *sarama.ProducerMessage {
msg := p.produceRawMessage(item)
var k key
if err := json.Unmarshal(item.Data, &k); err == nil && len(k.ID) > 0 {
msg.Key = sarama.ByteEncoder(k.ID)
}
return msg
}

func (p *provider) ComponentConfig() interface{} {
return p.Cfg
}
Expand All @@ -120,6 +144,10 @@ func (p *provider) Init(ctx servicehub.Context) error {
return err
}
p.writer = producer
p.rawFunc = p.produceRawMessage
if p.Cfg.ProduceRawWithKey {
p.rawFunc = p.produceRawMessageWithKey
}
return nil
}

Expand Down
Loading