Skip to content

Commit aefb31b

Browse files
committed
Add unmarshaler for otlpgrpc.[*]Request and otlpgrp.[*]Response
Fixes #4052 Signed-off-by: Bogdan Drutu <[email protected]>
1 parent f85f13e commit aefb31b

File tree

5 files changed

+123
-70
lines changed

5 files changed

+123
-70
lines changed

model/otlpgrpc/logs.go

+37
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
)
2828

2929
var jsonMarshaler = &jsonpb.Marshaler{}
30+
var jsonUnmarshaler = &jsonpb.Unmarshaler{}
3031

3132
// LogsResponse represents the response for gRPC client/server.
3233
type LogsResponse struct {
@@ -38,6 +39,24 @@ func NewLogsResponse() LogsResponse {
3839
return LogsResponse{orig: &otlpcollectorlog.ExportLogsServiceResponse{}}
3940
}
4041

42+
// UnmarshalLogsResponse unmarshalls LogsResponse from proto bytes.
43+
func UnmarshalLogsResponse(data []byte) (LogsResponse, error) {
44+
var orig otlpcollectorlog.ExportLogsServiceResponse
45+
if err := orig.Unmarshal(data); err != nil {
46+
return LogsResponse{}, err
47+
}
48+
return LogsResponse{orig: &orig}, nil
49+
}
50+
51+
// UnmarshalJSONLogsResponse unmarshalls LogsResponse from JSON bytes.
52+
func UnmarshalJSONLogsResponse(data []byte) (LogsResponse, error) {
53+
var orig otlpcollectorlog.ExportLogsServiceResponse
54+
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
55+
return LogsResponse{}, err
56+
}
57+
return LogsResponse{orig: &orig}, nil
58+
}
59+
4160
// Marshal marshals LogsResponse into proto bytes.
4261
func (lr LogsResponse) Marshal() ([]byte, error) {
4362
return lr.orig.Marshal()
@@ -62,6 +81,24 @@ func NewLogsRequest() LogsRequest {
6281
return LogsRequest{orig: &otlpcollectorlog.ExportLogsServiceRequest{}}
6382
}
6483

84+
// UnmarshalLogsRequest unmarshalls LogsRequest from proto bytes.
85+
func UnmarshalLogsRequest(data []byte) (LogsRequest, error) {
86+
var orig otlpcollectorlog.ExportLogsServiceRequest
87+
if err := orig.Unmarshal(data); err != nil {
88+
return LogsRequest{}, err
89+
}
90+
return LogsRequest{orig: &orig}, nil
91+
}
92+
93+
// UnmarshalJSONLogsRequest unmarshalls LogsRequest from JSON bytes.
94+
func UnmarshalJSONLogsRequest(data []byte) (LogsRequest, error) {
95+
var orig otlpcollectorlog.ExportLogsServiceRequest
96+
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
97+
return LogsRequest{}, err
98+
}
99+
return LogsRequest{orig: &orig}, nil
100+
}
101+
65102
// Marshal marshals LogsRequest into proto bytes.
66103
func (lr LogsRequest) Marshal() ([]byte, error) {
67104
return lr.orig.Marshal()

model/otlpgrpc/metrics.go

+36-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
"go.opentelemetry.io/collector/model/pdata"
2626
)
2727

