config_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package datadogexporter
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "go.opentelemetry.io/collector/confmap"
  8. )
  9. func TestValidate(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. cfg *Config
  13. err string
  14. }{
  15. {
  16. name: "no api::key",
  17. cfg: &Config{},
  18. err: errUnsetAPIKey.Error(),
  19. },
  20. {
  21. name: "invalid hostname",
  22. cfg: &Config{
  23. API: APIConfig{Key: "notnull"},
  24. TagsConfig: TagsConfig{Hostname: "invalid_host"},
  25. },
  26. err: "hostname field is invalid: 'invalid_host' is not RFC1123 compliant",
  27. },
  28. {
  29. name: "no metadata",
  30. cfg: &Config{
  31. API: APIConfig{Key: "notnull"},
  32. OnlyMetadata: true,
  33. HostMetadata: HostMetadataConfig{Enabled: false},
  34. },
  35. err: errNoMetadata.Error(),
  36. },
  37. {
  38. name: "span name remapping valid",
  39. cfg: &Config{
  40. API: APIConfig{Key: "notnull"},
  41. Traces: TracesConfig{SpanNameRemappings: map[string]string{"old.opentelemetryspan.name": "updated.name"}},
  42. },
  43. },
  44. {
  45. name: "span name remapping empty val",
  46. cfg: &Config{
  47. API: APIConfig{Key: "notnull"},
  48. Traces: TracesConfig{SpanNameRemappings: map[string]string{"oldname": ""}},
  49. },
  50. err: "'' is not valid value for span name remapping",
  51. },
  52. {
  53. name: "span name remapping empty key",
  54. cfg: &Config{
  55. API: APIConfig{Key: "notnull"},
  56. Traces: TracesConfig{SpanNameRemappings: map[string]string{"": "newname"}},
  57. },
  58. err: "'' is not valid key for span name remapping",
  59. },
  60. {
  61. name: "ignore resources valid",
  62. cfg: &Config{
  63. API: APIConfig{Key: "notnull"},
  64. Traces: TracesConfig{IgnoreResources: []string{"[123]"}},
  65. },
  66. },
  67. {
  68. name: "ignore resources missing bracket",
  69. cfg: &Config{
  70. API: APIConfig{Key: "notnull"},
  71. Traces: TracesConfig{IgnoreResources: []string{"[123"}},
  72. },
  73. err: "'[123' is not valid resource filter regular expression",
  74. },
  75. {
  76. name: "invalid histogram settings",
  77. cfg: &Config{
  78. API: APIConfig{Key: "notnull"},
  79. Metrics: MetricsConfig{
  80. HistConfig: HistogramConfig{
  81. Mode: HistogramModeNoBuckets,
  82. SendAggregations: false,
  83. },
  84. },
  85. },
  86. err: "'nobuckets' mode and `send_aggregation_metrics` set to false will send no histogram metrics",
  87. },
  88. {
  89. name: "TLS settings are valid",
  90. cfg: &Config{
  91. API: APIConfig{Key: "notnull"},
  92. LimitedHTTPClientSettings: LimitedHTTPClientSettings{
  93. TLSSetting: LimitedTLSClientSettings{
  94. InsecureSkipVerify: true,
  95. },
  96. },
  97. },
  98. },
  99. {
  100. name: "With trace_buffer",
  101. cfg: &Config{
  102. API: APIConfig{Key: "notnull"},
  103. Traces: TracesConfig{TraceBuffer: 10},
  104. },
  105. },
  106. }
  107. for _, testInstance := range tests {
  108. t.Run(testInstance.name, func(t *testing.T) {
  109. err := testInstance.cfg.Validate()
  110. if testInstance.err != "" {
  111. assert.EqualError(t, err, testInstance.err)
  112. } else {
  113. assert.NoError(t, err)
  114. }
  115. })
  116. }
  117. }
  118. func TestUnmarshal(t *testing.T) {
  119. tests := []struct {
  120. name string
  121. configMap *confmap.Conf
  122. cfg Config
  123. err string
  124. }{
  125. {
  126. name: "invalid cumulative monotonic mode",
  127. configMap: confmap.NewFromStringMap(map[string]any{
  128. "metrics": map[string]any{
  129. "sums": map[string]any{
  130. "cumulative_monotonic_mode": "invalid_mode",
  131. },
  132. },
  133. }),
  134. err: "1 error(s) decoding:\n\n* error decoding 'metrics.sums.cumulative_monotonic_mode': invalid cumulative monotonic sum mode \"invalid_mode\"",
  135. },
  136. {
  137. name: "invalid host metadata hostname source",
  138. configMap: confmap.NewFromStringMap(map[string]any{
  139. "host_metadata": map[string]any{
  140. "hostname_source": "invalid_source",
  141. },
  142. }),
  143. err: "1 error(s) decoding:\n\n* error decoding 'host_metadata.hostname_source': invalid host metadata hostname source \"invalid_source\"",
  144. },
  145. {
  146. name: "invalid summary mode",
  147. configMap: confmap.NewFromStringMap(map[string]any{
  148. "metrics": map[string]any{
  149. "summaries": map[string]any{
  150. "mode": "invalid_mode",
  151. },
  152. },
  153. }),
  154. err: "1 error(s) decoding:\n\n* error decoding 'metrics.summaries.mode': invalid summary mode \"invalid_mode\"",
  155. },
  156. {
  157. name: "metrics::send_monotonic_counter custom error",
  158. configMap: confmap.NewFromStringMap(map[string]any{
  159. "metrics": map[string]any{
  160. "send_monotonic_counter": true,
  161. },
  162. }),
  163. err: "\"metrics::send_monotonic_counter\" was removed in favor of \"metrics::sums::cumulative_monotonic_mode\". See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/8489",
  164. },
  165. {
  166. name: "tags custom error",
  167. configMap: confmap.NewFromStringMap(map[string]any{
  168. "tags": []string{},
  169. }),
  170. err: "\"tags\" was removed in favor of \"host_metadata::tags\". See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/9099",
  171. },
  172. {
  173. name: "send_metadata custom error",
  174. configMap: confmap.NewFromStringMap(map[string]any{
  175. "send_metadata": false,
  176. }),
  177. err: "\"send_metadata\" was removed in favor of \"host_metadata::enabled\". See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/9099",
  178. },
  179. {
  180. name: "use_resource_metadata custom error",
  181. configMap: confmap.NewFromStringMap(map[string]any{
  182. "use_resource_metadata": false,
  183. }),
  184. err: "\"use_resource_metadata\" was removed in favor of \"host_metadata::hostname_source\". See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/9099",
  185. },
  186. {
  187. name: "metrics::report_quantiles custom error",
  188. configMap: confmap.NewFromStringMap(map[string]any{
  189. "metrics": map[string]any{
  190. "report_quantiles": true,
  191. },
  192. }),
  193. err: "\"metrics::report_quantiles\" was removed in favor of \"metrics::summaries::mode\". See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/8845",
  194. },
  195. {
  196. name: "instrumentation_library_metadata_as_tags custom error",
  197. configMap: confmap.NewFromStringMap(map[string]any{
  198. "metrics": map[string]any{
  199. "instrumentation_library_metadata_as_tags": true,
  200. },
  201. }),
  202. err: "\"metrics::instrumentation_library_metadata_as_tags\" was removed in favor of \"metrics::instrumentation_scope_as_tags\". See https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/11135",
  203. },
  204. {
  205. name: "Empty metric endpoint",
  206. configMap: confmap.NewFromStringMap(map[string]any{
  207. "metrics": map[string]any{
  208. "endpoint": "",
  209. },
  210. }),
  211. err: errEmptyEndpoint.Error(),
  212. },
  213. {
  214. name: "Empty trace endpoint",
  215. configMap: confmap.NewFromStringMap(map[string]any{
  216. "traces": map[string]any{
  217. "endpoint": "",
  218. },
  219. }),
  220. err: errEmptyEndpoint.Error(),
  221. },
  222. {
  223. name: "Empty log endpoint",
  224. configMap: confmap.NewFromStringMap(map[string]any{
  225. "logs": map[string]any{
  226. "endpoint": "",
  227. },
  228. }),
  229. err: errEmptyEndpoint.Error(),
  230. },
  231. {
  232. name: "invalid initial cumulative monotonic value mode",
  233. configMap: confmap.NewFromStringMap(map[string]any{
  234. "metrics": map[string]any{
  235. "sums": map[string]any{
  236. "initial_cumulative_monotonic_value": "invalid_mode",
  237. },
  238. },
  239. }),
  240. err: "1 error(s) decoding:\n\n* error decoding 'metrics.sums.initial_cumulative_monotonic_value': invalid initial value mode \"invalid_mode\"",
  241. },
  242. {
  243. name: "initial cumulative monotonic value mode set with raw_value",
  244. configMap: confmap.NewFromStringMap(map[string]any{
  245. "metrics": map[string]any{
  246. "sums": map[string]any{
  247. "cumulative_monotonic_mode": "raw_value",
  248. "initial_cumulative_monotonic_value": "drop",
  249. },
  250. },
  251. }),
  252. err: "\"metrics::sums::initial_cumulative_monotonic_value\" can only be configured when \"metrics::sums::cumulative_monotonic_mode\" is set to \"to_delta\"",
  253. },
  254. }
  255. f := NewFactory()
  256. for _, testInstance := range tests {
  257. t.Run(testInstance.name, func(t *testing.T) {
  258. cfg := f.CreateDefaultConfig().(*Config)
  259. err := cfg.Unmarshal(testInstance.configMap)
  260. if err != nil || testInstance.err != "" {
  261. assert.EqualError(t, err, testInstance.err)
  262. } else {
  263. assert.Equal(t, testInstance.cfg, cfg)
  264. }
  265. })
  266. }
  267. }