jaeger_unmarshaler.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package kafkareceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkareceiver"
  4. import (
  5. "bytes"
  6. "github.com/gogo/protobuf/jsonpb"
  7. jaegerproto "github.com/jaegertracing/jaeger/model"
  8. "go.opentelemetry.io/collector/pdata/ptrace"
  9. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
  10. )
  11. type jaegerProtoSpanUnmarshaler struct {
  12. }
  13. var _ TracesUnmarshaler = (*jaegerProtoSpanUnmarshaler)(nil)
  14. func (j jaegerProtoSpanUnmarshaler) Unmarshal(bytes []byte) (ptrace.Traces, error) {
  15. span := &jaegerproto.Span{}
  16. err := span.Unmarshal(bytes)
  17. if err != nil {
  18. return ptrace.NewTraces(), err
  19. }
  20. return jaegerSpanToTraces(span)
  21. }
  22. func (j jaegerProtoSpanUnmarshaler) Encoding() string {
  23. return "jaeger_proto"
  24. }
  25. type jaegerJSONSpanUnmarshaler struct {
  26. }
  27. var _ TracesUnmarshaler = (*jaegerJSONSpanUnmarshaler)(nil)
  28. func (j jaegerJSONSpanUnmarshaler) Unmarshal(data []byte) (ptrace.Traces, error) {
  29. span := &jaegerproto.Span{}
  30. err := jsonpb.Unmarshal(bytes.NewReader(data), span)
  31. if err != nil {
  32. return ptrace.NewTraces(), err
  33. }
  34. return jaegerSpanToTraces(span)
  35. }
  36. func (j jaegerJSONSpanUnmarshaler) Encoding() string {
  37. return "jaeger_json"
  38. }
  39. func jaegerSpanToTraces(span *jaegerproto.Span) (ptrace.Traces, error) {
  40. batch := jaegerproto.Batch{
  41. Spans: []*jaegerproto.Span{span},
  42. Process: span.Process,
  43. }
  44. return jaeger.ProtoToTraces([]*jaegerproto.Batch{&batch})
  45. }