Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #445

Merged
merged 4 commits into from
Mar 28, 2024
Merged

Tests #445

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,7 @@ var defaultConfig = Config{
Tracing: candlelight.Config{
ApplicationName: applicationName,
},
Sender: SenderConfig{
Linger: 3 * time.Minute,
},
}
18 changes: 9 additions & 9 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,37 @@
}
type HandlerTelemetryIn struct {
fx.In
ErrorRequests prometheus.Counter `name:"error_request_body_counter"`
EmptyRequests prometheus.Counter `name:"empty_request_boyd_counter"`
InvalidCount prometheus.Counter `name:"drops_due_to_invalid_payload"`
IncomingQueueDepthMetric prometheus.Gauge `name:"incoming_queue_depth"`
ModifiedWRPCount prometheus.CounterVec `name:"modified_wrp_count"`
IncomingQueueLatency prometheus.HistogramVec `name:"incoming_queue_latency_histogram_seconds"`
ErrorRequests prometheus.Counter `name:"error_request_body_count"`
EmptyRequests prometheus.Counter `name:"empty_request_body_count"`
InvalidCount prometheus.Counter `name:"drops_due_to_invalid_payload"`
IncomingQueueDepthMetric prometheus.Gauge `name:"incoming_queue_depth"`
ModifiedWRPCount *prometheus.CounterVec `name:"modified_wrp_count"`
IncomingQueueLatency prometheus.ObserverVec `name:"incoming_queue_latency_histogram_seconds"`
}
type HandlerTelemetry struct {
errorRequests prometheus.Counter
emptyRequests prometheus.Counter
invalidCount prometheus.Counter
incomingQueueDepthMetric prometheus.Gauge
modifiedWRPCount prometheus.CounterVec
incomingQueueLatency prometheus.HistogramVec
modifiedWRPCount *prometheus.CounterVec
incomingQueueLatency prometheus.ObserverVec
}

func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
eventType := unknownEventType
log := sallust.Get(request.Context())

Check warning on line 61 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L61

Added line #L61 was not covered by tests
// find time difference, add to metric after function finishes
defer func(s time.Time) {
sh.recordQueueLatencyToHistogram(s, eventType)
}(sh.now())

log.Info("Receiving incoming request...")

Check warning on line 67 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L67

Added line #L67 was not covered by tests

if len(request.Header["Content-Type"]) != 1 || request.Header["Content-Type"][0] != "application/msgpack" {
//return a 415
response.WriteHeader(http.StatusUnsupportedMediaType)
response.Write([]byte("Invalid Content-Type header(s). Expected application/msgpack. \n"))
log.Debug("Invalid Content-Type header(s). Expected application/msgpack. \n")

Check warning on line 73 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L73

Added line #L73 was not covered by tests
return
}

Expand All @@ -81,7 +81,7 @@
// return a 503
response.WriteHeader(http.StatusServiceUnavailable)
response.Write([]byte("Incoming queue is full.\n"))
log.Debug("Incoming queue is full.\n")

Check warning on line 84 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L84

Added line #L84 was not covered by tests
return
}

Expand All @@ -91,14 +91,14 @@
payload, err := io.ReadAll(request.Body)
if err != nil {
sh.telemetry.errorRequests.Add(1.0)
log.Error("Unable to retrieve the request body.", zap.Error(err))

Check warning on line 94 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L94

Added line #L94 was not covered by tests
response.WriteHeader(http.StatusBadRequest)
return
}

if len(payload) == 0 {
sh.telemetry.emptyRequests.Add(1.0)
log.Error("Empty payload.")

Check warning on line 101 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L101

Added line #L101 was not covered by tests
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte("Empty payload.\n"))
return
Expand All @@ -114,10 +114,10 @@
response.WriteHeader(http.StatusBadRequest)
if err != nil {
response.Write([]byte("Invalid payload format.\n"))
log.Debug("Invalid payload format.")

Check warning on line 117 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L117

Added line #L117 was not covered by tests
} else {
response.Write([]byte("Invalid MessageType.\n"))
log.Debug("Invalid MessageType.")

Check warning on line 120 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L120

Added line #L120 was not covered by tests
}
return
}
Expand All @@ -128,7 +128,7 @@
sh.telemetry.invalidCount.Add(1.0)
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte("Strings must be UTF-8.\n"))
log.Debug("Strings must be UTF-8.")

