util_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awsemfexporter
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "go.opentelemetry.io/collector/pdata/pcommon"
  8. "go.opentelemetry.io/collector/pdata/pmetric"
  9. conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
  10. "go.uber.org/zap"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/occonventions"
  12. )
  13. func TestReplacePatternValidTaskId(t *testing.T) {
  14. logger := zap.NewNop()
  15. input := "{TaskId}"
  16. attrMap := pcommon.NewMap()
  17. attrMap.PutStr("aws.ecs.cluster.name", "test-cluster-name")
  18. attrMap.PutStr("aws.ecs.task.id", "test-task-id")
  19. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  20. assert.Equal(t, "test-task-id", s)
  21. assert.True(t, success)
  22. }
  23. func TestReplacePatternValidServiceName(t *testing.T) {
  24. logger := zap.NewNop()
  25. input := "{ServiceName}"
  26. attrMap := pcommon.NewMap()
  27. attrMap.PutStr("service.name", "some-test-service")
  28. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  29. assert.Equal(t, "some-test-service", s)
  30. assert.True(t, success)
  31. }
  32. func TestReplacePatternValidClusterName(t *testing.T) {
  33. logger := zap.NewNop()
  34. input := "/aws/ecs/containerinsights/{ClusterName}/performance"
  35. attrMap := pcommon.NewMap()
  36. attrMap.PutStr("aws.ecs.cluster.name", "test-cluster-name")
  37. attrMap.PutStr("aws.ecs.task.id", "test-task-id")
  38. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  39. assert.Equal(t, "/aws/ecs/containerinsights/test-cluster-name/performance", s)
  40. assert.True(t, success)
  41. }
  42. func TestReplacePatternMissingAttribute(t *testing.T) {
  43. logger := zap.NewNop()
  44. input := "/aws/ecs/containerinsights/{ClusterName}/performance"
  45. attrMap := pcommon.NewMap()
  46. attrMap.PutStr("aws.ecs.task.id", "test-task-id")
  47. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  48. assert.Equal(t, "/aws/ecs/containerinsights/undefined/performance", s)
  49. assert.False(t, success)
  50. }
  51. func TestReplacePatternValidPodName(t *testing.T) {
  52. logger := zap.NewNop()
  53. input := "/aws/eks/containerinsights/{PodName}/performance"
  54. attrMap := pcommon.NewMap()
  55. attrMap.PutStr("aws.eks.cluster.name", "test-cluster-name")
  56. attrMap.PutStr("PodName", "test-pod-001")
  57. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  58. assert.Equal(t, "/aws/eks/containerinsights/test-pod-001/performance", s)
  59. assert.True(t, success)
  60. }
  61. func TestReplacePatternValidPod(t *testing.T) {
  62. logger := zap.NewNop()
  63. input := "/aws/eks/containerinsights/{PodName}/performance"
  64. attrMap := pcommon.NewMap()
  65. attrMap.PutStr("aws.eks.cluster.name", "test-cluster-name")
  66. attrMap.PutStr("pod", "test-pod-001")
  67. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  68. assert.Equal(t, "/aws/eks/containerinsights/test-pod-001/performance", s)
  69. assert.True(t, success)
  70. }
  71. func TestReplacePatternMissingPodName(t *testing.T) {
  72. logger := zap.NewNop()
  73. input := "/aws/eks/containerinsights/{PodName}/performance"
  74. attrMap := pcommon.NewMap()
  75. attrMap.PutStr("aws.eks.cluster.name", "test-cluster-name")
  76. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  77. assert.Equal(t, "/aws/eks/containerinsights/undefined/performance", s)
  78. assert.False(t, success)
  79. }
  80. func TestReplacePatternAttrPlaceholderClusterName(t *testing.T) {
  81. logger := zap.NewNop()
  82. input := "/aws/ecs/containerinsights/{ClusterName}/performance"
  83. attrMap := pcommon.NewMap()
  84. attrMap.PutStr("ClusterName", "test-cluster-name")
  85. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  86. assert.Equal(t, "/aws/ecs/containerinsights/test-cluster-name/performance", s)
  87. assert.True(t, success)
  88. }
  89. func TestReplacePatternWrongKey(t *testing.T) {
  90. logger := zap.NewNop()
  91. input := "/aws/ecs/containerinsights/{WrongKey}/performance"
  92. attrMap := pcommon.NewMap()
  93. attrMap.PutStr("ClusterName", "test-task-id")
  94. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  95. assert.Equal(t, "/aws/ecs/containerinsights/{WrongKey}/performance", s)
  96. assert.True(t, success)
  97. }
  98. func TestReplacePatternNilAttrValue(t *testing.T) {
  99. logger := zap.NewNop()
  100. input := "/aws/ecs/containerinsights/{ClusterName}/performance"
  101. attrMap := pcommon.NewMap()
  102. attrMap.PutEmpty("ClusterName")
  103. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  104. assert.Equal(t, "/aws/ecs/containerinsights/undefined/performance", s)
  105. assert.False(t, success)
  106. }
  107. func TestReplacePatternValidTaskDefinitionFamily(t *testing.T) {
  108. logger := zap.NewNop()
  109. input := "{TaskDefinitionFamily}"
  110. attrMap := pcommon.NewMap()
  111. attrMap.PutStr("aws.ecs.cluster.name", "test-cluster-name")
  112. attrMap.PutStr("aws.ecs.task.family", "test-task-definition-family")
  113. s, success := replacePatterns(input, attrMaptoStringMap(attrMap), logger)
  114. assert.Equal(t, "test-task-definition-family", s)
  115. assert.True(t, success)
  116. }
  117. func TestGetNamespace(t *testing.T) {
  118. defaultRM := createTestResourceMetrics()
  119. testCases := []struct {
  120. testName string
  121. rm pmetric.ResourceMetrics
  122. configNamespace string
  123. namespace string
  124. }{
  125. {
  126. "non-empty namespace",
  127. defaultRM,
  128. "namespace",
  129. "namespace",
  130. },
  131. {
  132. "empty namespace",
  133. defaultRM,
  134. "",
  135. "myServiceNS/myServiceName",
  136. },
  137. {
  138. "empty namespace, no service namespace",
  139. func() pmetric.ResourceMetrics {
  140. rm := pmetric.NewResourceMetrics()
  141. rm.Resource().Attributes().PutStr(conventions.AttributeServiceName, "myServiceName")
  142. return rm
  143. }(),
  144. "",
  145. "myServiceName",
  146. },
  147. {
  148. "empty namespace, no service name",
  149. func() pmetric.ResourceMetrics {
  150. rm := pmetric.NewResourceMetrics()
  151. rm.Resource().Attributes().PutStr(conventions.AttributeServiceNamespace, "myServiceNS")
  152. return rm
  153. }(),
  154. "",
  155. "myServiceNS",
  156. },
  157. }
  158. for _, tc := range testCases {
  159. t.Run(tc.testName, func(t *testing.T) {
  160. namespace := getNamespace(tc.rm, tc.configNamespace)
  161. assert.Equal(t, tc.namespace, namespace)
  162. })
  163. }
  164. }
  165. func TestGetLogInfo(t *testing.T) {
  166. rm1 := pmetric.NewResourceMetrics()
  167. rm1.Resource().Attributes().PutStr(conventions.AttributeServiceName, "myServiceName")
  168. rm1.Resource().Attributes().PutStr(occonventions.AttributeExporterVersion, "SomeVersion")
  169. rm1.Resource().Attributes().PutStr("aws.ecs.cluster.name", "test-cluster-name")
  170. rm1.Resource().Attributes().PutStr("aws.ecs.task.id", "test-task-id")
  171. rm1.Resource().Attributes().PutStr("k8s.node.name", "ip-192-168-58-245.ec2.internal")
  172. rm1.Resource().Attributes().PutStr("aws.ecs.container.instance.id", "203e0410260d466bab7873bb4f317b4e")
  173. rm1.Resource().Attributes().PutStr("aws.ecs.task.family", "test-task-definition-family")
  174. rm2 := pmetric.NewResourceMetrics()
  175. rm2.Resource().Attributes().PutStr(conventions.AttributeServiceName, "test-emf")
  176. rm2.Resource().Attributes().PutStr(occonventions.AttributeExporterVersion, "SomeVersion")
  177. rm2.Resource().Attributes().PutStr("ClusterName", "test-cluster-name")
  178. rm2.Resource().Attributes().PutStr("TaskId", "test-task-id")
  179. rm2.Resource().Attributes().PutStr("NodeName", "ip-192-168-58-245.ec2.internal")
  180. rm2.Resource().Attributes().PutStr("ContainerInstanceId", "203e0410260d466bab7873bb4f317b4e")
  181. rm2.Resource().Attributes().PutStr("TaskDefinitionFamily", "test-task-definition-family")
  182. rms := []pmetric.ResourceMetrics{rm1, rm2}
  183. testCases := []struct {
  184. testName string
  185. namespace string
  186. configLogGroup string
  187. configLogStream string
  188. logGroup string
  189. logStream string
  190. }{
  191. {
  192. "non-empty namespace, no config",
  193. "namespace",
  194. "",
  195. "",
  196. "/metrics/namespace",
  197. "",
  198. },
  199. {
  200. "empty namespace, no config",
  201. "",
  202. "",
  203. "",
  204. "",
  205. "",
  206. },
  207. {
  208. "non-empty namespace, config w/o pattern",
  209. "namespace",
  210. "test-logGroupName",
  211. "test-logStreamName",
  212. "test-logGroupName",
  213. "test-logStreamName",
  214. },
  215. {
  216. "empty namespace, config w/o pattern",
  217. "",
  218. "test-logGroupName",
  219. "test-logStreamName",
  220. "test-logGroupName",
  221. "test-logStreamName",
  222. },
  223. {
  224. "non-empty namespace, config w/ pattern",
  225. "namespace",
  226. "/aws/ecs/containerinsights/{ClusterName}/performance",
  227. "{TaskId}",
  228. "/aws/ecs/containerinsights/test-cluster-name/performance",
  229. "test-task-id",
  230. },
  231. {
  232. "empty namespace, config w/ pattern",
  233. "",
  234. "/aws/ecs/containerinsights/{ClusterName}/performance",
  235. "{TaskId}",
  236. "/aws/ecs/containerinsights/test-cluster-name/performance",
  237. "test-task-id",
  238. },
  239. // test case for aws container insight usage
  240. {
  241. "empty namespace, config w/ pattern",
  242. "",
  243. "/aws/containerinsights/{ClusterName}/performance",
  244. "{NodeName}",
  245. "/aws/containerinsights/test-cluster-name/performance",
  246. "ip-192-168-58-245.ec2.internal",
  247. },
  248. // test case for AWS ECS EC2 container insights usage
  249. {
  250. "empty namespace, config w/ pattern",
  251. "",
  252. "/aws/containerinsights/{ClusterName}/performance",
  253. "instanceTelemetry/{ContainerInstanceId}",
  254. "/aws/containerinsights/test-cluster-name/performance",
  255. "instanceTelemetry/203e0410260d466bab7873bb4f317b4e",
  256. },
  257. {
  258. "empty namespace, config w/ pattern",
  259. "",
  260. "/aws/containerinsights/{ClusterName}/performance",
  261. "{TaskDefinitionFamily}-{TaskId}",
  262. "/aws/containerinsights/test-cluster-name/performance",
  263. "test-task-definition-family-test-task-id",
  264. },
  265. }
  266. for i := range rms {
  267. for _, tc := range testCases {
  268. t.Run(tc.testName, func(t *testing.T) {
  269. config := &Config{
  270. LogGroupName: tc.configLogGroup,
  271. LogStreamName: tc.configLogStream,
  272. }
  273. logGroup, logStream, success := getLogInfo(rms[i], tc.namespace, config)
  274. assert.Equal(t, tc.logGroup, logGroup)
  275. assert.Equal(t, tc.logStream, logStream)
  276. assert.True(t, success)
  277. })
  278. }
  279. }
  280. }