28-
// TODO: Consider to add `MetricsRequest`. If we add non pdata properties we can add them to the request.
29-
3028
// MetricsResponse represents the response for gRPC client/server.
3129
type MetricsResponse struct {
3230
orig *otlpcollectormetrics.ExportMetricsServiceResponse
@@ -37,6 +35,24 @@ func NewMetricsResponse() MetricsResponse {
3735
return MetricsResponse{orig: &otlpcollectormetrics.ExportMetricsServiceResponse{}}
3836
}
3937

38+
// UnmarshalMetricsResponse unmarshalls MetricsResponse from proto bytes.
39+
func UnmarshalMetricsResponse(data []byte) (MetricsResponse, error) {
40+
var orig otlpcollectormetrics.ExportMetricsServiceResponse
41+
if err := orig.Unmarshal(data); err != nil {
42+
return MetricsResponse{}, err
43+
}
44+
return MetricsResponse{orig: &orig}, nil
45+
}
46+
47+
// UnmarshalJSONMetricsResponse unmarshalls MetricsResponse from JSON bytes.
48+
func UnmarshalJSONMetricsResponse(data []byte) (MetricsResponse, error) {
49+
var orig otlpcollectormetrics.ExportMetricsServiceResponse
50+
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
51+
return MetricsResponse{}, err
52+
}
53+
return MetricsResponse{orig: &orig}, nil
54+
}
55+
4056
// Marshal marshals MetricsResponse into proto bytes.
4157
func (mr MetricsResponse) Marshal() ([]byte, error) {
4258
return mr.orig.Marshal()
@@ -61,6 +77,24 @@ func NewMetricsRequest() MetricsRequest {
6177
return MetricsRequest{orig: &otlpcollectormetrics.ExportMetricsServiceRequest{}}
6278
}
6379

80+
// UnmarshalMetricsRequest unmarshalls MetricsRequest from proto bytes.
81+
func UnmarshalMetricsRequest(data []byte) (MetricsRequest, error) {
82+
var orig otlpcollectormetrics.ExportMetricsServiceRequest
83+
if err := orig.Unmarshal(data); err != nil {
84+
return MetricsRequest{}, err
85+
}
86+
return MetricsRequest{orig: &orig}, nil
87+
}
88+
89+
// UnmarshalJSONMetricsRequest unmarshalls MetricsRequest from JSON bytes.
90+
func UnmarshalJSONMetricsRequest(data []byte) (MetricsRequest, error) {
91+
var orig otlpcollectormetrics.ExportMetricsServiceRequest
92+
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
93+
return MetricsRequest{}, err
94+
}
95+
return MetricsRequest{orig: &orig}, nil
96+
}
97+
6498
// Marshal marshals MetricsRequest into proto bytes.
6599
func (mr MetricsRequest) Marshal() ([]byte, error) {
66100
return mr.orig.Marshal()

model/otlpgrpc/traces.go

+36-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
"go.opentelemetry.io/collector/model/pdata"
2626
)
2727

28-
// TODO: Consider to add `TracesRequest`. If we add non pdata properties we can add them to the request.
29-
3028
// TracesResponse represents the response for gRPC client/server.
3129
type TracesResponse struct {
3230
orig *otlpcollectortrace.ExportTraceServiceResponse
@@ -37,6 +35,24 @@ func NewTracesResponse() TracesResponse {
3735
return TracesResponse{orig: &otlpcollectortrace.ExportTraceServiceResponse{}}
3836
}
3937

38+
// UnmarshalTracesResponse unmarshalls TracesResponse from proto bytes.
39+
func UnmarshalTracesResponse(data []byte) (TracesResponse, error) {
40+
var orig otlpcollectortrace.ExportTraceServiceResponse
41+
if err := orig.Unmarshal(data); err != nil {
42+
return TracesResponse{}, err
43+
}
44+
return TracesResponse{orig: &orig}, nil
45+
}
46+
47+
// UnmarshalJSONTracesResponse unmarshalls TracesResponse from JSON bytes.
48+
func UnmarshalJSONTracesResponse(data []byte) (TracesResponse, error) {
49+
var orig otlpcollectortrace.ExportTraceServiceResponse
50+
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
51+
return TracesResponse{}, err
52+
}
53+
return TracesResponse{orig: &orig}, nil
54+
}
55+
4056
// Marshal marshals TracesResponse into proto bytes.
4157
func (tr TracesResponse) Marshal() ([]byte, error) {
4258
return tr.orig.Marshal()
@@ -61,6 +77,24 @@ func NewTracesRequest() TracesRequest {
6177
return TracesRequest{orig: &otlpcollectortrace.ExportTraceServiceRequest{}}
6278
}
6379

80+
// UnmarshalTracesRequest unmarshalls TracesRequest from proto bytes.
81+
func UnmarshalTracesRequest(data []byte) (TracesRequest, error) {
82+
var orig otlpcollectortrace.ExportTraceServiceRequest
83+
if err := orig.Unmarshal(data); err != nil {
84+
return TracesRequest{}, err
85+
}
86+
return TracesRequest{orig: &orig}, nil
87+
}
88+
89+
// UnmarshalJSONTracesRequest unmarshalls TracesRequest from JSON bytes.
90+
func UnmarshalJSONTracesRequest(data []byte) (TracesRequest, error) {
91+
var orig otlpcollectortrace.ExportTraceServiceRequest
92+
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
93+
return TracesRequest{}, err
94+
}
95+
return TracesRequest{orig: &orig}, nil
96+
}
97+
6498
// Marshal marshals TracesRequest into proto bytes.
6599
func (tr TracesRequest) Marshal() ([]byte, error) {
66100
return tr.orig.Marshal()

receiver/otlpreceiver/encoder.go

+8-55
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/gogo/protobuf/proto"
2222
spb "google.golang.org/genproto/googleapis/rpc/status"
2323

24-
"go.opentelemetry.io/collector/model/otlp"
2524
"go.opentelemetry.io/collector/model/otlpgrpc"
2625
)
2726