Check warning on line 131 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L131

Added line #L131 was not covered by tests
return
}
eventType = msg.FindEventStringSubMatch()
Expand All @@ -139,12 +139,12 @@
response.WriteHeader(http.StatusAccepted)
response.Write([]byte("Request placed on to queue.\n"))

log.Debug("event passed to senders.", zap.Any("event", msg))

Check warning on line 142 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L142

Added line #L142 was not covered by tests
}

func (sh *ServerHandler) recordQueueLatencyToHistogram(startTime time.Time, eventType string) {
endTime := sh.now()
sh.telemetry.incomingQueueLatency.With(prometheus.Labels{"event": eventType}).Observe(float64(endTime.Sub(startTime).Seconds()))
sh.telemetry.incomingQueueLatency.With(prometheus.Labels{"event": eventType}).Observe(endTime.Sub(startTime).Seconds())

Check warning on line 147 in http.go

View check run for this annotation

Codecov / codecov/patch

http.go#L147

Added line #L147 was not covered by tests
}

func (sh *ServerHandler) fixWrp(msg *wrp.Message) *wrp.Message {
Expand Down
6 changes: 3 additions & 3 deletions httpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@

type metricWrapper struct {
now func() time.Time
queryLatency prometheus.HistogramVec
queryLatency prometheus.ObserverVec
id string
}

func newMetricWrapper(now func() time.Time, queryLatency prometheus.HistogramVec, id string) (*metricWrapper, error) {
func newMetricWrapper(now func() time.Time, queryLatency prometheus.ObserverVec, id string) (*metricWrapper, error) {

Check warning on line 40 in httpClient.go

View check run for this annotation

Codecov / codecov/patch

httpClient.go#L40

Added line #L40 was not covered by tests
if now == nil {
now = time.Now
}
if queryLatency.MetricVec == nil {
if queryLatency == nil {

Check warning on line 44 in httpClient.go

View check run for this annotation

Codecov / codecov/patch

httpClient.go#L44

Added line #L44 was not covered by tests
return nil, errNilHistogram
}
return &metricWrapper{
Expand Down
34 changes: 17 additions & 17 deletions http_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
// // SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC
// // SPDX-License-Identifier: Apache-2.0
package main

// import (
Expand All @@ -13,8 +13,8 @@ package main

// "github.com/stretchr/testify/assert"
// "github.com/stretchr/testify/mock"
// "go.uber.org/zap/zaptest"

// "github.com/xmidt-org/webpa-common/v2/adapter"
// "github.com/xmidt-org/wrp-go/v3"
// )

Expand Down Expand Up @@ -88,7 +88,7 @@ package main
// for _, tc := range tcs {
// assert := assert.New(t)

// logger := adapter.DefaultLogger().Logger
// logger := zaptest.NewLogger(t)
// fakeHandler := new(mockHandler)
// if !tc.throwStatusBadRequest {
// fakeHandler.On("HandleRequest", mock.AnythingOfType("int"),
Expand All @@ -112,7 +112,7 @@ package main
// fakeHist.On("Observe", fakeLatency.Seconds()).Return().Once()

// serverWrapper := &ServerHandler{
// Logger: logger,
// log: logger,
// caduceusHandler: fakeHandler,
// errorRequests: fakeErrorRequests,
// emptyRequests: fakeEmptyRequests,
Expand Down Expand Up @@ -145,7 +145,7 @@ package main

// assert := assert.New(t)

// logger := adapter.DefaultLogger().Logger
// logger := zaptest.NewLogger(t)
// fakeHandler := new(mockHandler)
// fakeHandler.On("HandleRequest", mock.AnythingOfType("int"),
// mock.AnythingOfType("*wrp.Message")).Return().Once()
Expand All @@ -172,7 +172,7 @@ package main
// fakeHist.On("Observe", fakeLatency.Seconds()).Return().Once()

// serverWrapper := &ServerHandler{
// Logger: logger,
// log: logger,
// caduceusHandler: fakeHandler,
// errorRequests: fakeErrorRequests,
// emptyRequests: fakeEmptyRequests,
Expand Down Expand Up @@ -206,7 +206,7 @@ package main

// assert := assert.New(t)

// logger := adapter.DefaultLogger().Logger
// logger := zaptest.NewLogger(t)
// fakeHandler := new(mockHandler)
// fakeHandler.On("HandleRequest", mock.AnythingOfType("int"),
// mock.AnythingOfType("*wrp.Message")).WaitUntil(time.After(time.Second)).Times(2)
Expand All @@ -221,7 +221,7 @@ package main
// fakeHist.On("Observe", fakeLatency.Seconds()).Return().Once()

// serverWrapper := &ServerHandler{
// Logger: logger,
// log: logger,
// caduceusHandler: fakeHandler,
// incomingQueueDepthMetric: fakeQueueDepth,
// maxOutstanding: 1,
Expand Down Expand Up @@ -259,7 +259,7 @@ package main
// req := httptest.NewRequest("POST", "localhost:8080", r)
// req.Header.Set("Content-Type", wrp.MimeTypeMsgpack)

// logger := adapter.DefaultLogger().Logger
// logger := zaptest.NewLogger(t)
// fakeHandler := new(mockHandler)
// fakeHandler.On("HandleRequest", mock.AnythingOfType("int"),
// mock.AnythingOfType("*wrp.Message")).WaitUntil(time.After(time.Second)).Times(2)
Expand All @@ -276,7 +276,7 @@ package main
// fakeHist.On("Observe", fakeLatency.Seconds()).Return().Once()

// serverWrapper := &ServerHandler{
// Logger: logger,
// log: logger,
// caduceusHandler: fakeHandler,
// emptyRequests: fakeEmptyRequests,
// incomingQueueDepthMetric: fakeQueueDepth,
Expand Down Expand Up @@ -314,7 +314,7 @@ package main
// req := httptest.NewRequest("POST", "localhost:8080", r)
// req.Header.Set("Content-Type", wrp.MimeTypeMsgpack)

// logger := adapter.DefaultLogger().Logger
// logger := zaptest.NewLogger(t)
// fakeHandler := new(mockHandler)
// fakeHandler.On("HandleRequest", mock.AnythingOfType("int"),
// mock.AnythingOfType("*wrp.Message")).WaitUntil(time.After(time.Second)).Once()
Expand All @@ -331,7 +331,7 @@ package main
// fakeHist.On("Observe", fakeLatency.Seconds()).Return().Once()

// serverWrapper := &ServerHandler{
// Logger: logger,
// log: logger,
// caduceusHandler: fakeHandler,
// errorRequests: fakeErrorRequests,
// incomingQueueDepthMetric: fakeQueueDepth,
Expand Down Expand Up @@ -368,7 +368,7 @@ package main
// req := httptest.NewRequest("POST", "localhost:8080", r)
// req.Header.Set("Content-Type", wrp.MimeTypeMsgpack)

// logger := adapter.DefaultLogger().Logger
// logger := zaptest.NewLogger(t)
// fakeHandler := new(mockHandler)
// fakeHandler.On("HandleRequest", mock.AnythingOfType("int"),
// mock.AnythingOfType("*wrp.Message")).WaitUntil(time.After(time.Second)).Once()
Expand All @@ -386,7 +386,7 @@ package main
// fakeHist.On("Observe", fakeLatency.Seconds()).Return().Once()

// serverWrapper := &ServerHandler{
// Logger: logger,
// log: logger,
// caduceusHandler: fakeHandler,
// invalidCount: fakeInvalidCount,
// incomingQueueDepthMetric: fakeQueueDepth,
Expand Down Expand Up @@ -420,13 +420,13 @@ package main

// assert := assert.New(t)

// logger := adapter.DefaultLogger().Logger
// logger := zaptest.NewLogger(t)
// fakeHandler := new(mockHandler)

// fakeQueueDepth := new(mockGauge)

// serverWrapper := &ServerHandler{
// Logger: logger,
// log: logger,
// caduceusHandler: fakeHandler,
// 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 @@ -140,6 +140,7 @@ func caduceus(arguments []string, run bool) error {
touchstone.Provide(),
touchhttp.Provide(),
ProvideMetrics(),
ProvideSenderMetrics(),
// ancla.ProvideMetrics(), //TODO: need to add back in once we fix the ancla/argus dependency issue

)
Expand Down
Loading
Loading