Skip to content

Commit

Permalink
Add test helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fedetorres93 authored and carrieedwards committed Jul 17, 2024
1 parent 83dd84e commit d5f7724
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions receiver/datadogreceiver/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package datadogreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/datadogreceiver"

import (
"testing"

"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pmetric"
)

func createMetricsTranslator() *MetricsTranslator {
mt := newMetricsTranslator()
mt.buildInfo = component.BuildInfo{
Command: "otelcol",
Description: "OpenTelemetry Collector",
Version: "latest",
}
return mt
}

func requireResourceMetrics(t *testing.T, result pmetric.Metrics, expectedAttrs pcommon.Map, expectedLen int) {
require.Equal(t, expectedLen, result.ResourceMetrics().Len())
require.Equal(t, expectedAttrs, result.ResourceMetrics().At(0).Resource().Attributes())
}

func requireScopeMetrics(t *testing.T, result pmetric.Metrics, expectedScopeMetricsLen, expectedMetricsLen int) {
require.Equal(t, expectedScopeMetricsLen, result.ResourceMetrics().At(0).ScopeMetrics().Len())
require.Equal(t, expectedMetricsLen, result.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().Len())
}

func requireScope(t *testing.T, result pmetric.Metrics, expectedAttrs pcommon.Map, expectedName, expectedVersion string) {
require.Equal(t, expectedName, result.ResourceMetrics().At(0).ScopeMetrics().At(0).Scope().Name())
require.Equal(t, expectedVersion, result.ResourceMetrics().At(0).ScopeMetrics().At(0).Scope().Version())
require.Equal(t, expectedAttrs, result.ResourceMetrics().At(0).ScopeMetrics().At(0).Scope().Attributes())
}

func requireMetricAndDataPointCounts(t *testing.T, result pmetric.Metrics, expectedMetricCount, expectedDpCount int) {
require.Equal(t, expectedMetricCount, result.MetricCount())
require.Equal(t, expectedDpCount, result.DataPointCount())
}

func requireSum(t *testing.T, metric pmetric.Metric, expectedName string, expectedAggregationTemporality pmetric.AggregationTemporality, expectedDpsLen int) {
require.Equal(t, expectedName, metric.Name())
require.Equal(t, pmetric.MetricTypeSum, metric.Type())
require.Equal(t, expectedAggregationTemporality, metric.Sum().AggregationTemporality())
require.Equal(t, expectedDpsLen, metric.Sum().DataPoints().Len())
}

func requireGauge(t *testing.T, metric pmetric.Metric, expectedName string, expectedDpsLen int) {
require.Equal(t, expectedName, metric.Name())
require.Equal(t, pmetric.MetricTypeGauge, metric.Type())
require.Equal(t, expectedDpsLen, metric.Gauge().DataPoints().Len())
}

func requireDp(t *testing.T, dp pmetric.NumberDataPoint, expectedAttrs pcommon.Map, expectedTime int64, expectedValue float64) {
require.Equal(t, expectedTime, dp.Timestamp().AsTime().Unix())
require.Equal(t, expectedValue, dp.DoubleValue())
require.Equal(t, expectedAttrs, dp.Attributes())
}

0 comments on commit d5f7724

Please sign in to comment.