-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add otlpjson as a serializer implementation
Signed-off-by: Bogdan Drutu <[email protected]>
- Loading branch information
1 parent
fe57963
commit 4fceba5
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |