| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otlptext |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
|
| 15 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 16 |
"go.opentelemetry.io/collector/pdata/testdata" |
| 17 |
) |
| 18 |
|
| 19 |
func TestMetricsText(t *testing.T) { |
| 20 |
tests := []struct { |
| 21 |
name string |
| 22 |
in pmetric.Metrics |
| 23 |
out string |
| 24 |
}{ |
| 25 |
{ |
| 26 |
name: "empty_metrics", |
| 27 |
in: pmetric.NewMetrics(), |
| 28 |
out: "empty.out", |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "metrics_with_all_types", |
| 32 |
in: testdata.GenerateMetricsAllTypes(), |
| 33 |
out: "metrics_with_all_types.out", |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "two_metrics", |
| 37 |
in: testdata.GenerateMetrics(2), |
| 38 |
out: "two_metrics.out", |
| 39 |
}, |
| 40 |
{ |
| 41 |
name: "invalid_metric_type", |
| 42 |
in: testdata.GenerateMetricsMetricTypeInvalid(), |
| 43 |
out: "invalid_metric_type.out", |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: "metrics_with_entity_refs", |
| 47 |
in: generateMetricsWithEntityRefs(), |
| 48 |
out: "metrics_with_entity_refs.out", |
| 49 |
}, |
| 50 |
} |
| 51 |
for _, tt := range tests { |
| 52 |
t.Run(tt.name, func(t *testing.T) { |
| 53 |
got, err := NewTextMetricsMarshaler().MarshalMetrics(tt.in) |
| 54 |
require.NoError(t, err) |
| 55 |
out, err := os.ReadFile(filepath.Join("testdata", "metrics", tt.out)) |
| 56 |
require.NoError(t, err) |
| 57 |
expected := strings.ReplaceAll(string(out), "\r", "") |
| 58 |
assert.Equal(t, expected, string(got)) |
| 59 |
}) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func generateMetricsWithEntityRefs() pmetric.Metrics { |
| 64 |
md := pmetric.NewMetrics() |
| 65 |
rm := md.ResourceMetrics().AppendEmpty() |
| 66 |
|
| 67 |
setupResourceWithEntityRefs(rm.Resource()) |
| 68 |
|
| 69 |
sm := rm.ScopeMetrics().AppendEmpty() |
| 70 |
sm.Scope().SetName("test-scope") |
| 71 |
metric := sm.Metrics().AppendEmpty() |
| 72 |
metric.SetName("test-metric") |
| 73 |
metric.SetDescription("A test metric") |
| 74 |
metric.SetUnit("1") |
| 75 |
|
| 76 |
gauge := metric.SetEmptyGauge() |
| 77 |
dp := gauge.DataPoints().AppendEmpty() |
| 78 |
dp.SetDoubleValue(123.45) |
| 79 |
dp.Attributes().PutStr("test.attribute", "test-value") |
| 80 |
|
| 81 |
return md |
| 82 |
} |