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 unmarshalling for pdata.Traces #1948

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions consumer/pdata/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,25 @@ 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.
// ToOtlpProtoBytes converts the internal Traces to OTLP Collector
// ExportTraceServiceRequest ProtoBuf bytes.
func (td Traces) ToOtlpProtoBytes() ([]byte, error) {
return proto.Marshal(&otlpcollectortrace.ExportTraceServiceRequest{
ResourceSpans: *td.orig,
})
}

// FromOtlpProtoBytes converts OTLP Collector ExportTraceServiceRequest
// ProtoBuf bytes to the internal Traces. Overrides current data.
func (td Traces) FromOtlpProtoBytes(data []byte) error {
traces := &otlpcollectortrace.ExportTraceServiceRequest{}
if err := proto.Unmarshal(data, traces); err != nil {
return err
}
*td.orig = traces.ResourceSpans
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for zero-initialized td. Instead I suggest to change FromOtlpProtoBytes to take a pointer receiver and here do

td.orig = &traces.ResourceSpans

Copy link
Contributor Author

@pkositsyn pkositsyn Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at similar ToOtlpProtoBytes - it has the same problem (actually I see no problem, no one guarantees zero-initialized structure to work properly). I cannot change abovementioned function, because this would break compatibility, and in my opinion it's better to be consistent in this case. Or even return to the first solution where function wasn't on the structure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it is unfortunate that we used value receivers on Traces, but it is too late now, so you are right, we need to use value receivers for consistency now. Please add a comment to FromOtlpProtoBytes to tell that NewTraces must be used to create the instance before calling this function. It is easy to make the mistake since one will probably try this first and will fail:

var td pdata.Traces
td.FromOtlpProtoBytes(someData)

return nil
}

// NewTraces creates a new Traces.
func NewTraces() Traces {
orig := []*otlptrace.ResourceSpans(nil)
Expand Down
15 changes: 14 additions & 1 deletion consumer/pdata/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestResourceSpansWireCompatibility(t *testing.T) {
assert.EqualValues(t, *pdataRS.orig, &gogoprotoRS2)
}

func TestTraces_ToOtlpProtoBytes(t *testing.T) {
func TestToOtlpProtoBytes(t *testing.T) {
td := NewTraces()
bytes, err := td.ToOtlpProtoBytes()
assert.Nil(t, err)
Expand All @@ -153,3 +153,16 @@ func TestTraces_ToOtlpProtoBytes(t *testing.T) {
assert.Nil(t, err)
assert.EqualValues(t, etsr.ResourceSpans, TracesToOtlp(td))
}

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

err = td.FromOtlpProtoBytes(bytes)
assert.Nil(t, err)
assert.EqualValues(t, NewTraces(), td)

err = td.FromOtlpProtoBytes([]byte{0xFF})
assert.EqualError(t, err, "unexpected EOF")
}