Skip to content

Commit

Permalink
feat: mapper process latency metric added
Browse files Browse the repository at this point in the history
  • Loading branch information
erayarslan committed May 8, 2024
1 parent 43faa37 commit 710d6ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
18 changes: 17 additions & 1 deletion connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dcpcouchbase
import (
"errors"
"os"
"time"

"github.com/Trendyol/go-dcp/helpers"

Expand All @@ -24,6 +25,7 @@ import (
type Connector interface {
Start()
Close()
GetMapperProcessLatencyMs() int64
}

type connector struct {
Expand All @@ -32,6 +34,11 @@ type connector struct {
mapper Mapper
processor *couchbase.Processor
targetClient couchbase.TargetClient
metric *Metric
}

type Metric struct {
MapperProcessLatencyMs int64
}

func (c *connector) Start() {
Expand All @@ -47,6 +54,10 @@ func (c *connector) Close() {
c.processor.Close()
}

func (c *connector) GetMapperProcessLatencyMs() int64 {
return c.metric.MapperProcessLatencyMs
}

func (c *connector) listener(ctx *models.ListenerContext) {
var e couchbase.Event
switch event := ctx.Event.(type) {
Expand All @@ -69,13 +80,17 @@ func (c *connector) listener(ctx *models.ListenerContext) {
return
}

beforeMapperTime := time.Now()

actions := c.mapper(
couchbase.EventContext{
TargetClient: c.targetClient,
Event: e,
},
)

c.metric.MapperProcessLatencyMs = time.Since(beforeMapperTime).Milliseconds()

if len(actions) == 0 {
ctx.Ack()
return
Expand Down Expand Up @@ -114,6 +129,7 @@ func newConnector(cf any, mapper Mapper, sinkResponseHandler couchbase.SinkRespo
connector := &connector{
mapper: mapper,
config: cfg,
metric: &Metric{},
}

if err != nil {
Expand Down Expand Up @@ -158,7 +174,7 @@ func newConnector(cf any, mapper Mapper, sinkResponseHandler couchbase.SinkRespo
processor: connector.processor,
})

metricCollector := metric.NewMetricCollector(connector.processor)
metricCollector := metric.NewMetricCollector(connector.processor, connector.GetMapperProcessLatencyMs)
dcp.SetMetricCollectors(metricCollector)

return connector, nil
Expand Down
23 changes: 20 additions & 3 deletions metric/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
)

type Collector struct {
processor *couchbase.Processor
processor *couchbase.Processor
getMapperProcessLatencyMs func() int64

processLatency *prometheus.Desc
mapperProcessLatency *prometheus.Desc
bulkRequestProcessLatency *prometheus.Desc
bulkRequestSize *prometheus.Desc
bulkRequestByteSize *prometheus.Desc
Expand All @@ -29,6 +31,13 @@ func (s *Collector) Collect(ch chan<- prometheus.Metric) {
[]string{}...,
)

ch <- prometheus.MustNewConstMetric(
s.mapperProcessLatency,
prometheus.GaugeValue,
float64(s.getMapperProcessLatencyMs()),
[]string{}...,
)

ch <- prometheus.MustNewConstMetric(
s.bulkRequestProcessLatency,
prometheus.GaugeValue,
Expand All @@ -51,9 +60,10 @@ func (s *Collector) Collect(ch chan<- prometheus.Metric) {
)
}

func NewMetricCollector(processor *couchbase.Processor) *Collector {
func NewMetricCollector(processor *couchbase.Processor, getMapperProcessLatencyMs func() int64) *Collector {
return &Collector{
processor: processor,
processor: processor,
getMapperProcessLatencyMs: getMapperProcessLatencyMs,

processLatency: prometheus.NewDesc(
prometheus.BuildFQName(helpers.Name, "couchbase_connector_latency_ms", "current"),
Expand All @@ -62,6 +72,13 @@ func NewMetricCollector(processor *couchbase.Processor) *Collector {
nil,
),

mapperProcessLatency: prometheus.NewDesc(
prometheus.BuildFQName(helpers.Name, "couchbase_connector_mapper_latency_ms", "current"),
"Couchbase connector mapper latency ms",
[]string{},
nil,
),

bulkRequestProcessLatency: prometheus.NewDesc(
prometheus.BuildFQName(helpers.Name, "couchbase_connector_bulk_request_process_latency_ms", "current"),
"Couchbase connector bulk request process latency ms",
Expand Down

0 comments on commit 710d6ed

Please sign in to comment.