Skip to content

Commit

Permalink
Merge pull request #216 from xmidt-org/encode-metric
Browse files Browse the repository at this point in the history
added metric to see how often caduceus is modifying the wrp
  • Loading branch information
schmidtw authored May 21, 2020
2 parents 0b5d084 + 8013bd7 commit 9b3e083
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- The next release needs to be v0.3.0 because #217 & #219.

## [Unreleased]
- added metric for counting when caduceus re-encodes the wrp [#216](https://github.com/xmidt-org/caduceus/pull/216)
- Made outgoing hostname validation configurable [#217](https://github.com/xmidt-org/caduceus/pull/217)
- **Note:** To be backwards compatable, the configuration value of `allowInsecureTLS: true` will need to be defined, otherwise hostname validation is enabled by default.
- removed contentTypeCounter [#218](https://github.com/xmidt-org/caduceus/pull/218)
- added configuration for which http codes Caduceus should retry on [#219](https://github.com/xmidt-org/caduceus/pull/219)
- **Note:** This configuration change causes the existing retry logic to change.



## [v0.2.8]
### Changed
- cleaned up shutdown logic for outbound sender [#205](https://github.com/xmidt-org/caduceus/pull/205)
Expand Down
16 changes: 14 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ServerHandler struct {
emptyRequests metrics.Counter
invalidCount metrics.Counter
incomingQueueDepthMetric metrics.Gauge
modifiedWRPCount metrics.Counter
incomingQueueDepth int64
maxOutstanding int64
}
Expand Down Expand Up @@ -91,26 +92,37 @@ 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)
response.Write([]byte("Request placed on to queue.\n"))
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
Expand Down
10 changes: 10 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,22 @@ func TestServerHandlerFixWrp(t *testing.T) {
fakeQueueDepth := new(mockGauge)
fakeQueueDepth.On("Add", mock.AnythingOfType("float64")).Return().Times(2)

fakeIncomingContentTypeCount := new(mockCounter)
fakeIncomingContentTypeCount.On("With", []string{"content_type", "application/msgpack"}).Return(fakeIncomingContentTypeCount)
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,
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func caduceus(arguments []string) int {
emptyRequests: metricsRegistry.NewCounter(EmptyRequestBodyCounter),
invalidCount: metricsRegistry.NewCounter(DropsDueToInvalidPayload),
incomingQueueDepthMetric: metricsRegistry.NewGauge(IncomingQueueDepth),
modifiedWRPCount: metricsRegistry.NewCounter(ModifiedWRPCounter),
maxOutstanding: 0,
}

Expand Down
19 changes: 19 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,6 +25,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{
{
Expand All @@ -46,6 +53,18 @@ 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.",
Type: "counter",
LabelNames: []string{"content_type"},
},
{
Name: DeliveryRetryCounter,
Help: "Number of delivery retries made",
Expand Down

0 comments on commit 9b3e083

Please sign in to comment.