json.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package zipkinv2 // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin/zipkinv2"
  4. import (
  5. "encoding/json"
  6. zipkinmodel "github.com/openzipkin/zipkin-go/model"
  7. zipkinreporter "github.com/openzipkin/zipkin-go/reporter"
  8. "go.opentelemetry.io/collector/pdata/ptrace"
  9. )
  10. type jsonUnmarshaler struct {
  11. toTranslator ToTranslator
  12. }
  13. // UnmarshalTraces from JSON bytes.
  14. func (j jsonUnmarshaler) UnmarshalTraces(buf []byte) (ptrace.Traces, error) {
  15. var spans []*zipkinmodel.SpanModel
  16. if err := json.Unmarshal(buf, &spans); err != nil {
  17. return ptrace.Traces{}, err
  18. }
  19. return j.toTranslator.ToTraces(spans)
  20. }
  21. // NewJSONTracesUnmarshaler returns an unmarshaler for JSON bytes.
  22. func NewJSONTracesUnmarshaler(parseStringTags bool) ptrace.Unmarshaler {
  23. return jsonUnmarshaler{toTranslator: ToTranslator{ParseStringTags: parseStringTags}}
  24. }
  25. // NewJSONTracesMarshaler returns a marshaler to JSON bytes.
  26. func NewJSONTracesMarshaler() ptrace.Marshaler {
  27. return marshaler{
  28. serializer: zipkinreporter.JSONSerializer{},
  29. }
  30. }