test_data.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sumologicexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sumologicexporter"
  4. import (
  5. "go.opentelemetry.io/collector/pdata/pcommon"
  6. "go.opentelemetry.io/collector/pdata/pmetric"
  7. )
  8. func exampleIntMetric() metricPair {
  9. return buildExampleIntMetric(true)
  10. }
  11. func buildExampleIntMetric(fillData bool) metricPair {
  12. metric := pmetric.NewMetric()
  13. metric.SetName("test.metric.data")
  14. metric.SetUnit("bytes")
  15. metric.SetEmptySum()
  16. if fillData {
  17. dp := metric.Sum().DataPoints().AppendEmpty()
  18. dp.SetTimestamp(1605534165 * 1e9)
  19. dp.SetIntValue(14500)
  20. }
  21. attributes := pcommon.NewMap()
  22. attributes.PutStr("test", "test_value")
  23. attributes.PutStr("test2", "second_value")
  24. return metricPair{
  25. metric: metric,
  26. attributes: attributes,
  27. }
  28. }
  29. func exampleIntGaugeMetric() metricPair {
  30. return buildExampleIntGaugeMetric(true)
  31. }
  32. func buildExampleIntGaugeMetric(fillData bool) metricPair {
  33. metric := metricPair{
  34. attributes: pcommon.NewMap(),
  35. metric: pmetric.NewMetric(),
  36. }
  37. metric.metric.SetName("gauge_metric_name")
  38. metric.metric.SetEmptyGauge()
  39. metric.attributes.PutStr("foo", "bar")
  40. if fillData {
  41. dp := metric.metric.Gauge().DataPoints().AppendEmpty()
  42. dp.Attributes().PutStr("remote_name", "156920")
  43. dp.Attributes().PutStr("url", "http://example_url")
  44. dp.SetIntValue(124)
  45. dp.SetTimestamp(1608124661.166 * 1e9)
  46. dp = metric.metric.Gauge().DataPoints().AppendEmpty()
  47. dp.Attributes().PutStr("remote_name", "156955")
  48. dp.Attributes().PutStr("url", "http://another_url")
  49. dp.SetIntValue(245)
  50. dp.SetTimestamp(1608124662.166 * 1e9)
  51. }
  52. return metric
  53. }
  54. func exampleDoubleGaugeMetric() metricPair {
  55. return buildExampleDoubleGaugeMetric(true)
  56. }
  57. func buildExampleDoubleGaugeMetric(fillData bool) metricPair {
  58. metric := metricPair{
  59. attributes: pcommon.NewMap(),
  60. metric: pmetric.NewMetric(),
  61. }
  62. metric.metric.SetEmptyGauge()
  63. metric.metric.SetName("gauge_metric_name_double_test")
  64. metric.attributes.PutStr("foo", "bar")
  65. if fillData {
  66. dp := metric.metric.Gauge().DataPoints().AppendEmpty()
  67. dp.Attributes().PutStr("local_name", "156720")
  68. dp.Attributes().PutStr("endpoint", "http://example_url")
  69. dp.SetDoubleValue(33.4)
  70. dp.SetTimestamp(1608124661.169 * 1e9)
  71. dp = metric.metric.Gauge().DataPoints().AppendEmpty()
  72. dp.Attributes().PutStr("local_name", "156155")
  73. dp.Attributes().PutStr("endpoint", "http://another_url")
  74. dp.SetDoubleValue(56.8)
  75. dp.SetTimestamp(1608124662.186 * 1e9)
  76. }
  77. return metric
  78. }
  79. func exampleIntSumMetric() metricPair {
  80. return buildExampleIntSumMetric(true)
  81. }
  82. func buildExampleIntSumMetric(fillData bool) metricPair {
  83. metric := metricPair{
  84. attributes: pcommon.NewMap(),
  85. metric: pmetric.NewMetric(),
  86. }
  87. metric.metric.SetEmptySum()
  88. metric.metric.SetName("sum_metric_int_test")
  89. metric.attributes.PutStr("foo", "bar")
  90. if fillData {
  91. dp := metric.metric.Sum().DataPoints().AppendEmpty()
  92. dp.Attributes().PutStr("name", "156720")
  93. dp.Attributes().PutStr("address", "http://example_url")
  94. dp.SetIntValue(45)
  95. dp.SetTimestamp(1608124444.169 * 1e9)
  96. dp = metric.metric.Sum().DataPoints().AppendEmpty()
  97. dp.Attributes().PutStr("name", "156155")
  98. dp.Attributes().PutStr("address", "http://another_url")
  99. dp.SetIntValue(1238)
  100. dp.SetTimestamp(1608124699.186 * 1e9)
  101. }
  102. return metric
  103. }
  104. func exampleDoubleSumMetric() metricPair {
  105. return buildExampleDoubleSumMetric(true)
  106. }
  107. func buildExampleDoubleSumMetric(fillData bool) metricPair {
  108. metric := metricPair{
  109. attributes: pcommon.NewMap(),
  110. metric: pmetric.NewMetric(),
  111. }
  112. metric.metric.SetEmptySum()
  113. metric.metric.SetName("sum_metric_double_test")
  114. metric.attributes.PutStr("foo", "bar")
  115. if fillData {
  116. dp := metric.metric.Sum().DataPoints().AppendEmpty()
  117. dp.Attributes().PutStr("pod_name", "lorem")
  118. dp.Attributes().PutStr("namespace", "default")
  119. dp.SetDoubleValue(45.6)
  120. dp.SetTimestamp(1618124444.169 * 1e9)
  121. dp = metric.metric.Sum().DataPoints().AppendEmpty()
  122. dp.Attributes().PutStr("pod_name", "opsum")
  123. dp.Attributes().PutStr("namespace", "kube-config")
  124. dp.SetDoubleValue(1238.1)
  125. dp.SetTimestamp(1608424699.186 * 1e9)
  126. }
  127. return metric
  128. }
  129. func exampleSummaryMetric() metricPair {
  130. return buildExampleSummaryMetric(true)
  131. }
  132. func buildExampleSummaryMetric(fillData bool) metricPair {
  133. metric := metricPair{
  134. attributes: pcommon.NewMap(),
  135. metric: pmetric.NewMetric(),
  136. }
  137. metric.metric.SetEmptySummary()
  138. metric.metric.SetName("summary_metric_double_test")
  139. metric.attributes.PutStr("foo", "bar")
  140. if fillData {
  141. dp := metric.metric.Summary().DataPoints().AppendEmpty()
  142. dp.Attributes().PutStr("pod_name", "dolor")
  143. dp.Attributes().PutStr("namespace", "sumologic")
  144. dp.SetSum(45.6)
  145. dp.SetCount(3)
  146. dp.SetTimestamp(1618124444.169 * 1e9)
  147. quantile := dp.QuantileValues().AppendEmpty()
  148. quantile.SetQuantile(0.6)
  149. quantile.SetValue(0.7)
  150. quantile = dp.QuantileValues().AppendEmpty()
  151. quantile.SetQuantile(2.6)
  152. quantile.SetValue(4)
  153. dp = metric.metric.Summary().DataPoints().AppendEmpty()
  154. dp.Attributes().PutStr("pod_name", "sit")
  155. dp.Attributes().PutStr("namespace", "main")
  156. dp.SetSum(1238.1)
  157. dp.SetCount(7)
  158. dp.SetTimestamp(1608424699.186 * 1e9)
  159. }
  160. return metric
  161. }
  162. func exampleHistogramMetric() metricPair {
  163. return buildExampleHistogramMetric(true)
  164. }
  165. func buildExampleHistogramMetric(fillData bool) metricPair {
  166. metric := metricPair{
  167. attributes: pcommon.NewMap(),
  168. metric: pmetric.NewMetric(),
  169. }
  170. metric.metric.SetEmptyHistogram()
  171. metric.metric.SetName("histogram_metric_double_test")
  172. metric.attributes.PutStr("bar", "foo")
  173. if fillData {
  174. dp := metric.metric.Histogram().DataPoints().AppendEmpty()
  175. dp.Attributes().PutStr("container", "dolor")
  176. dp.Attributes().PutStr("branch", "sumologic")
  177. dp.BucketCounts().FromRaw([]uint64{0, 12, 7, 5, 8, 13})
  178. dp.ExplicitBounds().FromRaw([]float64{0.1, 0.2, 0.5, 0.8, 1})
  179. dp.SetTimestamp(1618124444.169 * 1e9)
  180. dp.SetSum(45.6)
  181. dp.SetCount(7)
  182. dp = metric.metric.Histogram().DataPoints().AppendEmpty()
  183. dp.Attributes().PutStr("container", "sit")
  184. dp.Attributes().PutStr("branch", "main")
  185. dp.BucketCounts().FromRaw([]uint64{0, 10, 1, 1, 4, 6})
  186. dp.ExplicitBounds().FromRaw([]float64{0.1, 0.2, 0.5, 0.8, 1})
  187. dp.SetTimestamp(1608424699.186 * 1e9)
  188. dp.SetSum(54.1)
  189. dp.SetCount(98)
  190. } else {
  191. dp := metric.metric.Histogram().DataPoints().AppendEmpty()
  192. dp.SetCount(0)
  193. }
  194. return metric
  195. }
  196. func metricPairToMetrics(mp []metricPair) pmetric.Metrics {
  197. metrics := pmetric.NewMetrics()
  198. metrics.ResourceMetrics().EnsureCapacity(len(mp))
  199. for num, record := range mp {
  200. record.attributes.CopyTo(metrics.ResourceMetrics().AppendEmpty().Resource().Attributes())
  201. // TODO: Change metricPair to have an init metric func.
  202. record.metric.CopyTo(metrics.ResourceMetrics().At(num).ScopeMetrics().AppendEmpty().Metrics().AppendEmpty())
  203. }
  204. return metrics
  205. }
  206. func fieldsFromMap(s map[string]string) fields {
  207. attrMap := pcommon.NewMap()
  208. for k, v := range s {
  209. attrMap.PutStr(k, v)
  210. }
  211. return newFields(attrMap)
  212. }