accumulator_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package prometheusexporter
  4. import (
  5. "log"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/pdata/pcommon"
  11. "go.opentelemetry.io/collector/pdata/pmetric"
  12. "go.uber.org/zap"
  13. )
  14. func TestAccumulateDeltaAggregation(t *testing.T) {
  15. tests := []struct {
  16. name string
  17. fillMetric func(time.Time, pmetric.Metric)
  18. }{
  19. {
  20. name: "Histogram",
  21. fillMetric: func(ts time.Time, metric pmetric.Metric) {
  22. metric.SetName("test_metric")
  23. metric.SetEmptyHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
  24. metric.SetDescription("test description")
  25. dp := metric.Histogram().DataPoints().AppendEmpty()
  26. dp.BucketCounts().FromRaw([]uint64{5, 2})
  27. dp.SetCount(7)
  28. dp.ExplicitBounds().FromRaw([]float64{3.5, 10.0})
  29. dp.SetSum(42.42)
  30. dp.Attributes().PutStr("label_1", "1")
  31. dp.Attributes().PutStr("label_2", "2")
  32. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  33. },
  34. },
  35. }
  36. for _, tt := range tests {
  37. t.Run(tt.name, func(t *testing.T) {
  38. resourceMetrics := pmetric.NewResourceMetrics()
  39. ilm := resourceMetrics.ScopeMetrics().AppendEmpty()
  40. ilm.Scope().SetName("test")
  41. tt.fillMetric(time.Now(), ilm.Metrics().AppendEmpty())
  42. a := newAccumulator(zap.NewNop(), 1*time.Hour).(*lastValueAccumulator)
  43. n := a.Accumulate(resourceMetrics)
  44. require.Equal(t, 0, n)
  45. signature := timeseriesSignature(ilm.Scope().Name(), ilm.Metrics().At(0), pcommon.NewMap(), pcommon.NewMap())
  46. v, ok := a.registeredMetrics.Load(signature)
  47. require.False(t, ok)
  48. require.Nil(t, v)
  49. })
  50. }
  51. }
  52. func TestAccumulateMetrics(t *testing.T) {
  53. tests := []struct {
  54. name string
  55. metric func(time.Time, float64, pmetric.MetricSlice)
  56. }{
  57. {
  58. name: "IntGauge",
  59. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  60. metric := metrics.AppendEmpty()
  61. metric.SetName("test_metric")
  62. metric.SetDescription("test description")
  63. dp := metric.SetEmptyGauge().DataPoints().AppendEmpty()
  64. dp.SetIntValue(int64(v))
  65. dp.Attributes().PutStr("label_1", "1")
  66. dp.Attributes().PutStr("label_2", "2")
  67. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  68. },
  69. },
  70. {
  71. name: "Gauge",
  72. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  73. metric := metrics.AppendEmpty()
  74. metric.SetName("test_metric")
  75. metric.SetDescription("test description")
  76. dp := metric.SetEmptyGauge().DataPoints().AppendEmpty()
  77. dp.SetDoubleValue(v)
  78. dp.Attributes().PutStr("label_1", "1")
  79. dp.Attributes().PutStr("label_2", "2")
  80. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  81. },
  82. },
  83. {
  84. name: "IntSum",
  85. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  86. metric := metrics.AppendEmpty()
  87. metric.SetName("test_metric")
  88. metric.SetDescription("test description")
  89. metric.SetEmptySum().SetIsMonotonic(false)
  90. metric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  91. dp := metric.Sum().DataPoints().AppendEmpty()
  92. dp.SetIntValue(int64(v))
  93. dp.Attributes().PutStr("label_1", "1")
  94. dp.Attributes().PutStr("label_2", "2")
  95. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  96. },
  97. },
  98. {
  99. name: "Sum",
  100. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  101. metric := metrics.AppendEmpty()
  102. metric.SetName("test_metric")
  103. metric.SetDescription("test description")
  104. metric.SetEmptySum().SetIsMonotonic(false)
  105. metric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  106. dp := metric.Sum().DataPoints().AppendEmpty()
  107. dp.SetDoubleValue(v)
  108. dp.Attributes().PutStr("label_1", "1")
  109. dp.Attributes().PutStr("label_2", "2")
  110. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  111. },
  112. },
  113. {
  114. name: "MonotonicIntSum",
  115. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  116. metric := metrics.AppendEmpty()
  117. metric.SetName("test_metric")
  118. metric.SetDescription("test description")
  119. metric.SetEmptySum().SetIsMonotonic(true)
  120. metric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  121. dp := metric.Sum().DataPoints().AppendEmpty()
  122. dp.SetIntValue(int64(v))
  123. dp.Attributes().PutStr("label_1", "1")
  124. dp.Attributes().PutStr("label_2", "2")
  125. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  126. },
  127. },
  128. {
  129. name: "MonotonicSum",
  130. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  131. metric := metrics.AppendEmpty()
  132. metric.SetName("test_metric")
  133. metric.SetEmptySum().SetIsMonotonic(true)
  134. metric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  135. metric.SetDescription("test description")
  136. dp := metric.Sum().DataPoints().AppendEmpty()
  137. dp.SetDoubleValue(v)
  138. dp.Attributes().PutStr("label_1", "1")
  139. dp.Attributes().PutStr("label_2", "2")
  140. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  141. },
  142. },
  143. {
  144. name: "Histogram",
  145. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  146. metric := metrics.AppendEmpty()
  147. metric.SetName("test_metric")
  148. metric.SetEmptyHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  149. metric.SetDescription("test description")
  150. dp := metric.Histogram().DataPoints().AppendEmpty()
  151. dp.BucketCounts().FromRaw([]uint64{5, 2})
  152. dp.SetCount(7)
  153. dp.ExplicitBounds().FromRaw([]float64{3.5, 10.0})
  154. dp.SetSum(v)
  155. dp.Attributes().PutStr("label_1", "1")
  156. dp.Attributes().PutStr("label_2", "2")
  157. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  158. },
  159. },
  160. {
  161. name: "Summary",
  162. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  163. metric := metrics.AppendEmpty()
  164. metric.SetName("test_metric")
  165. metric.SetDescription("test description")
  166. dp := metric.SetEmptySummary().DataPoints().AppendEmpty()
  167. dp.SetCount(10)
  168. dp.SetSum(0.012)
  169. dp.SetCount(10)
  170. dp.Attributes().PutStr("label_1", "1")
  171. dp.Attributes().PutStr("label_2", "2")
  172. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  173. fillQuantileValue := func(pN, value float64, dest pmetric.SummaryDataPointValueAtQuantile) {
  174. dest.SetQuantile(pN)
  175. dest.SetValue(value)
  176. }
  177. fillQuantileValue(0.50, 190, dp.QuantileValues().AppendEmpty())
  178. fillQuantileValue(0.99, 817, dp.QuantileValues().AppendEmpty())
  179. },
  180. },
  181. {
  182. name: "StalenessMarkerGauge",
  183. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  184. metric := metrics.AppendEmpty()
  185. metric.SetName("test_metric")
  186. metric.SetDescription("test description")
  187. dp := metric.SetEmptyGauge().DataPoints().AppendEmpty()
  188. dp.SetDoubleValue(v)
  189. dp.Attributes().PutStr("label_1", "1")
  190. dp.Attributes().PutStr("label_2", "2")
  191. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  192. dp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
  193. },
  194. },
  195. {
  196. name: "StalenessMarkerSum",
  197. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  198. metric := metrics.AppendEmpty()
  199. metric.SetName("test_metric")
  200. metric.SetDescription("test description")
  201. metric.SetEmptySum().SetIsMonotonic(false)
  202. metric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  203. dp := metric.Sum().DataPoints().AppendEmpty()
  204. dp.SetDoubleValue(v)
  205. dp.Attributes().PutStr("label_1", "1")
  206. dp.Attributes().PutStr("label_2", "2")
  207. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  208. dp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
  209. },
  210. },
  211. {
  212. name: "StalenessMarkerHistogram",
  213. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  214. metric := metrics.AppendEmpty()
  215. metric.SetName("test_metric")
  216. metric.SetEmptyHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  217. metric.SetDescription("test description")
  218. dp := metric.Histogram().DataPoints().AppendEmpty()
  219. dp.BucketCounts().FromRaw([]uint64{5, 2})
  220. dp.SetCount(7)
  221. dp.ExplicitBounds().FromRaw([]float64{3.5, 10.0})
  222. dp.SetSum(v)
  223. dp.Attributes().PutStr("label_1", "1")
  224. dp.Attributes().PutStr("label_2", "2")
  225. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  226. dp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
  227. },
  228. },
  229. {
  230. name: "StalenessMarkerSummary",
  231. metric: func(ts time.Time, v float64, metrics pmetric.MetricSlice) {
  232. metric := metrics.AppendEmpty()
  233. metric.SetName("test_metric")
  234. metric.SetDescription("test description")
  235. dp := metric.SetEmptySummary().DataPoints().AppendEmpty()
  236. dp.SetCount(10)
  237. dp.SetSum(0.012)
  238. dp.SetCount(10)
  239. dp.Attributes().PutStr("label_1", "1")
  240. dp.Attributes().PutStr("label_2", "2")
  241. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  242. dp.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
  243. fillQuantileValue := func(pN, value float64, dest pmetric.SummaryDataPointValueAtQuantile) {
  244. dest.SetQuantile(pN)
  245. dest.SetValue(value)
  246. }
  247. fillQuantileValue(0.50, 190, dp.QuantileValues().AppendEmpty())
  248. fillQuantileValue(0.99, 817, dp.QuantileValues().AppendEmpty())
  249. },
  250. },
  251. }
  252. for _, tt := range tests {
  253. t.Run(tt.name, func(t *testing.T) {
  254. ts1 := time.Now().Add(-3 * time.Second)
  255. ts2 := time.Now().Add(-2 * time.Second)
  256. ts3 := time.Now().Add(-1 * time.Second)
  257. resourceMetrics2 := pmetric.NewResourceMetrics()
  258. ilm2 := resourceMetrics2.ScopeMetrics().AppendEmpty()
  259. ilm2.Scope().SetName("test")
  260. tt.metric(ts2, 21, ilm2.Metrics())
  261. tt.metric(ts1, 13, ilm2.Metrics())
  262. a := newAccumulator(zap.NewNop(), 1*time.Hour).(*lastValueAccumulator)
  263. // 2 metric arrived
  264. n := a.Accumulate(resourceMetrics2)
  265. if strings.HasPrefix(tt.name, "StalenessMarker") {
  266. require.Equal(t, 0, n)
  267. return
  268. }
  269. require.Equal(t, 1, n)
  270. m2Labels, _, m2Value, m2Temporality, m2IsMonotonic := getMetricProperties(ilm2.Metrics().At(0))
  271. signature := timeseriesSignature(ilm2.Scope().Name(), ilm2.Metrics().At(0), m2Labels, pcommon.NewMap())
  272. m, ok := a.registeredMetrics.Load(signature)
  273. require.True(t, ok)
  274. v := m.(*accumulatedValue)
  275. vLabels, vTS, vValue, vTemporality, vIsMonotonic := getMetricProperties(ilm2.Metrics().At(0))
  276. require.Equal(t, v.scope.Name(), "test")
  277. require.Equal(t, v.value.Type(), ilm2.Metrics().At(0).Type())
  278. vLabels.Range(func(k string, v pcommon.Value) bool {
  279. r, _ := m2Labels.Get(k)
  280. require.Equal(t, r, v)
  281. return true
  282. })
  283. require.Equal(t, m2Labels.Len(), vLabels.Len())
  284. require.Equal(t, m2Value, vValue)
  285. require.Equal(t, ts2.Unix(), vTS.Unix())
  286. require.Greater(t, v.updated.Unix(), vTS.Unix())
  287. require.Equal(t, m2Temporality, vTemporality)
  288. require.Equal(t, m2IsMonotonic, vIsMonotonic)
  289. // 3 metrics arrived
  290. resourceMetrics3 := pmetric.NewResourceMetrics()
  291. ilm3 := resourceMetrics3.ScopeMetrics().AppendEmpty()
  292. ilm3.Scope().SetName("test")
  293. tt.metric(ts2, 21, ilm3.Metrics())
  294. tt.metric(ts3, 34, ilm3.Metrics())
  295. tt.metric(ts1, 13, ilm3.Metrics())
  296. _, _, m3Value, _, _ := getMetricProperties(ilm3.Metrics().At(1))
  297. n = a.Accumulate(resourceMetrics3)
  298. require.Equal(t, 2, n)
  299. m, ok = a.registeredMetrics.Load(signature)
  300. require.True(t, ok)
  301. v = m.(*accumulatedValue)
  302. _, vTS, vValue, _, _ = getMetricProperties(v.value)
  303. require.Equal(t, m3Value, vValue)
  304. require.Equal(t, ts3.Unix(), vTS.Unix())
  305. })
  306. }
  307. }
  308. func TestAccumulateDeltaToCumulative(t *testing.T) {
  309. tests := []struct {
  310. name string
  311. metric func(time.Time, time.Time, float64, pmetric.MetricSlice)
  312. }{
  313. {
  314. name: "MonotonicDeltaIntSum",
  315. metric: func(startTimestamp, ts time.Time, v float64, metrics pmetric.MetricSlice) {
  316. metric := metrics.AppendEmpty()
  317. metric.SetName("test_metric")
  318. metric.SetDescription("test description")
  319. metric.SetEmptySum().SetIsMonotonic(true)
  320. metric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
  321. dp := metric.Sum().DataPoints().AppendEmpty()
  322. dp.SetIntValue(int64(v))
  323. dp.Attributes().PutStr("label_1", "1")
  324. dp.Attributes().PutStr("label_2", "2")
  325. dp.SetStartTimestamp(pcommon.NewTimestampFromTime(startTimestamp))
  326. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  327. },
  328. },
  329. {
  330. name: "MonotonicDeltaSum",
  331. metric: func(startTimestamp, timestamp time.Time, v float64, metrics pmetric.MetricSlice) {
  332. metric := metrics.AppendEmpty()
  333. metric.SetName("test_metric")
  334. metric.SetEmptySum().SetIsMonotonic(true)
  335. metric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
  336. metric.SetDescription("test description")
  337. dp := metric.Sum().DataPoints().AppendEmpty()
  338. dp.SetDoubleValue(v)
  339. dp.Attributes().PutStr("label_1", "1")
  340. dp.Attributes().PutStr("label_2", "2")
  341. dp.SetStartTimestamp(pcommon.NewTimestampFromTime(startTimestamp))
  342. dp.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
  343. },
  344. },
  345. }
  346. for _, tt := range tests {
  347. t.Run(tt.name, func(t *testing.T) {
  348. ts1 := time.Now().Add(-3 * time.Second)
  349. ts2 := time.Now().Add(-2 * time.Second)
  350. ts3 := time.Now().Add(-1 * time.Second)
  351. resourceMetrics := pmetric.NewResourceMetrics()
  352. ilm := resourceMetrics.ScopeMetrics().AppendEmpty()
  353. ilm.Scope().SetName("test")
  354. a := newAccumulator(zap.NewNop(), 1*time.Hour).(*lastValueAccumulator)
  355. dataPointValue1 := float64(11)
  356. dataPointValue2 := float64(32)
  357. // The first point arrived
  358. tt.metric(ts1, ts2, dataPointValue1, ilm.Metrics())
  359. n := a.Accumulate(resourceMetrics)
  360. require.Equal(t, 1, n)
  361. // The next point arrived
  362. tt.metric(ts2, ts3, dataPointValue2, ilm.Metrics())
  363. n = a.Accumulate(resourceMetrics)
  364. require.Equal(t, 2, n)
  365. mLabels, _, mValue, _, _ := getMetricProperties(ilm.Metrics().At(1))
  366. signature := timeseriesSignature(ilm.Scope().Name(), ilm.Metrics().At(0), mLabels, pcommon.NewMap())
  367. m, ok := a.registeredMetrics.Load(signature)
  368. require.True(t, ok)
  369. v := m.(*accumulatedValue)
  370. vLabels, vTS, vValue, vTemporality, vIsMonotonic := getMetricProperties(v.value)
  371. require.Equal(t, v.scope.Name(), "test")
  372. require.Equal(t, v.value.Type(), ilm.Metrics().At(0).Type())
  373. require.Equal(t, v.value.Type(), ilm.Metrics().At(1).Type())
  374. vLabels.Range(func(k string, v pcommon.Value) bool {
  375. r, _ := mLabels.Get(k)
  376. require.Equal(t, r, v)
  377. return true
  378. })
  379. require.Equal(t, mLabels.Len(), vLabels.Len())
  380. require.Equal(t, mValue, vValue)
  381. require.Equal(t, dataPointValue1+dataPointValue2, vValue)
  382. require.Equal(t, pmetric.AggregationTemporalityCumulative, vTemporality)
  383. require.Equal(t, true, vIsMonotonic)
  384. require.Equal(t, ts3.Unix(), vTS.Unix())
  385. })
  386. }
  387. }
  388. func TestAccumulateDroppedMetrics(t *testing.T) {
  389. tests := []struct {
  390. name string
  391. fillMetric func(time.Time, pmetric.Metric)
  392. }{
  393. {
  394. name: "NonMonotonicIntSum",
  395. fillMetric: func(ts time.Time, metric pmetric.Metric) {
  396. metric.SetName("test_metric")
  397. metric.SetEmptySum().SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
  398. metric.Sum().SetIsMonotonic(false)
  399. dp := metric.Sum().DataPoints().AppendEmpty()
  400. dp.SetIntValue(42)
  401. dp.Attributes().PutStr("label_1", "1")
  402. dp.Attributes().PutStr("label_2", "2")
  403. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  404. },
  405. },
  406. {
  407. name: "NonMonotonicSum",
  408. fillMetric: func(ts time.Time, metric pmetric.Metric) {
  409. metric.SetName("test_metric")
  410. metric.SetEmptySum().SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
  411. metric.Sum().SetIsMonotonic(false)
  412. dp := metric.Sum().DataPoints().AppendEmpty()
  413. dp.SetDoubleValue(42.42)
  414. dp.Attributes().PutStr("label_1", "1")
  415. dp.Attributes().PutStr("label_2", "2")
  416. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  417. },
  418. },
  419. {
  420. name: "UnspecifiedIntSum",
  421. fillMetric: func(ts time.Time, metric pmetric.Metric) {
  422. metric.SetName("test_metric")
  423. metric.SetEmptySum().SetAggregationTemporality(pmetric.AggregationTemporalityUnspecified)
  424. dp := metric.Sum().DataPoints().AppendEmpty()
  425. dp.SetIntValue(42)
  426. dp.Attributes().PutStr("label_1", "1")
  427. dp.Attributes().PutStr("label_2", "2")
  428. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  429. },
  430. },
  431. {
  432. name: "UnspecifiedSum",
  433. fillMetric: func(ts time.Time, metric pmetric.Metric) {
  434. metric.SetName("test_metric")
  435. metric.SetEmptySum().SetAggregationTemporality(pmetric.AggregationTemporalityUnspecified)
  436. dp := metric.Sum().DataPoints().AppendEmpty()
  437. dp.SetDoubleValue(42.42)
  438. dp.Attributes().PutStr("label_1", "1")
  439. dp.Attributes().PutStr("label_2", "2")
  440. dp.SetTimestamp(pcommon.NewTimestampFromTime(ts))
  441. },
  442. },
  443. }
  444. for _, tt := range tests {
  445. t.Run(tt.name, func(t *testing.T) {
  446. resourceMetrics := pmetric.NewResourceMetrics()
  447. ilm := resourceMetrics.ScopeMetrics().AppendEmpty()
  448. ilm.Scope().SetName("test")
  449. tt.fillMetric(time.Now(), ilm.Metrics().AppendEmpty())
  450. a := newAccumulator(zap.NewNop(), 1*time.Hour).(*lastValueAccumulator)
  451. n := a.Accumulate(resourceMetrics)
  452. require.Equal(t, 0, n)
  453. signature := timeseriesSignature(ilm.Scope().Name(), ilm.Metrics().At(0), pcommon.NewMap(), pcommon.NewMap())
  454. v, ok := a.registeredMetrics.Load(signature)
  455. require.False(t, ok)
  456. require.Nil(t, v)
  457. })
  458. }
  459. }
  460. func TestTimeseriesSignatureNotMutating(t *testing.T) {
  461. attrs := pcommon.NewMap()
  462. attrs.PutStr("label_2", "2")
  463. attrs.PutStr("label_1", "1")
  464. origAttrs := pcommon.NewMap()
  465. attrs.CopyTo(origAttrs)
  466. timeseriesSignature("test_il", pmetric.NewMetric(), attrs, attrs)
  467. require.Equal(t, origAttrs, attrs) // make sure attrs are not mutated
  468. }
  469. func getMetricProperties(metric pmetric.Metric) (
  470. attributes pcommon.Map,
  471. ts time.Time,
  472. value float64,
  473. temporality pmetric.AggregationTemporality,
  474. isMonotonic bool,
  475. ) {
  476. switch metric.Type() {
  477. case pmetric.MetricTypeGauge:
  478. attributes = metric.Gauge().DataPoints().At(0).Attributes()
  479. ts = metric.Gauge().DataPoints().At(0).Timestamp().AsTime()
  480. dp := metric.Gauge().DataPoints().At(0)
  481. switch dp.ValueType() {
  482. case pmetric.NumberDataPointValueTypeInt:
  483. value = float64(dp.IntValue())
  484. case pmetric.NumberDataPointValueTypeDouble:
  485. value = dp.DoubleValue()
  486. }
  487. temporality = pmetric.AggregationTemporalityUnspecified
  488. isMonotonic = false
  489. case pmetric.MetricTypeSum:
  490. attributes = metric.Sum().DataPoints().At(0).Attributes()
  491. ts = metric.Sum().DataPoints().At(0).Timestamp().AsTime()
  492. dp := metric.Sum().DataPoints().At(0)
  493. switch dp.ValueType() {
  494. case pmetric.NumberDataPointValueTypeInt:
  495. value = float64(dp.IntValue())
  496. case pmetric.NumberDataPointValueTypeDouble:
  497. value = dp.DoubleValue()
  498. }
  499. temporality = metric.Sum().AggregationTemporality()
  500. isMonotonic = metric.Sum().IsMonotonic()
  501. case pmetric.MetricTypeHistogram:
  502. attributes = metric.Histogram().DataPoints().At(0).Attributes()
  503. ts = metric.Histogram().DataPoints().At(0).Timestamp().AsTime()
  504. value = metric.Histogram().DataPoints().At(0).Sum()
  505. temporality = metric.Histogram().AggregationTemporality()
  506. isMonotonic = true
  507. case pmetric.MetricTypeSummary:
  508. attributes = metric.Summary().DataPoints().At(0).Attributes()
  509. ts = metric.Summary().DataPoints().At(0).Timestamp().AsTime()
  510. value = metric.Summary().DataPoints().At(0).Sum()
  511. default:
  512. log.Panicf("Invalid data type %s", metric.Type().String())
  513. }
  514. return
  515. }