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

[receiver/purefa] Ensuring test efficiency for the endpoints scraped on Mock server #23270

Closed
wants to merge 3 commits into from
Closed
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
20 changes: 15 additions & 5 deletions receiver/purefareceiver/internal/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ func (h *scraper) ToPrometheusReceiverConfig(host component.Host, _ receiver.Fac
return nil, err
}

bearerToken, err := RetrieveBearerToken(arr.Auth, host.GetExtensions())
if err != nil {
httpConfig, err := getHTTPConfig(arr, host)
if err != nil {
return nil, err
}

httpConfig := configutil.HTTPClientConfig{}
httpConfig.BearerToken = configutil.Secret(bearerToken)

scrapeConfig := &config.ScrapeConfig{
HTTPClientConfig: httpConfig,
ScrapeInterval: model.Duration(h.scrapeInterval),
Expand Down Expand Up @@ -101,3 +98,16 @@ func (h *scraper) ToPrometheusReceiverConfig(host component.Host, _ receiver.Fac

return scrapeCfgs, nil
}

func getHTTPConfig(scCfgs ScraperConfig, host component.Host) (configutil.HTTPClientConfig, error){
ret := configutil.HTTPClientConfig{}
if scCfgs.Auth != nil {
bearerToken, err := RetrieveBearerToken(*scCfgs.Auth, host.GetExtensions())
if err != nil {
return ret, err
}

ret.BearerToken = configutil.Secret(bearerToken)
}
return ret, nil
}
2 changes: 1 addition & 1 deletion receiver/purefareceiver/internal/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestToPrometheusConfig(t *testing.T) {
cfgs := []ScraperConfig{
{
Address: "array01",
Auth: configauth.Authentication{
Auth: &configauth.Authentication{
AuthenticatorID: component.NewIDWithName("bearertokenauth", "array01"),
},
},
Expand Down
2 changes: 1 addition & 1 deletion receiver/purefareceiver/internal/scraperconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import "go.opentelemetry.io/collector/config/configauth"

type ScraperConfig struct {
Address string `mapstructure:"address"`
Auth configauth.Authentication `mapstructure:"auth"`
Auth *configauth.Authentication `mapstructure:"auth"`
}
62 changes: 62 additions & 0 deletions receiver/purefareceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,77 @@ package purefareceiver // import "github.com/open-telemetry/opentelemetry-collec

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/purefareceiver/internal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
)

func TestReceiverArray(t *testing.T) {
// prepare
wg := &sync.WaitGroup{}

once := &sync.Once{}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var data []byte
var err error

once.Do(func() {
data, err = os.ReadFile("testdata/array.txt")
require.NoError(t, err)
wg.Done()
})

fmt.Println("Received request:", r.URL)

_, err = w.Write(data)
require.NoError(t, err)
fmt.Println(string(data))

}))
defer ts.Close()

cfg, ok := createDefaultConfig().(*Config)
require.True(t, ok)

cfg.Endpoint = ts.URL
cfg.Array = []internal.ScraperConfig{{
Address: "array01",
}}
cfg.Settings = &Settings{
ReloadIntervals: &ReloadIntervals{
Array: 10 * time.Millisecond,
},
}

sink := &consumertest.MetricsSink{}
recv := newReceiver(cfg, receivertest.NewNopCreateSettings(), sink)
wg.Add(1)

// test
err := recv.Start(context.Background(), componenttest.NewNopHost())
wg.Wait()

// verify
assert.NoError(t, err)
//assert.Greater(t, len(sink.AllMetrics()), 0, "expected to have received more than 0 metrics")
//require.Equal(t, len(sink.AllMetrics()), 0)
assert.Eventually(t, func() bool {
return len(sink.AllMetrics()) == 1
}, 10*time.Second, 10*time.Millisecond)

}

func TestStart(t *testing.T) {
// prepare
cfg, ok := createDefaultConfig().(*Config)
Expand Down
15 changes: 15 additions & 0 deletions receiver/purefareceiver/testdata/array.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 3.7743e-05
go_gc_duration_seconds{quantile="0.25"} 6.0311e-05
go_gc_duration_seconds{quantile="0.5"} 8.5667e-05
go_gc_duration_seconds{quantile="0.75"} 0.000104831
go_gc_duration_seconds{quantile="1"} 0.000112094
go_gc_duration_seconds_sum 0.000486284
go_gc_duration_seconds_count 6
# HELP purefa_array_performance_average_bytes FlashArray array average operations size
# TYPE purefa_array_performance_average_bytes gauge
purefa_array_performance_average_bytes{dimension="bytes_per_mirrored_write"} 0
purefa_array_performance_average_bytes{dimension="bytes_per_op"} 88064
purefa_array_performance_average_bytes{dimension="bytes_per_read"} 0
purefa_array_performance_average_bytes{dimension="bytes_per_mirrored_write}