| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package pmetricotlp |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
gootlpcollectormetrics "go.opentelemetry.io/proto/slim/otlp/collector/metrics/v1" |
| 14 |
goproto "google.golang.org/protobuf/proto" |
| 15 |
|
| 16 |
"go.opentelemetry.io/collector/pdata/internal" |
| 17 |
"go.opentelemetry.io/collector/pdata/internal/otlp" |
| 18 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
_ json.Unmarshaler = ExportRequest{} |
| 23 |
_ json.Marshaler = ExportRequest{} |
| 24 |
) |
| 25 |
|
| 26 |
var metricsRequestJSON = []byte(` |
| 27 |
{ |
| 28 |
"resourceMetrics": [ |
| 29 |
{ |
| 30 |
"resource": {}, |
| 31 |
"scopeMetrics": [ |
| 32 |
{ |
| 33 |
"scope": {}, |
| 34 |
"metrics": [ |
| 35 |
{ |
| 36 |
"name": "test_metric" |
| 37 |
} |
| 38 |
] |
| 39 |
} |
| 40 |
] |
| 41 |
} |
| 42 |
] |
| 43 |
}`) |
| 44 |
|
| 45 |
func TestRequestToPData(t *testing.T) { |
| 46 |
tr := NewExportRequest() |
| 47 |
assert.Equal(t, 0, tr.Metrics().MetricCount()) |
| 48 |
tr.Metrics().ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty() |
| 49 |
assert.Equal(t, 1, tr.Metrics().MetricCount()) |
| 50 |
} |
| 51 |
|
| 52 |
func TestRequestJSON(t *testing.T) { |
| 53 |
mr := NewExportRequest() |
| 54 |
require.NoError(t, mr.UnmarshalJSON(metricsRequestJSON)) |
| 55 |
assert.Equal(t, "test_metric", mr.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Name()) |
| 56 |
|
| 57 |
got, err := mr.MarshalJSON() |
| 58 |
require.NoError(t, err) |
| 59 |
assert.Equal(t, strings.Join(strings.Fields(string(metricsRequestJSON)), ""), string(got)) |
| 60 |
} |
| 61 |
|
| 62 |
func TestMetricsProtoWireCompatibility(t *testing.T) { |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
md := NewExportRequestFromMetrics(pmetric.Metrics(internal.GenTestMetricsWrapper())) |
| 69 |
|
| 70 |
|
| 71 |
wire1, err := md.MarshalProto() |
| 72 |
require.NoError(t, err) |
| 73 |
assert.NotNil(t, wire1) |
| 74 |
|
| 75 |
|
| 76 |
var goprotoMessage gootlpcollectormetrics.ExportMetricsServiceRequest |
| 77 |
err = goproto.Unmarshal(wire1, &goprotoMessage) |
| 78 |
require.NoError(t, err) |
| 79 |
|
| 80 |
|
| 81 |
wire2, err := goproto.Marshal(&goprotoMessage) |
| 82 |
require.NoError(t, err) |
| 83 |
assert.NotNil(t, wire2) |
| 84 |
|
| 85 |
|
| 86 |
md2 := NewExportRequest() |
| 87 |
err = md2.UnmarshalProto(wire2) |
| 88 |
require.NoError(t, err) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
otlp.MigrateMetrics(md.orig.ResourceMetrics) |
| 94 |
assert.Equal(t, md, md2) |
| 95 |
} |