Skip to content

Commit

Permalink
Add otlpjson as a serializer implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed May 21, 2021
1 parent fe57963 commit 4fceba5
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
69 changes: 69 additions & 0 deletions internal/otlpjson/marshaler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 otlpjson

import (
"bytes"

"github.com/gogo/protobuf/jsonpb"

otlpcollectorlogs "go.opentelemetry.io/collector/internal/data/protogen/collector/logs/v1"
otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1"
otlpcollectortrace "go.opentelemetry.io/collector/internal/data/protogen/collector/trace/v1"
"go.opentelemetry.io/collector/internal/model/serializer"
)

type marshaler struct {
delegate jsonpb.Marshaler
}

// NewTracesMarshaler returns a serializer.TracesUnmarshaler to marshal to OTLP json bytes.
func NewTracesMarshaler() serializer.TracesMarshaler {
return &marshaler{delegate: jsonpb.Marshaler{}}
}

// NewMetricsMarshaler returns a serializer.MetricsMarshaler to marshal to OTLP json bytes.
func NewMetricsMarshaler() serializer.MetricsMarshaler {
return &marshaler{delegate: jsonpb.Marshaler{}}
}

// NewLogsMarshaler returns a serializer.LogsMarshaler to marshal to OTLP json bytes.
func NewLogsMarshaler() serializer.LogsMarshaler {
return &marshaler{delegate: jsonpb.Marshaler{}}
}

func (en *marshaler) MarshalLogs(model interface{}) ([]byte, error) {
buf := bytes.Buffer{}
if err := en.delegate.Marshal(&buf, model.(*otlpcollectorlogs.ExportLogsServiceRequest)); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (en *marshaler) MarshalMetrics(model interface{}) ([]byte, error) {
buf := bytes.Buffer{}
if err := en.delegate.Marshal(&buf, model.(*otlpcollectormetrics.ExportMetricsServiceRequest)); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (en *marshaler) MarshalTraces(model interface{}) ([]byte, error) {
buf := bytes.Buffer{}
if err := en.delegate.Marshal(&buf, model.(*otlpcollectortrace.ExportTraceServiceRequest)); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
68 changes: 68 additions & 0 deletions internal/otlpjson/unmarshaler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 otlpjson

import (
"bytes"

"github.com/gogo/protobuf/jsonpb"

"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal"
"go.opentelemetry.io/collector/internal/model/serializer"
)

type unmarshaler struct {
delegate jsonpb.Unmarshaler
}

// NewTracesUnmarshaler returns a serializer.TracesUnmarshaler to unmarshal from OTLP json bytes.
func NewTracesUnmarshaler() serializer.TracesUnmarshaler {
return &unmarshaler{delegate: jsonpb.Unmarshaler{}}
}

// NewMetricsUnmarshaler returns a serializer.MetricsUnmarshaler to unmarshal from OTLP json bytes.
func NewMetricsUnmarshaler() serializer.MetricsUnmarshaler {
return &unmarshaler{delegate: jsonpb.Unmarshaler{}}
}

// NewLogsUnmarshaler returns a serializer.LogsUnmarshaler to unmarshal from OTLP json bytes.
func NewLogsUnmarshaler() serializer.LogsUnmarshaler {
return &unmarshaler{delegate: jsonpb.Unmarshaler{}}
}

func (d *unmarshaler) UnmarshalLogs(buf []byte) (interface{}, error) {
ld := pdata.NewLogs()
if err := d.delegate.Unmarshal(bytes.NewReader(buf), internal.LogsToOtlp(ld.InternalRep())); err != nil {
return nil, err
}
return ld, nil
}

func (d *unmarshaler) UnmarshalMetrics(buf []byte) (interface{}, error) {
md := pdata.NewMetrics()
if err := d.delegate.Unmarshal(bytes.NewReader(buf), internal.MetricsToOtlp(md.InternalRep())); err != nil {
return nil, err
}
return md, nil
}

func (d *unmarshaler) UnmarshalTraces(buf []byte) (interface{}, error) {
td := pdata.NewTraces()
if err := d.delegate.Unmarshal(bytes.NewReader(buf), internal.TracesToOtlp(td.InternalRep())); err != nil {
return nil, err
}
return td, nil
}

0 comments on commit 4fceba5

Please sign in to comment.