Skip to content

Commit

Permalink
[pdata] Document that Marshalers/Unmarshalers use OTLP/JSON format ex…
Browse files Browse the repository at this point in the history
…plicitly (open-telemetry#8974)

Explicitly documents that the `JSONMarshaler` and `JSONUnmarshaler` as
conforming to the format in the OTLP/JSON specification.

The intent is to be explicit that we follow this spec and that
deviations (such as supporting snake case) are bugs that can be removed
in a minor version update.

**Link to tracking Issue:** Relates to open-telemetry#6287
  • Loading branch information
mx-psi authored and pantuza committed Dec 8, 2023
1 parent 3e15791 commit da9a104
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pdata/plog/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
"go.opentelemetry.io/collector/pdata/internal/otlp"
)

// JSONMarshaler marshals pdata.Logs to JSON bytes using the OTLP/JSON format.
type JSONMarshaler struct{}

// MarshalLogs to the OTLP/JSON format.
func (*JSONMarshaler) MarshalLogs(ld Logs) ([]byte, error) {
buf := bytes.Buffer{}
pb := internal.LogsToProto(internal.Logs(ld))
Expand All @@ -26,8 +28,10 @@ func (*JSONMarshaler) MarshalLogs(ld Logs) ([]byte, error) {

var _ Unmarshaler = (*JSONUnmarshaler)(nil)

// JSONUnmarshaler unmarshals OTLP/JSON formatted-bytes to pdata.Logs.
type JSONUnmarshaler struct{}

// UnmarshalLogs from OTLP/JSON format into pdata.Logs.
func (*JSONUnmarshaler) UnmarshalLogs(buf []byte) (Logs, error) {
iter := jsoniter.ConfigFastest.BorrowIterator(buf)
defer jsoniter.ConfigFastest.ReturnIterator(iter)
Expand Down
4 changes: 4 additions & 0 deletions pdata/pmetric/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ import (

var _ Marshaler = (*JSONMarshaler)(nil)

// JSONMarshaler marshals pdata.Metrics to JSON bytes using the OTLP/JSON format.
type JSONMarshaler struct{}

// MarshalMetrics to the OTLP/JSON format.
func (*JSONMarshaler) MarshalMetrics(md Metrics) ([]byte, error) {
buf := bytes.Buffer{}
pb := internal.MetricsToProto(internal.Metrics(md))
err := json.Marshal(&buf, &pb)
return buf.Bytes(), err
}

// JSONUnmarshaler unmarshals OTLP/JSON formatted-bytes to pdata.Metrics.
type JSONUnmarshaler struct{}

// UnmarshalMetrics from OTLP/JSON format into pdata.Metrics.
func (*JSONUnmarshaler) UnmarshalMetrics(buf []byte) (Metrics, error) {
iter := jsoniter.ConfigFastest.BorrowIterator(buf)
defer jsoniter.ConfigFastest.ReturnIterator(iter)
Expand Down
4 changes: 4 additions & 0 deletions pdata/ptrace/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ import (
"go.opentelemetry.io/collector/pdata/internal/otlp"
)

// JSONMarshaler marshals pdata.Traces to JSON bytes using the OTLP/JSON format.
type JSONMarshaler struct{}

// MarshalTraces to the OTLP/JSON format.
func (*JSONMarshaler) MarshalTraces(td Traces) ([]byte, error) {
buf := bytes.Buffer{}
pb := internal.TracesToProto(internal.Traces(td))
err := json.Marshal(&buf, &pb)
return buf.Bytes(), err
}

// JSONUnmarshaler unmarshals OTLP/JSON formatted-bytes to pdata.Traces.
type JSONUnmarshaler struct{}

// UnmarshalTraces from OTLP/JSON format into pdata.Traces.
func (*JSONUnmarshaler) UnmarshalTraces(buf []byte) (Traces, error) {
iter := jsoniter.ConfigFastest.BorrowIterator(buf)
defer jsoniter.ConfigFastest.ReturnIterator(iter)
Expand Down

0 comments on commit da9a104

Please sign in to comment.