Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add public functions to export pdata to ExportXServicesRequest Protobuf bytes #1741

Merged
merged 11 commits into from
Sep 17, 2020
9 changes: 9 additions & 0 deletions consumer/pdata/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/gogo/protobuf/proto"

"go.opentelemetry.io/collector/internal"
otlpcollectorlog "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/logs/v1"
otlplogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/logs/v1"
)

Expand Down Expand Up @@ -53,6 +54,14 @@ func (ld Logs) InternalRep() internal.OtlpLogsWrapper {
return internal.OtlpLogsWrapper{Orig: ld.orig}
}

// ToOtlpProtoBytes returns the internal Logs to OTLP Collector ExportTraceServiceRequest
// ProtoBuf bytes. This is intended to export OTLP Protobuf bytes for OTLP/HTTP transports.
func (ld Logs) ToOtlpProtoBytes() ([]byte, error) {
return proto.Marshal(&otlpcollectorlog.ExportLogsServiceRequest{
ResourceLogs: *ld.orig,
})
}

// Clone returns a copy of Logs.
func (ld Logs) Clone() Logs {
otlp := *ld.orig
Expand Down
14 changes: 14 additions & 0 deletions consumer/pdata/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ package pdata
import (
"testing"

gogoproto "github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/internal"
otlpcollectorlogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/logs/v1"
otlplogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/logs/v1"
)

Expand Down Expand Up @@ -70,3 +72,15 @@ func TestToFromLogProto(t *testing.T) {
assert.EqualValues(t, NewLogs(), td)
assert.EqualValues(t, otlp, *td.orig)
}

func TestLogs_ToOtlpProtoBytes(t *testing.T) {
otlp := []*otlplogs.ResourceLogs(nil)
ld := LogsFromInternalRep(internal.LogsFromOtlp(otlp))
bytes, err := ld.ToOtlpProtoBytes()
assert.Nil(t, err)

elsr := otlpcollectorlogs.ExportLogsServiceRequest{}
err = gogoproto.Unmarshal(bytes, &elsr)
assert.Nil(t, err)
assert.EqualValues(t, elsr.ResourceLogs, *ld.orig)
}
10 changes: 10 additions & 0 deletions consumer/pdata/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pdata
import (
"github.com/gogo/protobuf/proto"

otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/metrics/v1"
otlpmetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/metrics/v1"
)

Expand Down Expand Up @@ -51,6 +52,15 @@ func MetricsToOtlp(md Metrics) []*otlpmetrics.ResourceMetrics {
return *md.orig
}

// ToOtlpProtoBytes returns the internal MetricData to the OTLP Collector
// ExportMetricsServiceRequest ProtoBuf bytes. This is intended to export
// OTLP Protobuf bytes for OTLP/HTTP transports.
func (md Metrics) ToOtlpProtoBytes() ([]byte, error) {
return proto.Marshal(&otlpcollectormetrics.ExportMetricsServiceRequest{
ResourceMetrics: *md.orig,
})
}

// NewMetricData creates a new MetricData.
func NewMetrics() Metrics {
orig := []*otlpmetrics.ResourceMetrics(nil)
Expand Down
41 changes: 41 additions & 0 deletions consumer/pdata/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
goproto "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"

otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/metrics/v1"
otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"
otlpmetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/metrics/v1"
otlpresource "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/resource/v1"
Expand Down Expand Up @@ -675,6 +676,27 @@ func TestOtlpToFromInternalHistogramMutating(t *testing.T) {
}, MetricsToOtlp(metricData))
}

func TestMetrics_ToOtlpProtoBytes(t *testing.T) {
md := MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{
{
Resource: generateTestProtoResource(),
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{
{
InstrumentationLibrary: generateTestProtoInstrumentationLibrary(),
Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()},
},
},
},
})
bytes, err := md.ToOtlpProtoBytes()
assert.Nil(t, err)

emsr := otlpcollectormetrics.ExportMetricsServiceRequest{}
err = gogoproto.Unmarshal(bytes, &emsr)
assert.Nil(t, err)
assert.EqualValues(t, emsr.GetResourceMetrics(), MetricsToOtlp(md))
}

func BenchmarkOtlpToFromInternal_PassThrough(b *testing.B) {
resourceMetricsList := []*otlpmetrics.ResourceMetrics{
{
Expand Down Expand Up @@ -758,6 +780,25 @@ func BenchmarkOtlpToFromInternal_HistogramPoints_MutateOneLabel(b *testing.B) {
}
}

func BenchmarkMetrics_ToOtlpProtoBytes_PassThrough(b *testing.B) {
metrics := MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{
{
Resource: generateTestProtoResource(),
InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{
{
InstrumentationLibrary: generateTestProtoInstrumentationLibrary(),
Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()},
},
},
},
})

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, _ = metrics.ToOtlpProtoBytes()
}
}

func generateTestProtoResource() *otlpresource.Resource {
return &otlpresource.Resource{
Attributes: []*otlpcommon.KeyValue{
Expand Down
10 changes: 10 additions & 0 deletions consumer/pdata/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/gogo/protobuf/proto"

otlpcollectortrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/trace/v1"
otlptrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/trace/v1"
)

Expand All @@ -41,6 +42,15 @@ func TracesToOtlp(td Traces) []*otlptrace.ResourceSpans {
return *td.orig
}

// ToOtlpProtoBytes returns the internal Traces to OTLP Collector
// ExportTraceServiceRequest ProtoBuf bytes. This is intended to export OTLP
// Protobuf bytes for OTLP/HTTP transports.
func (td Traces) ToOtlpProtoBytes() ([]byte, error) {
return proto.Marshal(&otlpcollectortrace.ExportTraceServiceRequest{
ResourceSpans: *td.orig,
})
}

// NewTraces creates a new Traces.
func NewTraces() Traces {
orig := []*otlptrace.ResourceSpans(nil)
Expand Down
12 changes: 12 additions & 0 deletions consumer/pdata/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
goproto "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"

otlpcollectortrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/trace/v1"
otlptrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/trace/v1"
)

Expand Down Expand Up @@ -141,3 +142,14 @@ func TestResourceSpansWireCompatibility(t *testing.T) {
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible.
assert.True(t, gogoproto.Equal(*pdataRS.orig, &gogoprotoRS2))
}

func TestTraces_ToOtlpProtoBytes(t *testing.T) {
td := NewTraces()
bytes, err := td.ToOtlpProtoBytes()
assert.Nil(t, err)

etsr := otlpcollectortrace.ExportTraceServiceRequest{}
err = gogoproto.Unmarshal(bytes, &etsr)
assert.Nil(t, err)
assert.EqualValues(t, etsr.ResourceSpans, TracesToOtlp(td))
}