testutils.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "go.opentelemetry.io/collector/pdata/pcommon"
  9. "go.opentelemetry.io/collector/pdata/pmetric"
  10. )
  11. func AssertContainsAttribute(t *testing.T, attr pcommon.Map, key string) {
  12. _, ok := attr.Get(key)
  13. assert.True(t, ok)
  14. }
  15. func AssertDescriptorEqual(t *testing.T, expected pmetric.Metric, actual pmetric.Metric) {
  16. assert.Equal(t, expected.Name(), actual.Name())
  17. assert.Equal(t, expected.Description(), actual.Description())
  18. assert.Equal(t, expected.Unit(), actual.Unit())
  19. assert.Equal(t, expected.Type(), actual.Type())
  20. }
  21. func AssertSumMetricHasAttributeValue(t *testing.T, metric pmetric.Metric, index int, labelName string, expectedVal pcommon.Value) {
  22. val, ok := metric.Sum().DataPoints().At(index).Attributes().Get(labelName)
  23. assert.Truef(t, ok, "Missing attribute %q in metric %q", labelName, metric.Name())
  24. assert.Equal(t, expectedVal, val)
  25. }
  26. func AssertSumMetricHasAttribute(t *testing.T, metric pmetric.Metric, index int, labelName string) {
  27. _, ok := metric.Sum().DataPoints().At(index).Attributes().Get(labelName)
  28. assert.Truef(t, ok, "Missing attribute %q in metric %q", labelName, metric.Name())
  29. }
  30. func AssertSumMetricStartTimeEquals(t *testing.T, metric pmetric.Metric, startTime pcommon.Timestamp) {
  31. ddps := metric.Sum().DataPoints()
  32. for i := 0; i < ddps.Len(); i++ {
  33. require.Equal(t, startTime, ddps.At(i).StartTimestamp())
  34. }
  35. }
  36. func AssertGaugeMetricHasAttributeValue(t *testing.T, metric pmetric.Metric, index int, labelName string, expectedVal pcommon.Value) {
  37. val, ok := metric.Gauge().DataPoints().At(index).Attributes().Get(labelName)
  38. assert.Truef(t, ok, "Missing attribute %q in metric %q", labelName, metric.Name())
  39. assert.Equal(t, expectedVal, val)
  40. }
  41. func AssertGaugeMetricHasAttribute(t *testing.T, metric pmetric.Metric, index int, labelName string) {
  42. _, ok := metric.Gauge().DataPoints().At(index).Attributes().Get(labelName)
  43. assert.Truef(t, ok, "Missing attribute %q in metric %q", labelName, metric.Name())
  44. }
  45. func AssertGaugeMetricStartTimeEquals(t *testing.T, metric pmetric.Metric, startTime pcommon.Timestamp) {
  46. ddps := metric.Gauge().DataPoints()
  47. for i := 0; i < ddps.Len(); i++ {
  48. require.Equal(t, startTime, ddps.At(i).StartTimestamp())
  49. }
  50. }
  51. func AssertSameTimeStampForAllMetrics(t *testing.T, metrics pmetric.MetricSlice) {
  52. AssertSameTimeStampForMetrics(t, metrics, 0, metrics.Len())
  53. }
  54. func AssertSameTimeStampForMetrics(t *testing.T, metrics pmetric.MetricSlice, startIdx, endIdx int) {
  55. var ts pcommon.Timestamp
  56. for i := startIdx; i < endIdx; i++ {
  57. metric := metrics.At(i)
  58. if metric.Type() == pmetric.MetricTypeSum {
  59. ddps := metric.Sum().DataPoints()
  60. for j := 0; j < ddps.Len(); j++ {
  61. if ts == 0 {
  62. ts = ddps.At(j).Timestamp()
  63. }
  64. require.Equalf(t, ts, ddps.At(j).Timestamp(), "metrics contained different end timestamp values")
  65. }
  66. }
  67. }
  68. }