Skip to content

Commit

Permalink
Update otlpreceiver tests to not depend on generated protos
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed May 19, 2021
1 parent fa27faf commit 4576668
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 225 deletions.
64 changes: 12 additions & 52 deletions receiver/otlpreceiver/logs/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import (
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal"
"go.opentelemetry.io/collector/internal/data"
collectorlog "go.opentelemetry.io/collector/internal/data/protogen/collector/logs/v1"
otlplog "go.opentelemetry.io/collector/internal/data/protogen/logs/v1"
"go.opentelemetry.io/collector/internal/pdatagrpc"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/obsreport"
)

Expand All @@ -48,43 +47,18 @@ func TestExport(t *testing.T) {
require.NoError(t, err, "Failed to create the TraceServiceClient: %v", err)
defer traceClientDoneFn()

// when

unixnanos := uint64(12578940000000012345)
traceID := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}
spanID := [8]byte{8, 7, 6, 5, 4, 3, 2, 1}
otlp := &collectorlog.ExportLogsServiceRequest{
ResourceLogs: []*otlplog.ResourceLogs{
{
InstrumentationLibraryLogs: []*otlplog.InstrumentationLibraryLogs{
{
Logs: []*otlplog.LogRecord{
{
TraceId: data.NewTraceID(traceID),
SpanId: data.NewSpanID(spanID),
Name: "operationB",
TimeUnixNano: unixnanos,
},
},
},
},
},
},
}

req := testdata.GenerateLogsOneLogRecord()
// Keep log data to compare the test result against it
// Clone needed because OTLP proto XXX_ fields are altered in the GRPC downstream
ld := pdata.LogsFromInternalRep(internal.LogsFromOtlp(otlp)).Clone()
logData := req.Clone()

resp, err := traceClient.Export(context.Background(), otlp)
resp, err := traceClient.Export(context.Background(), req)
require.NoError(t, err, "Failed to export trace: %v", err)
require.NotNil(t, resp, "The response is missing")

// assert

require.Equal(t, 1, len(logSink.AllLogs()), "unexpected length: %v", len(logSink.AllLogs()))

assert.EqualValues(t, ld, logSink.AllLogs()[0])
lds := logSink.AllLogs()
require.Len(t, lds, 1)
assert.EqualValues(t, logData, lds[0])
}

