logs_receiver_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sqlqueryreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlqueryreceiver"
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "go.opentelemetry.io/collector/pdata/pcommon"
  10. )
  11. func TestLogsQueryReceiver_Collect(t *testing.T) {
  12. now := time.Now()
  13. fakeClient := &fakeDBClient{
  14. stringMaps: [][]stringMap{
  15. {{"col1": "42"}, {"col1": "63"}},
  16. },
  17. }
  18. queryReceiver := logsQueryReceiver{
  19. client: fakeClient,
  20. query: Query{
  21. Logs: []LogsCfg{
  22. {
  23. BodyColumn: "col1",
  24. },
  25. },
  26. },
  27. }
  28. logs, err := queryReceiver.collect(context.Background())
  29. assert.NoError(t, err)
  30. assert.NotNil(t, logs)
  31. assert.Equal(t, 2, logs.LogRecordCount())
  32. logRecord := logs.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0)
  33. assert.Equal(t, "42", logRecord.Body().Str())
  34. assert.GreaterOrEqual(t, logRecord.ObservedTimestamp(), pcommon.NewTimestampFromTime(now))
  35. logRecord = logs.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(1)
  36. assert.Equal(t, "63", logRecord.Body().Str())
  37. assert.GreaterOrEqual(t, logRecord.ObservedTimestamp(), pcommon.NewTimestampFromTime(now))
  38. assert.Equal(t,
  39. logs.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).ObservedTimestamp(),
  40. logs.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(1).ObservedTimestamp(),
  41. "Observed timestamps of all log records collected in a single scrape should be equal",
  42. )
  43. }