-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: promauto and pushgateway measurement middlewares (#83)
- Loading branch information
Showing
6 changed files
with
117 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package promauto | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/confluentinc/confluent-kafka-go/v2/kafka" | ||
"github.com/honestbank/kp/v2/middlewares" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
var operationDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ | ||
Name: "kp_operation_duration_milliseconds", | ||
Buckets: []float64{1, 5, 50, 200, 500, 1_000, 2_000, 5_000, 15_000, 45_000}, | ||
}, []string{"application_name", "result", "error"}) | ||
|
||
type measurementMiddleware struct { | ||
applicationName string | ||
} | ||
|
||
func (m measurementMiddleware) Process(ctx context.Context, item *kafka.Message, next func(ctx context.Context, item *kafka.Message) error) error { | ||
startTime := time.Now() | ||
err := next(ctx, item) | ||
if err != nil { | ||
operationDuration.WithLabelValues(m.applicationName, "failure", err.Error()).Observe(float64(time.Since(startTime).Milliseconds())) | ||
|
||
return err | ||
} | ||
operationDuration.WithLabelValues(m.applicationName, "success", "").Observe(float64(time.Since(startTime).Milliseconds())) | ||
|
||
return err | ||
} | ||
|
||
func NewMeasurementMiddleware(applicationName string) middlewares.KPMiddleware[*kafka.Message] { | ||
return measurementMiddleware{ | ||
applicationName: applicationName, | ||
} | ||
} |
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,66 @@ | ||
package promauto_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"net/http/httptest" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/confluentinc/confluent-kafka-go/v2/kafka" | ||
"github.com/honestbank/kp/v2/middlewares/measurement/promauto" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMeasure(t *testing.T) { | ||
t.Run("calls next", func(t *testing.T) { | ||
called := false | ||
promauto.NewMeasurementMiddleware("integration_test").Process(context.Background(), nil, func(ctx context.Context, msg *kafka.Message) error { | ||
called = true | ||
time.Sleep(time.Millisecond * 550) | ||
return nil | ||
}) | ||
assert.True(t, called) | ||
}) | ||
t.Run("works if message errors", func(t *testing.T) { | ||
promauto.NewMeasurementMiddleware("integration_test").Process(context.Background(), nil, func(ctx context.Context, msg *kafka.Message) error { | ||
return errors.New("some error") | ||
}) | ||
}) | ||
t.Run("works even when push gateway url is not set", func(t *testing.T) { | ||
called := false | ||
promauto.NewMeasurementMiddleware("integration_test").Process(context.Background(), nil, func(ctx context.Context, msg *kafka.Message) error { | ||
called = true | ||
time.Sleep(time.Millisecond * 550) | ||
|
||
return errors.New("some random error") | ||
}) | ||
assert.True(t, called) | ||
}) | ||
time.Sleep(time.Second * 6) | ||
t.Run("pushes to prometheus", func(t *testing.T) { | ||
success200, _ := regexp.Compile(`kp_operation_duration_milliseconds_bucket\{.+success.+le="200"\}.+`) | ||
success500, _ := regexp.Compile(`kp_operation_duration_milliseconds_bucket\{.+success.+le="1000"\}.+`) | ||
recorder := httptest.NewRecorder() | ||
handler := promhttp.Handler() | ||
promauto.NewMeasurementMiddleware("integration_test").Process(context.Background(), nil, func(ctx context.Context, msg *kafka.Message) error { | ||
time.Sleep(time.Millisecond * 550) | ||
|
||
return nil | ||
}) | ||
handler.ServeHTTP(recorder, httptest.NewRequest("GET", "/metrics", nil)) | ||
bytes, err := io.ReadAll(recorder.Result().Body) | ||
assert.NoError(t, err) | ||
scrapedValues := string(bytes) | ||
successMatches200 := success200.FindStringSubmatch(scrapedValues) | ||
successMatches500 := success500.FindStringSubmatch(scrapedValues) | ||
assert.Len(t, successMatches200, 1) | ||
assert.True(t, strings.HasSuffix(successMatches200[0], "0")) | ||
assert.Len(t, successMatches500, 1) | ||
assert.False(t, strings.HasSuffix(successMatches500[0], "0")) | ||
}) | ||
} |
5 changes: 2 additions & 3 deletions
5
v2/middlewares/measurement/measurement.go → ...es/measurement/pushgateway/measurement.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
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