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

Add name for requests in stats #272

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 8 additions & 5 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ type ReaderConfig struct {
}

type ConsumeConfig struct {
Limit int64 `json:"limit"`
Limit int64 `json:"limit"`
Name string `json:"name"`
}

type Duration struct {
Expand Down Expand Up @@ -323,6 +324,7 @@ func (k *Kafka) consume(
}

messages := make([]map[string]interface{}, 0)
messagesName := consumeConfig.Name

maxWait := reader.Config().MaxWait

Expand All @@ -331,15 +333,15 @@ func (k *Kafka) consume(
msg, err := reader.ReadMessage(ctxWithTimeout)
cancel()
if errors.Is(err, io.EOF) {
k.reportReaderStats(reader.Stats())
k.reportReaderStats(reader.Stats(), messagesName)

err = NewXk6KafkaError(noMoreMessages, "No more messages.", nil)
logger.WithField("error", err).Info(err)
return messages
}

if err != nil {
k.reportReaderStats(reader.Stats())
k.reportReaderStats(reader.Stats(), messagesName)

err = NewXk6KafkaError(failedReadMessage, "Unable to read messages.", nil)
logger.WithField("error", err).Error(err)
Expand Down Expand Up @@ -376,13 +378,13 @@ func (k *Kafka) consume(
messages = append(messages, message)
}

k.reportReaderStats(reader.Stats())
k.reportReaderStats(reader.Stats(), messagesName)
return messages
}

// reportReaderStats reports the reader stats
// nolint:funlen
func (k *Kafka) reportReaderStats(currentStats kafkago.ReaderStats) {
func (k *Kafka) reportReaderStats(currentStats kafkago.ReaderStats, messagesName string) {
state := k.vu.State()
if state == nil {
logger.WithField("error", ErrForbiddenInInitContext).Error(ErrForbiddenInInitContext)
Expand All @@ -400,6 +402,7 @@ func (k *Kafka) reportReaderStats(currentStats kafkago.ReaderStats) {
sampleTags := ctm.Tags.With("topic", currentStats.Topic)
sampleTags = sampleTags.With("clientid", currentStats.ClientID)
sampleTags = sampleTags.With("partition", currentStats.Partition)
sampleTags = sampleTags.With("name", messagesName)

now := time.Now()
metrics.PushIfNotDone(ctx, state.Samples, metrics.ConnectedSamples{
Expand Down
7 changes: 5 additions & 2 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Message struct {
Key []byte `json:"key"`
Value []byte `json:"value"`
Headers map[string]interface{} `json:"headers"`
Name string `json:"name"`

// If not set at the creation, Time will be automatically set when
// writing the message.
Expand Down Expand Up @@ -207,6 +208,7 @@ func (k *Kafka) produce(writer *kafkago.Writer, produceConfig *ProduceConfig) {
}

kafkaMessages := make([]kafkago.Message, len(produceConfig.Messages))
kafkaMessagesName := produceConfig.Messages[0].Name
for index, message := range produceConfig.Messages {
kafkaMessages[index] = kafkago.Message{
Offset: message.Offset,
Expand Down Expand Up @@ -247,7 +249,7 @@ func (k *Kafka) produce(writer *kafkago.Writer, produceConfig *ProduceConfig) {

originalErr := writer.WriteMessages(k.vu.Context(), kafkaMessages...)

k.reportWriterStats(writer.Stats())
k.reportWriterStats(writer.Stats(), kafkaMessagesName)

if originalErr != nil {
err := NewXk6KafkaError(writerError, "Error writing messages.", originalErr)
Expand All @@ -258,7 +260,7 @@ func (k *Kafka) produce(writer *kafkago.Writer, produceConfig *ProduceConfig) {

// reportWriterStats reports the writer stats to the state.
// nolint: funlen
func (k *Kafka) reportWriterStats(currentStats kafkago.WriterStats) {
func (k *Kafka) reportWriterStats(currentStats kafkago.WriterStats, messagesName string) {
state := k.vu.State()
if state == nil {
logger.WithField("error", ErrForbiddenInInitContext).Error(ErrForbiddenInInitContext)
Expand All @@ -274,6 +276,7 @@ func (k *Kafka) reportWriterStats(currentStats kafkago.WriterStats) {

ctm := k.vu.State().Tags.GetCurrentValues()
sampleTags := ctm.Tags.With("topic", currentStats.Topic)
sampleTags = sampleTags.With("name", messagesName)

now := time.Now()

Expand Down
Loading