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

"metric reset" disable via http header #88

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ module github.com/circonus-labs/circonus-agent
require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
github.com/circonus-labs/circonus-gometrics/v3 v3.0.0
github.com/circonus-labs/circonus-gometrics/v3 v3.0.2
github.com/circonus-labs/circonusllhist v0.1.4
github.com/circonus-labs/go-apiclient v0.7.6
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/gojuno/minimock/v3 v3.0.6
github.com/hashicorp/go-hclog v0.10.1 // indirect
github.com/hashicorp/go-retryablehttp v0.6.4 // indirect
github.com/maier/go-appstats v0.2.0
github.com/pelletier/go-toml v1.8.0
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/circonus-labs/circonus-gometrics/v3 v3.0.0 h1:5hbWwgfrYaSNCe+eKPGo457QiCe4T5+CfvY+bFGZBNw=
github.com/circonus-labs/circonus-gometrics/v3 v3.0.0/go.mod h1:1K33fx/wP96fC1uc7ZEp3BoLafJhL0kpjejIXpQHyLM=
github.com/circonus-labs/circonus-gometrics/v3 v3.0.2 h1:3HWtWWPpHUuBgjALLtE7PaQLEcAmxhmZjtYOoNofa7A=
github.com/circonus-labs/circonus-gometrics/v3 v3.0.2/go.mod h1:hbHb81YGFfRAgDZHE8J5kws/aDP1D40tkaTnhVfnqSw=
github.com/circonus-labs/circonusllhist v0.1.3 h1:TJH+oke8D16535+jHExHj4nQvzlZrj7ug5D7I/orNUA=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
github.com/circonus-labs/circonusllhist v0.1.4 h1:G5qJPuD16akpIXMUR7KcfBvrQOVm95+qyqUm+SEAZks=
Expand Down Expand Up @@ -120,6 +122,8 @@ github.com/hashicorp/go-retryablehttp v0.5.4 h1:1BZvpawXoJCWX6pNtow9+rpEj+3itIlu
github.com/hashicorp/go-retryablehttp v0.5.4/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.6.4 h1:BbgctKO892xEyOXnGiaAwIoSq1QZ/SS4AhjoAh9DnfY=
github.com/hashicorp/go-retryablehttp v0.6.4/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-retryablehttp v0.6.6 h1:HJunrbHTDDbBb/ay4kxa1n+dLmttUlnP3V9oNE4hmsM=
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
Expand Down
8 changes: 7 additions & 1 deletion internal/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,14 @@ func (s *Server) run(w http.ResponseWriter, r *http.Request) {
start := time.Now()
conduitID := "receiver"
numMetrics := 0
reset := strings.ToLower(r.Header.Get("x-circonus-receiver-reset"))
s.logger.Debug().Str("conduit_id", conduitID).Msg("start")
receiverMetrics := receiver.Flush()
var receiverMetrics *cgm.Metrics
if reset == "false" {
receiverMetrics = receiver.FlushNoReset()
} else {
receiverMetrics = receiver.Flush()
}
if receiverMetrics != nil && len(*receiverMetrics) > 0 {
numMetrics = len(*receiverMetrics)
conduitCh <- conduit{id: conduitID, metrics: receiverMetrics}
Expand Down
6 changes: 3 additions & 3 deletions internal/server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func TestPromOutput(t *testing.T) {
t.Fatal("expected NOT nil")
}

t.Logf("GET /prom -> %d (w/o metrics)", http.StatusNoContent)
t.Logf("GET /prom -> %d (w/o metrics)", http.StatusOK)
{
_ = httptest.NewRequest("GET", "/prom", nil)
w := httptest.NewRecorder()
Expand All @@ -430,8 +430,8 @@ func TestPromOutput(t *testing.T) {
resp := w.Result()
resp.Body.Close()

if resp.StatusCode != http.StatusNoContent {
t.Fatalf("expected %d, got %d", http.StatusNoContent, resp.StatusCode)
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected %d, got %d", http.StatusOK, resp.StatusCode)
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/server/receiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func Flush() *cgm.Metrics {
return metrics.FlushMetrics()
}

func FlushNoReset() *cgm.Metrics {
_ = initCGM()
return metrics.FlushMetricsNoReset()
}

// Parse handles incoming PUT/POST requests
func Parse(id string, data io.Reader) error {
if err := initCGM(); err != nil {
Expand Down
35 changes: 35 additions & 0 deletions internal/server/receiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,41 @@ func TestFlush(t *testing.T) {
}
}

func TestFlushNoReset(t *testing.T) {
t.Log("Testing FlushNoReset")

zerolog.SetGlobalLevel(zerolog.Disabled)

t.Log("\tno metrics")
{
m := FlushNoReset()
if len(*m) != 0 {
t.Fatalf("expected 0 metrics, got %d", len(*m))
}
}

t.Log("\tw/metric(s)")
{
err := initCGM()
if err != nil {
t.Fatalf("expected no error, got %s", err)
}
metrics.SetText("test", "test")

m := FlushNoReset()
if len(*m) != 1 {
t.Fatalf("expected 1 metric, got %d", len(*m))
}
m = FlushNoReset()
if len(*m) != 1 {
t.Fatalf("expected 1 metric, got %d", len(*m))
}
// this removes the test metric,
// since TestParse expects no metrics, and tests share a metric store
_ = Flush()
}
}

func TestParse(t *testing.T) {
t.Log("Testing Parse")

Expand Down
4 changes: 2 additions & 2 deletions internal/server/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func TestRouter(t *testing.T) {
{"GET", "/inventory/", http.StatusOK},
{"GET", "/stats", http.StatusOK},
{"GET", "/stats/", http.StatusOK},
{"GET", "/prom", http.StatusNoContent},
{"GET", "/prom/", http.StatusNoContent},
{"GET", "/prom", http.StatusOK},
{"GET", "/prom/", http.StatusOK},
}
// zerolog.SetGlobalLevel(zerolog.DebugLevel)

Expand Down