Files
Javier Molina ReyesandGitHub cbc23d6a17 feat: rename batches property of Trace to ResourceSpans to be OTEL compatible (#3895)
* rename batches property of Trace to ResourceSpans to be OTEL compatible

* added test to assert that the response hasn't change

* update test data

* fix unmarshall test

* changelog

* fix e2e

* try to fix flaky test

* create helper functions to marshal and unmarshal traces to OTEL compatible schema

* added better documentation
2024-07-25 09:55:35 -04:00

35 lines
1.3 KiB
Go

package tempopb
import (
"strings"
"github.com/gogo/protobuf/jsonpb"
)
// It marshal a Trace to an OTEL compatible JSON.
// Historically, our Trace proto message used `batches` to define the array of resourses spans.
// To be OTEL compatible we renamed it to `resourcesSpans`.
// To be backward compatible, this function use jsonpb to marshal the Trace to an OTEL compatible JSON
// and then replace the first occurrence of `resourceSpan` by `batches`.
func MarshalToJSONV1(t *Trace) ([]byte, error) {
marshaler := &jsonpb.Marshaler{}
jsonStr, err := marshaler.MarshalToString(t)
if err != nil {
return nil, err
}
jsonStr = strings.Replace(jsonStr, `"resourceSpans":`, `"batches":`, 1)
return []byte(jsonStr), nil
}
// It unmarshal an OTEL compatible JSON to a Trace.
// Historically, our Trace proto message used `batches` to define the array of resourses spans.
// To be OTEL compatible we renamed it to `resourcesSpan`.
// To be backward compatible, this function replaces the first occurrence of `batches` by `resourcesSpan`
// and then use jsonpb to unmarshal JSON into a Trace.
func UnmarshalFromJSONV1(data []byte, t *Trace) error {
marshaler := &jsonpb.Unmarshaler{}
jsonStr := strings.Replace(string(data), `"batches":`, `"resourceSpans":`, 1)
err := marshaler.Unmarshal(strings.NewReader(jsonStr), t)
return err
}