@@ -31,18 +30,8 @@ const (
3130
)
3231

3332
var (
34-
pbEncoder = &protoEncoder{}
35-
jsEncoder = &jsonEncoder{}
36-
37-
tracesPbUnmarshaler = otlp.NewProtobufTracesUnmarshaler()
38-
tracesJSONUnmarshaler = otlp.NewJSONTracesUnmarshaler()
39-
40-
metricsPbUnmarshaler = otlp.NewProtobufMetricsUnmarshaler()
41-
metricsJSONUnmarshaler = otlp.NewJSONMetricsUnmarshaler()
42-
43-
logsPbUnmarshaler = otlp.NewProtobufLogsUnmarshaler()
44-
logsJSONUnmarshaler = otlp.NewJSONLogsUnmarshaler()
45-
33+
pbEncoder = &protoEncoder{}
34+
jsEncoder = &jsonEncoder{}
4635
jsonMarshaler = &jsonpb.Marshaler{}
4736
)
4837

@@ -63,33 +52,15 @@ type encoder interface {
6352
type protoEncoder struct{}
6453

6554
func (protoEncoder) unmarshalTracesRequest(buf []byte) (otlpgrpc.TracesRequest, error) {
66-
td, err := tracesPbUnmarshaler.UnmarshalTraces(buf)
67-
if err != nil {
68-
return otlpgrpc.TracesRequest{}, err
69-
}
70-
req := otlpgrpc.NewTracesRequest()
71-
req.SetTraces(td)
72-
return req, nil
55+
return otlpgrpc.UnmarshalTracesRequest(buf)
7356
}
7457

7558
func (protoEncoder) unmarshalMetricsRequest(buf []byte) (otlpgrpc.MetricsRequest, error) {
76-
td, err := metricsPbUnmarshaler.UnmarshalMetrics(buf)
77-
if err != nil {
78-
return otlpgrpc.MetricsRequest{}, err
79-
}
80-
req := otlpgrpc.NewMetricsRequest()
81-
req.SetMetrics(td)
82-
return req, nil
59+
return otlpgrpc.UnmarshalMetricsRequest(buf)
8360
}
8461

8562
func (protoEncoder) unmarshalLogsRequest(buf []byte) (otlpgrpc.LogsRequest, error) {
86-
ld, err := logsPbUnmarshaler.UnmarshalLogs(buf)
87-
if err != nil {
88-
return otlpgrpc.LogsRequest{}, err
89-
}
90-
req := otlpgrpc.NewLogsRequest()
91-
req.SetLogs(ld)
92-
return req, nil
63+
return otlpgrpc.UnmarshalLogsRequest(buf)
9364
}
9465

9566
func (protoEncoder) marshalTracesResponse(resp otlpgrpc.TracesResponse) ([]byte, error) {
@@ -115,33 +86,15 @@ func (protoEncoder) contentType() string {
11586
type jsonEncoder struct{}
11687

11788
func (jsonEncoder) unmarshalTracesRequest(buf []byte) (otlpgrpc.TracesRequest, error) {
118-
td, err := tracesJSONUnmarshaler.UnmarshalTraces(buf)
119-
if err != nil {
120-
return otlpgrpc.TracesRequest{}, err
121-
}
122-
req := otlpgrpc.NewTracesRequest()
123-
req.SetTraces(td)
124-
return req, nil
89+
return otlpgrpc.UnmarshalJSONTracesRequest(buf)
12590
}
12691

12792
func (jsonEncoder) unmarshalMetricsRequest(buf []byte) (otlpgrpc.MetricsRequest, error) {
128-
td, err := metricsJSONUnmarshaler.UnmarshalMetrics(buf)
129-
if err != nil {
130-
return otlpgrpc.MetricsRequest{}, err
131-
}
132-
req := otlpgrpc.NewMetricsRequest()
133-
req.SetMetrics(td)
134-
return req, nil
93+
return otlpgrpc.UnmarshalJSONMetricsRequest(buf)
13594
}
13695

13796
func (jsonEncoder) unmarshalLogsRequest(buf []byte) (otlpgrpc.LogsRequest, error) {
138-
ld, err := logsJSONUnmarshaler.UnmarshalLogs(buf)
139-
if err != nil {
140-
return otlpgrpc.LogsRequest{}, err
141-
}
142-
req := otlpgrpc.NewLogsRequest()
143-
req.SetLogs(ld)
144-
return req, nil
97+
return otlpgrpc.UnmarshalJSONLogsRequest(buf)
14598
}
14699

147100
func (jsonEncoder) marshalTracesResponse(resp otlpgrpc.TracesResponse) ([]byte, error) {

receiver/otlpreceiver/otlp_test.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"testing"
2828
"time"
2929

30-
"github.com/gogo/protobuf/types"
3130
"github.com/stretchr/testify/assert"
3231
"github.com/stretchr/testify/require"
3332
spb "google.golang.org/genproto/googleapis/rpc/status"
@@ -179,13 +178,12 @@ func testHTTPJSONRequest(t *testing.T, url string, sink *internalconsumertest.Er
179178

180179
client := &http.Client{}
181180
resp, err := client.Do(req)
182-
require.NoError(t, err, "Error posting trace to grpc-gateway server: %v", err)
181+
require.NoError(t, err, "Error posting trace to http server: %v", err)
183182

184183
respBytes, err := ioutil.ReadAll(resp.Body)
185184
if err != nil {
186-
t.Errorf("Error reading response from trace grpc-gateway, %v", err)
185+
t.Errorf("Error reading response from trace http server, %v", err)
187186
}
188-
respStr := string(respBytes)
189187
err = resp.Body.Close()
190188
if err != nil {
191189
t.Errorf("Error closing response body, %v", err)
@@ -194,17 +192,16 @@ func testHTTPJSONRequest(t *testing.T, url string, sink *internalconsumertest.Er
194192
allTraces := sink.AllTraces()
195193
if expectedErr == nil {
196194
assert.Equal(t, 200, resp.StatusCode)
197-
var respJSON map[string]interface{}
198-
assert.NoError(t, json.Unmarshal([]byte(respStr), &respJSON))
199-
assert.Len(t, respJSON, 0, "Got unexpected response from trace grpc-gateway")
195+
_, err = otlpgrpc.UnmarshalJSONTracesResponse(respBytes)
196+
assert.NoError(t, err)
200197

201198
require.Len(t, allTraces, 1)
202199

203200
got := allTraces[0]
204201
assert.EqualValues(t, got, traceOtlp)
205202
} else {
206203
errStatus := &spb.Status{}
207-
assert.NoError(t, json.Unmarshal([]byte(respStr), errStatus))
204+
assert.NoError(t, json.Unmarshal(respBytes, errStatus))
208205
if s, ok := status.FromError(expectedErr); ok {
209206
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
210207
assert.True(t, proto.Equal(errStatus, s.Proto()))
@@ -326,9 +323,7 @@ func testHTTPProtobufRequest(
326323

327324
if expectedErr == nil {
328325
require.Equal(t, 200, resp.StatusCode, "Unexpected return status")
329-
// TODO: Parse otlp response here instead of empty proto when pdata allows that.
330-
tmp := &types.Empty{}
331-
err := tmp.Unmarshal(respBytes)
326+
_, err := otlpgrpc.UnmarshalTracesResponse(respBytes)
332327
require.NoError(t, err, "Unable to unmarshal response to ExportTraceServiceResponse proto")
333328

334329
require.Len(t, allTraces, 1)

0 commit comments

Comments
 (0)