-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from xataio/add-metrics
Add metrics
- Loading branch information
Showing
33 changed files
with
434 additions
and
64 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
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
87 changes: 87 additions & 0 deletions
87
internal/kafka/instrumentation/instrumented_kafka_writer.go
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,87 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package instrumentation | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/xataio/pgstream/internal/kafka" | ||
|
||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
type Writer struct { | ||
inner kafka.MessageWriter | ||
meter metric.Meter | ||
metrics *writerMetrics | ||
} | ||
|
||
type writerMetrics struct { | ||
batchSize metric.Int64Histogram | ||
batchBytes metric.Int64Histogram | ||
writeLatency metric.Int64Histogram | ||
} | ||
|
||
func NewWriter(inner kafka.MessageWriter, meter metric.Meter) (*Writer, error) { | ||
i := &Writer{ | ||
inner: inner, | ||
meter: meter, | ||
metrics: &writerMetrics{}, | ||
} | ||
|
||
if err := i.initMetrics(); err != nil { | ||
return nil, fmt.Errorf("error initialising kafka writer metrics: %w", err) | ||
} | ||
|
||
return i, nil | ||
} | ||
|
||
func (i *Writer) initMetrics() error { | ||
var err error | ||
i.metrics.batchSize, err = i.meter.Int64Histogram("pgstream.kafka.writer.batch.size", | ||
metric.WithUnit("messages"), | ||
metric.WithDescription("Distribution of message batch size written by the kafka writer")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
i.metrics.batchBytes, err = i.meter.Int64Histogram("pgstream.kafka.writer.batch.bytes", | ||
metric.WithUnit("bytes"), | ||
metric.WithDescription("Distribution of message batch bytes written by the kafka writer")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
i.metrics.writeLatency, err = i.meter.Int64Histogram("pgstream.kafka.writer.latency", | ||
metric.WithUnit("ms"), | ||
metric.WithDescription("Distribution of time taken by the writer to send messages to kafka")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *Writer) WriteMessages(ctx context.Context, msgs ...kafka.Message) (err error) { | ||
startTime := time.Now() | ||
defer func() { | ||
i.metrics.writeLatency.Record(ctx, time.Since(startTime).Milliseconds()) | ||
}() | ||
i.metrics.batchSize.Record(ctx, int64(len(msgs))) | ||
|
||
go func() { | ||
batchBytes := 0 | ||
for _, msg := range msgs { | ||
batchBytes += len(msg.Value) | ||
} | ||
i.metrics.batchBytes.Record(ctx, int64(batchBytes)) | ||
}() | ||
|
||
return i.inner.WriteMessages(ctx, msgs...) | ||
} | ||
|
||
func (i *Writer) Close() error { | ||
return i.inner.Close() | ||
} |
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
Oops, something went wrong.