scraper_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/purefareceiver/internal"
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. "github.com/prometheus/common/model"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/config/configauth"
  13. "go.opentelemetry.io/collector/extension/extensiontest"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/extension/bearertokenauthextension"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
  16. )
  17. func TestToPrometheusConfig(t *testing.T) {
  18. // prepare
  19. prFactory := prometheusreceiver.NewFactory()
  20. baFactory := bearertokenauthextension.NewFactory()
  21. baCfg := baFactory.CreateDefaultConfig().(*bearertokenauthextension.Config)
  22. baCfg.BearerToken = "the-token"
  23. baExt, err := baFactory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), baCfg)
  24. require.NoError(t, err)
  25. host := &mockHost{
  26. extensions: map[component.ID]component.Component{
  27. component.NewIDWithName("bearertokenauth", "array01"): baExt,
  28. },
  29. }
  30. endpoint := "http://example.com"
  31. interval := 15 * time.Second
  32. cfgs := []ScraperConfig{
  33. {
  34. Address: "array01",
  35. Auth: configauth.Authentication{
  36. AuthenticatorID: component.NewIDWithName("bearertokenauth", "array01"),
  37. },
  38. },
  39. }
  40. scraper := NewScraper(context.Background(), "hosts", endpoint, cfgs, interval, model.LabelSet{})
  41. // test
  42. scCfgs, err := scraper.ToPrometheusReceiverConfig(host, prFactory)
  43. // verify
  44. assert.NoError(t, err)
  45. assert.Len(t, scCfgs, 1)
  46. assert.EqualValues(t, "the-token", scCfgs[0].HTTPClientConfig.BearerToken)
  47. assert.Equal(t, "array01", scCfgs[0].Params.Get("endpoint"))
  48. assert.Equal(t, "/metrics/hosts", scCfgs[0].MetricsPath)
  49. assert.Equal(t, "purefa/hosts/array01", scCfgs[0].JobName)
  50. assert.EqualValues(t, interval, scCfgs[0].ScrapeTimeout)
  51. assert.EqualValues(t, interval, scCfgs[0].ScrapeInterval)
  52. }