ConfigTest.groovy 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.opentelemetry.auto.config
  17. import io.opentelemetry.auto.util.test.AgentSpecification
  18. import org.junit.Rule
  19. import org.junit.contrib.java.lang.system.EnvironmentVariables
  20. import org.junit.contrib.java.lang.system.RestoreSystemProperties
  21. import static io.opentelemetry.auto.config.Config.CONFIGURATION_FILE
  22. import static io.opentelemetry.auto.config.Config.HTTP_CLIENT_ERROR_STATUSES
  23. import static io.opentelemetry.auto.config.Config.HTTP_SERVER_ERROR_STATUSES
  24. import static io.opentelemetry.auto.config.Config.PREFIX
  25. import static io.opentelemetry.auto.config.Config.RUNTIME_CONTEXT_FIELD_INJECTION
  26. import static io.opentelemetry.auto.config.Config.TRACE_ENABLED
  27. import static io.opentelemetry.auto.config.Config.TRACE_METHODS
  28. class ConfigTest extends AgentSpecification {
  29. @Rule
  30. public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties()
  31. @Rule
  32. public final EnvironmentVariables environmentVariables = new EnvironmentVariables()
  33. private static final TRACE_ENABLED_ENV = "OTA_TRACE_ENABLED"
  34. private static final TRACE_METHODS_ENV = "OTA_TRACE_METHODS"
  35. def "verify defaults"() {
  36. when:
  37. Config config = provider()
  38. then:
  39. config.traceEnabled == true
  40. config.httpServerErrorStatuses == toBitSet((500..599))
  41. config.httpClientErrorStatuses == toBitSet((400..599))
  42. config.runtimeContextFieldInjection == true
  43. config.toString().contains("traceEnabled=true")
  44. where:
  45. provider << [{ new Config() }, { Config.get() }, {
  46. def props = new Properties()
  47. props.setProperty("something", "unused")
  48. Config.get(props)
  49. }]
  50. }
  51. def "specify overrides via properties"() {
  52. setup:
  53. def prop = new Properties()
  54. prop.setProperty(TRACE_ENABLED, "false")
  55. prop.setProperty(TRACE_METHODS, "mypackage.MyClass[myMethod]")
  56. prop.setProperty(HTTP_SERVER_ERROR_STATUSES, "123-456,457,124-125,122")
  57. prop.setProperty(HTTP_CLIENT_ERROR_STATUSES, "111")
  58. prop.setProperty(RUNTIME_CONTEXT_FIELD_INJECTION, "false")
  59. when:
  60. Config config = Config.get(prop)
  61. then:
  62. config.traceEnabled == false
  63. config.traceMethods == "mypackage.MyClass[myMethod]"
  64. config.httpServerErrorStatuses == toBitSet((122..457))
  65. config.httpClientErrorStatuses == toBitSet((111..111))
  66. config.runtimeContextFieldInjection == false
  67. }
  68. def "specify overrides via system properties"() {
  69. setup:
  70. System.setProperty(PREFIX + TRACE_ENABLED, "false")
  71. System.setProperty(PREFIX + TRACE_METHODS, "mypackage.MyClass[myMethod]")
  72. System.setProperty(PREFIX + HTTP_SERVER_ERROR_STATUSES, "123-456,457,124-125,122")
  73. System.setProperty(PREFIX + HTTP_CLIENT_ERROR_STATUSES, "111")
  74. System.setProperty(PREFIX + RUNTIME_CONTEXT_FIELD_INJECTION, "false")
  75. when:
  76. Config config = new Config()
  77. then:
  78. config.traceEnabled == false
  79. config.traceMethods == "mypackage.MyClass[myMethod]"
  80. config.httpServerErrorStatuses == toBitSet((122..457))
  81. config.httpClientErrorStatuses == toBitSet((111..111))
  82. config.runtimeContextFieldInjection == false
  83. }
  84. def "specify overrides via env vars"() {
  85. setup:
  86. environmentVariables.set(TRACE_ENABLED_ENV, "false")
  87. environmentVariables.set(TRACE_METHODS_ENV, "mypackage.MyClass[myMethod]")
  88. when:
  89. def config = new Config()
  90. then:
  91. config.traceEnabled == false
  92. config.traceMethods == "mypackage.MyClass[myMethod]"
  93. }
  94. def "sys props override env vars"() {
  95. setup:
  96. environmentVariables.set(TRACE_METHODS_ENV, "mypackage.MyClass[myMethod]")
  97. System.setProperty(PREFIX + TRACE_METHODS, "mypackage2.MyClass2[myMethod2]")
  98. when:
  99. def config = new Config()
  100. then:
  101. config.traceMethods == "mypackage2.MyClass2[myMethod2]"
  102. }
  103. def "default when configured incorrectly"() {
  104. setup:
  105. System.setProperty(PREFIX + TRACE_ENABLED, " ")
  106. System.setProperty(PREFIX + TRACE_METHODS, " ")
  107. System.setProperty(PREFIX + HTTP_SERVER_ERROR_STATUSES, "1111")
  108. System.setProperty(PREFIX + HTTP_CLIENT_ERROR_STATUSES, "1:1")
  109. when:
  110. def config = new Config()
  111. then:
  112. config.traceEnabled == true
  113. config.traceMethods == " "
  114. config.httpServerErrorStatuses == toBitSet((500..599))
  115. config.httpClientErrorStatuses == toBitSet((400..599))
  116. }
  117. def "sys props override properties"() {
  118. setup:
  119. Properties properties = new Properties()
  120. properties.setProperty(TRACE_ENABLED, "false")
  121. properties.setProperty(TRACE_METHODS, "mypackage.MyClass[myMethod]")
  122. properties.setProperty(HTTP_SERVER_ERROR_STATUSES, "123-456,457,124-125,122")
  123. properties.setProperty(HTTP_CLIENT_ERROR_STATUSES, "111")
  124. when:
  125. def config = Config.get(properties)
  126. then:
  127. config.traceEnabled == false
  128. config.traceMethods == "mypackage.MyClass[myMethod]"
  129. config.httpServerErrorStatuses == toBitSet((122..457))
  130. config.httpClientErrorStatuses == toBitSet((111..111))
  131. }
  132. def "override null properties"() {
  133. when:
  134. def config = Config.get(null)
  135. then:
  136. config.traceEnabled == true
  137. }
  138. def "override empty properties"() {
  139. setup:
  140. Properties properties = new Properties()
  141. when:
  142. def config = Config.get(properties)
  143. then:
  144. config.traceEnabled == true
  145. }
  146. def "override non empty properties"() {
  147. setup:
  148. Properties properties = new Properties()
  149. properties.setProperty("foo", "bar")
  150. when:
  151. def config = Config.get(properties)
  152. then:
  153. config.traceEnabled == true
  154. }
  155. def "verify integration config"() {
  156. setup:
  157. environmentVariables.set("OTA_INTEGRATION_ORDER_ENABLED", "false")
  158. environmentVariables.set("OTA_INTEGRATION_TEST_ENV_ENABLED", "true")
  159. environmentVariables.set("OTA_INTEGRATION_DISABLED_ENV_ENABLED", "false")
  160. System.setProperty("ota.integration.order.enabled", "true")
  161. System.setProperty("ota.integration.test-prop.enabled", "true")
  162. System.setProperty("ota.integration.disabled-prop.enabled", "false")
  163. expect:
  164. Config.get().isIntegrationEnabled(integrationNames, defaultEnabled) == expected
  165. where:
  166. names | defaultEnabled | expected
  167. [] | true | true
  168. [] | false | false
  169. ["invalid"] | true | true
  170. ["invalid"] | false | false
  171. ["test-prop"] | false | true
  172. ["test-env"] | false | true
  173. ["disabled-prop"] | true | false
  174. ["disabled-env"] | true | false
  175. ["other", "test-prop"] | false | true
  176. ["other", "test-env"] | false | true
  177. ["order"] | false | true
  178. ["test-prop", "disabled-prop"] | false | true
  179. ["disabled-env", "test-env"] | false | true
  180. ["test-prop", "disabled-prop"] | true | false
  181. ["disabled-env", "test-env"] | true | false
  182. integrationNames = new TreeSet<>(names)
  183. }
  184. def "verify integer range configs on tracer"() {
  185. setup:
  186. System.setProperty(PREFIX + HTTP_SERVER_ERROR_STATUSES, value)
  187. System.setProperty(PREFIX + HTTP_CLIENT_ERROR_STATUSES, value)
  188. def props = new Properties()
  189. props.setProperty(HTTP_CLIENT_ERROR_STATUSES, value)
  190. props.setProperty(HTTP_SERVER_ERROR_STATUSES, value)
  191. when:
  192. def config = new Config()
  193. def propConfig = Config.get(props)
  194. then:
  195. if (expected) {
  196. assert config.httpServerErrorStatuses == toBitSet(expected)
  197. assert config.httpClientErrorStatuses == toBitSet(expected)
  198. assert propConfig.httpServerErrorStatuses == toBitSet(expected)
  199. assert propConfig.httpClientErrorStatuses == toBitSet(expected)
  200. } else {
  201. assert config.httpServerErrorStatuses == Config.DEFAULT_HTTP_SERVER_ERROR_STATUSES
  202. assert config.httpClientErrorStatuses == Config.DEFAULT_HTTP_CLIENT_ERROR_STATUSES
  203. assert propConfig.httpServerErrorStatuses == Config.DEFAULT_HTTP_SERVER_ERROR_STATUSES
  204. assert propConfig.httpClientErrorStatuses == Config.DEFAULT_HTTP_CLIENT_ERROR_STATUSES
  205. }
  206. where:
  207. value | expected // null means default value
  208. "1" | null
  209. "a" | null
  210. "" | null
  211. "1000" | null
  212. "100-200-300" | null
  213. "500" | [500]
  214. "100,999" | [100, 999]
  215. "999-888" | 888..999
  216. "400-403,405-407" | [400, 401, 402, 403, 405, 406, 407]
  217. " 400 - 403 , 405 " | [400, 401, 402, 403, 405]
  218. }
  219. def "verify fallback to properties file"() {
  220. setup:
  221. System.setProperty(PREFIX + CONFIGURATION_FILE, "src/test/resources/java-tracer.properties")
  222. when:
  223. def config = new Config()
  224. then:
  225. config.traceMethods == "mypackage.MyClass[myMethod]"
  226. cleanup:
  227. System.clearProperty(PREFIX + CONFIGURATION_FILE)
  228. }
  229. def "verify fallback to properties file has lower priority than system property"() {
  230. setup:
  231. System.setProperty(PREFIX + CONFIGURATION_FILE, "src/test/resources/java-tracer.properties")
  232. System.setProperty(PREFIX + TRACE_METHODS, "mypackage2.MyClass2[myMethod2]")
  233. when:
  234. def config = new Config()
  235. then:
  236. config.traceMethods == "mypackage2.MyClass2[myMethod2]"
  237. cleanup:
  238. System.clearProperty(PREFIX + CONFIGURATION_FILE)
  239. System.clearProperty(PREFIX + TRACE_METHODS)
  240. }
  241. def "verify fallback to properties file has lower priority than env var"() {
  242. setup:
  243. System.setProperty(PREFIX + CONFIGURATION_FILE, "src/test/resources/java-tracer.properties")
  244. environmentVariables.set("OTA_TRACE_METHODS", "mypackage2.MyClass2[myMethod2]")
  245. when:
  246. def config = new Config()
  247. then:
  248. config.traceMethods == "mypackage2.MyClass2[myMethod2]"
  249. cleanup:
  250. System.clearProperty(PREFIX + CONFIGURATION_FILE)
  251. System.clearProperty(PREFIX + TRACE_METHODS)
  252. environmentVariables.clear("OTA_TRACE_METHODS")
  253. }
  254. def "verify fallback to properties file that does not exist does not crash app"() {
  255. setup:
  256. System.setProperty(PREFIX + CONFIGURATION_FILE, "src/test/resources/do-not-exist.properties")
  257. when:
  258. def config = new Config()
  259. then:
  260. config.traceEnabled == true
  261. cleanup:
  262. System.clearProperty(PREFIX + CONFIGURATION_FILE)
  263. }
  264. static BitSet toBitSet(Collection<Integer> set) {
  265. BitSet bs = new BitSet()
  266. for (Integer i : set) {
  267. bs.set(i)
  268. }
  269. return bs
  270. }
  271. }