123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package collectdreceiver
- import (
- "encoding/json"
- "os"
- "path/filepath"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/pdata/pcommon"
- "go.opentelemetry.io/collector/pdata/pmetric"
- "go.uber.org/zap"
- "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
- "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
- )
- func TestDecodeEvent(t *testing.T) {
- metrics := pmetric.NewMetrics()
- scopeMetrics := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
- jsonData, err := os.ReadFile(filepath.Join("testdata", "event.json"))
- require.NoError(t, err)
- var records []collectDRecord
- err = json.Unmarshal(jsonData, &records)
- require.NoError(t, err)
- for _, cdr := range records {
- err := cdr.appendToMetrics(zap.NewNop(), scopeMetrics, map[string]string{})
- assert.NoError(t, err)
- assert.Equal(t, 0, metrics.MetricCount())
- }
- }
- func TestDecodeMetrics(t *testing.T) {
- metrics := pmetric.NewMetrics()
- scopeMemtrics := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
- jsonData, err := os.ReadFile(filepath.Join("testdata", "collectd.json"))
- require.NoError(t, err)
- var records []collectDRecord
- err = json.Unmarshal(jsonData, &records)
- require.NoError(t, err)
- for _, cdr := range records {
- err = cdr.appendToMetrics(zap.NewNop(), scopeMemtrics, map[string]string{})
- assert.NoError(t, err)
- }
- assert.Equal(t, 10, metrics.MetricCount())
- assertMetricsEqual(t, metrics)
- }
- func createPtrFloat64(v float64) *float64 {
- return &v
- }
- func TestStartTimestamp(t *testing.T) {
- tests := []struct {
- name string
- record collectDRecord
- metricDescriptorType string
- wantStartTimestamp pcommon.Timestamp
- }{
- {
- name: "metric type cumulative distribution",
- record: collectDRecord{
- Time: createPtrFloat64(10),
- Interval: createPtrFloat64(5),
- },
- metricDescriptorType: "cumulative",
- wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(5, 0)),
- },
- {
- name: "metric type cumulative double",
- record: collectDRecord{
- Time: createPtrFloat64(10),
- Interval: createPtrFloat64(5),
- },
- metricDescriptorType: "cumulative",
- wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(5, 0)),
- },
- {
- name: "metric type cumulative int64",
- record: collectDRecord{
- Time: createPtrFloat64(10),
- Interval: createPtrFloat64(5),
- },
- metricDescriptorType: "cumulative",
- wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(5, 0)),
- },
- {
- name: "metric type non-cumulative gauge distribution",
- record: collectDRecord{
- Time: createPtrFloat64(0),
- Interval: createPtrFloat64(0),
- },
- metricDescriptorType: "gauge",
- wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(0, 0)),
- },
- {
- name: "metric type non-cumulative gauge int64",
- record: collectDRecord{
- Time: createPtrFloat64(0),
- Interval: createPtrFloat64(0),
- },
- metricDescriptorType: "gauge",
- wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(0, 0)),
- },
- {
- name: "metric type non-cumulativegauge double",
- record: collectDRecord{
- Time: createPtrFloat64(0),
- Interval: createPtrFloat64(0),
- },
- metricDescriptorType: "gauge",
- wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(0, 0)),
- },
- }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- gotStartTimestamp := tc.record.startTimestamp(tc.metricDescriptorType)
- assert.Equal(t, tc.wantStartTimestamp.AsTime(), gotStartTimestamp.AsTime())
- })
- }
- }
- func assertMetricsEqual(t *testing.T, actual pmetric.Metrics) {
- goldenPath := filepath.Join("testdata", "expected.yaml")
- expectedMetrics, err := golden.ReadMetrics(goldenPath)
- require.NoError(t, err)
- err = pmetrictest.CompareMetrics(expectedMetrics, actual, pmetrictest.IgnoreStartTimestamp(),
- pmetrictest.IgnoreTimestamp(),
- pmetrictest.IgnoreMetricsOrder())
- require.NoError(t, err)
- }
|