integration_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build integration
  4. // +build integration
  5. package dockerstatsreceiver
  6. import (
  7. "context"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "github.com/testcontainers/testcontainers-go"
  13. "github.com/testcontainers/testcontainers-go/wait"
  14. "go.opentelemetry.io/collector/component"
  15. "go.opentelemetry.io/collector/consumer/consumertest"
  16. "go.opentelemetry.io/collector/pdata/pmetric"
  17. rcvr "go.opentelemetry.io/collector/receiver"
  18. "go.opentelemetry.io/collector/receiver/receivertest"
  19. conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
  20. "go.uber.org/zap"
  21. "go.uber.org/zap/zaptest"
  22. )
  23. type testHost struct {
  24. component.Host
  25. t *testing.T
  26. }
  27. // ReportFatalError causes the test to be run to fail.
  28. func (h *testHost) ReportFatalError(err error) {
  29. h.t.Fatalf("receiver reported a fatal error: %v", err)
  30. }
  31. var _ component.Host = (*testHost)(nil)
  32. func factory() (rcvr.Factory, *Config) {
  33. f := NewFactory()
  34. config := f.CreateDefaultConfig().(*Config)
  35. config.CollectionInterval = 1 * time.Second
  36. return f, config
  37. }
  38. func paramsAndContext(t *testing.T) (rcvr.CreateSettings, context.Context, context.CancelFunc) {
  39. ctx, cancel := context.WithCancel(context.Background())
  40. logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.AddCaller()))
  41. settings := receivertest.NewNopCreateSettings()
  42. settings.Logger = logger
  43. return settings, ctx, cancel
  44. }
  45. func createNginxContainer(ctx context.Context, t *testing.T) testcontainers.Container {
  46. req := testcontainers.ContainerRequest{
  47. Image: "docker.io/library/nginx:1.17",
  48. ExposedPorts: []string{"80/tcp"},
  49. WaitingFor: wait.ForExposedPort(),
  50. }
  51. container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
  52. ContainerRequest: req,
  53. Started: true,
  54. })
  55. require.Nil(t, err)
  56. require.NotNil(t, container)
  57. return container
  58. }
  59. func hasResourceScopeMetrics(containerID string, metrics []pmetric.Metrics) bool {
  60. for _, m := range metrics {
  61. for i := 0; i < m.ResourceMetrics().Len(); i++ {
  62. rm := m.ResourceMetrics().At(i)
  63. id, ok := rm.Resource().Attributes().Get(conventions.AttributeContainerID)
  64. if ok && id.AsString() == containerID && rm.ScopeMetrics().Len() > 0 {
  65. return true
  66. }
  67. }
  68. }
  69. return false
  70. }
  71. func TestDefaultMetricsIntegration(t *testing.T) {
  72. t.Parallel()
  73. params, ctx, cancel := paramsAndContext(t)
  74. defer cancel()
  75. container := createNginxContainer(ctx, t)
  76. consumer := new(consumertest.MetricsSink)
  77. f, config := factory()
  78. recv, err := f.CreateMetricsReceiver(ctx, params, config, consumer)
  79. require.NoError(t, err, "failed creating metrics receiver")
  80. require.NoError(t, recv.Start(ctx, &testHost{
  81. t: t,
  82. }))
  83. assert.Eventuallyf(t, func() bool {
  84. return hasResourceScopeMetrics(container.GetContainerID(), consumer.AllMetrics())
  85. }, 5*time.Second, 1*time.Second, "failed to receive any metrics")
  86. assert.NoError(t, recv.Shutdown(ctx))
  87. }
  88. func TestMonitoringAddedAndRemovedContainerIntegration(t *testing.T) {
  89. t.Parallel()
  90. params, ctx, cancel := paramsAndContext(t)
  91. defer cancel()
  92. consumer := new(consumertest.MetricsSink)
  93. f, config := factory()
  94. recv, err := f.CreateMetricsReceiver(ctx, params, config, consumer)
  95. require.NoError(t, err, "failed creating metrics receiver")
  96. require.NoError(t, recv.Start(ctx, &testHost{
  97. t: t,
  98. }))
  99. container := createNginxContainer(ctx, t)
  100. // Check metrics are collected for added container.
  101. assert.Eventuallyf(t, func() bool {
  102. return hasResourceScopeMetrics(container.GetContainerID(), consumer.AllMetrics())
  103. }, 5*time.Second, 1*time.Second, "failed to receive any metrics")
  104. require.NoError(t, container.Terminate(ctx))
  105. consumer.Reset()
  106. // Check metrics are not collected for removed container.
  107. assert.Never(t, func() bool {
  108. return hasResourceScopeMetrics(container.GetContainerID(), consumer.AllMetrics())
  109. }, 5*time.Second, 1*time.Second, "received undesired metrics")
  110. assert.NoError(t, recv.Shutdown(ctx))
  111. }
  112. func TestExcludedImageProducesNoMetricsIntegration(t *testing.T) {
  113. t.Parallel()
  114. params, ctx, cancel := paramsAndContext(t)
  115. defer cancel()
  116. container := createNginxContainer(ctx, t)
  117. f, config := factory()
  118. config.ExcludedImages = append(config.ExcludedImages, "*nginx*")
  119. consumer := new(consumertest.MetricsSink)
  120. recv, err := f.CreateMetricsReceiver(ctx, params, config, consumer)
  121. require.NoError(t, err, "failed creating metrics receiver")
  122. require.NoError(t, recv.Start(ctx, &testHost{
  123. t: t,
  124. }))
  125. assert.Never(t, func() bool {
  126. return hasResourceScopeMetrics(container.GetContainerID(), consumer.AllMetrics())
  127. }, 5*time.Second, 1*time.Second, "received undesired metrics")
  128. assert.NoError(t, recv.Shutdown(ctx))
  129. }