Skip to content

Commit

Permalink
Change structs in otlpgrpc to follow standard go encoding interfaces (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#5062)

* Change `UnmarshalJSON[Traces|Metrics|Logs][Reques|Response]` to be a func `UnmarshalJSON` on the struct.
* Rename `[Traces|Metrics|Logs][Reques|Response].Marshal` to `MarshalProto` consistent with JSON/Text standard interfaces.
* Change `UnmarshalJSON[Traces|Metrics|Logs][Reques|Response]` to be a func `UnmarshalProto` on the struct.

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Mar 24, 2022
1 parent 1b76187 commit f54e83e
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 97 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
- Deprecate `pdata.AttributeValueTypeArray` type in favor of `pdata.ValueTypeSlice`
- Deprecate `pdata.NewAttributeValueArray` func in favor of `pdata.NewValueSlice`
- Deprecate global flag in `featuregates` (#5060)
- Change structs in otlpgrpc to follow standard go encoding interfaces (#5062)
- Deprecate UnmarshalJSON[Traces|Metrics|Logs][Reques|Response] in favor of `UnmarshalJSON`.
- Deprecate [Traces|Metrics|Logs][Reques|Response].Marshal in favor of `MarshalProto`.
- Deprecate UnmarshalJSON[Traces|Metrics|Logs][Reques|Response] in favor of `UnmarshalProto`.

### 💡 Enhancements 💡

Expand Down
6 changes: 3 additions & 3 deletions exporter/otlphttpexporter/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (e *exporter) start(_ context.Context, host component.Host) error {
func (e *exporter) pushTraces(ctx context.Context, td pdata.Traces) error {
tr := otlpgrpc.NewTracesRequest()
tr.SetTraces(td)
request, err := tr.Marshal()
request, err := tr.MarshalProto()
if err != nil {
return consumererror.NewPermanent(err)
}
Expand All @@ -105,7 +105,7 @@ func (e *exporter) pushTraces(ctx context.Context, td pdata.Traces) error {
func (e *exporter) pushMetrics(ctx context.Context, md pdata.Metrics) error {
tr := otlpgrpc.NewMetricsRequest()
tr.SetMetrics(md)
request, err := tr.Marshal()
request, err := tr.MarshalProto()
if err != nil {
return consumererror.NewPermanent(err)
}
Expand All @@ -115,7 +115,7 @@ func (e *exporter) pushMetrics(ctx context.Context, md pdata.Metrics) error {
func (e *exporter) pushLogs(ctx context.Context, ld pdata.Logs) error {
tr := otlpgrpc.NewLogsRequest()
tr.SetLogs(ld)
request, err := tr.Marshal()
request, err := tr.MarshalProto()
if err != nil {
return consumererror.NewPermanent(err)
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/otlphttpexporter/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ func TestIssue_4221(t *testing.T) {
assert.Equal(t, "CscBCkkKIAoMc2VydmljZS5uYW1lEhAKDnVvcC5zdGFnZS1ldS0xCiUKGW91dHN5c3RlbXMubW9kdWxlLnZlcnNpb24SCAoGOTAzMzg2EnoKEQoMdW9wX2NhbmFyaWVzEgExEmUKEEMDhT8Ib0+Mhs8Zi2VR34QSCOVRPDJ5XEG5IgA5QE41aASRrxZBQE41aASRrxZKEAoKc3Bhbl9pbmRleBICGANKHwoNY29kZS5mdW5jdGlvbhIOCgxteUZ1bmN0aW9uMzZ6AA==", base64Data)
unbase64Data, err := base64.StdEncoding.DecodeString(base64Data)
require.NoError(t, err)
tr, err := otlpgrpc.UnmarshalTracesRequest(unbase64Data)
require.NoError(t, err)
tr := otlpgrpc.NewTracesRequest()
require.NoError(t, tr.UnmarshalProto(unbase64Data))
span := tr.Traces().ResourceSpans().At(0).InstrumentationLibrarySpans().At(0).Spans().At(0)
assert.Equal(t, "4303853f086f4f8c86cf198b6551df84", span.TraceID().HexString())
assert.Equal(t, "e5513c32795c41b9", span.SpanID().HexString())
Expand Down
86 changes: 60 additions & 26 deletions model/otlpgrpc/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,38 @@ func NewLogsResponse() LogsResponse {
return LogsResponse{orig: &otlpcollectorlog.ExportLogsServiceResponse{}}
}

// UnmarshalLogsResponse unmarshalls LogsResponse from proto bytes.
// Deprecated: [v0.48.0] use LogsResponse.UnmarshalProto.
func UnmarshalLogsResponse(data []byte) (LogsResponse, error) {
var orig otlpcollectorlog.ExportLogsServiceResponse
if err := orig.Unmarshal(data); err != nil {
return LogsResponse{}, err
}
return LogsResponse{orig: &orig}, nil
lr := NewLogsResponse()
err := lr.UnmarshalProto(data)
return lr, err
}

// UnmarshalJSONLogsResponse unmarshalls LogsResponse from JSON bytes.
// Deprecated: [v0.48.0] use LogsResponse.UnmarshalJSON.
func UnmarshalJSONLogsResponse(data []byte) (LogsResponse, error) {
var orig otlpcollectorlog.ExportLogsServiceResponse
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
return LogsResponse{}, err
}
return LogsResponse{orig: &orig}, nil
lr := NewLogsResponse()
err := lr.UnmarshalJSON(data)
return lr, err
}

// Marshal marshals LogsResponse into proto bytes.
// Deprecated: [v0.48.0] use MarshalProto.
func (lr LogsResponse) Marshal() ([]byte, error) {
return lr.MarshalProto()
}

// MarshalProto marshals LogsResponse into proto bytes.
func (lr LogsResponse) MarshalProto() ([]byte, error) {
return lr.orig.Marshal()
}

// UnmarshalProto unmarshalls LogsResponse from proto bytes.
func (lr LogsResponse) UnmarshalProto(data []byte) error {
if err := lr.orig.Unmarshal(data); err != nil {
return err
}
return nil
}

// MarshalJSON marshals LogsResponse into JSON bytes.
func (lr LogsResponse) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
Expand All @@ -72,6 +81,14 @@ func (lr LogsResponse) MarshalJSON() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON unmarshalls LogsResponse from JSON bytes.
func (lr LogsResponse) UnmarshalJSON(data []byte) error {
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), lr.orig); err != nil {
return err
}
return nil
}

// LogsRequest represents the response for gRPC client/server.
type LogsRequest struct {
orig *otlpcollectorlog.ExportLogsServiceRequest
Expand All @@ -82,29 +99,38 @@ func NewLogsRequest() LogsRequest {
return LogsRequest{orig: &otlpcollectorlog.ExportLogsServiceRequest{}}
}

// UnmarshalLogsRequest unmarshalls LogsRequest from proto bytes.
// Deprecated: [v0.48.0] use LogsRequest.UnmarshalProto.
func UnmarshalLogsRequest(data []byte) (LogsRequest, error) {
var orig otlpcollectorlog.ExportLogsServiceRequest
if err := orig.Unmarshal(data); err != nil {
return LogsRequest{}, err
}
return LogsRequest{orig: &orig}, nil
lr := NewLogsRequest()
err := lr.UnmarshalProto(data)
return lr, err
}

// UnmarshalJSONLogsRequest unmarshalls LogsRequest from JSON bytes.
// Deprecated: [v0.48.0] use LogsRequest.UnmarshalJSON.
func UnmarshalJSONLogsRequest(data []byte) (LogsRequest, error) {
var orig otlpcollectorlog.ExportLogsServiceRequest
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
return LogsRequest{}, err
}
return LogsRequest{orig: &orig}, nil
lr := NewLogsRequest()
err := lr.UnmarshalJSON(data)
return lr, err
}

// Marshal marshals LogsRequest into proto bytes.
// Deprecated: [v0.48.0] use MarshalProto.
func (lr LogsRequest) Marshal() ([]byte, error) {
return lr.MarshalProto()
}

// MarshalProto marshals LogsRequest into proto bytes.
func (lr LogsRequest) MarshalProto() ([]byte, error) {
return lr.orig.Marshal()
}

// UnmarshalProto unmarshalls LogsRequest from proto bytes.
func (lr LogsRequest) UnmarshalProto(data []byte) error {
if err := lr.orig.Unmarshal(data); err != nil {
return err
}
return nil
}

// MarshalJSON marshals LogsRequest into JSON bytes.
func (lr LogsRequest) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
Expand All @@ -114,6 +140,14 @@ func (lr LogsRequest) MarshalJSON() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON unmarshalls LogsRequest from JSON bytes.
func (lr LogsRequest) UnmarshalJSON(data []byte) error {
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), lr.orig); err != nil {
return err
}
return nil
}

func (lr LogsRequest) SetLogs(ld pdata.Logs) {
lr.orig.ResourceLogs = ipdata.LogsToOtlp(ld).ResourceLogs
}
Expand Down
62 changes: 62 additions & 0 deletions model/otlpgrpc/logs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package otlpgrpc

import (
"encoding/json"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

var _ json.Unmarshaler = LogsResponse{}
var _ json.Marshaler = LogsResponse{}

var _ json.Unmarshaler = LogsRequest{}
var _ json.Marshaler = LogsRequest{}

var logsRequestJSON = []byte(`
{
"resourceLogs": [
{
"resource": {},
"instrumentationLibraryLogs": [
{
"instrumentationLibrary": {},
"logRecords": [
{
"body": {
"stringValue": "test_log_record"
},
"traceId": "",
"spanId": ""
}
]
}
]
}
]
}`)

func TestLogsRequestJSON(t *testing.T) {
lr := NewLogsRequest()
assert.NoError(t, lr.UnmarshalJSON(logsRequestJSON))
assert.Equal(t, "test_log_record", lr.Logs().ResourceLogs().At(0).InstrumentationLibraryLogs().At(0).LogRecords().At(0).Body().AsString())

got, err := lr.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, strings.Join(strings.Fields(string(logsRequestJSON)), ""), string(got))
}
88 changes: 61 additions & 27 deletions model/otlpgrpc/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,38 @@ func NewMetricsResponse() MetricsResponse {
return MetricsResponse{orig: &otlpcollectormetrics.ExportMetricsServiceResponse{}}
}

// UnmarshalMetricsResponse unmarshalls MetricsResponse from proto bytes.
// Deprecated: [v0.48.0] use MetricsResponse.UnmarshalProto.
func UnmarshalMetricsResponse(data []byte) (MetricsResponse, error) {
var orig otlpcollectormetrics.ExportMetricsServiceResponse
if err := orig.Unmarshal(data); err != nil {
return MetricsResponse{}, err
}
return MetricsResponse{orig: &orig}, nil
mr := NewMetricsResponse()
err := mr.UnmarshalProto(data)
return mr, err
}

// UnmarshalJSONMetricsResponse unmarshalls MetricsResponse from JSON bytes.
// Deprecated: [v0.48.0] use MetricsResponse.UnmarshalJSON.
func UnmarshalJSONMetricsResponse(data []byte) (MetricsResponse, error) {
var orig otlpcollectormetrics.ExportMetricsServiceResponse
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
return MetricsResponse{}, err
}
return MetricsResponse{orig: &orig}, nil
mr := NewMetricsResponse()
err := mr.UnmarshalJSON(data)
return mr, err
}

// Marshal marshals MetricsResponse into proto bytes.
// Deprecated: [v0.48.0] use MarshalProto.
func (mr MetricsResponse) Marshal() ([]byte, error) {
return mr.MarshalProto()
}

// MarshalProto marshals MetricsResponse into proto bytes.
func (mr MetricsResponse) MarshalProto() ([]byte, error) {
return mr.orig.Marshal()
}

// UnmarshalProto unmarshalls MetricsResponse from proto bytes.
func (mr MetricsResponse) UnmarshalProto(data []byte) error {
if err := mr.orig.Unmarshal(data); err != nil {
return err
}
return nil
}

// MarshalJSON marshals MetricsResponse into JSON bytes.
func (mr MetricsResponse) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
Expand All @@ -68,6 +77,14 @@ func (mr MetricsResponse) MarshalJSON() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON unmarshalls MetricsResponse from JSON bytes.
func (mr MetricsResponse) UnmarshalJSON(data []byte) error {
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), mr.orig); err != nil {
return err
}
return nil
}

// MetricsRequest represents the response for gRPC client/server.
type MetricsRequest struct {
orig *otlpcollectormetrics.ExportMetricsServiceRequest
Expand All @@ -78,30 +95,39 @@ func NewMetricsRequest() MetricsRequest {
return MetricsRequest{orig: &otlpcollectormetrics.ExportMetricsServiceRequest{}}
}

// UnmarshalMetricsRequest unmarshalls MetricsRequest from proto bytes.
// Deprecated: [v0.48.0] use MetricsRequest.UnmarshalProto.
func UnmarshalMetricsRequest(data []byte) (MetricsRequest, error) {
var orig otlpcollectormetrics.ExportMetricsServiceRequest
if err := orig.Unmarshal(data); err != nil {
return MetricsRequest{}, err
}
return MetricsRequest{orig: &orig}, nil
mr := NewMetricsRequest()
err := mr.UnmarshalProto(data)
return mr, err
}

// UnmarshalJSONMetricsRequest unmarshalls MetricsRequest from JSON bytes.
// Deprecated: [v0.48.0] use MetricsRequest.UnmarshalJSON.
func UnmarshalJSONMetricsRequest(data []byte) (MetricsRequest, error) {
var orig otlpcollectormetrics.ExportMetricsServiceRequest
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), &orig); err != nil {
return MetricsRequest{}, err
}
return MetricsRequest{orig: &orig}, nil
mr := NewMetricsRequest()
err := mr.UnmarshalJSON(data)
return mr, err
}

// Marshal marshals MetricsRequest into proto bytes.
// Deprecated: [v0.48.0] use MarshalProto.
func (mr MetricsRequest) Marshal() ([]byte, error) {
return mr.MarshalProto()
}

// MarshalProto marshals MetricsRequest into proto bytes.
func (mr MetricsRequest) MarshalProto() ([]byte, error) {
return mr.orig.Marshal()
}

// MarshalJSON marshals LogsRequest into JSON bytes.
// UnmarshalProto unmarshalls MetricsRequest from proto bytes.
func (mr MetricsRequest) UnmarshalProto(data []byte) error {
if err := mr.orig.Unmarshal(data); err != nil {
return err
}
return nil
}

// MarshalJSON marshals MetricsRequest into JSON bytes.
func (mr MetricsRequest) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
if err := jsonMarshaler.Marshal(&buf, mr.orig); err != nil {
Expand All @@ -110,6 +136,14 @@ func (mr MetricsRequest) MarshalJSON() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON unmarshalls MetricsRequest from JSON bytes.
func (mr MetricsRequest) UnmarshalJSON(data []byte) error {
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), mr.orig); err != nil {
return err
}
return nil
}

func (mr MetricsRequest) SetMetrics(ld pdata.Metrics) {
mr.orig.ResourceMetrics = ipdata.MetricsToOtlp(ld).ResourceMetrics
}
Expand Down
Loading

0 comments on commit f54e83e

Please sign in to comment.