Skip to content

Commit

Permalink
export: Added "promtest" framework allowing tests against GCM and Pro…
Browse files Browse the repository at this point in the history
…metheus; Added counter test.

Signed-off-by: bwplotka <bwplotka@google.com>
  • Loading branch information
bwplotka committed Oct 17, 2023
1 parent ad833c5 commit 41a4d93
Show file tree
Hide file tree
Showing 56 changed files with 6,004 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/build
/**/node_modules/.cache
.idea/
e2e_*
.idea/
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ go 1.20
require (
cloud.google.com/go/compute/metadata v0.2.3
cloud.google.com/go/monitoring v1.16.0
github.com/efficientgo/e2e v0.14.0
github.com/go-kit/log v0.2.1
github.com/go-logr/logr v1.2.3
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.3
github.com/google/go-cmp v0.5.9
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/googleapis/gax-go/v2 v2.12.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/oklog/run v1.1.0
github.com/oklog/ulid v1.3.1
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_model v0.3.0
github.com/prometheus/common v0.41.0
github.com/prometheus/common/assets v0.2.0
github.com/prometheus/prometheus v1.8.2-0.20211119115433-692a54649ed7
Expand Down Expand Up @@ -48,6 +52,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/efficientgo/core v1.0.0-rc.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
Expand All @@ -64,7 +69,6 @@ require (
github.com/go-openapi/strfmt v0.21.3 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/validate v0.22.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
Expand All @@ -86,11 +90,9 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/alertmanager v0.25.1 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
6 changes: 5 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/edsrzf/mmap-go v1.1.0 h1:6EUwBLQ/Mcr1EYLE4Tn1VdW1A4ckqCQWZBw8Hr0kjpQ=
github.com/edsrzf/mmap-go v1.1.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q=
github.com/efficientgo/core v1.0.0-rc.0 h1:jJoA0N+C4/knWYVZ6GrdHOtDyrg8Y/TR4vFpTaqTsqs=
github.com/efficientgo/core v1.0.0-rc.0/go.mod h1:kQa0V74HNYMfuJH6jiPiwNdpWXl4xd/K4tzlrcvYDQI=
github.com/efficientgo/e2e v0.14.0 h1:Jxgeus4nq4COPhACC7nYRTKX1BTzSS86Z/Z2sW9W+kE=
github.com/efficientgo/e2e v0.14.0/go.mod h1:Hi+sz0REtlhVZ8zcdeTC3j6LUEEpJpPtNjOaOKuNcgI=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc=
github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=
github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
Expand Down Expand Up @@ -344,8 +348,8 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxv
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
6 changes: 6 additions & 0 deletions pkg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ type ExporterOpts struct {
Compression string
// Credentials file for authentication with the GCM API.
CredentialsFile string
// CredentialsFromJSON represents content of credentials file for
// authentication with the GCM API. CredentialsFile has priority over this.
CredentialsFromJSON []byte
// Disable authentication (for debugging purposes).
DisableAuth bool
// A user agent product string added to the regular user agent.
Expand Down Expand Up @@ -283,7 +286,10 @@ func newMetricClient(ctx context.Context, opts ExporterOpts) (*monitoring.Metric
}
if opts.CredentialsFile != "" {
clientOpts = append(clientOpts, option.WithCredentialsFile(opts.CredentialsFile))
} else if len(opts.CredentialsFromJSON) > 0 {
clientOpts = append(clientOpts, option.WithCredentialsJSON(opts.CredentialsFromJSON))
}

if opts.TokenURL != "" && opts.TokenBody != "" {
tokenSource := NewAltTokenSource(opts.TokenURL, opts.TokenBody)
clientOpts = append(clientOpts, option.WithTokenSource(tokenSource))
Expand Down
96 changes: 96 additions & 0 deletions pkg/export/export_gcm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package export_test

import (
"testing"
"time"

"github.com/GoogleCloudPlatform/prometheus-engine/pkg/export/promtest"
"github.com/prometheus/client_golang/prometheus"
)

func TestExportGCM_CounterReset(t *testing.T) {
const interval = 30 * time.Second

prom := promtest.Prometheus("quay.io/prometheus/prometheus:v2.47.2")
export := promtest.LocalExportWithGCM(promtest.GCMServiceAccountOrSkip(t))

it := promtest.NewIngestionTest(t, []promtest.Backend{prom, export})

counter := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "pe_test_counter_total",
Help: "Test counter used by prometheus-engine export GCM acceptance tests.",
}, []string{"foo"})
var c prometheus.Counter

it.RecordScrapes(func(r prometheus.Registerer, scrape promtest.Scrape) {
r.MustRegister(counter)

// No metric.
scrape(interval)

c = counter.WithLabelValues("bar")
c.Add(200)

scrape(interval).
Expect(200, c, prom)
// Nothing is expected for GMP due to cannibalization.
// TODO(bwplotka): Fix with b/259261536.

c.Add(10)
scrape(interval).
Expect(10, c, export).
Expect(210, c, prom)

c.Add(50)
scrape(interval).
Expect(60, c, export).
Expect(260, c, prom)

// Reset to 0, then add something that still should indicate "decreased counter"
// in absolute values, but our current cannibalization logic is wrong here.
// It's perhaps unusual that reset happens without scrape target restart
// so, it was easy to forget about this case.
counter.Reset()
c = counter.WithLabelValues("bar")
c.Add(250)
scrape(interval).
Expect(310, c, export). // TODO(bwplotka): Fix with b/259261536.
Expect(250, c, prom)

c.Add(50)
scrape(interval).
Expect(360, c, export).
Expect(300, c, prom)

// Reset to 0 again, our export does not detect it again.
counter.Reset()
c = counter.WithLabelValues("bar")
c.Add(100)
scrape(interval).
Expect(460, c, export). // TODO(bwplotka): Fix with b/259261536.
Expect(100, c, prom)

c.Add(50)
scrape(interval).
Expect(510, c, export).
Expect(150, c, prom)

// Tricky reset case, unnoticeable reset for Prometheus without created timestamp as well.
counter.Reset()
c = counter.WithLabelValues("bar")
c.Add(600)
scrape(interval).
// TODO(bwplotka): Fix using created timestamp.
Expect(960, c, export). // Also something is off, why 960, not 1110?
Expect(600, c, prom)
})

// Expect what we set we want for each backend.
for _, b := range []promtest.Backend{export, prom} {
t.Run(b.Ref(), func(t *testing.T) {
t.Parallel()

it.FatalOnUnexpectedPromQLResults(b, c, 2*time.Minute)
})
}
}
Loading

0 comments on commit 41a4d93

Please sign in to comment.