config_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package datasetexporter
  4. import (
  5. "fmt"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "go.opentelemetry.io/collector/confmap"
  10. "go.opentelemetry.io/collector/exporter/exporterhelper"
  11. )
  12. func TestConfigUnmarshalUnknownAttributes(t *testing.T) {
  13. f := NewFactory()
  14. config := f.CreateDefaultConfig().(*Config)
  15. configMap := confmap.NewFromStringMap(map[string]any{
  16. "dataset_url": "https://example.com",
  17. "api_key": "secret",
  18. "unknown_attribute": "some value",
  19. })
  20. err := config.Unmarshal(configMap)
  21. unmarshalErr := fmt.Errorf("1 error(s) decoding:\n\n* '' has invalid keys: unknown_attribute")
  22. expectedError := fmt.Errorf("cannot unmarshal config: %w", unmarshalErr)
  23. assert.Equal(t, expectedError.Error(), err.Error())
  24. }
  25. func TestConfigUseDefaults(t *testing.T) {
  26. f := NewFactory()
  27. config := f.CreateDefaultConfig().(*Config)
  28. configMap := confmap.NewFromStringMap(map[string]any{
  29. "dataset_url": "https://example.com",
  30. "api_key": "secret",
  31. })
  32. err := config.Unmarshal(configMap)
  33. assert.Nil(t, err)
  34. assert.Equal(t, "https://example.com", config.DatasetURL)
  35. assert.Equal(t, "secret", string(config.APIKey))
  36. assert.Equal(t, bufferMaxLifetime, config.MaxLifetime)
  37. assert.Equal(t, logsExportResourceInfoDefault, config.LogsSettings.ExportResourceInfo)
  38. assert.Equal(t, logsExportResourcePrefixDefault, config.LogsSettings.ExportResourcePrefix)
  39. assert.Equal(t, logsExportScopeInfoDefault, config.LogsSettings.ExportScopeInfo)
  40. assert.Equal(t, logsExportScopePrefixDefault, config.LogsSettings.ExportScopePrefix)
  41. assert.Equal(t, logsDecomposeComplexMessageFieldDefault, config.LogsSettings.DecomposeComplexMessageField)
  42. assert.Equal(t, exportSeparatorDefault, config.LogsSettings.exportSettings.ExportSeparator)
  43. assert.Equal(t, exportDistinguishingSuffix, config.LogsSettings.exportSettings.ExportDistinguishingSuffix)
  44. assert.Equal(t, exportSeparatorDefault, config.TracesSettings.exportSettings.ExportSeparator)
  45. assert.Equal(t, exportDistinguishingSuffix, config.TracesSettings.exportSettings.ExportDistinguishingSuffix)
  46. }
  47. func TestConfigValidate(t *testing.T) {
  48. tests := []struct {
  49. name string
  50. config Config
  51. expected error
  52. }{
  53. {
  54. name: "valid config",
  55. config: Config{
  56. DatasetURL: "https://example.com",
  57. APIKey: "secret",
  58. BufferSettings: BufferSettings{
  59. MaxLifetime: 123 * time.Millisecond,
  60. },
  61. },
  62. expected: nil,
  63. },
  64. {
  65. name: "missing api_key",
  66. config: Config{
  67. DatasetURL: "https://example.com",
  68. BufferSettings: BufferSettings{
  69. MaxLifetime: bufferMaxLifetime,
  70. },
  71. },
  72. expected: fmt.Errorf("api_key is required"),
  73. },
  74. {
  75. name: "missing dataset_url",
  76. config: Config{
  77. APIKey: "1234",
  78. BufferSettings: BufferSettings{
  79. MaxLifetime: bufferMaxLifetime,
  80. },
  81. },
  82. expected: fmt.Errorf("dataset_url is required"),
  83. },
  84. }
  85. for _, tt := range tests {
  86. t.Run(tt.name, func(t *testing.T) {
  87. err := tt.config.Validate()
  88. if err == nil {
  89. assert.Nil(t, tt.expected, tt.name)
  90. } else {
  91. assert.Equal(t, tt.expected.Error(), err.Error(), tt.name)
  92. }
  93. })
  94. }
  95. }
  96. func TestConfigString(t *testing.T) {
  97. config := Config{
  98. DatasetURL: "https://example.com",
  99. APIKey: "secret",
  100. Debug: true,
  101. BufferSettings: BufferSettings{
  102. MaxLifetime: 123,
  103. GroupBy: []string{"field1", "field2"},
  104. },
  105. TracesSettings: TracesSettings{
  106. exportSettings: exportSettings{
  107. ExportSeparator: "TTT",
  108. ExportDistinguishingSuffix: "UUU",
  109. },
  110. },
  111. LogsSettings: LogsSettings{
  112. ExportResourceInfo: true,
  113. ExportResourcePrefix: "AAA",
  114. ExportScopeInfo: true,
  115. ExportScopePrefix: "BBB",
  116. DecomposeComplexMessageField: true,
  117. DecomposedComplexMessagePrefix: "EEE",
  118. exportSettings: exportSettings{
  119. ExportSeparator: "CCC",
  120. ExportDistinguishingSuffix: "DDD",
  121. },
  122. },
  123. ServerHostSettings: ServerHostSettings{
  124. ServerHost: "foo-bar",
  125. UseHostName: false,
  126. },
  127. RetrySettings: exporterhelper.NewDefaultRetrySettings(),
  128. QueueSettings: exporterhelper.NewDefaultQueueSettings(),
  129. TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
  130. }
  131. assert.Equal(t,
  132. "DatasetURL: https://example.com; APIKey: [REDACTED] (6); Debug: true; BufferSettings: {MaxLifetime:123ns GroupBy:[field1 field2] RetryInitialInterval:0s RetryMaxInterval:0s RetryMaxElapsedTime:0s RetryShutdownTimeout:0s}; LogsSettings: {ExportResourceInfo:true ExportResourcePrefix:AAA ExportScopeInfo:true ExportScopePrefix:BBB DecomposeComplexMessageField:true DecomposedComplexMessagePrefix:EEE exportSettings:{ExportSeparator:CCC ExportDistinguishingSuffix:DDD}}; TracesSettings: {exportSettings:{ExportSeparator:TTT ExportDistinguishingSuffix:UUU}}; ServerHostSettings: {UseHostName:false ServerHost:foo-bar}; RetrySettings: {Enabled:true InitialInterval:5s RandomizationFactor:0.5 Multiplier:1.5 MaxInterval:30s MaxElapsedTime:5m0s}; QueueSettings: {Enabled:true NumConsumers:10 QueueSize:1000 StorageID:<nil>}; TimeoutSettings: {Timeout:5s}",
  133. config.String(),
  134. )
  135. }
  136. func TestConfigUseProvidedExportResourceInfoValue(t *testing.T) {
  137. f := NewFactory()
  138. config := f.CreateDefaultConfig().(*Config)
  139. configMap := confmap.NewFromStringMap(map[string]any{
  140. "dataset_url": "https://example.com",
  141. "api_key": "secret",
  142. "logs": map[string]any{
  143. "export_resource_info_on_event": true,
  144. },
  145. })
  146. err := config.Unmarshal(configMap)
  147. assert.Nil(t, err)
  148. assert.Equal(t, true, config.LogsSettings.ExportResourceInfo)
  149. }
  150. func TestConfigUseProvidedExportScopeInfoValue(t *testing.T) {
  151. f := NewFactory()
  152. config := f.CreateDefaultConfig().(*Config)
  153. configMap := confmap.NewFromStringMap(map[string]any{
  154. "dataset_url": "https://example.com",
  155. "api_key": "secret",
  156. "logs": map[string]any{
  157. "export_scope_info_on_event": false,
  158. },
  159. })
  160. err := config.Unmarshal(configMap)
  161. assert.Nil(t, err)
  162. assert.Equal(t, false, config.LogsSettings.ExportScopeInfo)
  163. }