func TestExport_EmptyRequest(t *testing.T) {
Expand All @@ -97,7 +71,7 @@ func TestExport_EmptyRequest(t *testing.T) {
require.NoError(t, err, "Failed to create the TraceServiceClient: %v", err)
defer logClientDoneFn()

resp, err := logClient.Export(context.Background(), &collectorlog.ExportLogsServiceRequest{})
resp, err := logClient.Export(context.Background(), pdata.NewLogs())
assert.NoError(t, err, "Failed to export trace: %v", err)
assert.NotNil(t, resp, "The response is missing")
}
Expand All @@ -110,34 +84,20 @@ func TestExport_ErrorConsumer(t *testing.T) {
require.NoError(t, err, "Failed to create the TraceServiceClient: %v", err)
defer logClientDoneFn()

req := &collectorlog.ExportLogsServiceRequest{
ResourceLogs: []*otlplog.ResourceLogs{
{
InstrumentationLibraryLogs: []*otlplog.InstrumentationLibraryLogs{
{
Logs: []*otlplog.LogRecord{
{
Name: "operationB",
},
},
},
},
},
},
}
req := testdata.GenerateLogsOneLogRecord()

resp, err := logClient.Export(context.Background(), req)
assert.EqualError(t, err, "rpc error: code = Unknown desc = my error")
assert.Nil(t, resp)
}

func makeLogsServiceClient(addr net.Addr) (collectorlog.LogsServiceClient, func(), error) {
func makeLogsServiceClient(addr net.Addr) (pdatagrpc.LogsClient, func(), error) {
cc, err := grpc.Dial(addr.String(), grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return nil, nil, err
}

logClient := collectorlog.NewLogsServiceClient(cc)
logClient := pdatagrpc.NewLogsClient(cc)

doneFn := func() { _ = cc.Close() }
return logClient, doneFn, nil
Expand Down
102 changes: 12 additions & 90 deletions receiver/otlpreceiver/metrics/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import (
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal"
collectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1"
otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1"
"go.opentelemetry.io/collector/internal/pdatagrpc"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/obsreport"
)

Expand All @@ -50,71 +49,21 @@ func TestExport(t *testing.T) {

// when

unixnanos1 := uint64(12578940000000012345)
unixnanos2 := uint64(12578940000000054321)

req := &collectormetrics.ExportMetricsServiceRequest{
ResourceMetrics: []*otlpmetrics.ResourceMetrics{
{
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{
{
Metrics: []*otlpmetrics.Metric{
{
Name: "mymetric",
Description: "My metric",
Unit: "ms",
Data: &otlpmetrics.Metric_IntSum{
IntSum: &otlpmetrics.IntSum{
IsMonotonic: true,
AggregationTemporality: otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
DataPoints: []*otlpmetrics.IntDataPoint{
{
Labels: []otlpcommon.StringKeyValue{
{
Key: "key1",
Value: "value1",
},
},
StartTimeUnixNano: unixnanos1,
TimeUnixNano: unixnanos2,
Value: 123,
},
{
Labels: []otlpcommon.StringKeyValue{
{
Key: "key2",
Value: "value2",
},
},
StartTimeUnixNano: unixnanos1,
TimeUnixNano: unixnanos2,
Value: 456,
},
},
},
},
},
},
},
},
},
},
}
req := testdata.GenerateMetricsOneMetric()

// Keep metric data to compare the test result against it
// Clone needed because OTLP proto XXX_ fields are altered in the GRPC downstream
metricData := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(req)).Clone()
metricData := req.Clone()

resp, err := metricsClient.Export(context.Background(), req)
require.NoError(t, err, "Failed to export metrics: %v", err)
require.NotNil(t, resp, "The response is missing")

// assert

require.Equal(t, 1, len(metricSink.AllMetrics()),
"unexpected length: %v", len(metricSink.AllMetrics()))

assert.EqualValues(t, metricData, metricSink.AllMetrics()[0])
mds := metricSink.AllMetrics()
require.Len(t, mds, 1)
assert.EqualValues(t, metricData, mds[0])
}

func TestExport_EmptyRequest(t *testing.T) {
Expand All @@ -129,7 +78,7 @@ func TestExport_EmptyRequest(t *testing.T) {
require.NoError(t, err, "Failed to create the MetricsServiceClient: %v", err)
defer metricsClientDoneFn()

resp, err := metricsClient.Export(context.Background(), &collectormetrics.ExportMetricsServiceRequest{})
resp, err := metricsClient.Export(context.Background(), pdata.NewMetrics())
require.NoError(t, err)
require.NotNil(t, resp)
}
Expand All @@ -144,47 +93,20 @@ func TestExport_ErrorConsumer(t *testing.T) {
require.NoError(t, err, "Failed to create the MetricsServiceClient: %v", err)
defer metricsClientDoneFn()

req := &collectormetrics.ExportMetricsServiceRequest{ResourceMetrics: []*otlpmetrics.ResourceMetrics{
{
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{
{
Metrics: []*otlpmetrics.Metric{
{
Name: "mymetric",
Description: "My metric",
Unit: "ms",
Data: &otlpmetrics.Metric_IntSum{
IntSum: &otlpmetrics.IntSum{
IsMonotonic: true,
AggregationTemporality: otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE,
DataPoints: []*otlpmetrics.IntDataPoint{
{
Value: 123,
},
{
Value: 456,
},
},
},
},
},
},
},
},
},
}}
req := testdata.GenerateMetricsOneMetric()

resp, err := metricsClient.Export(context.Background(), req)
assert.EqualError(t, err, "rpc error: code = Unknown desc = my error")
assert.Nil(t, resp)
}

func makeMetricsServiceClient(addr net.Addr) (collectormetrics.MetricsServiceClient, func(), error) {
func makeMetricsServiceClient(addr net.Addr) (pdatagrpc.MetricsClient, func(), error) {
cc, err := grpc.Dial(addr.String(), grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return nil, nil, err
}

metricsClient := collectormetrics.NewMetricsServiceClient(cc)
metricsClient := pdatagrpc.NewMetricsClient(cc)

doneFn := func() { _ = cc.Close() }
return metricsClient, doneFn, nil
Expand Down
Loading

0 comments on commit 4576668

Please sign in to comment.