-
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 5581599
Showing
4 changed files
with
156 additions
and
35 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
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
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,58 @@ | ||
// 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 { | ||
jsonpb.Marshaler | ||
} | ||
|
||
func NewTracesMarshaler() serializer.TracesMarshaler { | ||
return &marshaler{jsonpb.Marshaler{}} | ||
} | ||
|
||
func (en *marshaler) MarshalLogs(model interface{}) ([]byte, error) { | ||
buf := bytes.Buffer{} | ||
if err := en.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.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.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,57 @@ | ||
// 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 { | ||
unmarshaler jsonpb.Unmarshaler | ||
} | ||
|
||
func NewTracesUnmarshaler() serializer.TracesUnmarshaler { | ||
return &Unmarshaler{unmarshaler: jsonpb.Unmarshaler{}} | ||
} | ||
|
||
func (d *Unmarshaler) UnmarshalLogs(buf []byte) (interface{}, error) { | ||
ld := pdata.NewLogs() | ||
if err := d.unmarshaler.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.unmarshaler.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.unmarshaler.Unmarshal(bytes.NewReader(buf), internal.TracesToOtlp(td.InternalRep())); err != nil { | ||
return nil, err | ||
} | ||
return td, nil | ||
} |