config_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlecloudexporter
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/confmap/confmaptest"
  13. "go.opentelemetry.io/collector/exporter/exporterhelper"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/googlecloudexporter/internal/metadata"
  15. )
  16. func TestLoadConfig(t *testing.T) {
  17. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  18. require.NoError(t, err)
  19. factory := NewFactory()
  20. cfg := factory.CreateDefaultConfig()
  21. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  22. require.NoError(t, err)
  23. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  24. assert.Equal(t, sanitize(cfg.(*Config)), sanitize(factory.CreateDefaultConfig().(*Config)))
  25. sub, err = cm.Sub(component.NewIDWithName(metadata.Type, "customname").String())
  26. require.NoError(t, err)
  27. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  28. assert.Equal(t,
  29. &Config{
  30. TimeoutSettings: exporterhelper.TimeoutSettings{
  31. Timeout: 20 * time.Second,
  32. },
  33. Config: collector.Config{
  34. ProjectID: "my-project",
  35. UserAgent: "opentelemetry-collector-contrib {{version}}",
  36. LogConfig: collector.LogConfig{
  37. ServiceResourceLabels: true,
  38. },
  39. MetricConfig: collector.MetricConfig{
  40. Prefix: "prefix",
  41. SkipCreateMetricDescriptor: true,
  42. KnownDomains: []string{"googleapis.com", "kubernetes.io", "istio.io", "knative.dev"},
  43. CreateMetricDescriptorBufferSize: 10,
  44. InstrumentationLibraryLabels: true,
  45. ServiceResourceLabels: true,
  46. ClientConfig: collector.ClientConfig{
  47. Endpoint: "test-metric-endpoint",
  48. UseInsecure: true,
  49. },
  50. },
  51. TraceConfig: collector.TraceConfig{
  52. ClientConfig: collector.ClientConfig{
  53. Endpoint: "test-trace-endpoint",
  54. UseInsecure: true,
  55. },
  56. },
  57. },
  58. QueueSettings: exporterhelper.QueueSettings{
  59. Enabled: true,
  60. NumConsumers: 2,
  61. QueueSize: 10,
  62. },
  63. },
  64. sanitize(cfg.(*Config)))
  65. }
  66. func sanitize(cfg *Config) *Config {
  67. cfg.Config.MetricConfig.MapMonitoredResource = nil
  68. cfg.Config.MetricConfig.GetMetricName = nil
  69. return cfg
  70. }