Skip to content

Commit

Permalink
[receiver/prometheus] speed up tests by setting skipOffsetting (#32341)
Browse files Browse the repository at this point in the history
Fixes #32298

This PR modifies prometheusreceiver to allow to set the skipOffsetting
option on its scrape option config. This option is private, so it can
only be set by reflection.

This removes the random offset to start added to scraping prometheus
metrics, so we may get faster CI builds.

---------

Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
Co-authored-by: David Ashpole <dashpole@google.com>
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
  • Loading branch information
4 people committed Apr 17, 2024
1 parent bc24c22 commit 8012101
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
18 changes: 16 additions & 2 deletions receiver/prometheusreceiver/metrics_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"sync"
"time"
"unsafe"

"github.com/go-kit/log"
"github.com/mitchellh/hashstructure/v2"
Expand Down Expand Up @@ -54,6 +56,7 @@ type pReceiver struct {
httpClient *http.Client
registerer prometheus.Registerer
unregisterMetrics func()
skipOffsetting bool // for testing only
}

// New creates a new prometheus.Receiver reference.
Expand Down Expand Up @@ -299,13 +302,24 @@ func (r *pReceiver) initPrometheusComponents(ctx context.Context, logger log.Log
return err
}

scrapeManager, err := scrape.NewManager(&scrape.Options{
opts := &scrape.Options{
PassMetadataInContext: true,
ExtraMetrics: r.cfg.ReportExtraScrapeMetrics,
HTTPClientOptions: []commonconfig.HTTPClientOption{
commonconfig.WithUserAgent(r.settings.BuildInfo.Command + "/" + r.settings.BuildInfo.Version),
},
}, logger, store, r.registerer)
}

// for testing only
if r.skipOffsetting {
optsValue := reflect.ValueOf(opts).Elem()
field := optsValue.FieldByName("skipOffsetting")
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).
Elem().
Set(reflect.ValueOf(true))
}

scrapeManager, err := scrape.NewManager(opts, logger, store, r.registerer)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ func testComponent(t *testing.T, targets []*testData, alterConfig func(*Config),

cms := new(consumertest.MetricsSink)
receiver := newPrometheusReceiver(receivertest.NewNopCreateSettings(), config, cms)
receiver.skipOffsetting = true

require.NoError(t, receiver.Start(ctx, componenttest.NewNopHost()))
// verify state after shutdown is called
Expand Down

0 comments on commit 8012101

Please sign in to comment.