Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: hfuss <hayden.fuss@kaleido.io>
  • Loading branch information
onelapahead committed Dec 16, 2024
1 parent ef634af commit 5fa7738
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/ffresty/ffresty.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func NewWithConfig(ctx context.Context, ffrestyConfig Config) (client *resty.Cli
}
rCtx := req.Context()
// Record host in context to avoid redundant parses in hooks
u, _ := url.Parse(req.URL)
u, _ := url.Parse(_url)
host := u.Host
rCtx = context.WithValue(rCtx, hostCtxKey{}, host)
rc := rCtx.Value(retryCtxKey{})
Expand Down
2 changes: 1 addition & 1 deletion pkg/ffresty/ffresty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,6 @@ func TestEnableClientMetrics(t *testing.T) {

err := EnableClientMetrics(ctx, mr)
assert.NoError(t, err)

}

func TestEnableClientMetricsIdempotent(t *testing.T) {
Expand All @@ -701,6 +700,7 @@ func TestHooks(t *testing.T) {
}

customOnSuccess := func(c *resty.Client, resp *resty.Response) {
assert.Equal(t, "localhost:12345", resp.Request.Context().Value(hostCtxKey{}).(string))
onSuccessCount++
}

Expand Down
32 changes: 19 additions & 13 deletions pkg/metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func TestMetricsManager(t *testing.T) {
mm.IncCounterMetric(ctx, "tx_request", nil)
mm.IncCounterMetricWithLabels(ctx, "tx_process", map[string]string{"status": "success"}, nil)
mm.SetGaugeMetric(ctx, "tx_stalled", 2, nil)
mm.SetGaugeMetricWithLabels(ctx, "tx_inflight", 2, map[string]string{"stage": "singing:)"}, nil)
mm.SetGaugeMetricWithLabels(ctx, "tx_inflight", 2, map[string]string{"stage": "signing"}, nil)
mm.ObserveHistogramMetric(ctx, "tx_timeout_seconds", 2000, nil)
mm.ObserveHistogramMetricWithLabels(ctx, "tx_stage_seconds", 2000, map[string]string{"stage": "singing:)"}, nil)
mm.ObserveHistogramMetricWithLabels(ctx, "tx_stage_seconds", 2000, map[string]string{"stage": "signing"}, nil)
mm.ObserveHistogramMetricWithLabels(ctx, "tx_stage_seconds", 2000, map[string]string{}, nil) // no label provided
mm.ObserveSummaryMetric(ctx, "tx_request_bytes", 2000, nil)
mm.ObserveSummaryMetricWithLabels(ctx, "tx_retry_bytes", 2000, map[string]string{"stage": "singing:)"}, nil)
mm.ObserveSummaryMetricWithLabels(ctx, "tx_retry_bytes", 2000, map[string]string{"stage": "signing"}, nil)
}

func TestMetricsManagerWithDefaultLabels(t *testing.T) {
Expand All @@ -87,6 +87,8 @@ func TestMetricsManagerWithDefaultLabels(t *testing.T) {
mm.NewCounterMetricWithLabels(ctx, "tx_process", "Transaction processed", []string{"status"}, true)
mm.NewGaugeMetric(ctx, "tx_stalled", "Transactions that are stuck in a loop", true)
mm.NewGaugeMetricWithLabels(ctx, "tx_inflight", "Transactions that are in flight", []string{"stage"}, true)
mm.NewGaugeMetric(ctx, "tx_deadlettered", "Transactions that are put onto the deadletter queue", true)
mm.NewGaugeMetricWithLabels(ctx, "tx_confirming", "Transactions that are waiting to be confirmed", []string{"stage"}, true)
mm.NewHistogramMetric(ctx, "tx_timeout_seconds", "Duration of timed out transactions", []float64{}, true)
mm.NewHistogramMetricWithLabels(ctx, "tx_stage_seconds", "Duration of each transaction stage", []float64{}, []string{"stage"}, true)
mm.NewSummaryMetric(ctx, "tx_request_bytes", "Request size of timed out transactions", true)
Expand All @@ -96,21 +98,25 @@ func TestMetricsManagerWithDefaultLabels(t *testing.T) {
mm.IncCounterMetric(ctx, "tx_request", nil)
mm.IncCounterMetricWithLabels(ctx, "tx_process", map[string]string{"status": "success"}, nil)
mm.SetGaugeMetric(ctx, "tx_stalled", 2, nil)
mm.SetGaugeMetricWithLabels(ctx, "tx_inflight", 2, map[string]string{"stage": "singing:)"}, nil)
mm.SetGaugeMetricWithLabels(ctx, "tx_inflight", 2, map[string]string{"stage": "signing"}, nil)
mm.IncGaugeMetric(ctx, "tx_deadlettered", nil)
mm.IncGaugeMetricWithLabels(ctx, "tx_confirming", map[string]string{"stage": "signing"}, nil)
mm.DecGaugeMetric(ctx, "tx_deadlettered", nil)
mm.DecGaugeMetricWithLabels(ctx, "tx_confirming", map[string]string{"stage": "signing"}, nil)
mm.ObserveHistogramMetric(ctx, "tx_timeout_seconds", 2000, nil)
mm.ObserveHistogramMetricWithLabels(ctx, "tx_stage_seconds", 2000, map[string]string{"stage": "singing:)"}, nil)
mm.ObserveHistogramMetricWithLabels(ctx, "tx_stage_seconds", 2000, map[string]string{"stage": "signing"}, nil)
mm.ObserveSummaryMetric(ctx, "tx_request_bytes", 2000, nil)
mm.ObserveSummaryMetricWithLabels(ctx, "tx_retry_bytes", 2000, map[string]string{"stage": "singing:)"}, nil)
mm.ObserveSummaryMetricWithLabels(ctx, "tx_retry_bytes", 2000, map[string]string{"stage": "signing"}, nil)

mm.IncCounterMetric(ctx, "tx_request", &FireflyDefaultLabels{Namespace: "ns_1"})
mm.IncCounterMetricWithLabels(ctx, "tx_process", map[string]string{"status": "success"}, &FireflyDefaultLabels{Namespace: "ns_1"})
mm.SetGaugeMetric(ctx, "tx_stalled", 2, &FireflyDefaultLabels{Namespace: "ns_1"})
mm.SetGaugeMetricWithLabels(ctx, "tx_inflight", 2, map[string]string{"stage": "singing:)"}, &FireflyDefaultLabels{Namespace: "ns_1"})
mm.SetGaugeMetricWithLabels(ctx, "tx_inflight", 2, map[string]string{"stage": "signing"}, &FireflyDefaultLabels{Namespace: "ns_1"})
mm.ObserveHistogramMetric(ctx, "tx_timeout_seconds", 2000, &FireflyDefaultLabels{Namespace: "ns_1"})
mm.ObserveHistogramMetricWithLabels(ctx, "tx_stage_seconds", 2000, map[string]string{"stage": "singing:)"}, &FireflyDefaultLabels{Namespace: "ns_1"})
mm.ObserveHistogramMetricWithLabels(ctx, "tx_stage_seconds", 2000, map[string]string{"stage": "signing"}, &FireflyDefaultLabels{Namespace: "ns_1"})
mm.ObserveHistogramMetricWithLabels(ctx, "tx_stage_seconds", 2000, map[string]string{}, nil) // no label provided
mm.ObserveSummaryMetric(ctx, "tx_request_bytes", 2000, &FireflyDefaultLabels{Namespace: "ns_1"})
mm.ObserveSummaryMetricWithLabels(ctx, "tx_retry_bytes", 2000, map[string]string{"stage": "singing:)"}, &FireflyDefaultLabels{Namespace: "ns_1"})
mm.ObserveSummaryMetricWithLabels(ctx, "tx_retry_bytes", 2000, map[string]string{"stage": "signing"}, &FireflyDefaultLabels{Namespace: "ns_1"})

}

Expand Down Expand Up @@ -142,12 +148,12 @@ func TestMetricsManagerErrors(t *testing.T) {
mm.IncCounterMetric(ctx, "tx_not_exist", nil)
mm.IncCounterMetricWithLabels(ctx, "tx_not_exist", map[string]string{"status": "success"}, nil)
mm.SetGaugeMetric(ctx, "tx_not_exist", 2, nil)
mm.SetGaugeMetricWithLabels(ctx, "tx_not_exist", 2, map[string]string{"stage": "singing:)"}, nil)
mm.SetGaugeMetricWithLabels(ctx, "tx_not_exist", 2, map[string]string{"stage": "signing"}, nil)
mm.ObserveHistogramMetric(ctx, "tx_not_exist", 2000, nil)
mm.ObserveHistogramMetricWithLabels(ctx, "tx_not_exist", 2000, map[string]string{"stage": "singing:)"}, nil)
mm.ObserveHistogramMetricWithLabels(ctx, "tx_not_exist", 2000, map[string]string{"stage": "signing"}, nil)
mm.ObserveSummaryMetric(ctx, "tx_not_exist", 2000, nil)
mm.ObserveSummaryMetricWithLabels(ctx, "tx_not_exist", 2000, map[string]string{"stage": "singing:)"}, nil)
mm.SetGaugeMetricWithLabels(ctx, "tx_inflight_invalid_labels", 2, map[string]string{"ff_stage": "singing:)"}, nil)
mm.ObserveSummaryMetricWithLabels(ctx, "tx_not_exist", 2000, map[string]string{"stage": "signing"}, nil)
mm.SetGaugeMetricWithLabels(ctx, "tx_inflight_invalid_labels", 2, map[string]string{"ff_stage": "signing"}, nil)
}

func TestHTTPMetricsInstrumentations(t *testing.T) {
Expand Down

0 comments on commit 5fa7738

Please sign in to comment.