From 1fba3737b68fe3a6d2024d6d924b27eb538873c1 Mon Sep 17 00:00:00 2001 From: Kristina Spring Date: Thu, 21 May 2020 15:12:59 -0700 Subject: [PATCH 1/3] added metric to see how often caduceus is modifying the wrp --- CHANGELOG.md | 1 + http.go | 16 ++++++++++++++-- main.go | 1 + metrics.go | 13 +++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf397cdc..75cf14d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +- added metric for counting when caduceus re-encodes the wrp [#]() ## [v0.2.8] ### Changed diff --git a/http.go b/http.go index 10b03eb1..d4a7bfe7 100644 --- a/http.go +++ b/http.go @@ -37,6 +37,7 @@ type ServerHandler struct { emptyRequests metrics.Counter invalidCount metrics.Counter incomingQueueDepthMetric metrics.Gauge + modifiedWRPCount metrics.Counter incomingQueueDepth int64 maxOutstanding int64 } @@ -91,7 +92,7 @@ func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.R return } - sh.caduceusHandler.HandleRequest(0, fixWrp(msg)) + sh.caduceusHandler.HandleRequest(0, sh.fixWrp(msg)) // return a 202 response.WriteHeader(http.StatusAccepted) @@ -99,18 +100,29 @@ func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.R debugLog.Log(messageKey, "Request placed on to queue.") } -func fixWrp(msg *wrp.Message) *wrp.Message { +func (sh *ServerHandler) fixWrp(msg *wrp.Message) *wrp.Message { // "Fix" the WRP if needed. + var reason string // Default to "application/json" if there is no content type, otherwise // use the one the source specified. if "" == msg.ContentType { msg.ContentType = "application/json" + reason = emptyContentTypeReason } // Ensure there is a transaction id even if we make one up if "" == msg.TransactionUUID { msg.TransactionUUID = uuid.NewV4().String() + if reason == "" { + reason = emptyUUIDReason + } else { + reason = bothEmptyReason + } + } + + if reason != "" { + sh.modifiedWRPCount.With("reason", reason).Add(1.0) } return msg diff --git a/main.go b/main.go index 550fd966..b45babf8 100644 --- a/main.go +++ b/main.go @@ -134,6 +134,7 @@ func caduceus(arguments []string) int { emptyRequests: metricsRegistry.NewCounter(EmptyRequestBodyCounter), invalidCount: metricsRegistry.NewCounter(DropsDueToInvalidPayload), incomingQueueDepthMetric: metricsRegistry.NewGauge(IncomingQueueDepth), + modifiedWRPCount: metricsRegistry.NewCounter(ModifiedWRPCounter), maxOutstanding: 0, } diff --git a/metrics.go b/metrics.go index b0c2f698..c36d93e6 100644 --- a/metrics.go +++ b/metrics.go @@ -7,6 +7,7 @@ import ( const ( ErrorRequestBodyCounter = "error_request_body_count" EmptyRequestBodyCounter = "empty_request_body_count" + ModifiedWRPCounter = "modified_wrp_count" DeliveryCounter = "delivery_count" DeliveryRetryCounter = "delivery_retry_count" DeliveryRetryMaxGauge = "delivery_retry_max" @@ -25,6 +26,12 @@ const ( ConsumerMaxDeliveryWorkersGauge = "consumer_delivery_workers_max" ) +const ( + emptyContentTypeReason = "empty_content_type" + emptyUUIDReason = "empty_uuid" + bothEmptyReason = "empty_uuid_and_content_type" +) + func Metrics() []xmetrics.Metric { return []xmetrics.Metric{ { @@ -47,6 +54,12 @@ func Metrics() []xmetrics.Metric { Help: "Dropped messages due to invalid payloads.", Type: "counter", }, + { + Name: ModifiedWRPCounter, + Help: "Number of times a WRP was modified by Caduceus", + Type: "counter", + LabelNames: []string{"reason"}, + }, { Name: IncomingContentTypeCounter, Help: "Count of the content type processed.", From f215f96c810482d745e7ac4999d85518181ba626 Mon Sep 17 00:00:00 2001 From: Kristina Spring Date: Thu, 21 May 2020 15:15:09 -0700 Subject: [PATCH 2/3] updated changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75cf14d5..a5b794f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] -- added metric for counting when caduceus re-encodes the wrp [#]() +- added metric for counting when caduceus re-encodes the wrp [#216](https://github.com/xmidt-org/caduceus/pull/216) ## [v0.2.8] ### Changed From 0f6d9ca2757708a7d89c7ff10b13c6ec46d39aed Mon Sep 17 00:00:00 2001 From: Kristina Spring Date: Thu, 21 May 2020 15:39:32 -0700 Subject: [PATCH 3/3] fixed test --- http_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/http_test.go b/http_test.go index 58619c05..7ca29423 100644 --- a/http_test.go +++ b/http_test.go @@ -123,12 +123,17 @@ func TestServerHandlerFixWrp(t *testing.T) { fakeIncomingContentTypeCount.On("With", []string{"content_type", ""}).Return(fakeIncomingContentTypeCount) fakeIncomingContentTypeCount.On("Add", 1.0).Return() + fakeModifiedWRPCount := new(mockCounter) + fakeModifiedWRPCount.On("With", []string{"reason", bothEmptyReason}).Return(fakeIncomingContentTypeCount).Once() + fakeModifiedWRPCount.On("Add", 1.0).Return().Once() + serverWrapper := &ServerHandler{ Logger: logger, caduceusHandler: fakeHandler, errorRequests: fakeErrorRequests, emptyRequests: fakeEmptyRequests, invalidCount: fakeInvalidCount, + modifiedWRPCount: fakeModifiedWRPCount, incomingQueueDepthMetric: fakeQueueDepth, maxOutstanding: 1, }